geomagpy / magpy

MagPy (or GeomagPy) is a Python package for analysing and displaying geomagnetic data.
BSD 3-Clause "New" or "Revised" License
45 stars 27 forks source link

On windows writelines and write methods add a line feed to much during blv export #107

Closed stephanbracke closed 2 years ago

stephanbracke commented 3 years ago

On windows 10 when writing the blv file we get an extra blank line between every baseline point. This is basically caused by the fact that the method writelines and write who are part of the io core python tools ( see https://docs.python.org/3/library/io.html ) add an extra linefeed at the end of the line written on windows platform (which should not be the case according to the specs of python). However this can easily be avoided by adding a newline parameter as empty while opening the file. In this case it is concentrated in the format_imf.py https://github.com/geomagpy/magpy/blob/146a350ecfb2b623ebc1d8a381ac6afe78d99417/magpy/lib/format_imf.py#L2309-L2324

and later on it is written as : https://github.com/geomagpy/magpy/blob/146a350ecfb2b623ebc1d8a381ac6afe78d99417/magpy/lib/format_imf.py#L2501-L2502

To solve it ( I tested it on windows and linux ) we can add newline = '' to the open method :

   if os.path.isfile(filename):
        if mode == 'skip': # skip existing inputs
            exst = read(path_or_url=filename)
            datastream = joinStreams(exst,datastream)
            myFile= open( filename, "wt" ,newline='')
        elif mode == 'replace': # replace existing inputs
            exst = read(path_or_url=filename)
            datastream = joinStreams(datastream,exst)
            myFile= open( filename, "wt",newline='')
        elif mode == 'append':
            myFile= open( filename, "at" ,newline='')
        else: # overwrite mode
            #os.remove(filename)  ?? necessary ??
            myFile= open( filename, "wt" ,newline='')
    else:
        myFile= open( filename, "wt",newline='')

You could also replace the writelines by write ( but this is not necessary) because we only write one line and don't give an array of lines. It should be checked for all textformats. I used iaga format and there the newline is set so no problems for that format.

leonro commented 2 years ago

Incoroprated as suggested in commit: 692b5930f8d537b2b03c1099fccf83cb2d974ca3