def testDifdateToPrintKnownValues(self): """Difdatr to print should give known result with known input""" for yyyymmdd,hhmmsshh,nhours,hhmmsshh2 in self.knownValues: idate1 = rmn.newdate(rmn.NEWDATE_PRINT2STAMP,yyyymmdd,hhmmsshh) idate2 = rmn.newdate(rmn.NEWDATE_PRINT2STAMP,yyyymmdd,hhmmsshh2) nhours2 = rmn.difdatr(idate2,idate1) self.assertEqual(nhours2,nhours,repr(nhours2)+' != '+repr(nhours))
def testIncdateFromPrintKnownValues(self): """Incdatr should give known result with known input""" for yyyymmdd,hhmmsshh,nhours,hhmmsshh2 in self.knownValues: idate1 = rmn.newdate(rmn.NEWDATE_PRINT2STAMP,yyyymmdd,hhmmsshh) idate2 = rmn.incdatr(idate1,nhours) (iout1,iout2) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT,idate2) self.assertEqual((iout1,iout2),(yyyymmdd,hhmmsshh2),repr((iout1,iout2))+' != '+repr((yyyymmdd,hhmmsshh2)))
def testSanity(self): """Convert back and fort from print to CMC data should give back same result""" for yyyymmdd, hhmmsshh, stamp in self.knownValues: iout = rmn.newdate(rmn.NEWDATE_PRINT2STAMP, yyyymmdd, hhmmsshh) (iout1, iout2) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT, iout) self.assertEqual((iout1, iout2), (yyyymmdd, hhmmsshh), repr((iout1, iout2)) + ' != ' + repr( (yyyymmdd, hhmmsshh)))
def test_7(self): """ Manipulating Dates Dates in FSTD files are encoded integers. Conversion to more human friendly formats and manipulation can be done using the RPNDate and RPNDateRange classes or with the newdate function. See also: rpnpy.rpndate.RPNDate rpnpy.rpndate.RPNDateRange rpnpy.librmn.base.newdate rpnpy.librmn.base.incdatr rpnpy.librmn.base.difdatr rpnpy.librmn.const """ from rpnpy.rpndate import RPNDate, RPNDateRange d1 = RPNDate(20030423, 11453500) d2 = RPNDate(d1) d2 = d2.incr(48) dr = RPNDateRange(d1, d2, 6) print(str(dr.lenght())) # 48.0 for d3 in dr: print(str(d3)) # RPNDate(20030423,11453500) ; RPNDate(20030423,11453500,dt= 3600.0,nstep= 0.0) # RPNDate(20030423,17453500) ; RPNDate(20030423,11453500,dt= 3600.0,nstep= 6.0) # RPNDate(20030423,23453500) ; RPNDate(20030423,11453500,dt= 3600.0,nstep= 12.0) # RPNDate(20030424,05453500) ; RPNDate(20030423,11453500,dt= 3600.0,nstep= 18.0) # RPNDate(20030424,11453500) ; RPNDate(20030423,11453500,dt= 3600.0,nstep= 24.0) # RPNDate(20030424,17453500) ; RPNDate(20030423,11453500,dt= 3600.0,nstep= 30.0) # RPNDate(20030424,23453500) ; RPNDate(20030423,11453500,dt= 3600.0,nstep= 36.0) # RPNDate(20030425,05453500) ; RPNDate(20030423,11453500,dt= 3600.0,nstep= 42.0) # RPNDate(20030425,11453500) ; RPNDate(20030423,11453500,dt= 3600.0,nstep= 48.0) ## Lower level functions can also be used wherever more convenient. import rpnpy.librmn.all as rmn yyyymmdd = 20150102 #Jan 2nd, 2015 hhmmsshh = 13141500 #13h 14min 15sec cmcdate = rmn.newdate(rmn.NEWDATE_PRINT2STAMP, yyyymmdd, hhmmsshh) nhours = 6. cmcdate2 = rmn.incdatr(cmcdate, nhours) (yyyymmdd2, hhmmsshh2) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT, cmcdate2) print("%06d.%06d + %4.1fh = %06d.%06d" % (yyyymmdd, hhmmsshh, nhours, yyyymmdd2, hhmmsshh2)) # 20150102.13141500 + 6.0h = 20150102.19141500 nhours2 = rmn.difdatr(cmcdate2, cmcdate) print("%06d.%06d - %06d.%06d = %4.1fh" % (yyyymmdd2, hhmmsshh2, yyyymmdd, hhmmsshh, nhours2))
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 testNewdateToPrintKnownValues(self): """Newdate to print should give known result with known input""" for yyyymmdd, hhmmsshh, stamp in self.knownValues: (iout1, iout2) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT, stamp) self.assertEqual((iout1, iout2), (yyyymmdd, hhmmsshh), repr((iout1, iout2)) + ' != ' + repr( (yyyymmdd, hhmmsshh)))
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_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_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 testLeapYearKnownValues(self): """ignore_leapyear option should give known result with known input""" yyyymmdd = 20120228 hhmmsshh = 0 nhours = 24. idate1 = rmn.newdate(rmn.NEWDATE_PRINT2STAMP, yyyymmdd, hhmmsshh) idate2 = rmn.incdatr(idate1, nhours) (yyyymmdd2, hhmmsshh2) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT, idate2) self.assertEqual(yyyymmdd2, 20120229) rmn.ignore_leapyear() idate2 = rmn.incdatr(idate1, nhours) (yyyymmdd2, hhmmsshh2) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT, idate2) self.assertEqual(yyyymmdd2, 20120301) rmn.accept_leapyear() idate2 = rmn.incdatr(idate1, nhours) (yyyymmdd2, hhmmsshh2) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT, idate2) self.assertEqual(yyyymmdd2, 20120229) rmn.newdate_options_set(rmn.NEWDATE_OPT_365DAYS) rmn.newdate_options_set(rmn.NEWDATE_OPT_360DAYS) idate2 = rmn.incdatr(idate1, nhours) (yyyymmdd2, hhmmsshh2) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT, idate2) self.assertEqual(yyyymmdd2, 20120301) rmn.newdate_options_set(rmn.NEWDATE_OPT_GREGORIAN) idate2 = rmn.incdatr(idate1, nhours) (yyyymmdd2, hhmmsshh2) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT, idate2) self.assertEqual(yyyymmdd2, 20120229)
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_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_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_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_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 txt2fstRec(txtRec, verbose=False): """ Create a new RPNStdRec from data in numpy array and some metadata See: https://wiki.cmc.ec.gc.ca/wiki/Python-RPN/2.1/examples#Edit:_New_file_and_records_from_scratch """ if verbose: sys.stdout.write('Converting to RPNStd Rec: {}\n'.format(filename)) try: nomvar = TXT2FSTDICT[txtRec['Title']]['name'] mul = TXT2FSTDICT[txtRec['Title']]['mul'] add = TXT2FSTDICT[txtRec['Title']]['add'] except: nomvar, mul, add = txtRec['Title'], 1., 0. yyyymmdd = int(txtRec['ValidTime'][0:8]) hhmmss00 = int(txtRec['ValidTime'][8:14]+'00') #TODO: get ip2, deet from txtRec['MinorProductParameters'] ip2 = 0 # Forecast of 0h deet = 3600 # Timestep in sec npas = int(ip2*3600/deet) # Step number etiket = txtRec['MajorProductType'] datyp = rmn.dtype_numpy2fst(txtRec['Data'].dtype) (ni, nj) = txtRec['Data'].shape fstRec = rmn.FST_RDE_META_DEFAULT.copy() # Copy default record meta fstRec.update(defineZPSGrid(txtRec, verbose)) # Update with grid info fstRec.update({ # Update with specific meta and data array 'typvar': 'A', 'nomvar': nomvar, 'ip1' : 0, # Level 'ip2' : ip2, 'dateo' : rmn.newdate(rmn.NEWDATE_PRINT2STAMP, yyyymmdd, hhmmss00), 'deet' : deet, 'npas' : npas, 'ni' : ni, 'nj' : nj, 'nk' : 1, 'etiket': etiket, 'nbits' : 16, 'datyp' : rmn.dtype_numpy2fst(txtRec['Data'].dtype), 'd' : txtRec['Data'] * mul + add }) return fstRec
def __init__(self, mydate, hms=None, dt=None, nstep=None): self.__updated = 1 self.__datev = 0 self.__dict__['dateo'] = 0 self.__dict__['dt'] = 0 self.__dict__['nstep'] = 0 if isinstance(mydate, datetime.datetime): (yyyy, mo, dd, hh, mn, ss, dummy, dummy2, dummy3) = \ mydate.utctimetuple() cs = int(mydate.microsecond//10000) mydate = yyyy*10000+mo*100+dd hms = hh*1000000+mn*10000+ss*100+cs RPNDate.__init__(self, mydate, hms, dt=dt, nstep=nstep) elif isinstance(mydate, RPNDate): self.dateo = mydate.dateo self.dt = mydate.dt self.nstep = mydate.nstep elif not type(mydate) == type(0): try: RPNDate.__init__(self, mydate.dateo, dt=mydate.deet, nstep=mydate.npas) except: raise TypeError('RPNDate: Cannot instanciate with arg of type :' + str(type(mydate))) else: if hms is None: self.dateo = mydate else: if not type(hms) == type(0): raise TypeError('RPNDate: arguments should be of type int') dummy = 0 self.dateo = _rmn.newdate(_rmn.NEWDATE_PRINT2STAMP, mydate, hms) if not dt is None: self.dt = dt if not nstep is None: self.nstep = nstep self.__update(1)
sys.stdout.write("Opening the output file: {}\n". format(args.outputFile)) try: fileIdOut = rmn.fstopenall(args.outputFile, rmn.FST_RW) except: sys.stderr.write("ERROR: Problem opening the file: {}\n". format(args.outputFile)) sys.exit(1) status = 0 for filename in args.inputFile: try: txtRec = txtFile2Rec(filename, args.verbose) fstRec = txt2fstRec(txtRec, args.verbose) if args.verbose: (yyyymmdd, hhmmss00) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT, fstRec['dateo']) sys.stdout.write("Writing to File: {} {}.{}\n". format(fstRec['nomvar'], yyyymmdd, hhmmss00)) rmn.fstecr(fileIdOut, fstRec['d'], fstRec) except: sys.stderr.write("ERROR: Problem encountered ---- ABORT\n") rmn.fstcloseall(fileIdOut) status = 1 break rmn.fstcloseall(fileIdOut) sys.exit(status) # -*- Mode: C; tab-width: 4; indent-tabs-mode: nil -*-
def __str__(self): ymd = hms = 0 (ymd, hms) = _rmn.newdate(_rmn.NEWDATE_STAMP2PRINT, self.datev) return "{0:08d}.{1:08d}".format(ymd, hms)
def testNewdateFromPrintKnownValues(self): """Newdate from print should give known result with known input""" for yyyymmdd, hhmmsshh, stamp in self.knownValues: iout = rmn.newdate(rmn.NEWDATE_PRINT2STAMP, yyyymmdd, hhmmsshh) self.assertEqual(iout, stamp, repr(iout) + ' != ' + repr(stamp))
def test_24(self): """ Edit: New file from scratch This example shows how to * Create a record meta and data from scratch * Create grid descriptors for the data * Create vgrid descriptor for the data * write the recod data + meta * write the grid descriptors * write the vgrid descriptor See also: rpnpy.librmn.fstd98.fstopt rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.fsrecr rpnpy.librmn.fstd98.dtype_fst2numpy rpnpy.librmn.grids.encodeGrid rpnpy.librmn.grids.defGrid_ZE rpnpy.librmn.grids.writeGrid rpnpy.librmn.base.newdate rpnpy.vgd.base.vgd_new rpnpy.vgd.base.vgd_new_pres rpnpy.vgd.base.vgd_new_get rpnpy.vgd.base.vgd_write rpnpy.librmn.const rpnpy.vgd.const """ import os, sys 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) # Create Record grid gp = { 'grtyp' : 'Z', 'grref' : 'E', 'ni' : 90, 'nj' : 45, 'lat0' : 35., 'lon0' : 250., 'dlat' : 0.5, 'dlon' : 0.5, 'xlat1' : 0., 'xlon1' : 180., 'xlat2' : 1., 'xlon2' : 270. } g = rmn.encodeGrid(gp) print("CB24: Defined a %s/%s grid of shape=%d, %d" % (gp['grtyp'], gp['grref'], gp['ni'], gp['nj'])) # Create Record vgrid lvls = (500.,850.,1000.) v = vgd.vgd_new_pres(lvls) ip1list = vgd.vgd_get(v, 'VIPT') print("CB24: Defined a Pres vgrid with lvls=%s" % str(lvls)) # Create Record data + meta 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() # Copy default record meta r.update(g) # Update with grid info r.update({ # Update with specific meta and data array 'nomvar': 'MASK', 'dateo' : rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20160302, 1800000), 'nk' : len(ip1list), 'ip1' : 0, # level, will be set later, level by level 'ip2' : 6, # Forecast of 6h 'deet' : 3600, # Timestep in sec 'npas' : 6, # Step number 'etiket': 'my_etk', 'nbits' : 32, # Keep full 32 bits precision for that field 'datyp' : datyp, # datyp (above) float_IEEE_compressed 'd' : np.empty(rshape, dtype=npdtype, order='FORTRAN') }) print("CB24: Defined a new record of shape=%d, %d" % (r['ni'], r['nj'])) # Compute record values r['d'][:,:,:] = 0. r['d'][10:-11,5:-6,:] = 1. # Open Files fileNameOut = 'newfromscratch.fst' try: fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) except: sys.stderr.write("Problem opening the file: %s, %s\n" % fileNameOut) sys.exit(1) # Write record data + meta + grid + vgrid to file try: r2d = r.copy() r2d['nk'] = 1 for k in range(len(ip1list)): r2d['ip1'] = ip1list[k] r2d['d'] = np.asfortranarray(r['d'][:,:,k]) rmn.fstecr(fileIdOut, r2d['d'], r2d) print("CB24: wrote %s at ip1=%d" % (r2d['nomvar'], r2d['ip1'])) rmn.writeGrid(fileIdOut, g) print("CB24: wrote the grid descriptors") vgd.vgd_write(v, fileIdOut) print("CB24: wrote the vgrid descriptor") except: raise finally: # Properly close files even if an error occured above # This is important when editing to avoid corrupted files rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file
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 test_24(self): """ Edit: New file from scratch This example shows how to * Create a record meta and data from scratch * Create grid descriptors for the data * Create vgrid descriptor for the data * write the recod data + meta * write the grid descriptors * write the vgrid descriptor See also: rpnpy.librmn.fstd98.fstopt rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.fsrecr rpnpy.librmn.fstd98.dtype_fst2numpy rpnpy.librmn.grids.encodeGrid rpnpy.librmn.grids.defGrid_ZE rpnpy.librmn.grids.writeGrid rpnpy.librmn.base.newdate rpnpy.vgd.base.vgd_new rpnpy.vgd.base.vgd_new_pres rpnpy.vgd.base.vgd_new_get rpnpy.vgd.base.vgd_write rpnpy.librmn.const rpnpy.vgd.const """ import os, sys 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) # Create Record grid gp = { 'grtyp': 'Z', 'grref': 'E', 'ni': 90, 'nj': 45, 'lat0': 35., 'lon0': 250., 'dlat': 0.5, 'dlon': 0.5, 'xlat1': 0., 'xlon1': 180., 'xlat2': 1., 'xlon2': 270. } g = rmn.encodeGrid(gp) print("CB24: Defined a %s/%s grid of shape=%d, %d" % (gp['grtyp'], gp['grref'], gp['ni'], gp['nj'])) # Create Record vgrid lvls = (500., 850., 1000.) v = vgd.vgd_new_pres(lvls) ip1list = vgd.vgd_get(v, 'VIPT') print("CB24: Defined a Pres vgrid with lvls=%s" % str(lvls)) # Create Record data + meta 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() # Copy default record meta r.update(g) # Update with grid info r.update({ # Update with specific meta and data array 'nomvar': 'MASK', 'dateo': rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20160302, 1800000), 'nk': len(ip1list), 'ip1': 0, # level, will be set later, level by level 'ip2': 6, # Forecast of 6h 'deet': 3600, # Timestep in sec 'npas': 6, # Step number 'etiket': 'my_etk', 'nbits': 32, # Keep full 32 bits precision for that field 'datyp': datyp, # datyp (above) float_IEEE_compressed 'd': np.empty(rshape, dtype=npdtype, order='FORTRAN') }) print("CB24: Defined a new record of shape=%d, %d" % (r['ni'], r['nj'])) # Compute record values r['d'][:, :, :] = 0. r['d'][10:-11, 5:-6, :] = 1. # Open Files fileNameOut = 'newfromscratch.fst' try: fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW) except: sys.stderr.write("Problem opening the file: %s, %s\n" % fileNameOut) sys.exit(1) # Write record data + meta + grid + vgrid to file try: r2d = r.copy() r2d['nk'] = 1 for k in range(len(ip1list)): r2d['ip1'] = ip1list[k] r2d['d'] = np.asfortranarray(r['d'][:, :, k]) rmn.fstecr(fileIdOut, r2d['d'], r2d) print("CB24: wrote %s at ip1=%d" % (r2d['nomvar'], r2d['ip1'])) rmn.writeGrid(fileIdOut, g) print("CB24: wrote the grid descriptors") vgd.vgd_write(v, fileIdOut) print("CB24: wrote the vgrid descriptor") except: raise finally: # Properly close files even if an error occured above # This is important when editing to avoid corrupted files rmn.fstcloseall(fileIdOut) os.unlink(fileNameOut) # Remove test file
zon_list[-1] = monthly_mean_data(zon_list[:-1]) # BOOKMARK: FSTFILE # #exit() ## this portion of the code simply opens a new file and ports PV onto it print "Total time taken: {0}".format(str(time.time() - start_time)) l += 1 for t, array in enumerate(pv_list): try: # copies the default record new_record = rmn.FST_RDE_META_DEFAULT.copy() if t < 124: time_int = build_hhmmss(t) new_record.update({ 'dateo': rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20120100 + int(math.floor(t / 4 + 1)), time_int) }) for rp1 in xrange( len(const_pressure) ): # writes a record for every level (as a different ip1) # 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': 'PV', 'typvar': 'C', 'etiket': 'MACCRean', 'ip2': t, 'ni': MACC_grid['ni'],