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_writeGrid(self): ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') file0 = os.path.join(ATM_MODEL_DFILES.strip(),'bcmk/geophy.fst') funit = rmn.fstopenall(file0) rec = rmn.fstlir(funit, nomvar='ME') grid0 = rmn.readGrid(funit, rec) rmn.fstcloseall(funit) grid1 = rmn.defGrid_L(180,60,0.,180.,1.,0.5) grid2 = rmn.defGrid_ZE(90,45,10.,11.,1.,0.5,0.,180.,1.,270.) grid3 = rmn.defGrid_YY(31,5,0.,180.,1.,270.) self.erase_testfile() myfile = self.fname funit = rmn.fstopenall(myfile, rmn.FST_RW) rmn.fstecr(funit,rec['d'],rec) rmn.writeGrid(funit, grid0) rmn.writeGrid(funit, grid1) rmn.writeGrid(funit, grid2) rmn.writeGrid(funit, grid3) rmn.fstcloseall(funit) funit = rmn.fstopenall(myfile, rmn.FST_RO) rec = rmn.fstlir(funit, nomvar='ME') grid0b = rmn.readGrid(funit, rec) rmn.fstcloseall(funit) self.erase_testfile() for k in grid0.keys(): if isinstance(grid0[k],np.ndarray): ok = np.any(np.abs(grid0b[k]-grid0[k]) > self.epsilon) self.assertFalse(ok, 'For k=%s, grid0b - grid0 = %s' % (k,str(np.abs(grid0b[k]-grid0[k])))) else: self.assertEqual(grid0b[k],grid0[k], 'For k=%s, expected:%s, got:%s' % (k, str(grid0[k]), str(grid0b[k])))
def test_23qd(self): import os, sys, datetime from scipy.constants import knot as KNOT2MS import numpy as np import rpnpy.librmn.all as rmn import rpnpy.vgd.all as vgd fdate = datetime.date.today().strftime('%Y%m%d') + '00_048' fileNameOut = 'uvfstfile.fst' fileIdIn = rmn.fstopenall( os.getenv('CMCGRIDF') + '/prog/regeta/' + fdate) fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) vgd.vgd_write(vgd.vgd_read(fileIdIn), fileIdOut) (uu, vv, uvarray, copyGrid) = ({'d': None}, {'d': None}, None, True) for k in rmn.fstinl(fileIdIn, nomvar='UU'): uu = rmn.fstluk(k, dataArray=uu['d']) vv = rmn.fstlir(fileIdIn, nomvar='VV', ip1=uu['ip1'], ip2=uu['ip2'], datev=uu['datev'], dataArray=vv['d']) if uvarray is None: uvarray = np.empty(uu['d'].shape, dtype=uu['d'].dtype, order='FORTRAN') uv = uu.copy() uv.update({'d': uvarray, 'nomvar': 'WSPD'}) uv['d'][:, :] = np.sqrt(uu['d']**2. + vv['d']**2.) * KNOT2MS rmn.fstecr(fileIdOut, uv) if copyGrid: copyGrid = False rmn.writeGrid(fileIdOut, rmn.readGrid(fileIdIn, uu)) rmn.fstcloseall(fileIdIn) rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file
def test_fsteff_list_rec(self): """fsteff accept list and dict as input""" rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) (la, lo) = self.create_basefile() #wrote 2 recs in that order: la, lo funit = rmn.fstopenall(self.fname, rmn.FST_RW) keylist = rmn.fstinl(funit) rmn.fsteff(keylist) rmn.fstcloseall(funit) funit = rmn.fstopenall(self.fname, rmn.FST_RO) kla = rmn.fstinf(funit, nomvar='LA') klo = rmn.fstinf(funit, nomvar='LO') rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(kla, None, 'LA found after delete: ' + repr(kla)) self.assertEqual(klo, None, 'LO found after delete: ' + repr(klo)) (la, lo) = self.create_basefile() #wrote 2 recs in that order: la, lo funit = rmn.fstopenall(self.fname, rmn.FST_RW) klo = rmn.fstinf(funit, nomvar='LO') rmn.fsteff(klo) rmn.fstcloseall(funit) funit = rmn.fstopenall(self.fname, rmn.FST_RO) klo = rmn.fstinf(funit, nomvar='LO') rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(klo, None, 'LO found after delete: ' + repr(klo))
def 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_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 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 test_22(self): """ Edit: Copy records (a la editfst desire) This example shows how to * select records in a RPNStd file * read the record data + meta * write the record data + meta See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.fsrinl rpnpy.librmn.fstd98.fsrluk rpnpy.librmn.fstd98.fsrecr rpnpy.librmn.const """ import os, os.path, sys import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileNameIn = os.path.join(ATM_MODEL_DFILES, 'bcmk') fileNameOut = 'myfstfile.fst' # Open Files try: fileIdIn = rmn.fstopenall(fileNameIn) fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) except: sys.stderr.write("Problem opening the files: %s, %s\n" % (fileNameIn, fileNameOut)) sys.exit(1) try: # Get the list of records to copy keylist1 = rmn.fstinl(fileIdIn, nomvar='UU') keylist2 = rmn.fstinl(fileIdIn, nomvar='VV') for k in keylist1 + keylist2: # Read record data and meta from fileNameIn r = rmn.fstluk(k) # Write the record to fileNameOut rmn.fstecr(fileIdOut, r) print("CB22: Copied %s ip1=%d, ip2=%d, dateo=%s" % (r['nomvar'], r['ip1'], r['ip2'], r['dateo'])) except: pass finally: # Properly close files even if an error occured above # This is important when editing to avoid corrupted files rmn.fstcloseall(fileIdIn) rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file
def test_22(self): """ Edit: Copy records (a la editfst desire) This example shows how to * select records in a RPNStd file * read the record data + meta * write the record data + meta See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.fsrinl rpnpy.librmn.fstd98.fsrluk rpnpy.librmn.fstd98.fsrecr rpnpy.librmn.const """ import os, os.path, sys import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileNameIn = os.path.join(ATM_MODEL_DFILES,'bcmk') fileNameOut = 'myfstfile.fst' # Open Files try: fileIdIn = rmn.fstopenall(fileNameIn) fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) except: sys.stderr.write("Problem opening the files: %s, %s\n" % (fileNameIn, fileNameOut)) sys.exit(1) try: # Get the list of records to copy keylist1 = rmn.fstinl(fileIdIn, nomvar='UU') keylist2 = rmn.fstinl(fileIdIn, nomvar='VV') for k in keylist1+keylist2: # Read record data and meta from fileNameIn r = rmn.fstluk(k) # Write the record to fileNameOut rmn.fstecr(fileIdOut, r) print("CB22: Copied %s ip1=%d, ip2=%d, dateo=%s" % (r['nomvar'], r['ip1'], r['ip2'], r['dateo'])) except: pass finally: # Properly close files even if an error occured above # This is important when editing to avoid corrupted files rmn.fstcloseall(fileIdIn) rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file
def test_22qd(self): import os, os.path import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileNameOut = 'myfstfile.fst' fileIdIn = rmn.fstopenall(ATM_MODEL_DFILES+'/bcmk') fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) for k in rmn.fstinl(fileIdIn, nomvar='UU') + rmn.fstinl(fileIdIn, nomvar='VV'): rmn.fstecr(fileIdOut, rmn.fstluk(k)) rmn.fstcloseall(fileIdIn) rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file
def test_22qd(self): import os, os.path import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() fileNameOut = 'myfstfile.fst' fileIdIn = rmn.fstopenall(ATM_MODEL_DFILES + '/bcmk') fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) for k in rmn.fstinl(fileIdIn, nomvar='UU') + rmn.fstinl(fileIdIn, nomvar='VV'): rmn.fstecr(fileIdOut, rmn.fstluk(k)) rmn.fstcloseall(fileIdIn) rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file
def 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 testWriteDesc(self): vgd0ptr = self._newReadBcmk() self._erase_testfile() fileName = self.fname fileId = rmn.fstopenall(fileName, rmn.FST_RW) vgd.vgd_write(vgd0ptr,fileId) rmn.fstcloseall(fileId) fileId = rmn.fstopenall(fileName, rmn.FST_RO) vgd1ptr = vgd.vgd_read(fileId) rmn.fstcloseall(fileId) self._erase_testfile() ok = vgd.vgd_cmp(vgd0ptr,vgd1ptr) self.assertTrue(ok)
def test_13(self): """ Interpolating Data Interpolating data to/from known FSTD grids is made easy with the Ezscint package. There are a few exceptions though * you can only interpolate to a Y grid, not from it. * multi-parts grids (Yin-Yang, ...) have to be dealth with in a special way (see below) In this example we'll interpolate forecast data onto the analysis grid to make some computations See also: """ import os import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') fileName0 = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_000') #Analysis fileName1 = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012') #Forecast # Get data and grid definition for P0 in the 1st FSTD file try: fileId = rmn.fstopenall(fileName0, rmn.FST_RO) p0Data1 = rmn.fstlir(fileId, nomvar='P0') # Get the record data and metadata along with partial grid info p0Data1['iunit'] = fileId p0GridId = rmn.ezqkdef(p0Data1) # use ezscint to retreive a grid id p0Grid1 = rmn.decodeGrid(p0GridId) # Decode all the grid parameters values rmn.fstcloseall(fileId) except: raise rmn.FSTDError("Problem getting P0 record grid meta from file: %s" % fileName0) # Get data and grid definition for P0 in the 2nd FSTD file try: fileId = rmn.fstopenall(fileName1, rmn.FST_RO) p0Data2 = rmn.fstlir(fileId, nomvar='P0', ip2=12) # Get the record data and metadata along with partial grid info p0Data2['iunit'] = fileId p0GridId = rmn.ezqkdef(p0Data2) # use ezscint to retreive a grid id p0Grid2 = rmn.decodeGrid(p0GridId) # Decode all the grid parameters values rmn.fstcloseall(fileId) except: raise rmn.FSTDError("Problem getting P0 record grid meta from file: %s " % fileName1) # Make a cubic interpolation of p0Data2 onto p0Grid1 with extrapolated values set to Minvalue of the field rmn.ezsetopt(rmn.EZ_OPT_EXTRAP_DEGREE, rmn.EZ_EXTRAP_MIN) rmn.ezsetopt(rmn.EZ_OPT_INTERP_DEGREE, rmn.EZ_INTERP_LINEAR) p0Data2_onGrid1 = rmn.ezsint(p0Grid1['id'], p0Grid2['id'], p0Data2['d']) # Make some computation p0Diff = p0Data2_onGrid1 - p0Data1['d']
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 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 testWriteDesc(self): vgd0ptr = self._newReadBcmk() self.erase_testfile() fileName = self.fname fileId = rmn.fstopenall(fileName, rmn.FST_RW) ok = vgd.c_vgd_write_desc(vgd0ptr, fileId) rmn.fstcloseall(fileId) self.assertEqual(ok, vgd.VGD_OK) fileId = rmn.fstopenall(fileName, rmn.FST_RO) vgd1ptr = vgd.c_vgd_construct() ok = vgd.c_vgd_new_read(vgd1ptr, fileId, -1, -1, -1, -1) rmn.fstcloseall(fileId) self.erase_testfile() ok = vgd.c_vgd_vgdcmp(vgd0ptr, vgd1ptr) self.assertEqual(ok, vgd.VGD_OK)
def test_readGridRef(self): ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') myfile = os.path.join(ATM_MODEL_DFILES.strip(),'bcmk/geophy.fst') funit = rmn.fstopenall(myfile) rec = rmn.fstlir(funit, nomvar='ME') grid = rmn.readGrid(funit,rec) self.assertEqual(grid['grref'],'E') self.assertEqual(grid['grtyp'],'Z') self.assertEqual(grid['ig1'],2002) self.assertEqual(grid['ig2'],1000) self.assertEqual(grid['ig3'],0) self.assertEqual(grid['ig4'],0) self.assertEqual(grid['ig1ref'],900) self.assertEqual(grid['ig2ref'],0) self.assertEqual(grid['ig3ref'],43200) self.assertEqual(grid['ig4ref'],43200) self.assertEqual(grid['ni'],201) self.assertEqual(grid['nj'],100) self.assertEqual(grid['xg1'],0.) self.assertEqual(grid['xg2'],180.) self.assertEqual(grid['xg3'],0.) self.assertEqual(grid['xg4'],270.) self.assertEqual(grid['xlat1'],0.) self.assertEqual(grid['xlon1'],180.) self.assertEqual(grid['xlat2'],0.) self.assertEqual(grid['xlon2'],270.) self.assertEqual(grid['tag1'],2002) self.assertEqual(grid['tag2'],1000) self.assertEqual(grid['tag3'],0)
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_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 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_readGridRef(self): ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') myfile = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/geophy.fst') funit = rmn.fstopenall(myfile) rec = rmn.fstlir(funit, nomvar='ME') grid = rmn.readGrid(funit, rec) self.assertEqual(grid['grref'], 'E') self.assertEqual(grid['grtyp'], 'Z') self.assertEqual(grid['ig1'], 2002) self.assertEqual(grid['ig2'], 1000) self.assertEqual(grid['ig3'], 0) self.assertEqual(grid['ig4'], 0) self.assertEqual(grid['ig1ref'], 900) self.assertEqual(grid['ig2ref'], 0) self.assertEqual(grid['ig3ref'], 43200) self.assertEqual(grid['ig4ref'], 43200) self.assertEqual(grid['ni'], 201) self.assertEqual(grid['nj'], 100) self.assertEqual(grid['xg1'], 0.) self.assertEqual(grid['xg2'], 180.) self.assertEqual(grid['xg3'], 0.) self.assertEqual(grid['xg4'], 270.) self.assertEqual(grid['xlat1'], 0.) self.assertEqual(grid['xlon1'], 180.) self.assertEqual(grid['xlat2'], 0.) self.assertEqual(grid['xlon2'], 270.) self.assertEqual(grid['tag1'], 2002) self.assertEqual(grid['tag2'], 1000) self.assertEqual(grid['tag3'], 0)
def getConc(filePath, level, spc, niRange, njRange): """ Get the concentration data for an FST file. Returns the data in an array, the datakey as a key and the fileID of the FST file. """ try: fileID = rmn.fstopenall(filePath, rmn.FST_RO) dataKey = rmn.fstinf(fileID, nomvar=spc, ip1=level)['key'] dataRec = rmn.fstluk(dataKey) tempConc = dataRec['d'] print(len(tempConc), len(tempConc[0])) print(tempConc) concData = dataRec['d'][niRange[0]:niRange[1] + 1, njRange[0]:njRange[1] + 1] print('File {} recorded'.format(filePath)) return {'concData': concData, 'dataKey': dataKey, 'fileID': fileID} except TypeError: print('Unable to record file {}. Please see log for details'.format( filePath)) # log an error into the log file logging.warning( 'nomvar {} and ip1 {} could not be found for file {}.'.format( fileID, spc, level, filePath)) pass
def test_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 getID(filePath): ''' Returns the ID of the fst file provided by the filePath. ''' rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) # open file to process fileID = rmn.fstopenall(filePath, rmn.FST_RW) return fileID
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 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 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 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_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_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_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_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_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 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 getID(filePath): """ Gets the fileID and opens the file in read/write mode. """ rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) # open file to process fileID = rmn.fstopenall(filePath, rmn.FST_RW) return 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 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_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_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 testWriteDesc(self): vgd0ptr = self._newReadBcmk() self.erase_testfile() fileName = self.fname fileId = rmn.fstopenall(fileName, rmn.FST_RW) ok = vgd.c_vgd_write_desc(vgd0ptr,fileId) rmn.fstcloseall(fileId) self.assertEqual(ok,vgd.VGD_OK) fileId = rmn.fstopenall(fileName, rmn.FST_RO) vgd1ptr = vgd.c_vgd_construct() ok = vgd.c_vgd_new_read(vgd1ptr,fileId,-1,-1,-1,-1) rmn.fstcloseall(fileId) self.erase_testfile() ok = vgd.c_vgd_vgdcmp(vgd0ptr,vgd1ptr) self.assertEqual(ok,vgd.VGD_OK)
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 _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 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_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 getGrid(directory): fileName = os.listdir(directory) fileID = rmn.fstopenall(directory + fileName, rmn.FST_RO) getKeys(level=defaultIp1, spc=defaultSpc, fileID=fileID) #now have data for all applicable fields grid = rmn.readGrid(fileID, rec) return grid
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 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 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 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 _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 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_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)