Changeset c77630a in flex_extract.git


Ignore:
Timestamp:
Aug 13, 2019, 12:32:22 PM (5 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
0690a6c
Parents:
33a4ccc
Message:

BUGFIX: for python3-eccodes the file openings need to be in binary mode! added binary marker in filemode

Location:
Source/Python
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • Source/Python/Classes/EcFlexpart.py

    r8c1d175 rc77630a  
    10941094
    10951095            print("outputfile = " + fnout)
    1096             f_handle = open(fnout, 'w')
    1097             h_handle = open(hnout, 'w')
    1098             g_handle = open(gnout, 'w')
     1096            f_handle = open(fnout, 'wb')
     1097            h_handle = open(hnout, 'wb')
     1098            g_handle = open(gnout, 'wb')
    10991099
    11001100            # read message for message and store relevant data fields, where
     
    14021402            # write original time step to flux file as usual
    14031403            fluxfile = GribUtil(os.path.join(c.inputdir, fluxfilename))
    1404             fluxfile.set_keys(tmpfile, filemode='a', strict=True,
     1404            fluxfile.set_keys(tmpfile, filemode='ab', strict=True,
    14051405                              wherekeynames=['paramId'], wherekeyvalues=[142],
    14061406                              keynames=['perturbationNumber','date','time','stepRange','values'],
     
    14081408                                         date.hour*100, 0, lsp_new_np[inumb,:,it]],
    14091409                             )
    1410             fluxfile.set_keys(tmpfile, filemode='a', strict=True,
     1410            fluxfile.set_keys(tmpfile, filemode='ab', strict=True,
    14111411                              wherekeynames=['paramId'], wherekeyvalues=[143],
    14121412                              keynames=['perturbationNumber','date','time','stepRange','values'],
     
    14161416
    14171417            # rr for first subgrid point is identified by step = 1
    1418             fluxfile.set_keys(tmpfile, filemode='a', strict=True,
     1418            fluxfile.set_keys(tmpfile, filemode='ab', strict=True,
    14191419                              wherekeynames=['paramId'], wherekeyvalues=[142],
    14201420                              keynames=['perturbationNumber','date','time','stepRange','values'],
     
    14221422                                         date.hour*100, '1', lsp_new_np[inumb,:,it+1]]
    14231423                              )
    1424             fluxfile.set_keys(tmpfile, filemode='a', strict=True,
     1424            fluxfile.set_keys(tmpfile, filemode='ab', strict=True,
    14251425                              wherekeynames=['paramId'], wherekeyvalues=[143],
    14261426                              keynames=['perturbationNumber','date','time','stepRange','values'],
     
    14301430
    14311431            # rr for second subgrid point is identified by step = 2
    1432             fluxfile.set_keys(tmpfile, filemode='a', strict=True,
     1432            fluxfile.set_keys(tmpfile, filemode='ab', strict=True,
    14331433                              wherekeynames=['paramId'], wherekeyvalues=[142],
    14341434                              keynames=['perturbationNumber','date','time','stepRange','values'],
     
    14361436                                         date.hour*100, '2', lsp_new_np[inumb,:,it+2]]
    14371437                              )
    1438             fluxfile.set_keys(tmpfile, filemode='a', strict=True,
     1438            fluxfile.set_keys(tmpfile, filemode='ab', strict=True,
    14391439                              wherekeynames=['paramId'], wherekeyvalues=[143],
    14401440                              keynames=['perturbationNumber','date','time','stepRange','values'],
     
    14661466
    14671467        gribfile.copy_dummy_msg(ifile, keynames=['paramId'],
    1468                       keyvalues=[142], filemode='w')
     1468                      keyvalues=[142], filemode='wb')
    14691469
    14701470        gribfile.copy_dummy_msg(ifile, keynames=['paramId'],
    1471                       keyvalues=[143], filemode='a')
     1471                      keyvalues=[143], filemode='ab')
    14721472
    14731473        return
     
    15731573                fortfile = os.path.join(c.inputdir, 'fort.' + k)
    15741574                silent_remove(fortfile)
    1575                 fdict[k] = open(fortfile, 'w')
     1575                fdict[k] = open(fortfile, 'wb')
    15761576#============================================================================================
    15771577            # create correct timestamp from the three time informations
     
    16081608            #    if 'olddate' not in locals() or cdate != olddate:
    16091609            #        fwrf = open(os.path.join(c.outputdir,
    1610             #                    'WRF' + cdate + '.' + ctime + '.000.grb2'), 'w')
     1610            #                    'WRF' + cdate + '.' + ctime + '.000.grb2'), 'wb')
    16111611            #        olddate = cdate[:]
    16121612#============================================================================================
  • Source/Python/Classes/GribUtil.py

    r44174de rc77630a  
    9191                             codes_release)
    9292
    93         fileid = open(self.filenames, 'r')
    94 
    9593        return_list = []
    9694
    97         while 1:
     95        with open(self.filenames, 'rb') as fileid:
     96
    9897            gid = codes_new_from_file(fileid)
    99 
    100             if gid is None:
    101                 break
    10298
    10399            if len(wherekeynames) != len(wherekeyvalues):
     
    123119            codes_release(gid)
    124120
    125         fileid.close()
    126 
    127121        return return_list
    128122
    129123
    130124    def set_keys(self, fromfile, keynames, keyvalues, wherekeynames=[],
    131                  wherekeyvalues=[], strict=False, filemode='w'):
     125                 wherekeyvalues=[], strict=False, filemode='wb'):
    132126        '''Opens the file to read the grib messages and then write
    133127        the selected messages (with wherekeys) to a new output file.
     
    161155
    162156        filemode : :obj:`string`, optional
    163             Sets the mode for the output file. Default is "w".
     157            Sets the mode for the output file. Default is "wb".
    164158
    165159        Return
     
    175169
    176170        fout = open(self.filenames, filemode)
    177         fin = open(fromfile)
    178 
    179         while 1:
     171
     172        with open(fromfile, 'rb') as fin:
    180173            gid = codes_grib_new_from_file(fin)
    181 
    182             if gid is None:
    183                 break
    184174
    185175            select = True
     
    206196            codes_release(gid)
    207197
    208         fin.close()
    209198        fout.close()
    210199
     
    212201
    213202    def copy_dummy_msg(self, filename_in, selectWhere=True,
    214                  keynames=[], keyvalues=[], filemode='w'):
     203                 keynames=[], keyvalues=[], filemode='wb'):
    215204        '''Add the content of another input grib file to the objects file but
    216205        only messages corresponding to keys/values passed to the function.
     
    235224
    236225        filemode : :obj:`string`, optional
    237             Sets the mode for the output file. Default is "w".
     226            Sets the mode for the output file. Default is "wb".
    238227
    239228        Return
     
    247236            raise Exception("Give a value for each keyname!")
    248237
    249         fin = open(filename_in, 'rb')
     238
    250239        fout = open(self.filenames, filemode)
    251240
    252241        fields = 0
    253242
    254         while fields < 1:
     243        with open(filename_in, 'rb') as fin:
     244            if fields >= 1:
     245                fout.close()
     246                return
     247
    255248            gid = codes_grib_new_from_file(fin)
    256 
    257             if gid is None:
    258                 break
    259249
    260250            select = True
     
    278268            codes_release(gid)
    279269
    280         fin.close()
    281270        fout.close()
    282271
  • Source/Python/Classes/MarsRetrieval.py

    r44174de rc77630a  
    488488            del attrs[key]
    489489
    490         attrs['ppengine'] = 'emos'
     490#        attrs['ppengine'] = 'emos'
    491491
    492492        # MARS request via Python script
     
    520520                request_str = request_str + ',' + key + '=' + str(value)
    521521            request_str += ',target="' + target + '"'
    522             p = subprocess.Popen(['mars', '-e'],
     522            p = subprocess.Popen(['mars'], #'-e'],
    523523                                 stdin=subprocess.PIPE,
    524524                                 stdout=subprocess.PIPE,
  • Source/Python/Mods/tools.py

    r44174de rc77630a  
    763763    # --- open file ---
    764764    print("Opening file for getting information data --- %s" % filename)
    765     with open(filename) as f:
     765    with open(filename, 'rb') as f:
    766766        # load first message from file
    767767        gid = codes_grib_new_from_file(f)
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG