def test_fsteff_list_rec(self): """fsteff accept list and dict as input""" rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) (la,lo) = self.create_basefile() #wrote 2 recs in that order: la, lo funit = rmn.fstopenall(self.fname,rmn.FST_RW) keylist = rmn.fstinl(funit) rmn.fsteff(keylist) rmn.fstcloseall(funit) funit = rmn.fstopenall(self.fname,rmn.FST_RO) kla = rmn.fstinf(funit,nomvar='LA') klo = rmn.fstinf(funit,nomvar='LO') rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(kla,None,'LA found after delete: '+repr(kla)) self.assertEqual(klo,None,'LO found after delete: '+repr(klo)) (la,lo) = self.create_basefile() #wrote 2 recs in that order: la, lo funit = rmn.fstopenall(self.fname,rmn.FST_RW) klo = rmn.fstinf(funit,nomvar='LO') rmn.fsteff(klo) rmn.fstcloseall(funit) funit = rmn.fstopenall(self.fname,rmn.FST_RO) klo = rmn.fstinf(funit,nomvar='LO') rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(klo,None,'LO found after delete: '+repr(klo))
def test_fsteff_list_rec(self): """fsteff accept list and dict as input""" rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) (la, lo) = self.create_basefile() #wrote 2 recs in that order: la, lo funit = rmn.fstopenall(self.fname, rmn.FST_RW) keylist = rmn.fstinl(funit) rmn.fsteff(keylist) rmn.fstcloseall(funit) funit = rmn.fstopenall(self.fname, rmn.FST_RO) kla = rmn.fstinf(funit, nomvar='LA') klo = rmn.fstinf(funit, nomvar='LO') rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(kla, None, 'LA found after delete: ' + repr(kla)) self.assertEqual(klo, None, 'LO found after delete: ' + repr(klo)) (la, lo) = self.create_basefile() #wrote 2 recs in that order: la, lo funit = rmn.fstopenall(self.fname, rmn.FST_RW) klo = rmn.fstinf(funit, nomvar='LO') rmn.fsteff(klo) rmn.fstcloseall(funit) funit = rmn.fstopenall(self.fname, rmn.FST_RO) klo = rmn.fstinf(funit, nomvar='LO') rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(klo, None, 'LO found after delete: ' + repr(klo))
def delRec(recKey): ''' Deletes a record in an fst by a key. Used to allow user to use the same nomvar as an existing record, where we no longer want to keep the data from the existing record.''' try: rmn.fsteff(recKey) #print('record {} deleted'.format(recKey)) except: pass
def fsteff(ihandle): """Erase a record (Interface to fsteff) Fstdc.fsteff(ihandle) @param ihandle handle of the record to erase (int) @exception TypeError @exception Fstdc.error """ try: _rmn.fsteff(ihandle) except: raise error("")
def test_5(self): """ Erase a record You can delete a record in an FSTD file (a la Editfst's exclude function) with a simple call to fsteff. No need to write all the other records in another file. See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinf rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fsteff rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.FSTDError rpnpy.librmn.RMNError rpnpy.librmn.const """ import os, os.path, stat, shutil import rpnpy.librmn.all as rmn # Take an editable copy of the file ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') fileName0 = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012') fileName = 'some_rpnstd_file.fst' shutil.copyfile(fileName0, fileName) st = os.stat(fileName) os.chmod(fileName, st.st_mode | stat.S_IWRITE) # Erase record named PR in file try: fileId = rmn.fstopenall(fileName, rmn.FST_RW) pr_key = rmn.fstinf( fileId, nomvar='PR')['key'] #Match the first record named PR if pr_key: rmn.fsteff(pr_key) #Erase previously found record except: raise rmn.FSTDError("Problem erasing record in File: %s" % fileName) # Close rmn.fstcloseall(fileId) # Erase test file os.unlink(fileName)
def test_5(self): """ Erase a record You can delete a record in an FSTD file (a la Editfst's exclude function) with a simple call to fsteff. No need to write all the other records in another file. See also: rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstinf rpnpy.librmn.fstd98.fstinl rpnpy.librmn.fstd98.fsteff rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.FSTDError rpnpy.librmn.RMNError rpnpy.librmn.const """ import os, os.path, stat, shutil import rpnpy.librmn.all as rmn # Take an editable copy of the file ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') fileName0 = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012') fileName = 'some_rpnstd_file.fst' shutil.copyfile(fileName0, fileName) st = os.stat(fileName) os.chmod(fileName, st.st_mode | stat.S_IWRITE) # Erase record named PR in file try: fileId = rmn.fstopenall(fileName, rmn.FST_RW) pr_key = rmn.fstinf(fileId, nomvar='PR')['key'] #Match the first record named PR if pr_key: rmn.fsteff(pr_key) #Erase previously found record except: raise rmn.FSTDError("Problem erasing record in File: %s" % fileName) # Close rmn.fstcloseall(fileId) # Erase test file os.unlink(fileName)
def test_fsteditdir_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 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 plotFSTs(season=season, spcs=spcs, spcsFiles=spcsFiles, outputName = outputName, saveDir=saveDir): # print minimum outputs rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) mInds = [] for m in season: mInds += [monthList.index(m)] if os.path.exists(saveDir) == False: nu.makeDir(saveDir) for spcInd, nomvar in enumerate(spcs): try: filename = os.path.join(saveDir, 'output_file_{0}_{1}.fst'.format(outputName, nomvar)) print('Creating and saving to {}'.format(filename)) tmp = open(filename, 'w+'); tmp.close() output_file = filename file_id = rmn.fnom(output_file) open_fst = rmn.fstouv(file_id, rmn.FST_RW) open_file = spcsFiles[spcInd] print "Parameter: " + nomvar seaSpcData = get_var(pyg.open(open_file), nomvar, mInds) nc_lnsp = pyg.open(lnsp_file) pressures = get_pressures(nc_lnsp, mInds) timelen, levlen, latlen, lonlen = seaSpcData.shape #NOTE: uncomment the following three lines to prep data for basemap use #lonShiftSSData = shift_lon(seaSpcData) #vertInterpSSData = vert_interp(pressures, lonShiftSSData) #meanSSData = np.mean(vertInterpSSData, axis=0) #NOTE: uncommment the following four liness to use for fst plotting vertInterpSSData = vert_interp(pressures, seaSpcData) meanSSData = np.mean(vertInterpSSData, axis=0) # temp for lvl, ray in enumerate(meanSSData): meanSSData[lvl] = np.flipud(ray) scaleFac = scaleSpcs[allSpcs.index(nomvar)] scaledSSData = meanSSData*scaleFac #define grid for this file - note that the MACC grid in the file is #defined for lons -180 to 180, but the python defGrid_L can't deal #with that and defines the grid from 0 to 360 so will have to reorder #the MACC fields a bit, or they end up 180 deg out of phase # Also necessary to add one more longitude to wrap around dlatlon = 360./lonlen # this is equal to the resolution of the grid params0 = { 'grtyp' : 'Z', 'grref' : 'L', 'nj' : latlen, 'ni' : lonlen, 'lat0' : -90., 'lon0' : 0., 'dlat' : dlatlon, 'dlon' : dlatlon } MACC_grid= rmn.encodeGrid(params0) print("Grids created.") print 'Grid Shape:' + str(MACC_grid['shape']) # copies the default record new_record = rmn.FST_RDE_META_DEFAULT.copy() tic_record = rmn.FST_RDE_META_DEFAULT.copy() tac_record = rmn.FST_RDE_META_DEFAULT.copy() try: rmn.writeGrid(file_id, MACC_grid) tac = rmn.fstinl(file_id, nomvar='>>')[0] tic = rmn.fstinl(file_id, nomvar='^^')[0] tic_record.update(rmn.fstprm(tic)) tac_record.update(rmn.fstprm(tac)) tic_record.update({'datyp' : rmn.FST_DATYP_LIST['float']}) tac_record.update({'datyp' : rmn.FST_DATYP_LIST['float']}) rmn.fsteff(tic) rmn.fsteff(tac) tic_record.update({'d': MACC_grid['ay']}) tac_record.update({'d': MACC_grid['ax']}) toc_record = vgd.vgd_new_pres(const_pressure, ip1=MACC_grid['ig1'], ip2=MACC_grid['ig2']) rmn.fstecr(file_id, tic_record) # write the dictionary record to the file as a new record rmn.fstecr(file_id, tac_record) # write the dictionary record to the file as a new record vgd.vgd_write(toc_record, file_id) except: raise for rp1 in xrange(len(const_pressure)): # writes a record for every level (as a different ip1) try: # converts rp1 into a ip1 with pressure kind ip1 = rmn.convertIp(rmn.CONVIP_ENCODE, const_pressure[rp1], rmn.KIND_PRESSURE) new_record.update(MACC_grid) new_record.update({ # Update with specific meta 'nomvar': nomvar, 'typvar': 'C', 'etiket': 'MACCRean', 'ni' : MACC_grid['ni'], 'nj' : MACC_grid['nj'], 'ig1' : tic_record['ip1'], 'ig2' : tic_record['ip2'], 'ig3' : tic_record['ip3'], 'ig4' : tic_record['ig4'], 'dateo' : rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20120101, 0000000), 'deet' : 0, # Timestep in sec 'ip1' : ip1 }) #tmp_nparray = np.asfortranarray(monthly_mean[rp1]) tmp = scaledSSData[rp1] tmp = np.transpose(tmp) # data array is structured as tmp = monthly_mean[level] where monthly_mean is [level, lat, lon] new_record.update({'d': tmp.astype(np.float32)}) # Updates with data array in the form (lon x lat) print "Defined a new record with dimensions ({0}, {1})".format(new_record['ni'], new_record['nj']) rmn.fstecr(file_id, new_record) # write the dictionary record to the file as a new record except: #rmn.closeall(file_id) rmn.fstfrm(file_id) rmn.fclos(file_id) raise rmn.fstfrm(file_id) rmn.fclos(file_id) print('{} complete~'.format(filename)) except: rmn.fstfrm(file_id) rmn.fclos(file_id) raise print('Finished plotting all FSTs. ')
def delRec(recKey): try: rmn.fsteff(recKey) print('record deleted') except: pass