def test_22(self): """ Edit: Copy records (a la editfst desire) This example shows how to * select records in a RPNStd file * read the record data + meta * write the record data + meta See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.fsrinl rpnpy.librmn.fstd98.fsrluk rpnpy.librmn.fstd98.fsrecr rpnpy.librmn.const """ import os, os.path, sys import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileNameIn = os.path.join(ATM_MODEL_DFILES, 'bcmk') fileNameOut = 'myfstfile.fst' # Open Files try: fileIdIn = rmn.fstopenall(fileNameIn) fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) except: sys.stderr.write("Problem opening the files: %s, %s\n" % (fileNameIn, fileNameOut)) sys.exit(1) try: # Get the list of records to copy keylist1 = rmn.fstinl(fileIdIn, nomvar='UU') keylist2 = rmn.fstinl(fileIdIn, nomvar='VV') for k in keylist1 + keylist2: # Read record data and meta from fileNameIn r = rmn.fstluk(k) # Write the record to fileNameOut rmn.fstecr(fileIdOut, r) print("CB22: Copied %s ip1=%d, ip2=%d, dateo=%s" % (r['nomvar'], r['ip1'], r['ip2'], r['dateo'])) except: pass finally: # Properly close files even if an error occured above # This is important when editing to avoid corrupted files rmn.fstcloseall(fileIdIn) rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file
def test_22(self): """ Edit: Copy records (a la editfst desire) This example shows how to * select records in a RPNStd file * read the record data + meta * write the record data + meta See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.fsrinl rpnpy.librmn.fstd98.fsrluk rpnpy.librmn.fstd98.fsrecr rpnpy.librmn.const """ import os, os.path, sys import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileNameIn = os.path.join(ATM_MODEL_DFILES,'bcmk') fileNameOut = 'myfstfile.fst' # Open Files try: fileIdIn = rmn.fstopenall(fileNameIn) fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) except: sys.stderr.write("Problem opening the files: %s, %s\n" % (fileNameIn, fileNameOut)) sys.exit(1) try: # Get the list of records to copy keylist1 = rmn.fstinl(fileIdIn, nomvar='UU') keylist2 = rmn.fstinl(fileIdIn, nomvar='VV') for k in keylist1+keylist2: # Read record data and meta from fileNameIn r = rmn.fstluk(k) # Write the record to fileNameOut rmn.fstecr(fileIdOut, r) print("CB22: Copied %s ip1=%d, ip2=%d, dateo=%s" % (r['nomvar'], r['ip1'], r['ip2'], r['dateo'])) except: pass finally: # Properly close files even if an error occured above # This is important when editing to avoid corrupted files rmn.fstcloseall(fileIdIn) rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file
def test_22qd(self): import os, os.path import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileNameOut = 'myfstfile.fst' fileIdIn = rmn.fstopenall(ATM_MODEL_DFILES+'/bcmk') fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) for k in rmn.fstinl(fileIdIn, nomvar='UU') + rmn.fstinl(fileIdIn, nomvar='VV'): rmn.fstecr(fileIdOut, rmn.fstluk(k)) rmn.fstcloseall(fileIdIn) rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file
def test_22qd(self): import os, os.path import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileNameOut = 'myfstfile.fst' fileIdIn = rmn.fstopenall(ATM_MODEL_DFILES + '/bcmk') fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) for k in rmn.fstinl(fileIdIn, nomvar='UU') + rmn.fstinl(fileIdIn, nomvar='VV'): rmn.fstecr(fileIdOut, rmn.fstluk(k)) rmn.fstcloseall(fileIdIn) rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file
def get_grid_descriptors(file_id): ''' gets grid descriptors from a fst file ''' tic_record = rmn.FST_RDE_META_DEFAULT.copy() tac_record = rmn.FST_RDE_META_DEFAULT.copy() tac = rmn.fstinl(file_id, nomvar='>>')[0] tic = rmn.fstinl(file_id, nomvar='^^')[0] tic_record.update(rmn.fstprm(tic)) tac_record.update(rmn.fstprm(tac)) return tic_record, tac_record
def test_23qd(self): import os, sys, datetime from scipy.constants import knot as KNOT2MS import numpy as np import rpnpy.librmn.all as rmn import rpnpy.vgd.all as vgd fdate = datetime.date.today().strftime('%Y%m%d') + '00_048' fileNameOut = 'uvfstfile.fst' fileIdIn = rmn.fstopenall(os.getenv('CMCGRIDF')+'/prog/regeta/'+fdate) fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) vgd.vgd_write(vgd.vgd_read(fileIdIn), fileIdOut) (uu, vv, uvarray, copyGrid) = ({'d': None}, {'d': None}, None, True) for k in rmn.fstinl(fileIdIn, nomvar='UU'): uu = rmn.fstluk(k, dataArray=uu['d']) vv = rmn.fstlir(fileIdIn, nomvar='VV', ip1=uu['ip1'], ip2=uu['ip2'], datev=uu['datev'],dataArray=vv['d']) if uvarray is None: uvarray = np.empty(uu['d'].shape, dtype=uu['d'].dtype, order='FORTRAN') uv = uu.copy() uv.update({'d':uvarray, 'nomvar': 'WSPD'}) uv['d'][:,:] = np.sqrt(uu['d']**2. + vv['d']**2.) * KNOT2MS rmn.fstecr(fileIdOut, uv) if copyGrid: copyGrid = False rmn.writeGrid(fileIdOut, rmn.readGrid(fileIdIn, uu)) rmn.fstcloseall(fileIdIn) rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file
def test_fsteff_list_rec(self): """fsteff accept list and dict as input""" rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) (la, lo) = self.create_basefile() #wrote 2 recs in that order: la, lo funit = rmn.fstopenall(self.fname, rmn.FST_RW) keylist = rmn.fstinl(funit) rmn.fsteff(keylist) rmn.fstcloseall(funit) funit = rmn.fstopenall(self.fname, rmn.FST_RO) kla = rmn.fstinf(funit, nomvar='LA') klo = rmn.fstinf(funit, nomvar='LO') rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(kla, None, 'LA found after delete: ' + repr(kla)) self.assertEqual(klo, None, 'LO found after delete: ' + repr(klo)) (la, lo) = self.create_basefile() #wrote 2 recs in that order: la, lo funit = rmn.fstopenall(self.fname, rmn.FST_RW) klo = rmn.fstinf(funit, nomvar='LO') rmn.fsteff(klo) rmn.fstcloseall(funit) funit = rmn.fstopenall(self.fname, rmn.FST_RO) klo = rmn.fstinf(funit, nomvar='LO') rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(klo, None, 'LO found after delete: ' + repr(klo))
def fstinl(iunit, nomvar, typvar, etiket, ip1, ip2, ip3, datev): """Find all records matching provided criterias (Interface to fstinl) Warning: list is limited to the first 50000 records in a file, subsequent matches raises Fstdc.tooManyRecError and are ignored. recList = Fstdc.fstinl(iunit, nomvar, typvar, etiket, ip1, ip2, ip3, datev) param iunit file unit number handle returned by Fstdc_fstouv (int) @param nomvar select according to var name, blank==wildcard (string) @param typvar select according to var type, blank==wildcard (string) @param etiket select according to etiket, blank==wildcard (string) @param ip1 select according to ip1, -1==wildcard (int) @param ip2 select according to ip2, -1==wildcard (int) @param ip3 select according to ip3, -1==wildcard (int) @param datev select according to date of validity, -1==wildcard (int) @returns python dict with handles+params of all matching records @exception TypeError @exception Fstdc.error @exception Fstdc.tooManyRecError """ try: keylist = _rmn.fstinl(iunit, datev, etiket, ip1, ip2, ip3, typvar, nomvar) except: raise error('Problem getting record list') recParamList = [] for k in keylist: recParams = _rmn.fstprm(k) recParams['handle'] = k recParams['nom'] = recParams['nomvar'] recParams['type'] = recParams['typvar'] recParams['datev'] = recParams['xtra1'] #TODO: Keep Fstdc original bug? recParamList.append(recParams) return recParamList
def test_fsteff_list_rec(self): """fsteff accept list and dict as input""" rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) (la,lo) = self.create_basefile() #wrote 2 recs in that order: la, lo funit = rmn.fstopenall(self.fname,rmn.FST_RW) keylist = rmn.fstinl(funit) rmn.fsteff(keylist) rmn.fstcloseall(funit) funit = rmn.fstopenall(self.fname,rmn.FST_RO) kla = rmn.fstinf(funit,nomvar='LA') klo = rmn.fstinf(funit,nomvar='LO') rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(kla,None,'LA found after delete: '+repr(kla)) self.assertEqual(klo,None,'LO found after delete: '+repr(klo)) (la,lo) = self.create_basefile() #wrote 2 recs in that order: la, lo funit = rmn.fstopenall(self.fname,rmn.FST_RW) klo = rmn.fstinf(funit,nomvar='LO') rmn.fsteff(klo) rmn.fstcloseall(funit) funit = rmn.fstopenall(self.fname,rmn.FST_RO) klo = rmn.fstinf(funit,nomvar='LO') rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(klo,None,'LO found after delete: '+repr(klo))
def test_23qd(self): import os, sys, datetime from scipy.constants import knot as KNOT2MS import numpy as np import rpnpy.librmn.all as rmn import rpnpy.vgd.all as vgd fdate = datetime.date.today().strftime('%Y%m%d') + '00_048' fileNameOut = 'uvfstfile.fst' fileIdIn = rmn.fstopenall( os.getenv('CMCGRIDF') + '/prog/regeta/' + fdate) fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) vgd.vgd_write(vgd.vgd_read(fileIdIn), fileIdOut) (uu, vv, uvarray, copyGrid) = ({'d': None}, {'d': None}, None, True) for k in rmn.fstinl(fileIdIn, nomvar='UU'): uu = rmn.fstluk(k, dataArray=uu['d']) vv = rmn.fstlir(fileIdIn, nomvar='VV', ip1=uu['ip1'], ip2=uu['ip2'], datev=uu['datev'], dataArray=vv['d']) if uvarray is None: uvarray = np.empty(uu['d'].shape, dtype=uu['d'].dtype, order='FORTRAN') uv = uu.copy() uv.update({'d': uvarray, 'nomvar': 'WSPD'}) uv['d'][:, :] = np.sqrt(uu['d']**2. + vv['d']**2.) * KNOT2MS rmn.fstecr(fileIdOut, uv) if copyGrid: copyGrid = False rmn.writeGrid(fileIdOut, rmn.readGrid(fileIdIn, uu)) rmn.fstcloseall(fileIdIn) rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file
def test_12(self): """ Queries: Get Data and Meta This example shows how to * get the data of all records matching selection creterions * then print statistics See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.fstluk rpnpy.librmn.fstd98.fstlir rpnpy.librmn.fstd98.DecodeIp rpnpy.librmn.fstd98.kindToString rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.const """ import os, sys import rpnpy.librmn.all as rmn # Restrict to the minimum the number of messages printed by librmn rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) # Open all RPNStd files in the $ATM_MODEL_DFILES/bcmk/ directory ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk') try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: sys.stderr.write("Problem opening the file: %s\n" % fileName) sys.exit(1) try: # Get the list of record keys matching nomvar='TT' keylist = rmn.fstinl(fileId, nomvar='TT', ip2=12) for k in keylist: # Get the record meta data r = rmn.fstluk(k) d = r['d'] # Decode level info and Print statistics rp1 = rmn.DecodeIp(r['ip1'], r['ip2'], r['ip3'])[0] level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind)) print( "CB12: %s (level=%s, ip2=%d) mean=%f, std=%f, min=%f, max=%f" % (r['nomvar'], level, r['ip2'], d.mean(), d.std(), d.min(), d.max())) except: pass finally: # Close file even if an error occured above rmn.fstcloseall(fileId)
def test_12(self): """ Queries: Get Data and Meta This example shows how to * get the data of all records matching selection creterions * then print statistics See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.fstluk rpnpy.librmn.fstd98.fstlir rpnpy.librmn.fstd98.DecodeIp rpnpy.librmn.fstd98.kindToString rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.const """ import os, sys import rpnpy.librmn.all as rmn # Restrict to the minimum the number of messages printed by librmn rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) # Open all RPNStd files in the $ATM_MODEL_DFILES/bcmk/ directory ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk') try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: sys.stderr.write("Problem opening the file: %s\n" % fileName) sys.exit(1) try: # Get the list of record keys matching nomvar='TT' keylist = rmn.fstinl(fileId, nomvar='TT', ip2=12) for k in keylist: # Get the record meta data r = rmn.fstluk(k) d = r['d'] # Decode level info and Print statistics rp1 = rmn.DecodeIp(r['ip1'], r['ip2'], r['ip3'])[0] level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind)) print("CB12: %s (level=%s, ip2=%d) mean=%f, std=%f, min=%f, max=%f" % (r['nomvar'], level, r['ip2'], d.mean(), d.std(), d.min(), d.max())) except: pass finally: # Close file even if an error occured above rmn.fstcloseall(fileId)
def test_11(self): """ Queries: Find records, Read meta This example shows how to * get the list of all records matching selection creterions * get the metadata and decode it * then print some values See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.DecodeIp rpnpy.librmn.fstd98.kindToString rpnpy.librmn.base.newdate rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.const """ import os, sys import rpnpy.librmn.all as rmn # Open all RPNStd files in the $ATM_MODEL_DFILES/bcmk/ directory ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk') try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: sys.stderr.write("Problem opening the file: %s\n" % fileName) sys.exit(1) try: # Get the list of record keys matching nomvar='TT', ip2=12 keylist = rmn.fstinl(fileId, nomvar='TT', ip2=12) for k in keylist: # Get the record meta data m = rmn.fstprm(k) # Decode ip1, and dateo (rp1, rp2, rp3) = rmn.DecodeIp(m['ip1'], m['ip2'], m['ip3']) level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind)) (yyyymmdd, hhmmsshh) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT, m['dateo']) dateo = "%8.8d.%8.8d" % (yyyymmdd, hhmmsshh) nhours = float(m['npas'] * m['deet']) / 3600. print("CB11: %s (%d, %d) level=%s, dateo=%s + %s" % (m['nomvar'], m['ip1'], m['ip2'], level, dateo, nhours)) except: pass finally: # Close file even if an error occured above rmn.fstcloseall(fileId)
def test_12qd(self): import os, sys import rpnpy.librmn.all as rmn rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileId = rmn.fstopenall(ATM_MODEL_DFILES+'/bcmk', rmn.FST_RO) for k in rmn.fstinl(fileId, nomvar='TT', ip2=2): r = rmn.fstluk(k) print("CB12qd: %s (ip1=%s, ip2=%d) mean=%f, std=%f, min=%f, max=%f" % (r['nomvar'], r['ip1'], r['ip2'], r['d'].mean(), r['d'].std(), r['d'].min(), r['d'].max())) rmn.fstcloseall(fileId)
def test_11bqd(self): import os, sys import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileId = rmn.fstopenall(ATM_MODEL_DFILES+'/bcmk_p/anlp2015070706_000', rmn.FST_RO) for k in rmn.fstinl(fileId, nomvar='TT', ip1=rmn.ip1_all(500., rmn.LEVEL_KIND_PMB), datev=rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20150707, 6000000)): m = rmn.fstprm(k) print("CB11bqd: %s (%d, %d, %s)" % (m['nomvar'], m['ip1'], m['ip2'], m['datev'])) rmn.fstcloseall(fileId)
def test_11(self): """ Queries: Find records, Read meta This example shows how to * get the list of all records matching selection creterions * get the metadata and decode it * then print some values See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.DecodeIp rpnpy.librmn.fstd98.kindToString rpnpy.librmn.base.newdate rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.const """ import os, sys import rpnpy.librmn.all as rmn # Open all RPNStd files in the $ATM_MODEL_DFILES/bcmk/ directory ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk') try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: sys.stderr.write("Problem opening the file: %s\n" % fileName) sys.exit(1) try: # Get the list of record keys matching nomvar='TT', ip2=12 keylist = rmn.fstinl(fileId, nomvar='TT', ip2=12) for k in keylist: # Get the record meta data m = rmn.fstprm(k) # Decode ip1, and dateo (rp1, rp2, rp3) = rmn.DecodeIp(m['ip1'], m['ip2'], m['ip3']) level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind)) (yyyymmdd,hhmmsshh) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT,m['dateo']) dateo = "%8.8d.%8.8d" % (yyyymmdd,hhmmsshh) nhours = float(m['npas'] * m['deet']) / 3600. print("CB11: %s (%d, %d) level=%s, dateo=%s + %s" % (m['nomvar'], m['ip1'], m['ip2'], level, dateo, nhours)) except: pass finally: # Close file even if an error occured above rmn.fstcloseall(fileId)
def test_11b(self): """ Queries: List/Find records This example shows how to * get the list of all records matching selection creterions * encode parameters (ip1, datev) values to use selection criteria See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.ip1_all rpnpy.librmn.fstd98.ip2_all rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.DecodeIp rpnpy.librmn.fstd98.kindToString rpnpy.librmn.base.newdate rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.const """ import os, sys import rpnpy.librmn.all as rmn # Open all RPNStd files in $ATM_MODEL_DFILES/bcmk_p/anlp2015070706_000 ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk_p', 'anlp2015070706_000') try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: sys.stderr.write("Problem opening the file: %s\n" % fileName) sys.exit(1) try: # Encode selection criteria ip1 = rmn.ip1_all(500., rmn.LEVEL_KIND_PMB) datev = rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20150707, 6000000) # Get the list of record keys matching # nomvar='TT' at 500mb and valide date=20150707.06000000 keylist = rmn.fstinl(fileId, nomvar='TT', ip1=ip1, datev=datev) print("CB11b: Found %d records matching TT, ip1=%d, datev=%d" % (len(keylist), ip1, datev)) # Get every record meta data for k in keylist: m = rmn.fstprm(k) print("CB11b: %s (%d, %d, %s)" % (m['nomvar'], m['ip1'], m['ip2'], m['datev'])) except: pass finally: # Close file even if an error occured above rmn.fstcloseall(fileId)
def test_11b(self): """ Queries: List/Find records This example shows how to * get the list of all records matching selection creterions * encode parameters (ip1, datev) values to use selection criteria See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.ip1_all rpnpy.librmn.fstd98.ip2_all rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.DecodeIp rpnpy.librmn.fstd98.kindToString rpnpy.librmn.base.newdate rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.const """ import os, sys import rpnpy.librmn.all as rmn # Open all RPNStd files in $ATM_MODEL_DFILES/bcmk_p/anlp2015070706_000 ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk_p','anlp2015070706_000') try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: sys.stderr.write("Problem opening the file: %s\n" % fileName) sys.exit(1) try: # Encode selection criteria ip1 = rmn.ip1_all(500., rmn.LEVEL_KIND_PMB) datev = rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20150707, 6000000) # Get the list of record keys matching # nomvar='TT' at 500mb and valide date=20150707.06000000 keylist = rmn.fstinl(fileId, nomvar='TT', ip1=ip1, datev=datev) print("CB11b: Found %d records matching TT, ip1=%d, datev=%d" % (len(keylist), ip1, datev)) # Get every record meta data for k in keylist: m = rmn.fstprm(k) print("CB11b: %s (%d, %d, %s)" % (m['nomvar'], m['ip1'], m['ip2'], m['datev'])) except: pass finally: # Close file even if an error occured above rmn.fstcloseall(fileId)
def test_9(self): """ Decoding values When reading an FSTD record metadata, the ip1, ip2, ip3 contains the encoded time and levels values. In the old format, ip1 is used for the level value and ip2 is a none encoded time value in hours. In the new format, all ip1, ip2, ip3 can be used to specify time and level as well as ranges. See also: rpnpy.librmn.fstd98.convertIp rpnpy.librmn.fstd98.convertIPtoPK rpnpy.librmn.fstd98.DecodeIp rpnpy.librmn.fstd98.kindToString rpnpy.librmn.proto.FLOAT_IP rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinf rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.FSTDError rpnpy.librmn.RMNError rpnpy.librmn.const """ import os import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') fileName = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012') # Get list of records try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) keyList = rmn.fstinl(fileId, nomvar='TT') except: raise rmn.FSTDError( "Problem getting list of TT records from file: %s" % fileName) # Get metadata and Decode level value try: for k in keyList: recMeta = rmn.fstprm(k) (level, ikind) = rmn.convertIp(rmn.CONVIP_DECODE, recMeta['ip1']) kindstring = rmn.kindToString(ikind) print("Found %s at level %f %s" % (recMeta['nomvar'], level, kindstring)) except: raise rmn.FSTDError( "Problem getting metadata for TT from file: %s " % fileName) rmn.fstcloseall(fileId)
def test_12qd(self): import os, sys import rpnpy.librmn.all as rmn rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileId = rmn.fstopenall(ATM_MODEL_DFILES + '/bcmk', rmn.FST_RO) for k in rmn.fstinl(fileId, nomvar='TT', ip2=2): r = rmn.fstluk(k) print( "CB12qd: %s (ip1=%s, ip2=%d) mean=%f, std=%f, min=%f, max=%f" % (r['nomvar'], r['ip1'], r['ip2'], r['d'].mean(), r['d'].std(), r['d'].min(), r['d'].max())) rmn.fstcloseall(fileId)
def test_21qd(self): import sys, os, os.path, stat, shutil import rpnpy.librmn.all as rmn fileName = 'geophy.fst' ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName0 = os.path.join(ATM_MODEL_DFILES,'bcmk',fileName) shutil.copyfile(fileName0, fileName) st = os.stat(fileName) os.chmod(fileName, st.st_mode | stat.S_IWRITE) fileId = rmn.fstopenall(fileName, rmn.FST_RW_OLD) rmn.fst_edit_dir(rmn.fstinl(fileId), etiket='MY_NEW_ETK') rmn.fstcloseall(fileId) os.unlink(fileName) # Remove test file
def test_11qd(self): import os, sys import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileId = rmn.fstopenall(ATM_MODEL_DFILES+'/bcmk', rmn.FST_RO) for k in rmn.fstinl(fileId, nomvar='TT', ip2=12): m = rmn.fstprm(k) rp1 = rmn.DecodeIp(m['ip1'], m['ip2'], m['ip3'])[0] level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind)) dateo = "%8.8d.%8.8d" % rmn.newdate(rmn.NEWDATE_STAMP2PRINT,m['dateo']) print("CB11qd: %s (%d, %d) level=%s, dateo=%s + %s" % (m['nomvar'], m['ip1'], m['ip2'], level, dateo, float(m['npas'] * m['deet']) / 3600.)) rmn.fstcloseall(fileId)
def test_21qd(self): import sys, os, os.path, stat, shutil import rpnpy.librmn.all as rmn fileName = 'geophy.fst' ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName0 = os.path.join(ATM_MODEL_DFILES, 'bcmk', fileName) shutil.copyfile(fileName0, fileName) st = os.stat(fileName) os.chmod(fileName, st.st_mode | stat.S_IWRITE) fileId = rmn.fstopenall(fileName, rmn.FST_RW_OLD) rmn.fst_edit_dir(rmn.fstinl(fileId), etiket='MY_NEW_ETK') rmn.fstcloseall(fileId) os.unlink(fileName) # Remove test file
def test_11qd(self): import os, sys import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileId = rmn.fstopenall(ATM_MODEL_DFILES + '/bcmk', rmn.FST_RO) for k in rmn.fstinl(fileId, nomvar='TT', ip2=12): m = rmn.fstprm(k) rp1 = rmn.DecodeIp(m['ip1'], m['ip2'], m['ip3'])[0] level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind)) dateo = "%8.8d.%8.8d" % rmn.newdate(rmn.NEWDATE_STAMP2PRINT, m['dateo']) print("CB11qd: %s (%d, %d) level=%s, dateo=%s + %s" % (m['nomvar'], m['ip1'], m['ip2'], level, dateo, float(m['npas'] * m['deet']) / 3600.)) rmn.fstcloseall(fileId)
def test_11bqd(self): import os, sys import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileId = rmn.fstopenall( ATM_MODEL_DFILES + '/bcmk_p/anlp2015070706_000', rmn.FST_RO) for k in rmn.fstinl(fileId, nomvar='TT', ip1=rmn.ip1_all(500., rmn.LEVEL_KIND_PMB), datev=rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20150707, 6000000)): m = rmn.fstprm(k) print("CB11bqd: %s (%d, %d, %s)" % (m['nomvar'], m['ip1'], m['ip2'], m['datev'])) rmn.fstcloseall(fileId)
def test_9(self): """ Decoding values When reading an FSTD record metadata, the ip1, ip2, ip3 contains the encoded time and levels values. In the old format, ip1 is used for the level value and ip2 is a none encoded time value in hours. In the new format, all ip1, ip2, ip3 can be used to specify time and level as well as ranges. See also: rpnpy.librmn.fstd98.convertIp rpnpy.librmn.fstd98.convertIPtoPK rpnpy.librmn.fstd98.DecodeIp rpnpy.librmn.fstd98.kindToString rpnpy.librmn.proto.FLOAT_IP rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinf rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.FSTDError rpnpy.librmn.RMNError rpnpy.librmn.const """ import os import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') fileName = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012') # Get list of records try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) keyList = rmn.fstinl(fileId, nomvar='TT') except: raise rmn.FSTDError("Problem getting list of TT records from file: %s" % fileName) # Get metadata and Decode level value try: for k in keyList: recMeta = rmn.fstprm(k) (level, ikind) = rmn.convertIp(rmn.CONVIP_DECODE, recMeta['ip1']) kindstring = rmn.kindToString(ikind) print("Found %s at level %f %s" % (recMeta['nomvar'], level, kindstring)) except: raise rmn.FSTDError("Problem getting metadata for TT from file: %s " % fileName) rmn.fstcloseall(fileId)
def test_21(self): """ Edit: In-Place Record Meta Edition (a la editfst zap w/o copy) This example shows how to * select records in a RPNStd file * edit in place their metadata See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.fst_edit_dir rpnpy.librmn.fstd98.fsteff rpnpy.librmn.const """ import sys, os, os.path, stat, shutil import rpnpy.librmn.all as rmn # Copy a file locally to be able to edit it and set write permission fileName = 'geophy.fst' ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName0 = os.path.join(ATM_MODEL_DFILES,'bcmk',fileName) shutil.copyfile(fileName0, fileName) st = os.stat(fileName) os.chmod(fileName, st.st_mode | stat.S_IWRITE) # Open existing file in Rear/Write mode try: fileId = rmn.fstopenall(fileName, rmn.FST_RW_OLD) except: sys.stderr.write("Problem opening the file: %s\n" % fileName) sys.exit(1) try: # Get the list of all records key in file keylist = rmn.fstinl(fileId) # Iterate implicitely on list of records to change the etiket rmn.fst_edit_dir(keylist, etiket='MY_NEW_ETK') except: pass finally: # Properly close files even if an error occured above # This is important when editing to avoid corrupted files rmn.fstcloseall(fileId) os.unlink(fileName) # Remove test file
def test_21(self): """ Edit: In-Place Record Meta Edition (a la editfst zap w/o copy) This example shows how to * select records in a RPNStd file * edit in place their metadata See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.fst_edit_dir rpnpy.librmn.fstd98.fsteff rpnpy.librmn.const """ import sys, os, os.path, stat, shutil import rpnpy.librmn.all as rmn # Copy a file locally to be able to edit it and set write permission fileName = 'geophy.fst' ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName0 = os.path.join(ATM_MODEL_DFILES, 'bcmk', fileName) shutil.copyfile(fileName0, fileName) st = os.stat(fileName) os.chmod(fileName, st.st_mode | stat.S_IWRITE) # Open existing file in Rear/Write mode try: fileId = rmn.fstopenall(fileName, rmn.FST_RW_OLD) except: sys.stderr.write("Problem opening the file: %s\n" % fileName) sys.exit(1) try: # Get the list of all records key in file keylist = rmn.fstinl(fileId) # Iterate implicitely on list of records to change the etiket rmn.fst_edit_dir(keylist, etiket='MY_NEW_ETK') except: pass finally: # Properly close files even if an error occured above # This is important when editing to avoid corrupted files rmn.fstcloseall(fileId) os.unlink(fileName) # Remove test file
def test_isfst_openall_dir_fstnbr(self): """isfst_openall__dir_fstnbr should give known result with known input""" rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) ## rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST,rmn.FSTOP_GET) ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') mydir = os.path.join(ATM_MODEL_DFILES.strip(),'bcmk/') funit = rmn.fstopenall(mydir,rmn.FILE_MODE_RO) self.assertTrue(funit>0,'fstopenall should return a valid file unit') reclist = rmn.fstinl(funit,nomvar='VF') self.assertEqual(len(reclist),26,' fstinl of VF found %d/26 rec ' % len(reclist)) #Note: fstnbr does not work on linked files... ## nrec = rmn.fstnbrv(funit) ## self.assertEqual(nrec,9999,' fstnbrv found %d/???? rec ' % nrec) rmn.fstcloseall(funit)
def smooth_my_field(fid_in, fid_out, varname, outname, etiket, grid_space, edge_value): """ Read, smooth and write field in file Args: fid_in (int) : Input file unit id fid_out (int) : Output file unit id varname (str) : input varname of field to interpolate outname (str) : varname of smoothed field in output file etiket (str) : etiket of smoothed field in output file grid_space (int) : Smoothing factor (nb of grid points) edge_value (float): Value to put a field edges Returns: None """ print("+ Read, smooth and write field: %s" % varname) # Get list of records try: klist = rmn.fstinl(fid_in, nomvar=varname) except: raise rmn.RMNError('Problem getting list of records for ' + varname) # read, smooth and write field for k in klist: try: myfield = rmn.fstluk(k) except: raise rmn.RMNError('Problem in reading var: ' + varname) try: myfield['d'] = smooth_my_data(myfield['d'], grid_space, edge_value) except: raise rmn.RMNError('Problem in smoothing var: ' + varname) try: myfield['nomvar'] = outname myfield['etiket'] = etiket rmn.fstecr(fid_out, myfield['d'], myfield) except: raise rmn.RMNError('Problem in writing var: %s at ip1=%d\n' % (outname, myfield['ip1'])) del myfield['d'], myfield return
def smooth_my_field(fid_in, fid_out, varname, outname, etiket, grid_space, edge_value): """ Read, smooth and write field in file Args: fid_in (int) : Input file unit id fid_out (int) : Output file unit id varname (str) : input varname of field to interpolate outname (str) : varname of smoothed field in output file etiket (str) : etiket of smoothed field in output file grid_space (int) : Smoothing factor (nb of grid points) edge_value (float): Value to put a field edges Returns: None """ print("+ Read, smooth and write field: %s" % varname) # Get list of records try: klist = rmn.fstinl(fid_in, nomvar=varname) except: raise rmn.RMNError('Problem getting list of records for '+varname) # read, smooth and write field for k in klist: try: myfield = rmn.fstluk(k) except: raise rmn.RMNError('Problem in reading var: '+varname) try: myfield['d'] = smooth_my_data(myfield['d'], grid_space, edge_value) except: raise rmn.RMNError('Problem in smoothing var: '+varname) try: myfield['nomvar'] = outname myfield['etiket'] = etiket rmn.fstecr(fid_out, myfield['d'], myfield) except: raise rmn.RMNError('Problem in writing var: %s at ip1=%d\n' % (outname,myfield['ip1'])) del myfield['d'], myfield return
def test_fsteditdir_list_rec(self): """fst_edit_dir accept list and dict as input""" rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) (la,lo) = self.create_basefile() #wrote 2 recs in that order: la, lo funit = rmn.fstopenall(self.fname,rmn.FST_RW) keylist = rmn.fstinl(funit) istat = rmn.fst_edit_dir(keylist, etiket='MY_NEW_ETK') klo = rmn.fstinf(funit,nomvar='LO') istat = rmn.fst_edit_dir(klo, nomvar='QW') rmn.fstcloseall(funit) funit = rmn.fstopenall(self.fname,rmn.FST_RO) la = rmn.fstlir(funit,nomvar='LA') lo = rmn.fstlir(funit,nomvar='QW') rmn.fstcloseall(funit) self.erase_testfile() self.assertNotEqual(lo,None,'QW not found after rename: '+repr(klo)) self.assertNotEqual(la['etiket'],'MY_NEW_ETK') self.assertNotEqual(lo['etiket'],'MY_NEW_ETK')
def test_isfst_openall_dir_fstnbr(self): """isfst_openall__dir_fstnbr should give known result with known input""" rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) ## rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST,rmn.FSTOP_GET) ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') mydir = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/') funit = rmn.fstopenall(mydir, rmn.FILE_MODE_RO) self.assertTrue(funit > 0, 'fstopenall should return a valid file unit') reclist = rmn.fstinl(funit, nomvar='VF') self.assertEqual(len(reclist), 26, ' fstinl of VF found %d/26 rec ' % len(reclist)) #Note: fstnbr does not work on linked files... ## nrec = rmn.fstnbrv(funit) ## self.assertEqual(nrec,9999,' fstnbrv found %d/???? rec ' % nrec) rmn.fstcloseall(funit)
def test_fsteditdir_list_rec(self): """fst_edit_dir accept list and dict as input""" rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) (la, lo) = self.create_basefile() #wrote 2 recs in that order: la, lo funit = rmn.fstopenall(self.fname, rmn.FST_RW) keylist = rmn.fstinl(funit) istat = rmn.fst_edit_dir(keylist, etiket='MY_NEW_ETK') klo = rmn.fstinf(funit, nomvar='LO') istat = rmn.fst_edit_dir(klo, nomvar='QW') rmn.fstcloseall(funit) funit = rmn.fstopenall(self.fname, rmn.FST_RO) la = rmn.fstlir(funit, nomvar='LA') lo = rmn.fstlir(funit, nomvar='QW') rmn.fstcloseall(funit) self.erase_testfile() self.assertNotEqual(lo, None, 'QW not found after rename: ' + repr(klo)) self.assertNotEqual(la['etiket'], 'MY_NEW_ETK') self.assertNotEqual(lo['etiket'], 'MY_NEW_ETK')
def test_fstecr_fstinf_fstluk(self): """fstinf, fstluk should give known result with known input""" rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) (la, lo) = self.create_basefile() #wrote 2 recs in that order: la, lo a = rmn.isFST(self.fname) self.assertTrue(a) funit = rmn.fstopenall(self.fname, rmn.FST_RW) nrec = rmn.c_fstnbrv(funit) keylist = rmn.fstinl(funit) kla = rmn.fstinf(funit, nomvar='LA')['key'] la2prm = rmn.fstprm(kla) #,rank=2) la2 = rmn.fstluk(kla) #,rank=2) klo = rmn.fstinf(funit, nomvar='LO') lo2 = rmn.fstluk(klo) #,rank=2) rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(nrec, 2, ' c_fstnbrv found %d/2 rec ' % nrec) self.assertEqual( len(keylist), 2, 'fstinl found %d/2 rec: %s' % (len(keylist), repr(keylist))) self.assertEqual(la2['nomvar'].strip(), la['nomvar'].strip()) self.assertEqual(lo2['nomvar'].strip(), lo['nomvar'].strip()) self.assertEqual(la2['d'].shape, la['d'].shape) self.assertEqual(lo2['d'].shape, lo['d'].shape) if np.any(np.fabs(la2['d'] - la['d']) > self.epsilon): print('la2:', la2['d']) print('la :', la['d']) print(np.fabs(la2['d'] - la['d'])) self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon)) if np.any(np.fabs(lo2['d'] - lo['d']) > self.epsilon): print('lo2:', lo2['d']) print('lo :', lo['d']) print(np.fabs(lo2['d'] - lo['d'])) self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon))
def test_fstecr_fstinf_fstluk(self): """fstinf, fstluk should give known result with known input""" rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) (la,lo) = self.create_basefile() #wrote 2 recs in that order: la, lo a = rmn.isFST(self.fname) self.assertTrue(a) funit = rmn.fstopenall(self.fname,rmn.FST_RW) nrec = rmn.c_fstnbrv(funit) keylist = rmn.fstinl(funit) kla = rmn.fstinf(funit,nomvar='LA')['key'] la2prm = rmn.fstprm(kla)#,rank=2) la2 = rmn.fstluk(kla)#,rank=2) klo = rmn.fstinf(funit,nomvar='LO') lo2 = rmn.fstluk(klo)#,rank=2) rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(nrec,2,' c_fstnbrv found %d/2 rec ' % nrec) self.assertEqual(len(keylist),2,'fstinl found %d/2 rec: %s' % (len(keylist),repr(keylist))) self.assertEqual(la2['nomvar'].strip(),la['nomvar'].strip()) self.assertEqual(lo2['nomvar'].strip(),lo['nomvar'].strip()) self.assertEqual(la2['d'].shape,la['d'].shape) self.assertEqual(lo2['d'].shape,lo['d'].shape) if np.any(np.fabs(la2['d'] - la['d']) > self.epsilon): print('la2:',la2['d']) print('la :',la['d']) print(np.fabs(la2['d'] - la['d'])) self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon)) if np.any(np.fabs(lo2['d'] - lo['d']) > self.epsilon): print('lo2:',lo2['d']) print('lo :',lo['d']) print(np.fabs(lo2['d'] - lo['d'])) self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon))
from os import environ from os.path import join import rpnpy.librmn.all as rmn print 'librmn=', rmn.librmn data_file = join(environ['CMCGRIDF'], 'prog', 'gsloce', '2015070706_042') funit = rmn.fstopenall(data_file, rmn.FST_RO) #r = rmn.fstlir(funit, nomvar='UUW', typvar='P@') k = rmn.fstinf(funit, nomvar='UUW', typvar='P@') ## print k ## print rmn.fstprm(k['key']) r = rmn.fstluk(k['key']) l = rmn.fstinl(funit) print 'data_file=', data_file, len(l) for k in l: p = rmn.fstprm(k) print p['nomvar'], p['typvar'], p['datyp'] if p['datyp'] != 1344: r = rmn.fstluk(k) del (r['d']) del r rmn.fstcloseall(funit)
// need to ID the inputs, can they be lists?, first one is the argument, second is the value? // alternatively, save the inputs into a list ## default values if not provided by commandline defaultPath = os.path.direname(os.path.realpath(__file__)) //writes the full path, can maybe use current working directory: os.getcwd() defaultSpc = 'TO3' deafaultHy = [1.5M] //double check that this is right defaultIncrLatLon = 0.1 def getSpcData (direct=defaultPath, spc=defaultSpc, hy=deafaultHy, incrLatLon=defaultIncrLatLon): //, dlat, dlong, date - don't think I need these //use sys arguments if provided # open file found in directory, for the concentration_plot function, loop through each one # for the demo file, assuming only one file and one hy fileName = os. something //get file name fileID = rmn.fstopenall(fileName, rmn.FST_RO) //open file in read only mode keylist = rmn.fstinl(fileName, nomvar=spc, ip1=hy) rec = rmn.fstluk(keylist) //reads metadata from keylist data = rec['d'] //saves metadata in a list // add something to record the data value, x and y position // plot end results latRange = (-90,90,defaultIncrLatLon) /all possible ranges of latitude lonRange = (0,180,defaultIncrLatLon) /all possible ranges of longitude //should actually take lat and lon as inputs, otherwise generating over very large area dataList = [] for i in data: #all rows of data // maybe need to get lat lon to get correct data? subList = [data[i]] //correct syntax for creating a list? //note,can't use tuples because need to modify later
def test_23(self): """ Edit: Read, Edit, Write records with meta, grid and vgrid This example shows how to * select records in a RPNStd file * read the record data + meta * edit/use record data and meta (compute the wind velocity) * write the recod data + meta * copy (read/write) the record grid descriptors * copy (read/write) the file vgrid descriptor See also: rpnpy.librmn.fstd98.fstopt rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.fsrinl rpnpy.librmn.fstd98.fsrluk rpnpy.librmn.fstd98.fsrlir rpnpy.librmn.fstd98.fsrecr rpnpy.librmn.grids.readGrid rpnpy.librmn.grids.writeGrid rpnpy.vgd.base.vgd_read rpnpy.vgd.base.vgd_write rpnpy.librmn.const rpnpy.vgd.const """ import os, sys, datetime from scipy.constants import knot as KNOT2MS import numpy as np import rpnpy.librmn.all as rmn import rpnpy.vgd.all as vgd fdate = datetime.date.today().strftime('%Y%m%d') + '00_048' CMCGRIDF = os.getenv('CMCGRIDF').strip() fileNameIn = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate) fileNameOut = 'uvfstfile.fst' # Restrict to the minimum the number of messages printed by librmn rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) # Open Files try: fileIdIn = rmn.fstopenall(fileNameIn) fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) except: sys.stderr.write("Problem opening the files: %s, %s\n" % (fileNameIn, fileNameOut)) sys.exit(1) try: # Copy the vgrid descriptor v = vgd.vgd_read(fileIdIn) vgd.vgd_write(v, fileIdOut) print("CB23: Copied the vgrid descriptor") # Loop over the list of UU records to copy uu = {'d': None} vv = {'d': None} uvarray = None copyGrid = True for k in rmn.fstinl(fileIdIn, nomvar='UU'): # Read the UU record data and meta from fileNameIn # Provide data array to re-use memory uu = rmn.fstluk(k, dataArray=uu['d']) # Read the corresponding VV # Provide data array to re-use memory vv = rmn.fstlir(fileIdIn, nomvar='VV', ip1=uu['ip1'], ip2=uu['ip2'], datev=uu['datev'], dataArray=vv['d']) # Compute the wind modulus in m/s # Copy metadata from the UU record # Create / re-use memory space for computation results uv = uu.copy() if uvarray is None: uvarray = np.empty(uu['d'].shape, dtype=uu['d'].dtype, order='FORTRAN') uv['d'] = uvarray uv['d'][:, :] = np.sqrt(uu['d']**2. + vv['d']**2.) uv['d'] *= KNOT2MS # Convert from knot to m/s # Set new record name and Write it to fileNameOut uv['nomvar'] = 'WSPD' rmn.fstecr(fileIdOut, uv) print("CB23: Wrote %s ip1=%d, ip2=%d, dateo=%s : mean=%f" % (uv['nomvar'], uv['ip1'], uv['ip2'], uv['dateo'], uv['d'].mean())) # Read and Write grid (only once, all rec are on the same grid) if copyGrid: copyGrid = False g = rmn.readGrid(fileIdIn, uu) rmn.writeGrid(fileIdOut, g) print("CB23: Copied the grid descriptors") except: pass finally: # Properly close files even if an error occured above # This is important when editing to avoid corrupted files rmn.fstcloseall(fileIdIn) rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file
from os import environ from os.path import join import rpnpy.librmn.all as rmn print 'librmn=',rmn.librmn data_file = join(environ['CMCGRIDF'], 'prog', 'gsloce', '2015070706_042') funit = rmn.fstopenall(data_file,rmn.FST_RO) #r = rmn.fstlir(funit, nomvar='UUW', typvar='P@') k = rmn.fstinf(funit, nomvar='UUW', typvar='P@') ## print k ## print rmn.fstprm(k['key']) r = rmn.fstluk(k['key']) l = rmn.fstinl(funit) print 'data_file=',data_file,len(l) for k in l: p = rmn.fstprm(k) print p['nomvar'], p['typvar'], p['datyp'] if p['datyp'] != 1344: r = rmn.fstluk(k) del(r['d']) del r rmn.fstcloseall(funit)
shell=True) # On recupere les lat/lon simulees par MLDP pathMLDP = '/fs/cetus/fs3/mrb/armn/armnkch/VerifGlace/MLDP_Output/SortiesBouees/' + str( date) filesMLDP = [f for f in listdir(pathMLDP) if isfile(join(pathMLDP, f))] for files in filesMLDP: if files.endswith('024'): fileName = pathMLDP + '/' + files try: fileId = rmn.fstopenall(fileName) except: sys.stderr.write("Problem opening the file: %s\n" % fileName) try: # Get the list of record keys matching nomvar='LA', 'LO' keylist1 = rmn.fstinl(fileId, nomvar='LA') keylist2 = rmn.fstinl(fileId, nomvar='LO') MLDP_lat = np.zeros(len(keylist1)) MLDP_lon = np.zeros(len(keylist1)) MLDP_etiket = [] for k in keylist1: r = rmn.fstluk(k) MLDP_lat[keylist1.index(k)] = r['d'] MLDP_etiket.append(r['etiket'].strip()) for k in keylist2: r = rmn.fstluk(k) MLDP_lon[keylist2.index(k)] = r['d'] - 360 except: pass finally: # Close file even if an error occured above
defaultSpc='TO3' defaultFile='2010071000_001' ## getConc (level,spc,fileName) returns an array of concentration data with dimensions of the file shape attirbute def getConc(level=defaultIp1, spc=defaultSpc, fileName=defaultFile): # may need full directory path to the file, thus may need to move the path where this program is taking place fileID = rmn.fstopenall(fileName, rmn.FST_RO) # opens file within the directory, else may need path dataKey = rmn.fstinf(fileID, nomvar=spc, ip1=level)['key'] # get key for the matching data # Note may need to turn this into a loop if it matches more than one dataRec = rmn.fstluk(dataKey) concData = dataRec['d'] return concData, dataKey >>> keyList = rmn.fstinl(fileOpen,nomvar=defaultSpc, ip1 ... =defaultIp1) >>> keyList [6311937] >>> rec = rmn.fstluk(int(keyList[0])) Read(999) TO3 P GM_DEV 772 642 1 363996800 76696048 1 0 300 12 f 16 Z 60193 56056 1 0 >>> O3=rmn.fstinf(fileOpen,nomvar=defaultSpc,ip1=defaultIp1) >>> O3 {'shape': (772, 642, 1), 'key': 6311937} >>> O3Key=rmn.fstinf(fileOpen,nomvar=defaultSpc,ip1=defaultIp1) >>> O3Key {'shape': (772, 642, 1), 'key': 6311937}
def get_model(overpass, lonArray, latArray, fst_dir, ctrl_dir=None, var='AF', threshold=4, filestart=00): if filestart == 12: file_time = (overpass if overpass.minute < 30 else overpass + datetime.timedelta(hours=1)) - datetime.timedelta( hours=12) file_path = "{0}/{1}".format(os.path.abspath(fst_dir), file_time.strftime('%Y%m%d12_0%H')) elif filestart == 0: file_time = (overpass if overpass.minute < 30 else overpass + datetime.timedelta(hours=1)) file_path = "{0}/{1}".format(os.path.abspath(fst_dir), file_time.strftime('%Y%m%d00_0%H')) rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) rmn.ezsetopt(rmn.EZ_OPT_INTERP_DEGREE, rmn.EZ_INTERP_NEAREST) fid = rmn.fstopenall(file_path, rmn.FST_RO) lonarr = np.array(lonArray, dtype='float32') latarr = np.array(latArray, dtype='float32') keylist = rmn.fstinl(fid, nomvar=var) dir = os.path.abspath(fst_dir) ctrl_dir = os.path.abspath(ctrl_dir) if ctrl_dir else None # assuming this for now ref_lvl = 'sea' height = [float('-inf')] * len(lonarr) value = [float('-inf')] * len(lonarr) points = [{} for _ in lonarr] iplist = [] for key in keylist: meta = rmn.fstprm(key) iplist.append( rmn.DecodeIp(meta['ip1'], meta['ip2'], meta['ip3'])[0].v1) sorted_keylist = (x for _, x in sorted(zip(iplist, keylist), reverse=True)) next(sorted_keylist, None) before_val = [float('inf')] * len(lonarr) cur_meta, cur_grid, cur_xypos, cur_val = data_from_key( next(sorted_keylist), fid, lonarr, latarr) if ctrl_dir: if filestart == 12: ctrl_path = "{0}/{1}".format(os.path.abspath(ctrl_dir), file_time.strftime('%Y%m%d12_0%H')) elif filestart == 0: ctrl_path = "{0}/{1}".format(os.path.abspath(ctrl_dir), file_time.strftime('%Y%m%d00_0%H')) ctrl_fid = rmn.fstopenall(ctrl_path, rmn.FST_RO) ctrl_keylist = rmn.fstinl(ctrl_fid, nomvar=var) sorted_ctrl_keylist = ( x for _, x in sorted(zip(iplist, ctrl_keylist), reverse=True)) next(sorted_ctrl_keylist, None) _, _, _, ctrl_val = data_from_key(next(sorted_ctrl_keylist), ctrl_fid, lonarr, latarr) cur_val -= ctrl_val for progress_ind, after_key in enumerate(sorted_keylist): after_meta, after_grid, after_xypos, after_val = data_from_key( after_key, fid, lonarr, latarr) if ctrl_dir: after_ctrl_key = next(sorted_ctrl_keylist) _, _, _, after_ctrl_val = data_from_key(after_ctrl_key, ctrl_fid, lonarr, latarr) after_val -= after_ctrl_val for ind, val in enumerate(cur_val): if ((val > before_val[ind]) and (val > after_val[ind]) and (val >= threshold) and (val > value[ind])): try: if int(ind) <= 20: print( 'Updating GZ, val: {}, existing val: {}'.format( val, value[ind]), ind, cur_meta['ip1']) gzkey = rmn.fstinf(fid, nomvar='GZ', ip1=cur_meta['ip1'])['key'] gzdata = rmn.fstluk(gzkey)['d'] meta = rmn.fstprm(gzkey) meta['iunit'] = fid gz_grid = rmn.ezqkdef(meta) heightList = rmn.gdxysval(gz_grid, cur_xypos['x'], cur_xypos['y'], gzdata) * 10 height[ind] = float(heightList[ind]) value[ind] = float(val) #print (height[ind], ind) print(height, value) except TypeError: continue before_val = cur_val cur_meta, cur_grid, cur_xypos, cur_val = after_meta, after_grid, after_xypos, after_val print height rmn.fstcloseall(fid) if ctrl_dir: rmn.fstcloseall(ctrl_fid) #print(height) return height, value
def getData(level, spc, fileID): keylist = rmn.fstinl(fileID, nomvar=spc, ip1=level) rec = rmn.fstluk(keylist) data = rec['d'] return rec, data
def plotFSTs(season=season, spcs=spcs, spcsFiles=spcsFiles, outputName = outputName, saveDir=saveDir): # print minimum outputs rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) mInds = [] for m in season: mInds += [monthList.index(m)] if os.path.exists(saveDir) == False: nu.makeDir(saveDir) for spcInd, nomvar in enumerate(spcs): try: filename = os.path.join(saveDir, 'output_file_{0}_{1}.fst'.format(outputName, nomvar)) print('Creating and saving to {}'.format(filename)) tmp = open(filename, 'w+'); tmp.close() output_file = filename file_id = rmn.fnom(output_file) open_fst = rmn.fstouv(file_id, rmn.FST_RW) open_file = spcsFiles[spcInd] print "Parameter: " + nomvar seaSpcData = get_var(pyg.open(open_file), nomvar, mInds) nc_lnsp = pyg.open(lnsp_file) pressures = get_pressures(nc_lnsp, mInds) timelen, levlen, latlen, lonlen = seaSpcData.shape #NOTE: uncomment the following three lines to prep data for basemap use #lonShiftSSData = shift_lon(seaSpcData) #vertInterpSSData = vert_interp(pressures, lonShiftSSData) #meanSSData = np.mean(vertInterpSSData, axis=0) #NOTE: uncommment the following four liness to use for fst plotting vertInterpSSData = vert_interp(pressures, seaSpcData) meanSSData = np.mean(vertInterpSSData, axis=0) # temp for lvl, ray in enumerate(meanSSData): meanSSData[lvl] = np.flipud(ray) scaleFac = scaleSpcs[allSpcs.index(nomvar)] scaledSSData = meanSSData*scaleFac #define grid for this file - note that the MACC grid in the file is #defined for lons -180 to 180, but the python defGrid_L can't deal #with that and defines the grid from 0 to 360 so will have to reorder #the MACC fields a bit, or they end up 180 deg out of phase # Also necessary to add one more longitude to wrap around dlatlon = 360./lonlen # this is equal to the resolution of the grid params0 = { 'grtyp' : 'Z', 'grref' : 'L', 'nj' : latlen, 'ni' : lonlen, 'lat0' : -90., 'lon0' : 0., 'dlat' : dlatlon, 'dlon' : dlatlon } MACC_grid= rmn.encodeGrid(params0) print("Grids created.") print 'Grid Shape:' + str(MACC_grid['shape']) # copies the default record new_record = rmn.FST_RDE_META_DEFAULT.copy() tic_record = rmn.FST_RDE_META_DEFAULT.copy() tac_record = rmn.FST_RDE_META_DEFAULT.copy() try: rmn.writeGrid(file_id, MACC_grid) tac = rmn.fstinl(file_id, nomvar='>>')[0] tic = rmn.fstinl(file_id, nomvar='^^')[0] tic_record.update(rmn.fstprm(tic)) tac_record.update(rmn.fstprm(tac)) tic_record.update({'datyp' : rmn.FST_DATYP_LIST['float']}) tac_record.update({'datyp' : rmn.FST_DATYP_LIST['float']}) rmn.fsteff(tic) rmn.fsteff(tac) tic_record.update({'d': MACC_grid['ay']}) tac_record.update({'d': MACC_grid['ax']}) toc_record = vgd.vgd_new_pres(const_pressure, ip1=MACC_grid['ig1'], ip2=MACC_grid['ig2']) rmn.fstecr(file_id, tic_record) # write the dictionary record to the file as a new record rmn.fstecr(file_id, tac_record) # write the dictionary record to the file as a new record vgd.vgd_write(toc_record, file_id) except: raise for rp1 in xrange(len(const_pressure)): # writes a record for every level (as a different ip1) try: # converts rp1 into a ip1 with pressure kind ip1 = rmn.convertIp(rmn.CONVIP_ENCODE, const_pressure[rp1], rmn.KIND_PRESSURE) new_record.update(MACC_grid) new_record.update({ # Update with specific meta 'nomvar': nomvar, 'typvar': 'C', 'etiket': 'MACCRean', 'ni' : MACC_grid['ni'], 'nj' : MACC_grid['nj'], 'ig1' : tic_record['ip1'], 'ig2' : tic_record['ip2'], 'ig3' : tic_record['ip3'], 'ig4' : tic_record['ig4'], 'dateo' : rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20120101, 0000000), 'deet' : 0, # Timestep in sec 'ip1' : ip1 }) #tmp_nparray = np.asfortranarray(monthly_mean[rp1]) tmp = scaledSSData[rp1] tmp = np.transpose(tmp) # data array is structured as tmp = monthly_mean[level] where monthly_mean is [level, lat, lon] new_record.update({'d': tmp.astype(np.float32)}) # Updates with data array in the form (lon x lat) print "Defined a new record with dimensions ({0}, {1})".format(new_record['ni'], new_record['nj']) rmn.fstecr(file_id, new_record) # write the dictionary record to the file as a new record except: #rmn.closeall(file_id) rmn.fstfrm(file_id) rmn.fclos(file_id) raise rmn.fstfrm(file_id) rmn.fclos(file_id) print('{} complete~'.format(filename)) except: rmn.fstfrm(file_id) rmn.fclos(file_id) raise print('Finished plotting all FSTs. ')
def getKeys(fileID, ip1, spc, spcApp): ''' Gets a list of keys from an FST based on ip1, spc (and any modifications to spc name). ''' spcs = '{}{}'.format(spc, spcApp) keyList = rmn.fstinl(fileID, nomvar=spcs, ip1=ip1) return keyList
def getDateO(fileID, s, ip1): keyList = rmn.fstinl(fileID, nomvar='{}1')
def test_23(self): """ Edit: Read, Edit, Write records with meta, grid and vgrid This example shows how to * select records in a RPNStd file * read the record data + meta * edit/use record data and meta (compute the wind velocity) * write the recod data + meta * copy (read/write) the record grid descriptors * copy (read/write) the file vgrid descriptor See also: rpnpy.librmn.fstd98.fstopt rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.fsrinl rpnpy.librmn.fstd98.fsrluk rpnpy.librmn.fstd98.fsrlir rpnpy.librmn.fstd98.fsrecr rpnpy.librmn.grids.readGrid rpnpy.librmn.grids.writeGrid rpnpy.vgd.base.vgd_read rpnpy.vgd.base.vgd_write rpnpy.librmn.const rpnpy.vgd.const """ import os, sys, datetime from scipy.constants import knot as KNOT2MS import numpy as np import rpnpy.librmn.all as rmn import rpnpy.vgd.all as vgd fdate = datetime.date.today().strftime('%Y%m%d') + '00_048' CMCGRIDF = os.getenv('CMCGRIDF').strip() fileNameIn = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate) fileNameOut = 'uvfstfile.fst' # Restrict to the minimum the number of messages printed by librmn rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) # Open Files try: fileIdIn = rmn.fstopenall(fileNameIn) fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) except: sys.stderr.write("Problem opening the files: %s, %s\n" % (fileNameIn, fileNameOut)) sys.exit(1) try: # Copy the vgrid descriptor v = vgd.vgd_read(fileIdIn) vgd.vgd_write(v, fileIdOut) print("CB23: Copied the vgrid descriptor") # Loop over the list of UU records to copy uu = {'d': None} vv = {'d': None} uvarray = None copyGrid = True for k in rmn.fstinl(fileIdIn, nomvar='UU'): # Read the UU record data and meta from fileNameIn # Provide data array to re-use memory uu = rmn.fstluk(k, dataArray=uu['d']) # Read the corresponding VV # Provide data array to re-use memory vv = rmn.fstlir(fileIdIn, nomvar='VV', ip1=uu['ip1'], ip2=uu['ip2'], datev=uu['datev'], dataArray=vv['d']) # Compute the wind modulus in m/s # Copy metadata from the UU record # Create / re-use memory space for computation results uv = uu.copy() if uvarray is None: uvarray = np.empty(uu['d'].shape, dtype=uu['d'].dtype, order='FORTRAN') uv['d'] = uvarray uv['d'][:,:] = np.sqrt(uu['d']**2. + vv['d']**2.) uv['d'] *= KNOT2MS # Convert from knot to m/s # Set new record name and Write it to fileNameOut uv['nomvar'] = 'WSPD' rmn.fstecr(fileIdOut, uv) print("CB23: Wrote %s ip1=%d, ip2=%d, dateo=%s : mean=%f" % (uv['nomvar'], uv['ip1'], uv['ip2'], uv['dateo'], uv['d'].mean())) # Read and Write grid (only once, all rec are on the same grid) if copyGrid: copyGrid = False g = rmn.readGrid(fileIdIn, uu) rmn.writeGrid(fileIdOut, g) print("CB23: Copied the grid descriptors") except: pass finally: # Properly close files even if an error occured above # This is important when editing to avoid corrupted files rmn.fstcloseall(fileIdIn) rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file