def test_fstlir_fstlirx_fstlir_witharray(self): """fstlir_fstlirx_fstlir_witharray should give known result with known input""" rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') myfile = os.path.join(ATM_MODEL_DFILES.strip(),'bcmk/2009042700_000') funit = rmn.fstopenall(myfile,rmn.FST_RO) k = rmn.fstinf(funit)['key'] a = rmn.fstprm(k) self.assertEqual(a['nomvar'].strip(),'P0','fstinf/fstprm wrong rec, Got %s expected P0' % (a['nomvar'])) k = rmn.fstsui(funit)['key'] a = rmn.fstprm(k) self.assertEqual(a['nomvar'].strip(),'TT','fstsui/fstprm wrong rec, Got %s expected TT' % (a['nomvar'])) k = rmn.fstinf(funit,nomvar='MX')['key'] a = rmn.fstlir(funit) a = rmn.fstlir(funit,dataArray=a['d']) self.assertEqual(a['nomvar'].strip(),'P0','fstlir wrong rec, Got %s expected P0' % (a['nomvar'])) self.assertEqual(int(np.amin(a['d'])),530) self.assertEqual(int(np.amax(a['d'])),1039) k = rmn.fstinf(funit,nomvar='MX')['key'] a = rmn.fstlirx(k,funit,dataArray=a['d']) self.assertEqual(a['nomvar'].strip(),'LA','fstlirx wrong rec, Got %s expected P0' % (a['nomvar'])) self.assertEqual(int(np.amin(a['d'])),-88) self.assertEqual(int(np.amax(a['d'])),88) a = rmn.fstlis(funit,dataArray=a['d']) self.assertEqual(a['nomvar'].strip(),'LO','fstlis wrong rec, Got %s expected P0' % (a['nomvar'])) self.assertEqual(int(np.amin(a['d'])),-180) self.assertEqual(int(np.amax(a['d'])),178) rmn.fstcloseall(funit)
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_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_2(self): """ Find Record / Get Metadata Most librmn FSTD functions are supported to look for records matching the provided selection criterion. Criteria not specified are not used for selection (wildcard). The fstprm function can then be used to get the record metadata. See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinf rpnpy.librmn.fstd98.fstinfx rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstsui 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') # Open try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: raise rmn.FSTDError("File not found/readable: %s" % fileName) # Find try: k1 = rmn.fstinf(fileId)[ 'key'] #No criterion, this match the first rec in the file pr_key = rmn.fstinf( fileId, nomvar='PR')['key'] #this match the first record named PR # pr_key2 = rmn.fstinfx(pr_key, fileId, nomvar='PR')['key'] #this would match the next record named PR except: raise rmn.FSTDError("Problem searching in File: %s" % fileName) if not pr_key: raise rmn.FSTDError("Record not found in File: %s" % fileName) # Read Metadata try: pr_meta = rmn.fstprm(pr_key) for k in ('nomvar', 'dateo', 'npas'): print("%s = %s" % (k, str(pr_meta[k]))) except: raise rmn.FSTDError("Error: Problem getting record metadata") # ... # Close rmn.fstcloseall(fileId)
def test_2(self): """ Find Record / Get Metadata Most librmn FSTD functions are supported to look for records matching the provided selection criterion. Criteria not specified are not used for selection (wildcard). The fstprm function can then be used to get the record metadata. See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinf rpnpy.librmn.fstd98.fstinfx rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstsui 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') # Open try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: raise rmn.FSTDError("File not found/readable: %s" % fileName) # Find try: k1 = rmn.fstinf(fileId)['key'] #No criterion, this match the first rec in the file pr_key = rmn.fstinf(fileId, nomvar='PR')['key'] #this match the first record named PR # pr_key2 = rmn.fstinfx(pr_key, fileId, nomvar='PR')['key'] #this would match the next record named PR except: raise rmn.FSTDError("Problem searching in File: %s" % fileName) if not pr_key: raise rmn.FSTDError("Record not found in File: %s" % fileName) # Read Metadata try: pr_meta = rmn.fstprm(pr_key) for k in ('nomvar', 'dateo', 'npas'): print("%s = %s" % (k, str(pr_meta[k]))) except: raise rmn.FSTDError("Error: Problem getting record metadata") # ... # Close rmn.fstcloseall(fileId)
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 fstinf(iunit, nomvar, typvar, etiket, ip1, ip2, ip3, datev, inhandle): """Find a record matching provided criterias (Interface to fstinf, dsfsui, fstinfx) recParamDict = Fstdc.fstinf(iunit, nomvar, typvar, etiket, ip1, ip2, ip3, datev, inhandle) @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) @param inhandle selcation criterion; inhandle=-2:search with criterion from start of file; inhandle=-1==fstsui, use previously provided criterion to find the next matching one; inhandle>=0 search with criterion from provided rec-handle (int) @returns python dict with record handle + record params keys/values @exception TypeError @exception Fstdc.error """ if inhandle < -1: mymatch = _rmn.fstinf(iunit, datev, etiket, ip1, ip2, ip3, typvar, nomvar) elif inhandle == -1: mymatch = _rmn.fstsui(iunit) else: mymatch = _rmn.fstinfx(inhandle, iunit, datev, etiket, ip1, ip2, ip3, typvar, nomvar) if not mymatch: raise error('No matching record') recParams = _rmn.fstprm(mymatch['key']) recParams['handle'] = recParams['key'] recParams['nom'] = recParams['nomvar'] recParams['type'] = recParams['typvar'] recParams['datev'] = recParams['xtra1'] #TODO: Keep Fstdc original bug? return recParams
def test_fst_edit_dir_dateo(self): """Changing dateo with fst_edit_dir should update datev accordingly""" [ref_file, new_file] = self.copy_file() #Compare before [datev, dateo, deet, npas] = self.read_dateo_npas(ref_file,vname) [datev1, dateo1, deet1, npas1] = self.read_dateo_npas(new_file,vname) self.assertEqual(deet,deet1) self.assertEqual(npas,npas1) self.assertEqual(datev,datev1) self.assertEqual(dateo,dateo1) #Edit dateo in ref_file [datev, dateo, deet, npas] = self.read_dateo_npas(ref_file,vname) dateo2 = rmn.incdatr(dateo,1.) datev2 = rmn.incdatr(datev,1.) fnew = rmn.fstopenall(new_file, rmn.FST_RW) key = rmn.fstinf(fnew, nomvar=vname)['key'] rmn.fst_edit_dir(key, dateo=dateo2) rmn.fstcloseall(fnew) #Compare after [datev, dateo, deet, npas] = self.read_dateo_npas(ref_file,vname) [datev1, dateo1, deet1, npas1] = self.read_dateo_npas(new_file,vname) self.assertEqual(deet,deet1) self.assertEqual(npas,npas1) self.assertNotEqual(datev,datev1) self.assertEqual(datev2,datev1) self.assertNotEqual(dateo,dateo1) self.assertEqual(dateo2,dateo1)
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 test_fst_edit_dir_npas_keepdateo(self): """Changing npas with keepdate in fst_edit_dir should update datev accordingly""" [ref_file, new_file] = self.copy_file() #Compare before [datev, dateo, deet, npas] = self.read_dateo_npas(ref_file,vname) [datev1, dateo1, deet1, npas1] = self.read_dateo_npas(new_file,vname) self.assertEqual(deet,deet1) self.assertEqual(npas,npas1) self.assertEqual(datev,datev1) self.assertEqual(dateo,dateo1) #Edit npas in ref_file [datev, dateo, deet, npas] = self.read_dateo_npas(ref_file,vname) npas2 = npas+1 dateo2 = dateo datev2 = rmn.incdatr(datev,deet/3600.) fnew = rmn.fstopenall(new_file, rmn.FST_RW) key = rmn.fstinf(fnew, nomvar=vname)['key'] rmn.fst_edit_dir(key, npas=npas2,keep_dateo=True) rmn.fstcloseall(fnew) #Compare after [datev, dateo, deet, npas] = self.read_dateo_npas(ref_file,vname) [datev1, dateo1, deet1, npas1] = self.read_dateo_npas(new_file,vname) self.assertEqual(deet,deet1) self.assertEqual(npas2,npas1) self.assertNotEqual(datev,datev1) self.assertEqual(datev2,datev1) self.assertEqual(dateo,dateo1) self.assertEqual(dateo2,dateo1)
def test_fst_edit_dir_dateo(self): """Changing dateo with fst_edit_dir should update datev accordingly""" [ref_file, new_file] = self.copy_file() #Compare before [datev, dateo, deet, npas] = self.read_dateo_npas(ref_file, vname) [datev1, dateo1, deet1, npas1] = self.read_dateo_npas(new_file, vname) self.assertEqual(deet, deet1) self.assertEqual(npas, npas1) self.assertEqual(datev, datev1) self.assertEqual(dateo, dateo1) #Edit dateo in ref_file [datev, dateo, deet, npas] = self.read_dateo_npas(ref_file, vname) dateo2 = rmn.incdatr(dateo, 1.) datev2 = rmn.incdatr(datev, 1.) fnew = rmn.fstopenall(new_file, rmn.FST_RW) key = rmn.fstinf(fnew, nomvar=vname)['key'] rmn.fst_edit_dir(key, dateo=dateo2) rmn.fstcloseall(fnew) #Compare after [datev, dateo, deet, npas] = self.read_dateo_npas(ref_file, vname) [datev1, dateo1, deet1, npas1] = self.read_dateo_npas(new_file, vname) self.assertEqual(deet, deet1) self.assertEqual(npas, npas1) self.assertNotEqual(datev, datev1) self.assertEqual(datev2, datev1) self.assertNotEqual(dateo, dateo1) self.assertEqual(dateo2, dateo1)
def test_fst_edit_dir_npas_keepdateo(self): """Changing npas with keepdate in fst_edit_dir should update datev accordingly""" [ref_file, new_file] = self.copy_file() #Compare before [datev, dateo, deet, npas] = self.read_dateo_npas(ref_file, vname) [datev1, dateo1, deet1, npas1] = self.read_dateo_npas(new_file, vname) self.assertEqual(deet, deet1) self.assertEqual(npas, npas1) self.assertEqual(datev, datev1) self.assertEqual(dateo, dateo1) #Edit npas in ref_file [datev, dateo, deet, npas] = self.read_dateo_npas(ref_file, vname) npas2 = npas + 1 dateo2 = dateo datev2 = rmn.incdatr(datev, deet / 3600.) fnew = rmn.fstopenall(new_file, rmn.FST_RW) key = rmn.fstinf(fnew, nomvar=vname)['key'] rmn.fst_edit_dir(key, npas=npas2, keep_dateo=True) rmn.fstcloseall(fnew) #Compare after [datev, dateo, deet, npas] = self.read_dateo_npas(ref_file, vname) [datev1, dateo1, deet1, npas1] = self.read_dateo_npas(new_file, vname) self.assertEqual(deet, deet1) self.assertEqual(npas2, npas1) self.assertNotEqual(datev, datev1) self.assertEqual(datev2, datev1) self.assertEqual(dateo, dateo1) self.assertEqual(dateo2, dateo1)
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 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 read_dateo_npas(self,fname,vname): 'Read date of original analysis and time-step number' iunit = rmn.fstopenall(fname, rmn.FST_RO) key = rmn.fstinf(iunit, nomvar=vname)['key'] recmeta = rmn.fstprm(key) rmn.fstcloseall(iunit) return recmeta['datev'],recmeta['dateo'],recmeta['deet'],recmeta['npas']
def test_fsteditdir_fsteff(self): """fst_edit_dir 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 funit = rmn.fstopenall(self.fname,rmn.FST_RW) kla = rmn.fstinf(funit,nomvar='LA')['key'] klo = rmn.fstinf(funit,nomvar='LO')['key'] istat = rmn.fst_edit_dir(klo,nomvar='QW') istat = rmn.fsteff(kla) rmn.fstcloseall(funit) funit = rmn.fstopenall(self.fname,rmn.FST_RO) kla = rmn.fstinf(funit,nomvar='LA') klo = rmn.fstinf(funit,nomvar='QW')['key'] rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(kla,None,'LA found after delete: '+repr(kla)) self.assertNotEqual(klo,None,'QW not found after rename: '+repr(klo))
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 read_dateo_npas(self, fname, vname): 'Read date of original analysis and time-step number' iunit = rmn.fstopenall(fname, rmn.FST_RO) key = rmn.fstinf(iunit, nomvar=vname)['key'] recmeta = rmn.fstprm(key) rmn.fstcloseall(iunit) return recmeta['datev'], recmeta['dateo'], recmeta['deet'], recmeta[ 'npas']
def test_fstlir_fstlirx_fstlir_witharray(self): """fstlir_fstlirx_fstlir_witharray should give known result with known input""" rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') myfile = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_000') funit = rmn.fstopenall(myfile, rmn.FST_RO) k = rmn.fstinf(funit)['key'] a = rmn.fstprm(k) self.assertEqual( a['nomvar'].strip(), 'P0', 'fstinf/fstprm wrong rec, Got %s expected P0' % (a['nomvar'])) k = rmn.fstsui(funit)['key'] a = rmn.fstprm(k) self.assertEqual( a['nomvar'].strip(), 'TT', 'fstsui/fstprm wrong rec, Got %s expected TT' % (a['nomvar'])) k = rmn.fstinf(funit, nomvar='MX')['key'] a = rmn.fstlir(funit) a = rmn.fstlir(funit, dataArray=a['d']) self.assertEqual( a['nomvar'].strip(), 'P0', 'fstlir wrong rec, Got %s expected P0' % (a['nomvar'])) self.assertEqual(int(np.amin(a['d'])), 530) self.assertEqual(int(np.amax(a['d'])), 1039) k = rmn.fstinf(funit, nomvar='MX')['key'] a = rmn.fstlirx(k, funit, dataArray=a['d']) self.assertEqual( a['nomvar'].strip(), 'LA', 'fstlirx wrong rec, Got %s expected P0' % (a['nomvar'])) self.assertEqual(int(np.amin(a['d'])), -88) self.assertEqual(int(np.amax(a['d'])), 88) a = rmn.fstlis(funit, dataArray=a['d']) self.assertEqual( a['nomvar'].strip(), 'LO', 'fstlis wrong rec, Got %s expected P0' % (a['nomvar'])) self.assertEqual(int(np.amin(a['d'])), -180) self.assertEqual(int(np.amax(a['d'])), 178) rmn.fstcloseall(funit)
def test_fsteditdir_fsteff(self): """fst_edit_dir 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 funit = rmn.fstopenall(self.fname, rmn.FST_RW) kla = rmn.fstinf(funit, nomvar='LA')['key'] klo = rmn.fstinf(funit, nomvar='LO')['key'] istat = rmn.fst_edit_dir(klo, nomvar='QW') istat = rmn.fsteff(kla) rmn.fstcloseall(funit) funit = rmn.fstopenall(self.fname, rmn.FST_RO) kla = rmn.fstinf(funit, nomvar='LA') klo = rmn.fstinf(funit, nomvar='QW')['key'] rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(kla, None, 'LA found after delete: ' + repr(kla)) self.assertNotEqual(klo, None, 'QW not found after rename: ' + repr(klo))
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 test_12(self): """ Defining Grids Grids can be defined from * a record in a FSTD file or * directly by providing parameters. These grids parameters can be used to geolocate the data points and for interpolation operations (see below). See also: """ 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') # Define a grid from parameters, a cylindrical equidistant (LatLon) grid with 0.5 deg spacing. paramsL = { 'grtyp': 'L', # Cylindrical equidistant projection 'ni': 90, # Grid dimension: 90 by 45 points 'nj': 45, 'lat0': 0., # Grid lower-left (south-west) corner (point 1,1) is located at 0N, 180E 'lon0': 180., 'dlat': 0.5, # The grid has a resolution (grid spacing) of 0.5 deg. on both axes 'dlon': 0.5 } try: gridL = rmn.encodeGrid(paramsL) except: raise rmn.FSTDError( "Problem defining a grid with provided parameters: %s " % str(paramsL)) # Get a grid definition from a record in a FSTD file try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) prKey = rmn.fstinf(fileId, nomvar='PR')['key'] prMeta = rmn.fstprm( prKey) # Get the record metadata along with partial grid info prMeta['iunit'] = fileId prGrid0 = rmn.ezqkdef( prMeta) # use ezscint to retreive full grid info prGrid = rmn.decodeGrid( prGrid0) # Decode all the grid parameters values rmn.fstcloseall(fileId) except: raise rmn.FSTDError( "Problem getting PR record grid meta from file: %s" % fileName)
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_10(self): """ Encoding values Encoding values are usefull in 2 situations: * providing the metadata when writing a record, it is best to encode in the new format then * specify search criterions to read a record, it is best to search for the old and new formats, the ip1_all and ip2_all functions can be used for that sake as long as no value range are needed. See also: rpnpy.librmn.fstd98.convertIp rpnpy.librmn.fstd98.convertIPtoPK rpnpy.librmn.fstd98.EncodeIp rpnpy.librmn.fstd98.ip1_all rpnpy.librmn.fstd98.ip2_all rpnpy.librmn.fstd98.ip3_all 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') ip1new = rmn.convertIp(rmn.CONVIP_ENCODE, 850., rmn.KIND_PRESSURE) ip1old = rmn.convertIp(rmn.CONVIP_ENCODE_OLD, 10., rmn.KIND_ABOVE_SEA) ip1newall = rmn.ip1_all(1., rmn.KIND_HYBRID) # Use ip1newall as a search criterion to find a record try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) ktt = rmn.fstinf(fileId, nomvar='TT', ip1=ip1newall) except: raise rmn.FSTDError( "Problem finding of TT with ip1=%d record from file: %s" % (ip1newall, fileName)) if not ktt: print("Not Found: TT with ip1=%d record from file: %s" % (ip1newall, fileName))
def get_levels_keys(fileId, nomvar, datev=-1, ip2=-1, ip3=-1, typvar=' ', etiket=' ', vGrid=None, thermoMom='VIPT', verbose=False): """ """ #TODO: try to get the sorted ip1 list w/o vgrid, because vgrid doesn;t support 2 different vertical coor in the same file (or list of linked files) # Get the vgrid definition present in the file if vGrid is None: if verbose: print("Getting vertical grid description") _vgd.vgd_put_opt('ALLOW_SIGMA', _vgd.VGD_ALLOW_SIGMA) vGrid = _vgd.vgd_read(fileId) vip = _vgd.vgd_get(vGrid, thermoMom) if verbose: vkind = _vgd.vgd_get(vGrid, 'KIND') vver = _vgd.vgd_get(vGrid, 'VERS') vtype = _vgd.VGD_KIND_VER_INV[(vkind,vver)] print("Found %d %s levels of type %s" % (len(vip), thermoMom, vtype)) # Trim the list of ip1 to actual levels in files for nomvar # since the vgrid in the file is a super set of all levels # and get their "key" vipkeys = [] for ip1 in vip: (lval, lkind) = _rmn.convertIp(_rmn.CONVIP_DECODE, ip1) key = _rmn.fstinf(fileId, nomvar=nomvar, datev=datev, ip2=ip2, ip3=ip3, ip1=_rmn.ip1_all(lval, lkind), typvar=typvar, etiket=etiket) if key is not None: vipkeys.append((ip1, key['key'])) if datev == -1 or ip2 == -1 or ip3 == -1 or typvar.strip() == '' or etiket.strip() == '': meta = _rmn.fstprm(key) datev = meta['datev'] ip2 = meta['ip2'] ip3 = meta['ip3'] typvar = meta['typvar'] etiket = meta['etiket'] return { 'nomvar' : nomvar, 'datev' : datev, 'ip2' : ip2, 'ip3' : ip3, 'typvar' : typvar, 'etiket' : etiket, 'v' : vGrid, 'ip1keys': vipkeys }
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_4(self): """ Change record metadata You can change the metadata of a record in an FSTD file in place (a la Editfst's zap function) with a simple call to fst_edit_dir. No need to write the record in another file. All parameters not specified in the call will be kept to their present value. See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinf rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.fst_edit_dir rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.FSTDError rpnpy.librmn.RMNError rpnpy.librmn.const """ import os, os.path, stat, shutil import rpnpy.librmn.all as rmn # Take an editable copy of the file ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') fileName0 = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012') fileName = 'some_rpnstd_file.fst' shutil.copyfile(fileName0, fileName) st = os.stat(fileName) os.chmod(fileName, st.st_mode | stat.S_IWRITE) # Change nomvar for the PR record try: fileId = rmn.fstopenall(fileName, rmn.FST_RW) pr_key = rmn.fstinf( fileId, nomvar='PR')['key'] #Match the first record named PR if pr_key: rmn.fst_edit_dir( pr_key, nomvar='PR0', ip2=0) #Rename the field to PR0 and set ip2 to 0 (zero) except: raise rmn.FSTDError("Problem editing record meta in File: %s" % fileName) # Close rmn.fstcloseall(fileId) # Erase test file os.unlink(fileName)
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_10(self): """ Encoding values Encoding values are usefull in 2 situations: * providing the metadata when writing a record, it is best to encode in the new format then * specify search criterions to read a record, it is best to search for the old and new formats, the ip1_all and ip2_all functions can be used for that sake as long as no value range are needed. See also: rpnpy.librmn.fstd98.convertIp rpnpy.librmn.fstd98.convertIPtoPK rpnpy.librmn.fstd98.EncodeIp rpnpy.librmn.fstd98.ip1_all rpnpy.librmn.fstd98.ip2_all rpnpy.librmn.fstd98.ip3_all 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') ip1new = rmn.convertIp(rmn.CONVIP_ENCODE, 850., rmn.KIND_PRESSURE) ip1old = rmn.convertIp(rmn.CONVIP_ENCODE_OLD, 10., rmn.KIND_ABOVE_SEA) ip1newall = rmn.ip1_all(1., rmn.KIND_HYBRID) # Use ip1newall as a search criterion to find a record try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) ktt = rmn.fstinf(fileId, nomvar='TT', ip1=ip1newall) except: raise rmn.FSTDError("Problem finding of TT with ip1=%d record from file: %s" % (ip1newall, fileName)) if not ktt: print("Not Found: TT with ip1=%d record from file: %s" % (ip1newall, fileName))
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 test_5(self): """ Erase a record You can delete a record in an FSTD file (a la Editfst's exclude function) with a simple call to fsteff. No need to write all the other records in another file. See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinf rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fsteff rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.FSTDError rpnpy.librmn.RMNError rpnpy.librmn.const """ import os, os.path, stat, shutil import rpnpy.librmn.all as rmn # Take an editable copy of the file ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') fileName0 = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012') fileName = 'some_rpnstd_file.fst' shutil.copyfile(fileName0, fileName) st = os.stat(fileName) os.chmod(fileName, st.st_mode | stat.S_IWRITE) # Erase record named PR in file try: fileId = rmn.fstopenall(fileName, rmn.FST_RW) pr_key = rmn.fstinf( fileId, nomvar='PR')['key'] #Match the first record named PR if pr_key: rmn.fsteff(pr_key) #Erase previously found record except: raise rmn.FSTDError("Problem erasing record in File: %s" % fileName) # Close rmn.fstcloseall(fileId) # Erase test file os.unlink(fileName)
def test_12(self): """ Defining Grids Grids can be defined from * a record in a FSTD file or * directly by providing parameters. These grids parameters can be used to geolocate the data points and for interpolation operations (see below). See also: """ 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') # Define a grid from parameters, a cylindrical equidistant (LatLon) grid with 0.5 deg spacing. paramsL = { 'grtyp' : 'L', # Cylindrical equidistant projection 'ni' : 90, # Grid dimension: 90 by 45 points 'nj' : 45, 'lat0' : 0., # Grid lower-left (south-west) corner (point 1,1) is located at 0N, 180E 'lon0' : 180., 'dlat' : 0.5,# The grid has a resolution (grid spacing) of 0.5 deg. on both axes 'dlon' : 0.5 } try: gridL = rmn.encodeGrid(paramsL) except: raise rmn.FSTDError("Problem defining a grid with provided parameters: %s " % str(paramsL)) # Get a grid definition from a record in a FSTD file try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) prKey = rmn.fstinf(fileId, nomvar='PR')['key'] prMeta = rmn.fstprm(prKey) # Get the record metadata along with partial grid info prMeta['iunit'] = fileId prGrid0 = rmn.ezqkdef(prMeta) # use ezscint to retreive full grid info prGrid = rmn.decodeGrid(prGrid0) # Decode all the grid parameters values rmn.fstcloseall(fileId) except: raise rmn.FSTDError("Problem getting PR record grid meta from file: %s" % fileName)
def test_4(self): """ Change record metadata You can change the metadata of a record in an FSTD file in place (a la Editfst's zap function) with a simple call to fst_edit_dir. No need to write the record in another file. All parameters not specified in the call will be kept to their present value. See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinf rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.fst_edit_dir rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.FSTDError rpnpy.librmn.RMNError rpnpy.librmn.const """ import os, os.path, stat, shutil import rpnpy.librmn.all as rmn # Take an editable copy of the file ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') fileName0 = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012') fileName = 'some_rpnstd_file.fst' shutil.copyfile(fileName0, fileName) st = os.stat(fileName) os.chmod(fileName, st.st_mode | stat.S_IWRITE) # Change nomvar for the PR record try: fileId = rmn.fstopenall(fileName, rmn.FST_RW) pr_key = rmn.fstinf(fileId, nomvar='PR')['key'] #Match the first record named PR if pr_key: rmn.fst_edit_dir(pr_key, nomvar='PR0', ip2=0) #Rename the field to PR0 and set ip2 to 0 (zero) except: raise rmn.FSTDError("Problem editing record meta in File: %s" % fileName) # Close rmn.fstcloseall(fileId) # Erase test file os.unlink(fileName)
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 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_5(self): """ Erase a record You can delete a record in an FSTD file (a la Editfst's exclude function) with a simple call to fsteff. No need to write all the other records in another file. See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinf rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fsteff rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.FSTDError rpnpy.librmn.RMNError rpnpy.librmn.const """ import os, os.path, stat, shutil import rpnpy.librmn.all as rmn # Take an editable copy of the file ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') fileName0 = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012') fileName = 'some_rpnstd_file.fst' shutil.copyfile(fileName0, fileName) st = os.stat(fileName) os.chmod(fileName, st.st_mode | stat.S_IWRITE) # Erase record named PR in file try: fileId = rmn.fstopenall(fileName, rmn.FST_RW) pr_key = rmn.fstinf(fileId, nomvar='PR')['key'] #Match the first record named PR if pr_key: rmn.fsteff(pr_key) #Erase previously found record except: raise rmn.FSTDError("Problem erasing record in File: %s" % fileName) # Close rmn.fstcloseall(fileId) # Erase test file os.unlink(fileName)
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 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
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)
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)
if not (options.varname and options.fstfile and options.outfile and options.lolafile and options.inttype): 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)
def getKey(fileID, spc, ip1): key = rmn.fstinf(fileID, nomvar=spc, ip1=ip1)['key'] return key
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)
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_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 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)
import rpnpy.librmn.all as rmn data_file = "/cnfs/dev/cmd/cmde/afsgbla/watroute2py/watroute/budget/io/test/input_test.fst" funit = rmn.fstopenall(data_file, rmn.FST_RO) k = rmn.fstinf(funit, nomvar="I1D") r = rmn.fstlir(funit, nomvar="I1D") print k print r rmn.fstcloseall(funit)