def writeRec(fname, rec): """ Write the record data along with grid info to file Args: fname (str): Filename to to write to rec (dict): Record meta + data + grid info to write Returns: None """ print("+ Write %s to: %s" % (rec['nomvar'], fname)) # Open File try: funit = rmn.fstopenall(fname, rmn.FST_RW) except: raise rmn.FSTDError("Problem Opening file: %s" % fname) # Write rec meta + data try: rmn.fstecr(funit,rec['d'],rec) except: raise rmn.FSTDError("Problem writing %s record" % rec['nomvar']) # Write grid (if need be) writeRecGrid(rec, funit, fname) # Close File rmn.fstcloseall(funit) return
def WriteFld(self, outfile, outrecord): filename = outfile if not os.path.isfile(filename): # Check that the output file exists print 'The output file was not found:', filename quit() # Open the file if not rmn.isFST( filename ): # Check that the output 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_RW_OLD) print(fstID) except: raise rmn.FSTDError("File not found/readable: %s" % filename) # Write out the standard format record varname = outrecord['nomvar'] try: rmn.fstecr(fstID, outrecord) except: raise rmn.FSTDError( "Problem writing the record " + varname + " to file: %s" % filename) # Issue a warning message rmn.fstcloseall(fstID) # Close the standard format file return
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_openall_closeall_loop(self): """Test if close all on linked file actually close them all""" ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') mydir = os.path.join(ATM_MODEL_DFILES.strip(),'bcmk/') for i in range(1000): funit = rmn.fstopenall(mydir) rmn.fstcloseall(funit)
def _newReadBcmk(self): ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES,'bcmk_toctoc','2009042700_000') fileId = rmn.fstopenall(fileName, rmn.FST_RO) vgd0ptr = vgd.vgd_read(fileId) rmn.fstcloseall(fileId) return vgd0ptr
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_fstdread3d(self): """ """ import os, sys, datetime import rpnpy.librmn.all as rmn import rpnpy.utils.fstd3d as fstd3d fdate = datetime.date.today().strftime('%Y%m%d') + '00_048' CMCGRIDF = os.getenv('CMCGRIDF').strip() fileNameIn = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate) # Restrict to the minimum the number of messages printed by librmn rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) try: fileId = rmn.fstopenall(fileNameIn) except: sys.stderr.write("Problem opening the files: %s, %s\n" % (fileNameIn, fileNameOut)) sys.exit(1) try: rec3d = fstd3d.fst_read_3d(fileId, nomvar='TT', getPress=True, verbose=True) except: raise finally: # Properly close files even if an error occured above # This is important when editing to avoid corrupted files rmn.fstcloseall(fileId)
def test_1(self): """ Open/Close File Note: The following constants may be used to set the file mode: rmn.FST_RW , rmn.FST_RW_OLD , rmn.FST_RO See also: rpnpy.librmn.fstd98.isFST rpnpy.librmn.fstd98.fstopenall 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/geophy.fst') if not rmn.isFST(fileName): raise rmn.FSTDError("Not an FSTD file: %s " % fileName) # Open try: fileId = rmn.fstopenall(fileName,rmn.FST_RO) except: raise rmn.FSTDError("File not found/readable: %s" % fileName) # ... # Close rmn.fstcloseall(fileId)
def testDiag_withref8_3d(self): ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES,'bcmk_toctoc','2009042700_000') fileId = rmn.fstopenall(fileName, rmn.FST_RO) vgd0ptr = vgd.c_vgd_construct() ok = vgd.c_vgd_new_read(vgd0ptr,fileId,-1,-1,-1,-1) rfld_name = C_MKSTR(' '*vgd.VGD_MAXSTR_NOMVAR) quiet = ct.c_int(0) ok = vgd.c_vgd_get_char(vgd0ptr, _C_WCHAR2CHAR('RFLD'), rfld_name, quiet) rfld = rmn.fstlir(fileId, nomvar=_C_CHAR2WCHAR(rfld_name.value).strip())['d'] MB2PA = 100. rfld = rfld * MB2PA rmn.fstcloseall(fileId) ip1list = ct.POINTER(ct.c_int)() nip1 = ct.c_int(0) quiet = ct.c_int(0) ok = vgd.c_vgd_get_int_1d(vgd0ptr, _C_WCHAR2CHAR('VIPM'), ct.byref(ip1list), ct.byref(nip1), quiet) ni = rfld.shape[0] ; nj = rfld.shape[1] ; in_log = 0 levels8 = np.empty((ni, nj, nip1.value), dtype=np.float64, order='FORTRAN') rfld8 = np.empty((ni, nj), dtype=np.float64, order='FORTRAN') rfld8[:,:] = rfld[:,:] ok = vgd.c_vgd_diag_withref_8(vgd0ptr, ni, nj, nip1, ip1list, levels8, rfld8, in_log, vgd.VGD_DIAG_DPIS) self.assertEqual(ok,vgd.VGD_OK) self.assertEqual([int(x) for x in levels8[ni//2,nj//2,0:5]*10000.], [100000, 138425, 176878, 241408, 305980])
def test_ezqkdef_file_ezgprm_ezgfstp(self): 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/geophy.fst') funit = rmn.fstopenall(myfile,rmn.FST_RO) (ni,nj) = (201,100) gp = { 'shape' : (ni,nj), 'ni' : ni, 'nj' : nj, 'grtyp' : 'Z', 'ig1' : 2002, 'ig2' : 1000, 'ig3' : 0, 'ig4' : 0, 'grref' : 'E', 'ig1ref' : 900, 'ig2ref' : 0, 'ig3ref' : 43200, 'ig4ref' : 43200, 'iunit' : funit } gid1 = rmn.ezqkdef(gp) a = rmn.ezgfstp(gid1) rmn.fstcloseall(funit) self.assertTrue(gid1>=0) gp['id'] = gid1 gprm = rmn.ezgxprm(gid1) for k in gprm.keys(): self.assertEqual(gp[k],gprm[k]) self.assertEqual(a['nomvarx'].strip(),'>>') self.assertEqual(a['nomvary'].strip(),'^^') rmn.gdrls(gid1)
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 writeRec(fname, rec): """ Write the record data along with grid info to file Args: fname (str): Filename to to write to rec (dict): Record meta + data + grid info to write Returns: None """ print("+ Write %s to: %s" % (rec['nomvar'], fname)) # Open File try: funit = rmn.fstopenall(fname, rmn.FST_RW) except: raise rmn.FSTDError("Problem Opening file: %s" % fname) # Write rec meta + data try: rmn.fstecr(funit, rec['d'], rec) except: raise rmn.FSTDError("Problem writing %s record" % rec['nomvar']) # Write grid (if need be) writeRecGrid(rec, funit, fname) # Close File rmn.fstcloseall(funit) return
def testDiag_withref8_3d(self): ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES,'bcmk_toctoc','2009042700_000') fileId = rmn.fstopenall(fileName, rmn.FST_RO) vgd0ptr = vgd.c_vgd_construct() ok = vgd.c_vgd_new_read(vgd0ptr,fileId,-1,-1,-1,-1) rfld_name = C_MKSTR(' '*vgd.VGD_MAXSTR_NOMVAR) quiet = ct.c_int(0) ok = vgd.c_vgd_get_char(vgd0ptr, 'RFLD', rfld_name, quiet) rfld = rmn.fstlir(fileId, nomvar=rfld_name.value.strip())['d'] MB2PA = 100. rfld = rfld * MB2PA rmn.fstcloseall(fileId) ip1list = ct.POINTER(ct.c_int)() nip1 = ct.c_int(0) quiet = ct.c_int(0) ok = vgd.c_vgd_get_int_1d(vgd0ptr, 'VIPM', ct.byref(ip1list), ct.byref(nip1), quiet) ni = rfld.shape[0] ; nj = rfld.shape[1] ; in_log = 0 levels8 = np.empty((ni, nj, nip1.value), dtype=np.float64, order='FORTRAN') rfld8 = np.empty((ni, nj), dtype=np.float64, order='FORTRAN') rfld8[:,:] = rfld[:,:] ok = vgd.c_vgd_diag_withref_8(vgd0ptr, ni, nj, nip1, ip1list, levels8, rfld8, in_log, vgd.VGD_DIAG_DPIS) self.assertEqual(ok,vgd.VGD_OK) self.assertEqual([int(x) for x in levels8[ni//2,nj//2,0:5]*10000.], [100000, 138425, 176878, 241408, 305980])
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 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 testLevels8_prof(self): ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk_toctoc', '2009042700_000') fileId = rmn.fstopenall(fileName, rmn.FST_RO) vgd0ptr = vgd.c_vgd_construct() ok = vgd.c_vgd_new_read(vgd0ptr, fileId, -1, -1, -1, -1) rmn.fstcloseall(fileId) ip1list = ct.POINTER(ct.c_int)() nip1 = ct.c_int(0) quiet = ct.c_int(0) ok = vgd.c_vgd_get_int_1d(vgd0ptr, _C_WCHAR2CHAR('VIPM'), ct.byref(ip1list), ct.byref(nip1), quiet) MB2PA = 100. p0_stn_mb = 1013. p0_stn = np.empty((1, ), dtype=np.float64, order='FORTRAN') p0_stn[0] = p0_stn_mb * MB2PA prof8 = np.empty((nip1.value, ), dtype=np.float64, order='FORTRAN') ni = 1 nj = 1 in_log = 0 ok = vgd.c_vgd_levels_8(vgd0ptr, ni, nj, nip1, ip1list, prof8, p0_stn, in_log) self.assertEqual(ok, vgd.VGD_OK) self.assertEqual([int(x) for x in prof8[0:5] * 10000.], [100000, 138426, 176879, 241410, 305984])
def test_openall_closeall_loop(self): """Test if close all on linked file actually close them all""" ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') mydir = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/') for i in range(1000): funit = rmn.fstopenall(mydir) rmn.fstcloseall(funit)
def test_14(self): """ Queries: Get Vertical Grid Info This example shows how to get the vertical grid definition and print some info. See also: rpnpy.librmn.fstd98.fstopt rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall 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 Some info about the vgrid vkind = vgd.vgd_get(v, 'KIND') vver = vgd.vgd_get(v, 'VERS') tlvl = vgd.vgd_get(v, 'VIPT') try: ip1diagt = vgd.vgd_get(v, 'DIPT') except: ip1diagt = 0 (ldiagval, ldiagkind) = rmn.convertIp(rmn.CONVIP_DECODE, ip1diagt) VGD_KIND_VER_INV = dict( (v, k) for k, v in vgd.VGD_KIND_VER.items()) vtype = VGD_KIND_VER_INV[(vkind, vver)] print( "CB14: Found vgrid type=%s (kind=%d, vers=%d) with %d levels and diag level=%7.2f%s (ip1=%d)" % (vtype, vkind, vver, len(tlvl), ldiagval, rmn.kindToString(ldiagkind), ip1diagt)) except: raise finally: # Close file even if an error occured above rmn.fstcloseall(fileId)
def test_fstdread3d(self): """ """ import os, sys, datetime import rpnpy.librmn.all as rmn import rpnpy.utils.fstd3d as fstd3d fdate = datetime.date.today().strftime('%Y%m%d') + '00_048' CMCGRIDF = os.getenv('CMCGRIDF').strip() fileNameIn = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate) # Restrict to the minimum the number of messages printed by librmn rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) try: fileId = rmn.fstopenall(fileNameIn) except: sys.stderr.write("Problem opening the files: %s, %s\n" % (fileNameIn, fileNameOut)) sys.exit(1) try: rec3d = fstd3d.fst_read_3d(fileId, nomvar='TT', getPress=True, verbose=True) except: raise finally: # Properly close files even if an error occured above # This is important when editing to avoid corrupted files rmn.fstcloseall(fileId)
def testLevels8_prof(self): ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES,'bcmk_toctoc','2009042700_000') fileId = rmn.fstopenall(fileName, rmn.FST_RO) vgd0ptr = vgd.c_vgd_construct() ok = vgd.c_vgd_new_read(vgd0ptr,fileId,-1,-1,-1,-1) rmn.fstcloseall(fileId) ip1list = ct.POINTER(ct.c_int)() nip1 = ct.c_int(0) quiet = ct.c_int(0) ok = vgd.c_vgd_get_int_1d(vgd0ptr, 'VIPM', ct.byref(ip1list), ct.byref(nip1), quiet) MB2PA = 100. p0_stn_mb = 1013. p0_stn = np.empty((1,), dtype=np.float64, order='FORTRAN') p0_stn[0] = p0_stn_mb * MB2PA prof8 = np.empty((nip1.value,), dtype=np.float64, order='FORTRAN') ni = 1 ; nj = 1 ; in_log = 0 ok = vgd.c_vgd_levels_8(vgd0ptr, ni, nj, nip1, ip1list, prof8, p0_stn, in_log); self.assertEqual(ok,vgd.VGD_OK) self.assertEqual([int(x) for x in prof8[0:5]*10000.], [100000, 138426, 176879, 241410, 305984])
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 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_1(self): """ Open/Close File Note: The following constants may be used to set the file mode: rmn.FST_RW , rmn.FST_RW_OLD , rmn.FST_RO See also: rpnpy.librmn.fstd98.isFST rpnpy.librmn.fstd98.fstopenall 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/geophy.fst') if not rmn.isFST(fileName): raise rmn.FSTDError("Not an FSTD file: %s " % fileName) # Open try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: raise rmn.FSTDError("File not found/readable: %s" % fileName) # ... # Close rmn.fstcloseall(fileId)
def test_24qd(self): import os, sys import numpy as np import rpnpy.librmn.all as rmn import rpnpy.vgd.all as vgd g = rmn.defGrid_ZE(90, 45, 35., 250., 0.5, 0.5, 0., 180., 1., 270.) lvls = (500.,850.,1000.) v = vgd.vgd_new_pres(lvls) ip1list = vgd.vgd_get(v, 'VIPT') datyp = rmn.FST_DATYP_LIST['float_IEEE_compressed'] npdtype = rmn.dtype_fst2numpy(datyp) rshape = (g['ni'], g['nj'], len(ip1list)) r = rmn.FST_RDE_META_DEFAULT.copy() r.update(g) r.update({ 'nomvar': 'MASK', 'nk' : len(ip1list), 'dateo' : rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20160302, 1800000), 'ip2' : 6, 'deet' : 3600, 'npas' : 6, 'etiket': 'my_etk', 'datyp' : datyp, 'd' : np.empty(rshape, dtype=npdtype, order='FORTRAN') }) r['d'][:,:,:] = 0. r['d'][10:-11,5:-6,:] = 1. fileNameOut = 'newfromscratch.fst' fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) r2d = r.copy() for k in range(len(ip1list)): r2d.update({'nk':1, 'ip1':ip1list[k], 'd':np.asfortranarray(r['d'][:,:,k])}) rmn.fstecr(fileIdOut, r2d['d'], r2d) rmn.writeGrid(fileIdOut, g) vgd.vgd_write(v, fileIdOut) rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file
def test_isfst_openall_fstnbr(self): """isfst_openall_fstnbr should give known result with known input""" rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) ## rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST,rmn.FSTOP_GET) HOME = os.getenv('HOME') a = rmn.isFST(os.path.join(HOME.strip(), '.profile')) self.assertFalse(a, 'isFST should return false on non FST files') ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') myfile = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_000') a = rmn.isFST(myfile) self.assertTrue(a, 'isFST should return true on FST files') funit = rmn.fstopenall(myfile, rmn.FST_RO) self.assertTrue(funit > 0, 'fstopenall should return a valid file unit') nrec = rmn.c_fstnbrv(funit) self.assertEqual(nrec, 1083, ' c_fstnbrv found %d/1083 rec ' % nrec) nrec = rmn.fstnbrv(funit) self.assertEqual(nrec, 1083, ' fstnbrv found %d/1083 rec ' % nrec) rmn.fstcloseall(funit)
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 testNewRead(self): ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES,'bcmk_toctoc','2009042700_000') fileId = rmn.fstopenall(fileName, rmn.FST_RO) vgd0ptr = vgd.c_vgd_construct() ok = vgd.c_vgd_new_read(vgd0ptr,fileId,-1,-1,-1,-1) rmn.fstcloseall(fileId) self.assertEqual(ok,vgd.VGD_OK)
def test_openall_closeall_list(self): """Test if close all on linked file actually close them all""" ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') mydir1 = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/') mydir2 = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk_p/') funit1 = rmn.fstopenall(mydir1) funit2 = rmn.fstopenall(mydir2) rmn.fstcloseall((funit1, funit2))
def test_6(self): """ Write a record Starting from the read a record example above we can change the data and meta before writing it as another record in the same file or in another file. 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.fstecr rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.FSTDError rpnpy.librmn.RMNError rpnpy.librmn.const """ import os, os.path, sys import numpy as np import rpnpy.librmn.all as rmn # open input file and read PR record ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') fileName = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012') try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: raise rmn.FSTDError("Problem opening File: %s" % fileName) try: pr_rec = rmn.fstlir( fileId, nomvar='PR') # Read 1st record matching nomvar=PR except: sys.stdout.write("Problem reading record in File: %s" % fileName) finally: rmn.fstcloseall(fileId) # open output file and write record fileName = 'some_rpnstd_file.fst' try: fileId = rmn.fstopenall(fileName, rmn.FST_RW) except: raise rmn.FSTDError("Problem opening File: %s" % fileName) try: pr_data = pr_rec['d'] pr_data /= max(1.e-5, np.amax(pr_data)) pr_rec['nomvar'] = 'PRN1' rmn.fstecr(fileId, pr_data, pr_rec) except: sys.stdout.write("Problem writing record in File: %s" % fileName) finally: rmn.fstcloseall(fileId) # Erase test file os.unlink(fileName)
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_12(self): """ Queries: Get Data and Meta This example shows how to * get the data of all records matching selection creterions * then print statistics See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.fstluk rpnpy.librmn.fstd98.fstlir rpnpy.librmn.fstd98.DecodeIp rpnpy.librmn.fstd98.kindToString rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.const """ import os, sys import rpnpy.librmn.all as rmn # Restrict to the minimum the number of messages printed by librmn rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) # Open all RPNStd files in the $ATM_MODEL_DFILES/bcmk/ directory ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk') try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: sys.stderr.write("Problem opening the file: %s\n" % fileName) sys.exit(1) try: # Get the list of record keys matching nomvar='TT' keylist = rmn.fstinl(fileId, nomvar='TT', ip2=12) for k in keylist: # Get the record meta data r = rmn.fstluk(k) d = r['d'] # Decode level info and Print statistics rp1 = rmn.DecodeIp(r['ip1'], r['ip2'], r['ip3'])[0] level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind)) print( "CB12: %s (level=%s, ip2=%d) mean=%f, std=%f, min=%f, max=%f" % (r['nomvar'], level, r['ip2'], d.mean(), d.std(), d.min(), d.max())) except: pass finally: # Close file even if an error occured above rmn.fstcloseall(fileId)
def test_6(self): """ Write a record Starting from the read a record example above we can change the data and meta before writing it as another record in the same file or in another file. 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.fstecr rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.FSTDError rpnpy.librmn.RMNError rpnpy.librmn.const """ import os, os.path, sys import numpy as np import rpnpy.librmn.all as rmn # open input file and read PR record ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') fileName = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012') try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: raise rmn.FSTDError("Problem opening File: %s" % fileName) try: pr_rec = rmn.fstlir(fileId, nomvar='PR') # Read 1st record matching nomvar=PR except: sys.stdout.write("Problem reading record in File: %s" % fileName) finally: rmn.fstcloseall(fileId) # open output file and write record fileName = 'some_rpnstd_file.fst' try: fileId = rmn.fstopenall(fileName, rmn.FST_RW) except: raise rmn.FSTDError("Problem opening File: %s" % fileName) try: pr_data = pr_rec['d'] pr_data /= max(1.e-5, np.amax(pr_data)) pr_rec['nomvar'] = 'PRN1' rmn.fstecr(fileId, pr_data, pr_rec) except: sys.stdout.write("Problem writing record in File: %s" % fileName) finally: rmn.fstcloseall(fileId) # Erase test file os.unlink(fileName)
def _newReadBcmk(self): ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES,'bcmk_toctoc','2009042700_000') fileId = rmn.fstopenall(fileName, rmn.FST_RO) vgd0ptr = vgd.c_vgd_construct() ok = vgd.c_vgd_new_read(vgd0ptr,fileId,-1,-1,-1,-1) ## vgd1ptr = vgd.c_vgd_construct() ## ok = vgd.c_vgd_new_read(vgd1ptr,fileId,-1,-1,-1,-1) rmn.fstcloseall(fileId) return vgd0ptr
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_14(self): """ Queries: Get Vertical Grid Info This example shows how to get the vertical grid definition and print some info. See also: rpnpy.librmn.fstd98.fstopt rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall 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 Some info about the vgrid vkind = vgd.vgd_get(v, 'KIND') vver = vgd.vgd_get(v, 'VERS') ip1diagt = vgd.vgd_get(v, 'DIPT') tlvl = vgd.vgd_get(v, 'VIPT') VGD_KIND_VER_INV = dict((v, k) for k, v in vgd.VGD_KIND_VER.iteritems()) vtype = VGD_KIND_VER_INV[(vkind,vver)] (ldiagval, ldiagkind) = rmn.convertIp(rmn.CONVIP_DECODE, ip1diagt) print("CB14: Found vgrid type=%s (kind=%d, vers=%d) with %d levels and diag level=%7.2f%s (ip1=%d)" % (vtype, vkind, vver, len(tlvl), ldiagval, rmn.kindToString(ldiagkind), ip1diagt)) except: raise finally: # Close file even if an error occured above rmn.fstcloseall(fileId)
def test_12(self): """ Queries: Get Data and Meta This example shows how to * get the data of all records matching selection creterions * then print statistics See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.fstluk rpnpy.librmn.fstd98.fstlir rpnpy.librmn.fstd98.DecodeIp rpnpy.librmn.fstd98.kindToString rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.const """ import os, sys import rpnpy.librmn.all as rmn # Restrict to the minimum the number of messages printed by librmn rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) # Open all RPNStd files in the $ATM_MODEL_DFILES/bcmk/ directory ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk') try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: sys.stderr.write("Problem opening the file: %s\n" % fileName) sys.exit(1) try: # Get the list of record keys matching nomvar='TT' keylist = rmn.fstinl(fileId, nomvar='TT', ip2=12) for k in keylist: # Get the record meta data r = rmn.fstluk(k) d = r['d'] # Decode level info and Print statistics rp1 = rmn.DecodeIp(r['ip1'], r['ip2'], r['ip3'])[0] level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind)) print("CB12: %s (level=%s, ip2=%d) mean=%f, std=%f, min=%f, max=%f" % (r['nomvar'], level, r['ip2'], d.mean(), d.std(), d.min(), d.max())) except: pass finally: # Close file even if an error occured above rmn.fstcloseall(fileId)
def test_11(self): """ Queries: Find records, Read meta This example shows how to * get the list of all records matching selection creterions * get the metadata and decode it * then print some values See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.DecodeIp rpnpy.librmn.fstd98.kindToString rpnpy.librmn.base.newdate rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.const """ import os, sys import rpnpy.librmn.all as rmn # Open all RPNStd files in the $ATM_MODEL_DFILES/bcmk/ directory ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk') try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: sys.stderr.write("Problem opening the file: %s\n" % fileName) sys.exit(1) try: # Get the list of record keys matching nomvar='TT', ip2=12 keylist = rmn.fstinl(fileId, nomvar='TT', ip2=12) for k in keylist: # Get the record meta data m = rmn.fstprm(k) # Decode ip1, and dateo (rp1, rp2, rp3) = rmn.DecodeIp(m['ip1'], m['ip2'], m['ip3']) level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind)) (yyyymmdd, hhmmsshh) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT, m['dateo']) dateo = "%8.8d.%8.8d" % (yyyymmdd, hhmmsshh) nhours = float(m['npas'] * m['deet']) / 3600. print("CB11: %s (%d, %d) level=%s, dateo=%s + %s" % (m['nomvar'], m['ip1'], m['ip2'], level, dateo, nhours)) except: pass finally: # Close file even if an error occured above rmn.fstcloseall(fileId)
def test_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_11bqd(self): import os, sys import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileId = rmn.fstopenall(ATM_MODEL_DFILES+'/bcmk_p/anlp2015070706_000', rmn.FST_RO) for k in rmn.fstinl(fileId, nomvar='TT', ip1=rmn.ip1_all(500., rmn.LEVEL_KIND_PMB), datev=rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20150707, 6000000)): m = rmn.fstprm(k) print("CB11bqd: %s (%d, %d, %s)" % (m['nomvar'], m['ip1'], m['ip2'], m['datev'])) rmn.fstcloseall(fileId)
def test_11b(self): """ Queries: List/Find records This example shows how to * get the list of all records matching selection creterions * encode parameters (ip1, datev) values to use selection criteria See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.ip1_all rpnpy.librmn.fstd98.ip2_all rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.DecodeIp rpnpy.librmn.fstd98.kindToString rpnpy.librmn.base.newdate rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.const """ import os, sys import rpnpy.librmn.all as rmn # Open all RPNStd files in $ATM_MODEL_DFILES/bcmk_p/anlp2015070706_000 ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk_p', 'anlp2015070706_000') try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: sys.stderr.write("Problem opening the file: %s\n" % fileName) sys.exit(1) try: # Encode selection criteria ip1 = rmn.ip1_all(500., rmn.LEVEL_KIND_PMB) datev = rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20150707, 6000000) # Get the list of record keys matching # nomvar='TT' at 500mb and valide date=20150707.06000000 keylist = rmn.fstinl(fileId, nomvar='TT', ip1=ip1, datev=datev) print("CB11b: Found %d records matching TT, ip1=%d, datev=%d" % (len(keylist), ip1, datev)) # Get every record meta data for k in keylist: m = rmn.fstprm(k) print("CB11b: %s (%d, %d, %s)" % (m['nomvar'], m['ip1'], m['ip2'], m['datev'])) except: pass finally: # Close file even if an error occured above rmn.fstcloseall(fileId)
def test_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_11(self): """ Queries: Find records, Read meta This example shows how to * get the list of all records matching selection creterions * get the metadata and decode it * then print some values See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.DecodeIp rpnpy.librmn.fstd98.kindToString rpnpy.librmn.base.newdate rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.const """ import os, sys import rpnpy.librmn.all as rmn # Open all RPNStd files in the $ATM_MODEL_DFILES/bcmk/ directory ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk') try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: sys.stderr.write("Problem opening the file: %s\n" % fileName) sys.exit(1) try: # Get the list of record keys matching nomvar='TT', ip2=12 keylist = rmn.fstinl(fileId, nomvar='TT', ip2=12) for k in keylist: # Get the record meta data m = rmn.fstprm(k) # Decode ip1, and dateo (rp1, rp2, rp3) = rmn.DecodeIp(m['ip1'], m['ip2'], m['ip3']) level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind)) (yyyymmdd,hhmmsshh) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT,m['dateo']) dateo = "%8.8d.%8.8d" % (yyyymmdd,hhmmsshh) nhours = float(m['npas'] * m['deet']) / 3600. print("CB11: %s (%d, %d) level=%s, dateo=%s + %s" % (m['nomvar'], m['ip1'], m['ip2'], level, dateo, nhours)) except: pass finally: # Close file even if an error occured above rmn.fstcloseall(fileId)
def test_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 testDiag_withref8_3d(self): ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES,'bcmk_toctoc','2009042700_000') fileId = rmn.fstopenall(fileName, rmn.FST_RO) vgd0ptr = vgd.vgd_read(fileId) levels8 = vgd.vgd_levels(vgd0ptr, fileId, double_precision=True) rmn.fstcloseall(fileId) (ni,nj,nk) = levels8.shape self.assertEqual([int(x) for x in levels8[ni//2,nj//2,0:5]*10000.], [100000, 138425, 176878, 241408, 305980]) self.assertEqual(len(levels8.shape),3) self.assertEqual(levels8.dtype,np.float64)
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_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_11b(self): """ Queries: List/Find records This example shows how to * get the list of all records matching selection creterions * encode parameters (ip1, datev) values to use selection criteria See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.ip1_all rpnpy.librmn.fstd98.ip2_all rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.DecodeIp rpnpy.librmn.fstd98.kindToString rpnpy.librmn.base.newdate rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.const """ import os, sys import rpnpy.librmn.all as rmn # Open all RPNStd files in $ATM_MODEL_DFILES/bcmk_p/anlp2015070706_000 ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk_p','anlp2015070706_000') try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: sys.stderr.write("Problem opening the file: %s\n" % fileName) sys.exit(1) try: # Encode selection criteria ip1 = rmn.ip1_all(500., rmn.LEVEL_KIND_PMB) datev = rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20150707, 6000000) # Get the list of record keys matching # nomvar='TT' at 500mb and valide date=20150707.06000000 keylist = rmn.fstinl(fileId, nomvar='TT', ip1=ip1, datev=datev) print("CB11b: Found %d records matching TT, ip1=%d, datev=%d" % (len(keylist), ip1, datev)) # Get every record meta data for k in keylist: m = rmn.fstprm(k) print("CB11b: %s (%d, %d, %s)" % (m['nomvar'], m['ip1'], m['ip2'], m['datev'])) except: pass finally: # Close file even if an error occured above rmn.fstcloseall(fileId)
def test_9(self): """ Decoding values When reading an FSTD record metadata, the ip1, ip2, ip3 contains the encoded time and levels values. In the old format, ip1 is used for the level value and ip2 is a none encoded time value in hours. In the new format, all ip1, ip2, ip3 can be used to specify time and level as well as ranges. See also: rpnpy.librmn.fstd98.convertIp rpnpy.librmn.fstd98.convertIPtoPK rpnpy.librmn.fstd98.DecodeIp rpnpy.librmn.fstd98.kindToString rpnpy.librmn.proto.FLOAT_IP rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinf rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.FSTDError rpnpy.librmn.RMNError rpnpy.librmn.const """ import os import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') fileName = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012') # Get list of records try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) keyList = rmn.fstinl(fileId, nomvar='TT') except: raise rmn.FSTDError( "Problem getting list of TT records from file: %s" % fileName) # Get metadata and Decode level value try: for k in keyList: recMeta = rmn.fstprm(k) (level, ikind) = rmn.convertIp(rmn.CONVIP_DECODE, recMeta['ip1']) kindstring = rmn.kindToString(ikind) print("Found %s at level %f %s" % (recMeta['nomvar'], level, kindstring)) except: raise rmn.FSTDError( "Problem getting metadata for TT from file: %s " % fileName) rmn.fstcloseall(fileId)
def test_11qd(self): import os, sys import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileId = rmn.fstopenall(ATM_MODEL_DFILES+'/bcmk', rmn.FST_RO) for k in rmn.fstinl(fileId, nomvar='TT', ip2=12): m = rmn.fstprm(k) rp1 = rmn.DecodeIp(m['ip1'], m['ip2'], m['ip3'])[0] level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind)) dateo = "%8.8d.%8.8d" % rmn.newdate(rmn.NEWDATE_STAMP2PRINT,m['dateo']) print("CB11qd: %s (%d, %d) level=%s, dateo=%s + %s" % (m['nomvar'], m['ip1'], m['ip2'], level, dateo, float(m['npas'] * m['deet']) / 3600.)) rmn.fstcloseall(fileId)
def test_21qd(self): import sys, os, os.path, stat, shutil import rpnpy.librmn.all as rmn fileName = 'geophy.fst' ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName0 = os.path.join(ATM_MODEL_DFILES,'bcmk',fileName) shutil.copyfile(fileName0, fileName) st = os.stat(fileName) os.chmod(fileName, st.st_mode | stat.S_IWRITE) fileId = rmn.fstopenall(fileName, rmn.FST_RW_OLD) rmn.fst_edit_dir(rmn.fstinl(fileId), etiket='MY_NEW_ETK') rmn.fstcloseall(fileId) os.unlink(fileName) # Remove test file
def test_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_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_21qd(self): import sys, os, os.path, stat, shutil import rpnpy.librmn.all as rmn fileName = 'geophy.fst' ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileName0 = os.path.join(ATM_MODEL_DFILES, 'bcmk', fileName) shutil.copyfile(fileName0, fileName) st = os.stat(fileName) os.chmod(fileName, st.st_mode | stat.S_IWRITE) fileId = rmn.fstopenall(fileName, rmn.FST_RW_OLD) rmn.fst_edit_dir(rmn.fstinl(fileId), etiket='MY_NEW_ETK') rmn.fstcloseall(fileId) os.unlink(fileName) # Remove test file