def test_fstecr_fstluk_order(self): rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) fname = '__rpnstd__testfile2__.fst' try: os.unlink(fname) except: pass funit = rmn.fstopenall(fname, rmn.FST_RW) (ig1, ig2, ig3, ig4) = rmn.cxgaig(self.grtyp, self.xg14[0], self.xg14[1], self.xg14[2], self.xg14[3]) (ni, nj) = (90, 45) la = rmn.FST_RDE_META_DEFAULT.copy() la.update({ 'nomvar': 'LA', 'typvar': 'C', 'ni': ni, 'nj': nj, 'nk': 1, 'grtyp': self.grtyp, 'ig1': ig1, 'ig2': ig2, 'ig3': ig3, 'ig4': ig4 }) lo = la.copy() lo['nomvar'] = 'LO' #Note: For the order to be ok in the FSTD file, order='FORTRAN' is mandatory la['d'] = np.empty((ni, nj), dtype=np.float32, order='FORTRAN') lo['d'] = np.empty((ni, nj), dtype=np.float32, order='FORTRAN') for j in range(nj): for i in range(ni): lo['d'][i, j] = 100. + float(i) la['d'][i, j] = float(j) rmn.fstecr(funit, la['d'], la) rmn.fstecr(funit, lo) rmn.fstcloseall(funit) funit = rmn.fstopenall(fname, rmn.FST_RW) kla = rmn.fstinf(funit, nomvar='LA')['key'] la2 = rmn.fstluk(kla) #,rank=2) klo = rmn.fstinf(funit, nomvar='LO')['key'] lo2 = rmn.fstluk(klo) #,rank=2) rmn.fstcloseall(funit) try: os.unlink(fname) except: pass self.assertTrue(np.isfortran(la2['d'])) self.assertTrue(np.isfortran(lo2['d'])) self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon)) self.assertFalse(np.any(np.fabs(lo2['d'] - lo['d']) > self.epsilon))
def test_fstecr_fstluk_order(self): rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) fname = '__rpnstd__testfile2__.fst' try: os.unlink(fname) except: pass funit = rmn.fstopenall(fname,rmn.FST_RW) (ig1,ig2,ig3,ig4) = rmn.cxgaig(self.grtyp,self.xg14[0],self.xg14[1],self.xg14[2],self.xg14[3]) (ni,nj) = (90,45) la = rmn.FST_RDE_META_DEFAULT.copy() la.update( {'nomvar' : 'LA', 'typvar' : 'C', 'ni' : ni, 'nj' : nj, 'nk' : 1, 'grtyp' : self.grtyp, 'ig1' : ig1, 'ig2' : ig2, 'ig3' : ig3, 'ig4' : ig4 } ) lo = la.copy() lo['nomvar'] = 'LO' #Note: For the order to be ok in the FSTD file, order='FORTRAN' is mandatory la['d'] = np.empty((ni,nj),dtype=np.float32,order='FORTRAN') lo['d'] = np.empty((ni,nj),dtype=np.float32,order='FORTRAN') for j in xrange(nj): for i in xrange(ni): lo['d'][i,j] = 100.+float(i) la['d'][i,j] = float(j) rmn.fstecr(funit,la['d'],la) rmn.fstecr(funit,lo) rmn.fstcloseall(funit) funit = rmn.fstopenall(fname,rmn.FST_RW) kla = rmn.fstinf(funit,nomvar='LA')['key'] la2 = rmn.fstluk(kla)#,rank=2) klo = rmn.fstinf(funit,nomvar='LO')['key'] lo2 = rmn.fstluk(klo)#,rank=2) rmn.fstcloseall(funit) try: os.unlink(fname) except: pass self.assertTrue(np.isfortran(la2['d'])) self.assertTrue(np.isfortran(lo2['d'])) self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon)) self.assertFalse(np.any(np.fabs(lo2['d'] - lo['d']) > self.epsilon))
def test_14bqd(self): import os, sys, datetime import numpy as np import rpnpy.librmn.all as rmn import rpnpy.vgd.all as vgd rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) fdate = datetime.date.today().strftime('%Y%m%d') + '00_048' CMCGRIDF = os.getenv('CMCGRIDF').strip() fileId = rmn.fstopenall(CMCGRIDF+'/prog/regpres/'+fdate, rmn.FST_RO) v = vgd.vgd_read(fileId) (tlvlkeys, rshape) = ([], None) for ip1 in vgd.vgd_get(v, 'VIPT'): (lval, lkind) = rmn.convertIp(rmn.CONVIP_DECODE, ip1) key = rmn.fstinf(fileId, nomvar='TT', ip2=48, ip1=rmn.ip1_all(lval, lkind)) if key is not None: tlvlkeys.append((ip1, key['key'])) if rshape is None and key is not None: rshape = key['shape'] (r2d, r3d, k, rshape) = ({'d' : None}, None, 0, (rshape[0], rshape[1], len(tlvlkeys))) for ip1, key in tlvlkeys: r2d = rmn.fstluk(key, dataArray=r2d['d']) if r3d is None: r3d = r2d.copy() r3d['d'] = np.empty(rshape, dtype=r2d['d'].dtype, order='FORTRAN') r3d['d'][:,:,k] = r2d['d'][:,:] rmn.fstcloseall(fileId) r3d.update({'vgd':v, 'ip1list':[x[0] for x in tlvlkeys], 'shape':rshape, 'nk':rshape[2]}) (i1, j1) = (rshape[0]//2, rshape[1]//2) print("CB14bqd: The TT profile at point (%d, %d) is:" % (i1, j1)) for k in xrange(rshape[2]): (ldiagval, ldiagkind) = rmn.convertIp(rmn.CONVIP_DECODE, r3d['ip1list'][k]) print("CB14bqd: TT(%d, %d, %7.2f %s) = %6.1f C [mean=%6.1f, std=%6.1f, min=%6.1f, max=%6.1f]" % (i1, j1, ldiagval, rmn.kindToString(ldiagkind), r3d['d'][i1,j1,k], r3d['d'][:,:,k].mean(), r3d['d'][:,:,k].std(), r3d['d'][:,:,k].min(), r3d['d'][:,:,k].max()))
def getConc(filePath, level, spc, niRange, njRange): """ Get the concentration data for an FST file. Returns the data in an array, the datakey as a key and the fileID of the FST file. """ try: fileID = rmn.fstopenall(filePath, rmn.FST_RO) dataKey = rmn.fstinf(fileID, nomvar=spc, ip1=level)['key'] dataRec = rmn.fstluk(dataKey) tempConc = dataRec['d'] print(len(tempConc), len(tempConc[0])) print(tempConc) concData = dataRec['d'][niRange[0]:niRange[1] + 1, njRange[0]:njRange[1] + 1] print('File {} recorded'.format(filePath)) return {'concData': concData, 'dataKey': dataKey, 'fileID': fileID} except TypeError: print('Unable to record file {}. Please see log for details'.format( filePath)) # log an error into the log file logging.warning( 'nomvar {} and ip1 {} could not be found for file {}.'.format( fileID, spc, level, filePath)) pass
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_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 getConc(fileID, spc, ip1): try: dataKey1 = rmn.fstinf(fileID, nomvar='{}1'.format(spc), ip1=ip1)['key'] dataRec1 = rmn.fstluk(dataKey1) concData1 = dataRec1['d'] meta = dataRec1 del meta['d'] del meta['datyp'] dataKey2 = rmn.fstinf(fileID, nomvar='{}2'.format(spc), ip1=ip1)['key'] dataRec2 = rmn.fstluk(dataKey2) concData2 = dataRec2['d'] concData = sum(concData1, concData2) return concData, meta, dataKey1, dataKey2 except: print('WARNING: Data does not exist for ip1 {} and spc {}.'.format( ip1, spc))
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
def data_from_key(key, file_id, lonarr, latarr): meta = rmn.fstprm(key) meta['iunit'] = file_id data = rmn.fstluk(key)['d'] grid = rmn.ezqkdef(meta) xypos = rmn.gdxyfll(grid, latarr, lonarr) val = rmn.gdxysval(grid, xypos['x'], xypos['y'], data) return meta, grid, xypos, val
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 getConc(fileID, spc): """ Get the concentration data for an FST file and a species. IP1 is a default value. The dataKey and dataRec is also returned for further use. """ dataKey = rmn.fstinf(fileID, nomvar=spc, ip1=ip1)['key'] dataRec = rmn.fstluk(dataKey) concData = dataRec['d'] return concData, dataKey, dataRec
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 getData(filePath, spc, level): try: fileID = rmn.fstopenall(filePath, rmn.FST_RO) print('Opened file!') dataKey = rmn.fstinf(fileID, nomvar=spc, ip1=level)['key'] dataRec = rmn.fstluk(dataKey) concData = dataRec['d'] return concData, dataKey, fileID except: print('ERROR: Could not find data for {} and {}. Please try again.'. format(spc, level))
def getMeta(fileID, key): ''' Gets the meta data from a key. Additionally returns the concentration data and the dateo. ''' meta = rmn.fstluk(key) concData = meta['d'] dateo = meta['dateo'] del meta['d'] del meta['datyp'] return meta, concData, dateo
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_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 fstluk(ihandle): """Read record data on file (Interface to fstluk) myRecDataDict = Fstdc.fstluk(ihandle) @param ihandle record handle (int) @return record data (numpy.ndarray) @exception TypeError @exception Fstdc.error """ try: return _rmn.fstluk(ihandle)['d'] except: raise error("")
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 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_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_3(self): """ Read a record fstluk is the main function used to read record data and metadata after a search for the record handle. Other Librmn functions that find/seclect and read at the same time can also be used: fstlir, fstlirx, fstlis See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinf rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.fstluk rpnpy.librmn.fstd98.fstlir rpnpy.librmn.fstd98.fstlirx rpnpy.librmn.fstd98.fstlis rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.FSTDError rpnpy.librmn.RMNError rpnpy.librmn.const """ import os import numpy as np 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') pr_rec = None #Open file and read record data and metadata try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) pr_key = rmn.fstinf( fileId, nomvar='PR')['key'] #this match the first record named PR if pr_key: pr_rec = rmn.fstluk(pr_key) except: raise rmn.FSTDError("Problem reading record in file: %s" % fileName) # Close rmn.fstcloseall(fileId) # Computations if pr_rec: average = np.average(pr_rec['d']) print("Read nomvar=%s, dateo=%s, hour=%s, average=%s" % (pr_rec['nomvar'], str(pr_rec['dateo']), str( pr_rec['ip2']), str(average)))
def test_14bqd(self): import os, sys, datetime import numpy as np import rpnpy.librmn.all as rmn import rpnpy.vgd.all as vgd rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) fdate = datetime.date.today().strftime('%Y%m%d') + '00_048' CMCGRIDF = os.getenv('CMCGRIDF').strip() fileId = rmn.fstopenall(CMCGRIDF + '/prog/regpres/' + fdate, rmn.FST_RO) v = vgd.vgd_read(fileId) (tlvlkeys, rshape) = ([], None) for ip1 in vgd.vgd_get(v, 'VIPT'): (lval, lkind) = rmn.convertIp(rmn.CONVIP_DECODE, ip1) key = rmn.fstinf(fileId, nomvar='TT', ip2=48, ip1=rmn.ip1_all(lval, lkind)) if key is not None: tlvlkeys.append((ip1, key['key'])) if rshape is None and key is not None: rshape = key['shape'] (r2d, r3d, k, rshape) = ({ 'd': None }, None, 0, (rshape[0], rshape[1], len(tlvlkeys))) for ip1, key in tlvlkeys: r2d = rmn.fstluk(key, dataArray=r2d['d']) if r3d is None: r3d = r2d.copy() r3d['d'] = np.empty(rshape, dtype=r2d['d'].dtype, order='FORTRAN') r3d['d'][:, :, k] = r2d['d'][:, :] rmn.fstcloseall(fileId) r3d.update({ 'vgd': v, 'ip1list': [x[0] for x in tlvlkeys], 'shape': rshape, 'nk': rshape[2] }) (i1, j1) = (rshape[0] // 2, rshape[1] // 2) print("CB14bqd: The TT profile at point (%d, %d) is:" % (i1, j1)) for k in range(rshape[2]): (ldiagval, ldiagkind) = rmn.convertIp(rmn.CONVIP_DECODE, r3d['ip1list'][k]) print( "CB14bqd: TT(%d, %d, %7.2f %s) = %6.1f C [mean=%6.1f, std=%6.1f, min=%6.1f, max=%6.1f]" % (i1, j1, ldiagval, rmn.kindToString(ldiagkind), r3d['d'][i1, j1, k], r3d['d'][:, :, k].mean(), r3d['d'][:, :, k].std(), r3d['d'][:, :, k].min(), r3d['d'][:, :, k].max()))
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 getConc(fileID, spc, ip1): """ Gets the concentratio and meta data for a fileID, spc and ip1 if it exists. Else prints a warning. meta data keys 'd' and 'datyp' are removed for this program. To retain original contents, remove the 'del' commands. """ try: dataKey = rmn.fstinf(fileID, nomvar=spc, ip1=ip1)['key'] dataRec = rmn.fstluk(dataKey) concData = dataRec['d'] meta = dataRec del meta['d'] del meta['datyp'] return concData, meta except: print('WARNING: Data does not exist for ip1 {} and spc {}.'.format( ip1, spc))
def test_3(self): """ Read a record fstluk is the main function used to read record data and metadata after a search for the record handle. Other Librmn functions that find/seclect and read at the same time can also be used: fstlir, fstlirx, fstlis See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinf rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.fstluk rpnpy.librmn.fstd98.fstlir rpnpy.librmn.fstd98.fstlirx rpnpy.librmn.fstd98.fstlis rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.FSTDError rpnpy.librmn.RMNError rpnpy.librmn.const """ import os import numpy as np 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') pr_rec = None #Open file and read record data and metadata try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) pr_key = rmn.fstinf(fileId, nomvar='PR')['key'] #this match the first record named PR if pr_key: pr_rec = rmn.fstluk(pr_key) except: raise rmn.FSTDError("Problem reading record in file: %s" % fileName) # Close rmn.fstcloseall(fileId) # Computations if pr_rec: average = np.average(pr_rec['d']) print("Read nomvar=%s, dateo=%s, hour=%s, average=%s" % (pr_rec['nomvar'], str(pr_rec['dateo']), str(pr_rec['ip2']), str(average)))
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 getMeta(filePath, level, spc): try: fileID = rmn.fstopenall(filePath,rmn.FST_RO) dataKey = rmn.fstinf(fileID,nomvar=spc,ip1=level)['key'] dataRec = rmn.fstluk(dataKey) # get the concentration data concData = dataRec['d'] fileMeta = rmn.fstprm(dataKey) fileMeta['iunit'] = fileID gridData = rmn.ezqkdef(fileMeta) gridDecode = rmn.decodeGrid(gridData) # get the lonlat data llGridData = rmn.gdll(gridData) latData = llGridData['lat'] lonData = llGridData['lon'] return concData, lonData.tolist(), latData.tolist() except: print('Could not read file.') pass
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 getConc(filePath, level, spc): """ Get the concentration data for an FST file. Returns the data in an array, the datakey as a key and the fileID of the FST file. """ try: fileID = rmn.fstopenall(filePath,rmn.FST_RO) dataKey = rmn.fstinf(fileID,nomvar=spc,ip1=level)['key'] dataRec = rmn.fstluk(dataKey) concData = dataRec['d'] fileMeta = rmn.fstprm(dataKey) fileMeta['iunit']=fileID gridData = rmn.ezqkdef(fileMeta) llGridData = rmn.gdll(gridData) print ('File {} recorded'.format(filePath)) rmn.fstcloseall(fileID) return concData,llGridData except TypeError: # log an error into the log file #logging.warning('nomvar {} and ip1 {} could not be found for file {}.'.format(fileID, spc, level, filePath)) return None
def ReadFld_etiket(self, infile, field, fhr, etik, fst_rec): filename = infile if not os.path.isfile(filename): # Check that the input file exists print 'The input file was not found:', filename quit() # Open the file if not rmn.isFST( filename ): # Check that the input file is a standard format file raise rmn.FSTDError("Not an FSTD file: %s " % filename) try: # Open the standard format file fstID = rmn.fstopenall(filename, rmn.FST_RO) except: raise rmn.FSTDError("File not found/readable: %s" % filename) # Read in the sample field and metadata varname = field try: var_key = rmn.fstinf(fstID, ip2=fhr, nomvar=varname, etiket=etik)[ 'key'] # Find the 1st record with the matching name except: raise rmn.FSTDError( "Problem searching for record " + varname + " in file: %s" % filename) # Issue a warning message if var_key: # Process the current record try: fst_rec = rmn.fstluk(var_key) # Read the record except: raise rmn.FSTDError("Problem defining grid of file: %s" % filename) rmn.fstcloseall(fstID) # Close the standard format file return fst_rec
def getConcData(key): dataRec = rmn.fstluk(key) concData = dataRec['d'] return concData
// 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 for lat in range latRange:
def test_51(self): """ Vertical Interpolation See also: scipy.interpolate.interp1d """ import os, sys, datetime import numpy as np from scipy.interpolate import interp1d as scipy_interp1d import rpnpy.librmn.all as rmn import rpnpy.vgd.all as vgd MB2PA = 100. # Restric to the minimum the number of messages printed by librmn rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) # Open Input file hour = 48 fdate = datetime.date.today().strftime('%Y%m%d') + '00_0' + str(hour) CMCGRIDF = os.getenv('CMCGRIDF').strip() fileNameOut = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate) try: fileIdIn = rmn.fstopenall(fileNameOut, rmn.FST_RO) except: sys.stderr.write("Problem opening the input file: %s\n" % fileNameOut) sys.exit(1) try: # Get the vgrid def present in the file # and the full list of ip1 # and the surface reference field name for the coor vIn = vgd.vgd_read(fileIdIn) ip1listIn0 = vgd.vgd_get(vIn, 'VIPT') rfldNameIn = vgd.vgd_get(vIn, 'RFLD') vkind = vgd.vgd_get(vIn, 'KIND') vver = vgd.vgd_get(vIn, 'VERS') VGD_KIND_VER_INV = dict((v, k) for k, v in vgd.VGD_KIND_VER.iteritems()) vtype = VGD_KIND_VER_INV[(vkind,vver)] print("CB51: Found vgrid type=%s (kind=%d, vers=%d) with %d levels, RFLD=%s" % (vtype, vkind, vver, len(ip1listIn0), rfldNameIn)) # Trim the list of thermo ip1 to actual levels in files for TT # since the vgrid in the file is a super set of all levels # and get their "key" ip1Keys = [] rshape = None for ip1 in ip1listIn0: (lval, lkind) = rmn.convertIp(rmn.CONVIP_DECODE, ip1) key = rmn.fstinf(fileIdIn, nomvar='TT', ip2=hour, ip1=rmn.ip1_all(lval, lkind)) if key is not None: print("CB51: Found TT at ip1=%d, ip2=%d" % (ip1, hour)) ip1Keys.append((ip1, key['key'])) if rshape is None: rshape = key['shape'] rshape = (rshape[0], rshape[1], len(ip1Keys)) # Read every level for TT at ip2=hour, re-use 2d array while reading # and store the data in a 3d array # with lower level at nk, top at 0 as in the model r2d = {'d' : None} r3d = None k = 0 gIn = None for ip1, key in ip1Keys: try: r2d = rmn.fstluk(key, dataArray=r2d['d']) print("CB51: Read TT at ip1=%d, ip2=%d" % (ip1, hour)) if r3d is None: r3d = r2d.copy() r3d['d'] = np.empty(rshape, dtype=r2d['d'].dtype, order='FORTRAN') r3d['d'][:,:,k] = r2d['d'][:,:] k += 1 if gIn is None: gIn = rmn.readGrid(fileIdIn, r2d) print("CB51: Read the horizontal grid descriptors") except: pass # Add the vgrid and the actual ip1 list in the r3d dict, update shape and nk r3d['vgd'] = vIn r3d['ip1list'] = [x[0] for x in ip1Keys] r3d['shape'] = rshape r3d['nk'] = rshape[2] # Read the Input reference fields rfldIn = None if rfldNameIn: rfldIn = rmn.fstlir(fileIdIn, nomvar=rfldNameIn, ip2=hour) if rfldNameIn.strip() == 'P0': rfldIn['d'][:] *= MB2PA print("CB51: Read input RFLD=%s at ip2=%d [min=%7.0f, max=%7.0f]" % (rfldNameIn, hour, rfldIn['d'].min(), rfldIn['d'].max())) except: raise # pass finally: # Close file even if an error occured above rmn.fstcloseall(fileIdIn) # Define the destination vertical grid/levels try: lvlsOut = (500.,850.,1000.) vOut = vgd.vgd_new_pres(lvlsOut) ip1listOut = vgd.vgd_get(vOut, 'VIPT') rfldNameOut = vgd.vgd_get(vIn, 'RFLD') rfldOut = None # in this case, Pressure levels, there are no RFLD print("CB51: Defined a Pres vgrid with lvls=%s" % str(lvlsOut)) except: sys.stderr.write("Problem creating a new vgrid\n") sys.exit(1) # Get input and output 3d pressure cubes try: ## if rfldIn is None: ## rfldIn = pIn = vgd.vgd_levels(vIn, ip1list=r3d['ip1list'], rfld=rfldIn['d']) print("CB51: Computed input pressure cube, k0:[min=%7.0f, max=%7.0f], nk:[min=%7.0f, max=%7.0f]" % (pIn[:,:,0].min(), pIn[:,:,0].max(), pIn[:,:,-1].min(), pIn[:,:,-1].max())) if rfldOut is None: rfldOut = rfldIn # provide a dummy rfld for array shape pOut = vgd.vgd_levels(vOut, ip1list=ip1listOut, rfld=rfldOut['d']) print("CB51: Computed output pressure cube, k0:[min=%7.0f, max=%7.0f], nk:[min=%7.0f, max=%7.0f]" % (pOut[:,:,0].min(), pOut[:,:,0].max(), pOut[:,:,-1].min(), pOut[:,:,-1].max())) except: raise sys.stderr.write("Problem computing pressure cubes\n") sys.exit(1) # Use scipy.interpolate.interp1d to vertically interpolate try: ## f = scipy_interp1d(fromLvls, toLvls, kind='cubic', ## assume_sorted=True, bounds_error=False, ## fill_value='extrapolate', copy=False) ## # Unfortunately, looks like interp1d take colomn data ## f = scipy_interp1d(pIn, r3d['d'], kind='cubic', ## bounds_error=False, ## fill_value='extrapolate', copy=False) ## r3dout = f(pOut) ## # Unfortunately, assume_sorted, 'extrapolate' not support in my version ## extrap_value = 'extrapolate' # -99999. ## # Way too slow, needs a C implementation extrap_value = -999. ## for j in xrange(rshape[1]): ## for i in xrange(rshape[0]): ## f = scipy_interp1d(pIn[i,j,:], r3d['d'][i,j,:], ## kind='cubic', ## bounds_error=False, ## fill_value=extrap_value, copy=False) ## r1d = f(pOut[i,j,:]) ## #print i,j,r1d except: raise sys.stderr.write("Problem Interpolating data\n") sys.exit(1)
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)
sys.stderr.write('Error: You need to specify a varname, an fst filename, an outfile name and a lolafile name.\n') parser.print_help() sys.exit(1) inttype = options.inttype[0].lower() if not (inttype in inttypelist.keys()): sys.stderr.write('Error: INTTYPE should be one of: nearest, linear or cubic.\n') parser.print_help() sys.exit(1) # Open and Read RPN STD file try: rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) funit = rmn.fstopenall(options.fstfile,rmn.FST_RO) k = rmn.fstinf(funit,nomvar=options.varname)['key'] data = rmn.fstluk(k)['d'] meta = rmn.fstprm(k) except: raise rmn.RMNError('Problem opening/reading var=%s in File=%s' % (options.varname,options.fstfile)) # Define input record grid try: meta['iunit'] = funit grid = rmn.ezqkdef(meta) except: raise rmn.RMNError('Problem defining input grid for var=%s in File=%s' % (options.varname,options.fstfile)) # Read lat lon file try: (lon,lat) = np.loadtxt(options.lolafile, dtype=np.float32, unpack=True) ## lat = np.asfortranarray(lat, dtype=np.float32)
def test_14b(self): """ Queries: Get Vertical Grid info, Read 3D Field This example shows how to * get the vertical grid definition. * use it to read a 3D field (records on all levels) * then print a profile for this var See also: rpnpy.librmn.fstd98.fstopt rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.fstlinf rpnpy.librmn.fstd98.fstlluk rpnpy.librmn.fstd98.convertIp rpnpy.librmn.fstd98.kindToString rpnpy.vgd.base.vgd_read rpnpy.vgd.base.vgd_get rpnpy.librmn.const rpnpy.vgd.const """ import os, sys, datetime import numpy as np import rpnpy.librmn.all as rmn import rpnpy.vgd.all as vgd # Restrict to the minimum the number of messages printed by librmn rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) # Open file fdate = datetime.date.today().strftime('%Y%m%d') + '00_048' CMCGRIDF = os.getenv('CMCGRIDF').strip() fileName = os.path.join(CMCGRIDF, 'prog', 'regpres', fdate) 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 vgrid definition present in the file v = vgd.vgd_read(fileId) # Get the list of ip1 on thermo levels in this file tlvl = vgd.vgd_get(v, 'VIPT') # Trim the list of thermo ip1 to actual levels in files for TT # since the vgrid in the file is a super set of all levels # and get their "key" tlvlkeys = [] rshape = None for ip1 in tlvl: (lval, lkind) = rmn.convertIp(rmn.CONVIP_DECODE, ip1) key = rmn.fstinf(fileId, nomvar='TT', ip2=48, ip1=rmn.ip1_all(lval, lkind)) if key is not None: tlvlkeys.append((ip1, key['key'])) if rshape is None: rshape = key['shape'] rshape = (rshape[0], rshape[1], len(tlvlkeys)) # Read every level for TT at ip2=48, re-use 2d array while reading # and store the data in a 3d array # with lower level at nk, top at 0 as in the model # Note that for efficiency reasons, if only a profile was needed, # only that profile would be saved instead of the whole 3d field r2d = {'d' : None} r3d = None k = 0 for ip1, key in tlvlkeys: try: r2d = rmn.fstluk(key, dataArray=r2d['d']) if r3d is None: r3d = r2d.copy() r3d['d'] = np.empty(rshape, dtype=r2d['d'].dtype, order='FORTRAN') r3d['d'][:,:,k] = r2d['d'][:,:] k += 1 except: pass except: pass finally: # Close file even if an error occured above rmn.fstcloseall(fileId) # Add the vgrid and the actual ip1 list in the r3d dict, update shape and nk r3d['vgd'] = v r3d['ip1list'] = [x[0] for x in tlvlkeys] r3d['shape'] = rshape r3d['nk'] = rshape[2] # Print a profile of TT and stats by level (i1, j1) = (rshape[0]//2, rshape[1]//2) print("CB14b: The TT profile at point (%d, %d) is:" % (i1, j1)) for k in xrange(rshape[2]): ip1 = r3d['ip1list'][k] (ldiagval, ldiagkind) = rmn.convertIp(rmn.CONVIP_DECODE, ip1) print("CB14b: TT(%d, %d, %7.2f %s) = %6.1f C [mean=%6.1f, std=%6.1f, min=%6.1f, max=%6.1f]" % (i1, j1, ldiagval, rmn.kindToString(ldiagkind), r3d['d'][i1,j1,k], r3d['d'][:,:,k].mean(), r3d['d'][:,:,k].std(), r3d['d'][:,:,k].min(), r3d['d'][:,:,k].max()))
def test_14b(self): """ Queries: Get Vertical Grid info, Read 3D Field This example shows how to * get the vertical grid definition. * use it to read a 3D field (records on all levels) * then print a profile for this var See also: rpnpy.librmn.fstd98.fstopt rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.fstlinf rpnpy.librmn.fstd98.fstlluk rpnpy.librmn.fstd98.convertIp rpnpy.librmn.fstd98.kindToString rpnpy.vgd.base.vgd_read rpnpy.vgd.base.vgd_get rpnpy.librmn.const rpnpy.vgd.const """ import os, sys, datetime import numpy as np import rpnpy.librmn.all as rmn import rpnpy.vgd.all as vgd # Restrict to the minimum the number of messages printed by librmn rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) # Open file fdate = datetime.date.today().strftime('%Y%m%d') + '00_048' CMCGRIDF = os.getenv('CMCGRIDF').strip() fileName = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate) 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 vgrid definition present in the file v = vgd.vgd_read(fileId) # Get the list of ip1 on thermo levels in this file tlvl = vgd.vgd_get(v, 'VIPT') # Trim the list of thermo ip1 to actual levels in files for TT # since the vgrid in the file is a super set of all levels # and get their "key" tlvlkeys = [] rshape = None for ip1 in tlvl: (lval, lkind) = rmn.convertIp(rmn.CONVIP_DECODE, ip1) key = rmn.fstinf(fileId, nomvar='TT', ip2=48, ip1=rmn.ip1_all(lval, lkind)) if key is not None: tlvlkeys.append((ip1, key['key'])) if rshape is None: rshape = key['shape'] rshape = (rshape[0], rshape[1], len(tlvlkeys)) # Read every level for TT at ip2=48, re-use 2d array while reading # and store the data in a 3d array # with lower level at nk, top at 0 as in the model # Note that for efficiency reasons, if only a profile was needed, # only that profile would be saved instead of the whole 3d field r2d = {'d': None} r3d = None k = 0 for ip1, key in tlvlkeys: try: r2d = rmn.fstluk(key, dataArray=r2d['d']) if r3d is None: r3d = r2d.copy() r3d['d'] = np.empty(rshape, dtype=r2d['d'].dtype, order='FORTRAN') r3d['d'][:, :, k] = r2d['d'][:, :] k += 1 except: pass except: pass finally: # Close file even if an error occured above rmn.fstcloseall(fileId) # Add the vgrid and the actual ip1 list in the r3d dict, update shape and nk r3d['vgd'] = v r3d['ip1list'] = [x[0] for x in tlvlkeys] r3d['shape'] = rshape r3d['nk'] = rshape[2] # Print a profile of TT and stats by level (i1, j1) = (rshape[0] // 2, rshape[1] // 2) print("CB14b: The TT profile at point (%d, %d) is:" % (i1, j1)) for k in range(rshape[2]): ip1 = r3d['ip1list'][k] (ldiagval, ldiagkind) = rmn.convertIp(rmn.CONVIP_DECODE, ip1) print( "CB14b: TT(%d, %d, %7.2f %s) = %6.1f C [mean=%6.1f, std=%6.1f, min=%6.1f, max=%6.1f]" % (i1, j1, ldiagval, rmn.kindToString(ldiagkind), r3d['d'][i1, j1, k], r3d['d'][:, :, k].mean(), r3d['d'][:, :, k].std(), r3d['d'][:, :, k].min(), r3d['d'][:, :, k].max()))
def fst_read_3d(fileId, datev=-1, etiket=' ', ip1=-1, ip2=-1, ip3=-1, typvar=' ', nomvar=' ', getPress=False, dtype=None, rank=None, dataArray=None, verbose=False): """ Reads the records matching the research keys into a 3D array along with horizontal and vertical grid info Only provided parameters with value different than default are used as selection criteria field3d = fst_read_3d(iunit, ... ) Args: iunit : unit number associated to the file obtained with fnom+fstouv datev : valid date etiket : label ip1 : vertical levels lists ip2 : forecast hour ip3 : user defined identifier typvar : type of field nomvar : variable name getPress: dtype : array type of the returned data Default is determined from records' datyp Could be any numpy.ndarray type See: http://docs.scipy.org/doc/numpy/user/basics.types.html rank : try to return an array with the specified rank dataArray (ndarray): (optional) allocated array where to put the data verbose : Returns: None if no matching record, else: { 'd' : data, # 3d field data (numpy.ndarray), Fortran order 'g' : hGridInfo, # horizontal grid info as returned by readGrid 'v' : vGridInfo, # vertical grid info as returned by vgd_read 'ip1s' : ip1List # List of ip1 of each level (tuple of int) ... # same params list as fstprm (less ip1) } Raises: TypeError on wrong input arg types ValueError on invalid input arg value FSTDError on any other error Examples: >>> import os, os.path >>> import rpnpy.librmn.all as rmn >>> import rpnpy.utils.fstd3d as fstd3d >>> ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() >>> filename = os.path.join(ATM_MODEL_DFILES,'bcmk') >>> >>> # Open existing file in Rear Only mode >>> funit = rmn.fstopenall(filename, rmn.FST_RO) >>> >>> # Find and read p0 meta and data, then print its min,max,mean values >>> tt3d = fstd3d.fst_read_3d(funit, nomvar='TT') >>> print("# TT ip2={0} min={1} max={2} avg={3}"\ .format(tt3d['ip2'], tt3d['d'].min(), tt3d['d'].max(), tt3d['d'].mean())) # TT ip2=0 min=530.641418 max=1039.641479 avg=966.500000 >>> rmn.fstcloseall(funit) See Also: get_levels_keys fst_write_3d rpnpy.librmn.fstd98.fstlir rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.fstluk rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.grids.readGrid rpnpy.vgd.base.vgd_read """ # Get the list of ip1 on thermo, momentum levels in this file #TODO: if ip1 is provided get the keys for these ip1 vGrid = None tlevels = get_levels_keys(fileId, nomvar, datev, ip2, ip3, typvar, etiket, vGrid=vGrid, thermoMom='VIPT', verbose=verbose) vGrid = tlevels['v'] mlevels = get_levels_keys(fileId, tlevels['nomvar'], tlevels['datev'], tlevels['ip2'], tlevels['ip3'], tlevels['typvar'], tlevels['etiket'], vGrid=vGrid, thermoMom='VIPM', verbose=verbose) ip1keys = tlevels['ip1keys'] if len(mlevels['ip1keys']) > len(tlevels['ip1keys']): if verbose: print("(fst_read_3d) Using Momentum level list") ip1keys = mlevels['ip1keys'] elif verbose: print("(fst_read_3d) Using Thermo level list") if verbose or len(ip1keys) == 0: print("(fst_read_3d) Found {0} records for {1} ip2={2} ip3={3} datev={4} typvar={5} etiket={6}"\ .format(len(ip1keys), nomvar, ip2, ip3, datev, typvar, etiket)) if len(ip1keys) == 0: return None # Read all 2d records and copy to 3d array r3d = None r2d = {'d' : None} k = 0 for ip1, key in ip1keys: r2d = _rmn.fstluk(key, dataArray=r2d['d']) print("Read {nomvar} ip1={ip1} ip2={ip2} ip3={ip3} typv={typvar} etk={etiket}".format(**r2d)) if r3d is None: r3d = r2d.copy() rshape = list(r2d['d'].shape[0:2]) + [len(ip1keys)] if dataArray is None: r3d['d'] = _np.empty(rshape, dtype=r2d['d'].dtype, order='FORTRAN') else: if isinstance(dataArray,_np.ndarray) and dataArray.shape == rshape: r3d['d'] = dataArray else: raise TypeError('Provided dataArray is not the right type or shape') r3d['g'] = _rmn.readGrid(fileId, r2d) print("Read the horizontal grid descriptors for {nomvar}".format(**r2d)) if r2d['d'].shape[0:2] != r3d['d'].shape[0:2]: raise _rmn.RMNError("Wrong shape for input data.") r3d['d'][:,:,k] = r2d['d'][:,:] k += 1 ip1list = [x[0] for x in ip1keys] r3d.update({ 'shape' : r3d['d'].shape, 'ni' : r3d['d'].shape[0], 'nj' : r3d['d'].shape[1], 'nk' : r3d['d'].shape[2], 'ip1' : -1, 'ip1s' : ip1list, 'v' : vGrid }) # Read RFLD and compute pressure on levels if getPress: press = get_levels_press(fileId, vGrid, r3d['d'].shape[0:2], ip1list, tlevels['datev'], tlevels['ip2'], tlevels['ip3'], tlevels['typvar'], tlevels['etiket'], verbose=False) r3d.update({ 'rfld' : press['rfld'], 'phPa' : press['phPa'] }) return r3d
def test_51(self): """ Vertical Interpolation See also: scipy.interpolate.interp1d """ import os, sys, datetime import numpy as np from scipy.interpolate import interp1d as scipy_interp1d import rpnpy.librmn.all as rmn import rpnpy.vgd.all as vgd MB2PA = 100. # Restrict to the minimum the number of messages printed by librmn rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) # Open Input file hour = 48 fdate = datetime.date.today().strftime('%Y%m%d') + '00_0' + str(hour) CMCGRIDF = os.getenv('CMCGRIDF').strip() fileNameOut = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate) try: fileIdIn = rmn.fstopenall(fileNameOut, rmn.FST_RO) except: sys.stderr.write("Problem opening the input file: %s\n" % fileNameOut) sys.exit(1) try: # Get the vgrid def present in the file # and the full list of ip1 # and the surface reference field name for the coor vIn = vgd.vgd_read(fileIdIn) ip1listIn0 = vgd.vgd_get(vIn, 'VIPT') rfldNameIn = vgd.vgd_get(vIn, 'RFLD') vkind = vgd.vgd_get(vIn, 'KIND') vver = vgd.vgd_get(vIn, 'VERS') VGD_KIND_VER_INV = dict( (v, k) for k, v in vgd.VGD_KIND_VER.items()) vtype = VGD_KIND_VER_INV[(vkind, vver)] print( "CB51: Found vgrid type=%s (kind=%d, vers=%d) with %d levels, RFLD=%s" % (vtype, vkind, vver, len(ip1listIn0), rfldNameIn)) # Trim the list of thermo ip1 to actual levels in files for TT # since the vgrid in the file is a super set of all levels # and get their "key" ip1Keys = [] rshape = None for ip1 in ip1listIn0: (lval, lkind) = rmn.convertIp(rmn.CONVIP_DECODE, ip1) key = rmn.fstinf(fileIdIn, nomvar='TT', ip2=hour, ip1=rmn.ip1_all(lval, lkind)) if key is not None: print("CB51: Found TT at ip1=%d, ip2=%d" % (ip1, hour)) ip1Keys.append((ip1, key['key'])) if rshape is None: rshape = key['shape'] rshape = (rshape[0], rshape[1], len(ip1Keys)) # Read every level for TT at ip2=hour, re-use 2d array while reading # and store the data in a 3d array # with lower level at nk, top at 0 as in the model r2d = {'d': None} r3d = None k = 0 gIn = None for ip1, key in ip1Keys: try: r2d = rmn.fstluk(key, dataArray=r2d['d']) print("CB51: Read TT at ip1=%d, ip2=%d" % (ip1, hour)) if r3d is None: r3d = r2d.copy() r3d['d'] = np.empty(rshape, dtype=r2d['d'].dtype, order='FORTRAN') r3d['d'][:, :, k] = r2d['d'][:, :] k += 1 if gIn is None: gIn = rmn.readGrid(fileIdIn, r2d) print("CB51: Read the horizontal grid descriptors") except: pass # Add the vgrid and the actual ip1 list in the r3d dict, update shape and nk r3d['vgd'] = vIn r3d['ip1list'] = [x[0] for x in ip1Keys] r3d['shape'] = rshape r3d['nk'] = rshape[2] # Read the Input reference fields rfldIn = None if rfldNameIn: rfldIn = rmn.fstlir(fileIdIn, nomvar=rfldNameIn, ip2=hour) if rfldNameIn.strip() == 'P0': rfldIn['d'][:] *= MB2PA print( "CB51: Read input RFLD=%s at ip2=%d [min=%7.0f, max=%7.0f]" % (rfldNameIn, hour, rfldIn['d'].min(), rfldIn['d'].max())) except: raise # pass finally: # Close file even if an error occured above rmn.fstcloseall(fileIdIn) # Define the destination vertical grid/levels try: lvlsOut = (500., 850., 1000.) vOut = vgd.vgd_new_pres(lvlsOut) ip1listOut = vgd.vgd_get(vOut, 'VIPT') rfldNameOut = vgd.vgd_get(vIn, 'RFLD') rfldOut = None # in this case, Pressure levels, there are no RFLD print("CB51: Defined a Pres vgrid with lvls=%s" % str(lvlsOut)) except: sys.stderr.write("Problem creating a new vgrid\n") sys.exit(1) # Get input and output 3d pressure cubes try: ## if rfldIn is None: ## rfldIn = pIn = vgd.vgd_levels(vIn, ip1list=r3d['ip1list'], rfld=rfldIn['d']) print( "CB51: Computed input pressure cube, k0:[min=%7.0f, max=%7.0f], nk:[min=%7.0f, max=%7.0f]" % (pIn[:, :, 0].min(), pIn[:, :, 0].max(), pIn[:, :, -1].min(), pIn[:, :, -1].max())) if rfldOut is None: rfldOut = rfldIn # provide a dummy rfld for array shape pOut = vgd.vgd_levels(vOut, ip1list=ip1listOut, rfld=rfldOut['d']) print( "CB51: Computed output pressure cube, k0:[min=%7.0f, max=%7.0f], nk:[min=%7.0f, max=%7.0f]" % (pOut[:, :, 0].min(), pOut[:, :, 0].max(), pOut[:, :, -1].min(), pOut[:, :, -1].max())) except: raise sys.stderr.write("Problem computing pressure cubes\n") sys.exit(1) # Use scipy.interpolate.interp1d to vertically interpolate try: ## f = scipy_interp1d(fromLvls, toLvls, kind='cubic', ## assume_sorted=True, bounds_error=False, ## fill_value='extrapolate', copy=False) ## # Unfortunately, looks like interp1d take colomn data ## f = scipy_interp1d(pIn, r3d['d'], kind='cubic', ## bounds_error=False, ## fill_value='extrapolate', copy=False) ## r3dout = f(pOut) ## # Unfortunately, assume_sorted, 'extrapolate' not support in my version ## extrap_value = 'extrapolate' # -99999. ## # Way too slow, needs a C implementation extrap_value = -999. ## for j in range(rshape[1]): ## for i in range(rshape[0]): ## f = scipy_interp1d(pIn[i,j,:], r3d['d'][i,j,:], ## kind='cubic', ## bounds_error=False, ## fill_value=extrap_value, copy=False) ## r1d = f(pOut[i,j,:]) ## #print i,j,r1d except: raise sys.stderr.write("Problem Interpolating data\n") sys.exit(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
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)
def getConc(fileID, spc): dataKey = rmn.fstinf(fileID, nomvar=spc, ip1=ip1)['key'] dataRec = rmn.fstluk(dataKey) concData = dataRec['d'] return concData, dataKey
def read_fst(file, var, level, fhour, yy): global field global lat global lon print var print level print fhour print yy try: file = rmn.fstopenall(file, rmn.FST_RO) except: sys.stderr.write("Problem opening the file: %s\n" % file) ###Read in lat/lon data### # Prefered method to get grid lat, lon. Works on any RPNSTD grid except 'x' ##### try: rec = rmn.fstlir(file, nomvar=var) except: sys.stderr.write('Error: Problem reading fields ' + var + ' in file: ' + file + '\n') sys.exit(1) try: rec['iunit'] = file gridid = rmn.ezqkdef(rec) # use ezscint to retreive full grid gridLatLon = rmn.gdll(gridid) lat = gridLatLon['lat'] lon = gridLatLon['lon'] if yy == True: print "This grid is Yin-Yang and the lat/lon arrays need to be constructed from the 2 subgrids" lat1 = gridLatLon['subgrid'][0]['lat'] lat2 = gridLatLon['subgrid'][1]['lat'] lon1 = gridLatLon['subgrid'][0]['lon'] lon2 = gridLatLon['subgrid'][1]['lon'] lat = np.append(lat1, lat2, axis=1) lon = np.append(lon1, lon2, axis=1) except: sys.stderr.write('Error: Problem getting grid info in file') sys.exit(1) ###### ###### try: #Get vertical grid definition from file v = vgd.vgd_read(file) #Get the list of ip1 on thermo levels in this file #tlvl = vgd.vgd_get(v, 'VIPT') tlvl = level # Trim the list of thermo ip1 to actual levels in files for TT # since the vgrid in the file is a super set of all levels # and get their "key" tlvlkeys = [] rshape = None #for ip1 in tlvl: (lval, lkind) = rmn.convertIp(rmn.CONVIP_DECODE, level) key = rmn.fstinf(file, nomvar=var, ip2=fhour, ip1=rmn.ip1_all(lval, lkind)) if key is not None: tlvlkeys.append((level, key['key'])) if rshape is None: rshape = key['shape'] rshape = (rshape[0], rshape[1], len(tlvlkeys)) r2d = {'d': None} r3d = None k = 0 try: r2d = rmn.fstluk(key, dataArray=r2d['d']) if r3d is None: r3d = r2d.copy() r3d['d'] = np.empty(rshape, dtype=r2d['d'].dtype, order='FORTRAN') r3d['d'][:, :, k] = r2d['d'][:, :] k += 1 except: raise except: raise finally: # Close file even if an error occured above rmn.fstcloseall(file) # Add the vgrid and the actual ip1 list in the r3d dict, update shape and nk r3d['vgd'] = v r3d['ip1list'] = [x[0] for x in tlvlkeys] r3d['shape'] = rshape r3d['nk'] = rshape[2] field = r3d['d'] return (field, lat, lon)
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