geomagpy/magpy

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

stephanbracke opened this issue · 1 comments

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

magpy/magpy/lib/format_imf.py

Lines 2309 to 2324 in 146a350

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" )
elif mode == 'replace': # replace existing inputs
exst = read(path_or_url=filename)
datastream = joinStreams(datastream,exst)
myFile= open( filename, "wt" )
elif mode == 'append':
myFile= open( filename, "at" )
else: # overwrite mode
#os.remove(filename) ?? necessary ??
myFile= open( filename, "wt" )
else:
myFile= open( filename, "wt" )

and later on it is written as :

magpy/magpy/lib/format_imf.py

Lines 2501 to 2502 in 146a350

line = '%s %9.2f %9.2f %9.2f %9.2f\r\n' % (day,x,y,z,df)
myFile.writelines( line ) #.encode('utf-8') )

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.

Incoroprated as suggested in commit:
692b593