def testNewPres(self): # pres = [x*self.MB2PA for x in (500.,850.,1000.)] pres = (500.,850.,1000.) vgd0ptr = vgd.vgd_new_pres(pres) vkind = vgd.vgd_get(vgd0ptr, 'KIND') vvers = vgd.vgd_get(vgd0ptr, 'VERS') self.assertEqual((vkind,vvers),vgd.VGD_KIND_VER['pres'])
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 testNewReadGetFloat(self): vgd0ptr = self._newReadBcmk() v1 = vgd.vgd_get(vgd0ptr, 'RC_1') self.assertEqual(int(v1*100),160) v2 = vgd.vgd_get(vgd0ptr, 'RC_2') self.assertEqual(int(v2),vgd.VGD_MISSING)
def testNewReadGetDouble(self): vgd0ptr = self._newReadBcmk() v1 = vgd.vgd_get(vgd0ptr, 'PREF') self.assertEqual(int(v1*100.),8000000) v2 = vgd.vgd_get(vgd0ptr, 'PTOP') self.assertEqual(int(v2*100.),1000)
def testNewHybM(self): rcoef1 = 0. rcoef2 = 1. pref = 1000. * self.MB2PA ptop = -1. # -2. vgd0ptr = vgd.vgd_new_hybm(self.hyblist, rcoef1, rcoef2, ptop, pref) vkind = vgd.vgd_get(vgd0ptr, 'KIND') vvers = vgd.vgd_get(vgd0ptr, 'VERS') self.assertEqual((vkind,vvers),vgd.VGD_KIND_VER['hybm'])
def testNewSigm(self): sigma = (0.011000, 0.027000, 0.051000, 0.075000, 0.101000, 0.127000, 0.155000, 0.185000, 0.219000, 0.258000, 0.302000, 0.351000, 0.405000, 0.460000, 0.516000, 0.574000, 0.631000, 0.688000, 0.744000, 0.796000, 0.842000, 0.884000, 0.922000, 0.955000, 0.980000, 0.993000, 1.000000) vgd0ptr = vgd.vgd_new_sigm(sigma) vkind = vgd.vgd_get(vgd0ptr, 'KIND') vvers = vgd.vgd_get(vgd0ptr, 'VERS') self.assertEqual((vkind,vvers),vgd.VGD_KIND_VER['sigm'])
def testNewHybH(self): rcoef1 = 1. rcoef2 = 5. dhm = 10. dht = 1.5 vgd0ptr = vgd.vgd_new_hybh(self.hgtlist, rcoef1, rcoef2, dhm, dht) vkind = vgd.vgd_get(vgd0ptr, 'KIND') vvers = vgd.vgd_get(vgd0ptr, 'VERS') self.assertEqual((vkind,vvers),vgd.VGD_KIND_VER['hybh'])
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 testNewHybMD(self): rcoef1 = 0. rcoef2 = 10. pref = 1000. * self.MB2PA dhm = 10. dht = 2. vgd0ptr = vgd.vgd_new_hybmd(self.hyblist, rcoef1, rcoef2, pref, dhm, dht) vkind = vgd.vgd_get(vgd0ptr, 'KIND') vvers = vgd.vgd_get(vgd0ptr, 'VERS') self.assertEqual((vkind,vvers),vgd.VGD_KIND_VER['hybmd'])
def testNewEta(self): hyb = (0.000, 0.011, 0.027, 0.051, 0.075, 0.101, 0.127, 0.155, 0.185, 0.219, 0.258, 0.302, 0.351, 0.405, 0.460, 0.516, 0.574, 0.631, 0.688, 0.744, 0.796, 0.842, 0.884, 0.922, 0.955, 0.980, 0.993, 1.000) ptop = 10. * self.MB2PA vgd0ptr = vgd.vgd_new_eta(hyb, ptop) vkind = vgd.vgd_get(vgd0ptr, 'KIND') vvers = vgd.vgd_get(vgd0ptr, 'VERS') self.assertEqual((vkind,vvers),vgd.VGD_KIND_VER['eta'])
def testNewHybPs(self): rcoef1 = 0. rcoef2 = 5. rcoef3 = 0. rcoef4 = 100. pref = 100000. dhm = 10. dht = 1.5 vgd0ptr = vgd.vgd_new_hybps(self.hyblist, rcoef1, rcoef2, rcoef3, rcoef4, pref, dhm, dht) vkind = vgd.vgd_get(vgd0ptr, 'KIND') vvers = vgd.vgd_get(vgd0ptr, 'VERS') self.assertEqual((vkind,vvers), vgd.VGD_KIND_VER['hybps'])
def testNewHybHls(self): rcoef1 = 0. rcoef2 = 5. rcoef3 = 0. rcoef4 = 100. dhm = 10. dht = 1.5 dhw = 10. vgd0ptr = vgd.vgd_new_hybhls(self.hgtlist, rcoef1, rcoef2, rcoef3, rcoef4, dhm, dht, dhw) vkind = vgd.vgd_get(vgd0ptr, 'KIND') vvers = vgd.vgd_get(vgd0ptr, 'VERS') self.assertEqual((vkind,vvers), vgd.VGD_KIND_VER['hybhls'])
def testNewHyb(self): hyb =(0.0125000,0.0233625,0.0391625,0.0628625,0.0865625, 0.1122375,0.1379125,0.1655625,0.1951875,0.2287625, 0.2672750,0.3107250,0.3591125,0.4124375,0.4667500, 0.5220500,0.5793250,0.6356125,0.6919000,0.7472000, 0.7985500,0.8439750,0.8854500,0.9229750,0.9555625, 0.9802499,0.9930875,1.0000000) rcoef1 = 1.6 ptop = 10. * self.MB2PA pref = 800. * self.MB2PA vgd0ptr = vgd.vgd_new_hyb(hyb, rcoef1, ptop, pref) vkind = vgd.vgd_get(vgd0ptr, 'KIND') vvers = vgd.vgd_get(vgd0ptr, 'VERS') self.assertEqual((vkind,vvers),vgd.VGD_KIND_VER['hyb'])
def get_levels_keys(fileId, nomvar, datev=-1, ip2=-1, ip3=-1, typvar=' ', etiket=' ', vGrid=None, thermoMom='VIPT', verbose=False): """ """ #TODO: try to get the sorted ip1 list w/o vgrid, because vgrid doesn;t support 2 different vertical coor in the same file (or list of linked files) # Get the vgrid definition present in the file if vGrid is None: if verbose: print("Getting vertical grid description") _vgd.vgd_put_opt('ALLOW_SIGMA', _vgd.VGD_ALLOW_SIGMA) vGrid = _vgd.vgd_read(fileId) vip = _vgd.vgd_get(vGrid, thermoMom) if verbose: vkind = _vgd.vgd_get(vGrid, 'KIND') vver = _vgd.vgd_get(vGrid, 'VERS') vtype = _vgd.VGD_KIND_VER_INV[(vkind,vver)] print("Found %d %s levels of type %s" % (len(vip), thermoMom, vtype)) # Trim the list of ip1 to actual levels in files for nomvar # since the vgrid in the file is a super set of all levels # and get their "key" vipkeys = [] for ip1 in vip: (lval, lkind) = _rmn.convertIp(_rmn.CONVIP_DECODE, ip1) key = _rmn.fstinf(fileId, nomvar=nomvar, datev=datev, ip2=ip2, ip3=ip3, ip1=_rmn.ip1_all(lval, lkind), typvar=typvar, etiket=etiket) if key is not None: vipkeys.append((ip1, key['key'])) if datev == -1 or ip2 == -1 or ip3 == -1 or typvar.strip() == '' or etiket.strip() == '': meta = _rmn.fstprm(key) datev = meta['datev'] ip2 = meta['ip2'] ip3 = meta['ip3'] typvar = meta['typvar'] etiket = meta['etiket'] return { 'nomvar' : nomvar, 'datev' : datev, 'ip2' : ip2, 'ip3' : ip3, 'typvar' : typvar, 'etiket' : etiket, 'v' : vGrid, 'ip1keys': vipkeys }
def testNewReadGetInt(self): vgd0ptr = self._newReadBcmk() vkind = vgd.vgd_get(vgd0ptr, 'KIND') self.assertEqual(vkind,vgd.VGD_HYB_KIND) vvers = vgd.vgd_get(vgd0ptr, 'VERS') self.assertEqual(vvers,vgd.VGD_HYB_VER) try: scrap = vgd.vgd_get(vgd0ptr, 'SCRAP') except KeyError: pass except: self.assertEqual(0,1,'vgd_get of Unknown key should raise a KeyError')
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 testNewReadPutChar(self): vgd0ptr = self._newReadBcmk() v1 = 'PRES' vgd.vgd_put(vgd0ptr, 'RFLD', v1) v2 = vgd.vgd_get(vgd0ptr, 'RFLD') self.assertEqual(v2.strip(),v1)
def testNewReadPutInt(self): vgd0ptr = self._newReadBcmk() v1 = 6 vgd.vgd_put(vgd0ptr, 'IG_1', v1) v2 = vgd.vgd_get(vgd0ptr, 'IG_1') self.assertEqual(v1,v2)
def testNewReadPutDouble(self): vgd0ptr = self._newReadBcmk() v1 = 70000. vgd.vgd_put(vgd0ptr, 'PREF', v1) v2 = vgd.vgd_get(vgd0ptr, 'PREF') self.assertEqual(int(v2*100.),int(v1*100.))
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 testNewReadPutChar(self): vgd0ptr = self._newReadBcmk() v1 = 'PRES' vgd.vgd_put(vgd0ptr, 'ETIK', v1) v2 = vgd.vgd_get(vgd0ptr, 'ETIK') self.assertEqual(v2.strip(),v1)
def testNewReadGetDouble1D(self): vgd0ptr = self._newReadBcmk() v1 = vgd.vgd_get(vgd0ptr, 'CA_M') self.assertEqual(len(v1),158) self.assertEqual(int(v1[0]*100.),1000) self.assertEqual(int(v1[1]*100.),1383) self.assertEqual(int(v1[2]*100.),1765)
def testFree(self): vgd0ptr = self._newReadBcmk() vgd.vgd_free(vgd0ptr) try: v1 = vgd.vgd_get(vgd0ptr, 'RFLD') except vgd.VGDError: pass except: self.assertEqual(0,1,'vgd_get of freed vgd should raise a VGDError')
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_14bqd(self): import os, sys, datetime import numpy as np import rpnpy.librmn.all as rmn import rpnpy.vgd.all as vgd rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) fdate = datetime.date.today().strftime('%Y%m%d') + '00_048' CMCGRIDF = os.getenv('CMCGRIDF').strip() fileId = rmn.fstopenall(CMCGRIDF + '/prog/regpres/' + fdate, rmn.FST_RO) v = vgd.vgd_read(fileId) (tlvlkeys, rshape) = ([], None) for ip1 in vgd.vgd_get(v, 'VIPT'): (lval, lkind) = rmn.convertIp(rmn.CONVIP_DECODE, ip1) key = rmn.fstinf(fileId, nomvar='TT', ip2=48, ip1=rmn.ip1_all(lval, lkind)) if key is not None: tlvlkeys.append((ip1, key['key'])) if rshape is None and key is not None: rshape = key['shape'] (r2d, r3d, k, rshape) = ({ 'd': None }, None, 0, (rshape[0], rshape[1], len(tlvlkeys))) for ip1, key in tlvlkeys: r2d = rmn.fstluk(key, dataArray=r2d['d']) if r3d is None: r3d = r2d.copy() r3d['d'] = np.empty(rshape, dtype=r2d['d'].dtype, order='FORTRAN') r3d['d'][:, :, k] = r2d['d'][:, :] rmn.fstcloseall(fileId) r3d.update({ 'vgd': v, 'ip1list': [x[0] for x in tlvlkeys], 'shape': rshape, 'nk': rshape[2] }) (i1, j1) = (rshape[0] // 2, rshape[1] // 2) print("CB14bqd: The TT profile at point (%d, %d) is:" % (i1, j1)) for k in range(rshape[2]): (ldiagval, ldiagkind) = rmn.convertIp(rmn.CONVIP_DECODE, r3d['ip1list'][k]) print( "CB14bqd: TT(%d, %d, %7.2f %s) = %6.1f C [mean=%6.1f, std=%6.1f, min=%6.1f, max=%6.1f]" % (i1, j1, ldiagval, rmn.kindToString(ldiagkind), r3d['d'][i1, j1, k], r3d['d'][:, :, k].mean(), r3d['d'][:, :, k].std(), r3d['d'][:, :, k].min(), r3d['d'][:, :, k].max()))
def get_levels_press(fileId, vGrid, shape, ip1list, datev=-1, ip2=-1, ip3=-1, typvar=' ', etiket=' ', verbose=False): """ """ rfldName = _vgd.vgd_get(vGrid, 'RFLD') rfld = _np.empty(shape, dtype=_np.float32, order='F') rfld[:] = 1000. * _cst.MB2PA if rfldName: r2d = _rmn.fstlir(fileId, nomvar=rfldName, datev=datev, ip2=ip2, ip3=ip3, typvar=typvar, etiket=etiket) if r2d is None: r2d = _rmn.fstlir(fileId, nomvar=rfldName, datev=datev, ip2=ip2, typvar=typvar, etiket=etiket) if r2d is None: r2d = _rmn.fstlir(fileId, nomvar=rfldName, datev=datev, ip2=ip2, etiket=etiket) if r2d is None: r2d = _rmn.fstlir(fileId, nomvar=rfldName, datev=datev, ip2=ip2) if r2d is None: r2d = _rmn.fstlir(fileId, nomvar=rfldName, datev=datev) if r2d is None: r2d = _rmn.fstlir(fileId, nomvar=rfldName) if not r2d is None: if verbose: print("Read {nomvar} ip1={ip1} ip2={ip2} ip3={ip3} typv={typvar} etk={etiket}".format(**r2d)) ## g = _rmn.readGrid(fileId, r2d) ## if len(xpts) > 0: ## v1 = _rmn.gdxysval(g['id'], xpts, ypts, r2d['d']) ## rfld[0:len(xy)] = v1[:] ## if len(lats) > 0: ## v1 = _rmn.gdllsval(g['id'], lats, lons, r2d['d']) ## rfld[len(xy):len(xy)+len(ll)] = v1[:] rfld[:,:] = r2d['d'][:,:] * _cst.MB2PA phPa = _vgd.vgd_levels(vGrid, rfld, ip1list) phPa[:,:,:] /= _cst.MB2PA return { 'rfld' : rfld, 'phPa' : phPa }
def testNewReadGetChar(self): vgd0ptr = self._newReadBcmk() v1 = vgd.vgd_get(vgd0ptr, 'RFLD') self.assertEqual(v1.strip(),'P0')
def get_levels_keys(fileId, nomvar, datev=-1, ip2=-1, ip3=-1, typvar=' ', etiket=' ', vGrid=None, thermoMom='VIPT', verbose=False): """ Get from file the list of ip1 and fstd-record-key matching provided filters ipkeys = get_levels_keys(fileId, nomvar) Args: fileId : unit number associated to the file obtained with fnom+fstouv nomvar : variable name datev : valid date ip2 : forecast hour ip3 : user defined identifier typvar : type of field etiket : label vGrid : vertical grid descriptor thermoMom : 'VIPT' to get Thermo levels, 'VIPT' for momentum levels verbose : Print some info when true Returns: { 'nomvar' : nomvar, # variable name 'datev' : datev, # valid date 'ip2' : ip2, # forecast hour 'ip3' : ip3, # user defined identifier 'typvar' : typvar, # type of field 'etiket' : etiket, # label 'vgrid' : vGrid, # vertical grid descriptor as returned by vgd_read 'ip1keys': vipkeys # list of ip1 and corresponding FSTD rec key as # ((ip1,key1), (ip1b, key2), ...) } Raises: TypeError on wrong input arg types ValueError on invalid input arg value FSTDError on any other error Examples: >>> import os, os.path >>> import rpnpy.librmn.all as rmn >>> import rpnpy.utils.fstd3d as fstd3d >>> ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() >>> filename = os.path.join(ATM_MODEL_DFILES,'bcmk') >>> >>> # Open existing file in Rear Only mode >>> fileId = rmn.fstopenall(filename, rmn.FST_RO) >>> >>> # Find ip1, key for all TT in file >>> ipkeys = fstd3d.get_levels_keys(fileId, 'TT', thermoMom='VIPT', verbose=True) Getting vertical grid description Found 158 VIPT levels of type hyb >>> print('# Found {} levels for TT'.format(len(ipkeys['ip1keys']))) # Found 80 levels for TT >>> rmn.fstcloseall(fileId) See Also: get_levels_press fst_read_3d rpnpy.librmn.fstd98.fstinf rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.vgd.base.vgd_read """ #TODO: try to get the sorted ip1 list w/o vgrid, because vgrid doesn;t support 2 different vertical coor in the same file (or list of linked files) # Get the vgrid definition present in the file if vGrid is None: if verbose: print("Getting vertical grid description") _vgd.vgd_put_opt('ALLOW_SIGMA', _vgd.VGD_ALLOW_SIGMA) vGrid = _vgd.vgd_read(fileId) vip = _vgd.vgd_get(vGrid, thermoMom) if verbose: vkind = _vgd.vgd_get(vGrid, 'KIND') vver = _vgd.vgd_get(vGrid, 'VERS') vtype = _vgd.VGD_KIND_VER_INV[(vkind, vver)] print("Found %d %s levels of type %s" % (len(vip), thermoMom, vtype)) # Trim the list of ip1 to actual levels in files for nomvar # since the vgrid in the file is a super set of all levels # and get their "key" vipkeys = [] for ip1 in vip: (lval, lkind) = _rmn.convertIp(_rmn.CONVIP_DECODE, ip1) key = _rmn.fstinf(fileId, nomvar=nomvar, datev=datev, ip2=ip2, ip3=ip3, ip1=_rmn.ip1_all(lval, lkind), typvar=typvar, etiket=etiket) if key is not None: vipkeys.append((ip1, key['key'])) if (datev == -1 or ip2 == -1 or ip3 == -1 or typvar.strip() == '' or etiket.strip() == ''): meta = _rmn.fstprm(key) datev = meta['datev'] ip2 = meta['ip2'] ip3 = meta['ip3'] typvar = meta['typvar'] etiket = meta['etiket'] return { 'nomvar': nomvar, 'datev': datev, 'ip2': ip2, 'ip3': ip3, 'typvar': typvar, 'etiket': etiket, 'vgrid': vGrid, 'ip1keys': vipkeys }
def get_levels_press(fileId, vGrid, shape, ip1list, datev=-1, ip2=-1, ip3=-1, typvar=' ', etiket=' ', verbose=False): """ Read the reference surface field and computer the pressure cube press = get_levels_press(fileId, vGrid, shape, ip1list) press = get_levels_press(fileId, vGrid, shape, ip1list, datev, ip2, ip3, typvar, etiket) Args: fileId : unit number associated to the file obtained with fnom+fstouv vGrid : vertical grid descriptor shape : shape of the field ip1list : vertical levels ip lists datev : valid date ip2 : forecast hour ip3 : user defined identifier typvar : type of field etiket : label verbose : Print some info when true Returns: { 'rfld' : rfld, # 2d field reference value 'phPa' : phPa # 3d pressure values } Raises: TypeError on wrong input arg types ValueError on invalid input arg value FSTDError on any other error Examples: >>> import os, os.path >>> import rpnpy.librmn.all as rmn >>> import rpnpy.utils.fstd3d as fstd3d >>> ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip() >>> filename = os.path.join(ATM_MODEL_DFILES,'bcmk') >>> >>> # Open existing file in Rear Only mode >>> fileId = rmn.fstopenall(filename, rmn.FST_RO) >>> >>> # Get the pressure cube >>> ipkeys = fstd3d.get_levels_keys(fileId, 'TT', thermoMom='VIPT') >>> ip1list = [ip1 for ip1,key in ipkeys['ip1keys']] >>> shape = rmn.fstinf(fileId, nomvar='TT')['shape'][0:2] >>> press = fstd3d.get_levels_press(fileId, ipkeys['vgrid'], shape, ip1list) >>> print('# {} {} {}'.format(shape, press['rfld'].shape, press['phPa'].shape)) # (200, 100) (200, 100) (200, 100, 80) >>> rmn.fstcloseall(fileId) See Also: get_levels_keys fst_read_3d rpnpy.librmn.fstd98.fstlir rpnpy.librmn.fstd98.fstprm rpnpy.librmn.fstd98.fstluk rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.vgd.base.vgd_levels """ rfldName = _vgd.vgd_get(vGrid, 'RFLD') rfld = _np.empty(shape, dtype=_np.float32, order='F') rfld[:] = 1000. * _cst.MB2PA if rfldName: r2d = _rmn.fstlir(fileId, nomvar=rfldName, datev=datev, ip2=ip2, ip3=ip3, typvar=typvar, etiket=etiket) if r2d is None: r2d = _rmn.fstlir(fileId, nomvar=rfldName, datev=datev, ip2=ip2, typvar=typvar, etiket=etiket) if r2d is None: r2d = _rmn.fstlir(fileId, nomvar=rfldName, datev=datev, ip2=ip2, etiket=etiket) if r2d is None: r2d = _rmn.fstlir(fileId, nomvar=rfldName, datev=datev, ip2=ip2) if r2d is None: r2d = _rmn.fstlir(fileId, nomvar=rfldName, datev=datev) if r2d is None: r2d = _rmn.fstlir(fileId, nomvar=rfldName) if not r2d is None: if verbose: print( "Read {nomvar} ip1={ip1} ip2={ip2} ip3={ip3} typv={typvar} etk={etiket}" .format(**r2d)) ## g = _rmn.readGrid(fileId, r2d) ## if len(xpts) > 0: ## v1 = _rmn.gdxysval(g['id'], xpts, ypts, r2d['d']) ## rfld[0:len(xy)] = v1[:] ## if len(lats) > 0: ## v1 = _rmn.gdllsval(g['id'], lats, lons, r2d['d']) ## rfld[len(xy):len(xy)+len(ll)] = v1[:] rfld[:, :] = r2d['d'][:, :] * _cst.MB2PA phPa = _vgd.vgd_levels(vGrid, rfld, ip1list) phPa[:, :, :] /= _cst.MB2PA return {'rfld': rfld, 'phPa': phPa}
for m_int, fst in enumerate(fsts): if m_int >= 1: break if m_int % 2 == 1: continue # extraction of grid data from fst files try: file_id = rmn.fstopenall(fst) for param in params.keys(): if param in special_params.keys(): var_name = special_params[param] else: var_name = param.upper() grid_data = np.zeros((lev, ni, nj)) v_grid = vgd.vgd_read(file_id) # gets the vgd object ip1s = vgd.vgd_get(v_grid, 'VIPM') # builds pressure levels pres_levs = [ rmn.convertIp(rmn.CONVIP_DECODE, ip1, rmn.KIND_PRESSURE)[0] for ip1 in ip1s ] for k, ip in enumerate(ip1s): rec_key = rmn.fstinf( file_id, ip1=ip, nomvar=var_name) # returns key based on params grid_data[k] = rmn.fstluk(rec_key)['d'] # reads grid data conv_param(param, grid_data) latkey = rmn.fstinf(file_id, nomvar='^^')
def test_14b(self): """ Queries: Get Vertical Grid info, Read 3D Field This example shows how to * get the vertical grid definition. * use it to read a 3D field (records on all levels) * then print a profile for this var See also: rpnpy.librmn.fstd98.fstopt rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.fstlinf rpnpy.librmn.fstd98.fstlluk rpnpy.librmn.fstd98.convertIp rpnpy.librmn.fstd98.kindToString rpnpy.vgd.base.vgd_read rpnpy.vgd.base.vgd_get rpnpy.librmn.const rpnpy.vgd.const """ import os, sys, datetime import numpy as np import rpnpy.librmn.all as rmn import rpnpy.vgd.all as vgd # Restrict to the minimum the number of messages printed by librmn rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) # Open file fdate = datetime.date.today().strftime('%Y%m%d') + '00_048' CMCGRIDF = os.getenv('CMCGRIDF').strip() fileName = os.path.join(CMCGRIDF, 'prog', '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 the list of ip1 on thermo levels in this file tlvl = vgd.vgd_get(v, 'VIPT') # Trim the list of thermo ip1 to actual levels in files for TT # since the vgrid in the file is a super set of all levels # and get their "key" tlvlkeys = [] rshape = None for ip1 in tlvl: (lval, lkind) = rmn.convertIp(rmn.CONVIP_DECODE, ip1) key = rmn.fstinf(fileId, nomvar='TT', ip2=48, ip1=rmn.ip1_all(lval, lkind)) if key is not None: tlvlkeys.append((ip1, key['key'])) if rshape is None: rshape = key['shape'] rshape = (rshape[0], rshape[1], len(tlvlkeys)) # Read every level for TT at ip2=48, re-use 2d array while reading # and store the data in a 3d array # with lower level at nk, top at 0 as in the model # Note that for efficiency reasons, if only a profile was needed, # only that profile would be saved instead of the whole 3d field r2d = {'d': None} r3d = None k = 0 for ip1, key in tlvlkeys: try: r2d = rmn.fstluk(key, dataArray=r2d['d']) if r3d is None: r3d = r2d.copy() r3d['d'] = np.empty(rshape, dtype=r2d['d'].dtype, order='FORTRAN') r3d['d'][:, :, k] = r2d['d'][:, :] k += 1 except: pass except: pass finally: # Close file even if an error occured above rmn.fstcloseall(fileId) # Add the vgrid and the actual ip1 list in the r3d dict, update shape and nk r3d['vgd'] = v r3d['ip1list'] = [x[0] for x in tlvlkeys] r3d['shape'] = rshape r3d['nk'] = rshape[2] # Print a profile of TT and stats by level (i1, j1) = (rshape[0] // 2, rshape[1] // 2) print("CB14b: The TT profile at point (%d, %d) is:" % (i1, j1)) for k in range(rshape[2]): ip1 = r3d['ip1list'][k] (ldiagval, ldiagkind) = rmn.convertIp(rmn.CONVIP_DECODE, ip1) print( "CB14b: TT(%d, %d, %7.2f %s) = %6.1f C [mean=%6.1f, std=%6.1f, min=%6.1f, max=%6.1f]" % (i1, j1, ldiagval, rmn.kindToString(ldiagkind), r3d['d'][i1, j1, k], r3d['d'][:, :, k].mean(), r3d['d'][:, :, k].std(), r3d['d'][:, :, k].min(), r3d['d'][:, :, k].max()))
def test_51(self): """ Vertical Interpolation See also: scipy.interpolate.interp1d """ import os, sys, datetime import numpy as np from scipy.interpolate import interp1d as scipy_interp1d import rpnpy.librmn.all as rmn import rpnpy.vgd.all as vgd MB2PA = 100. # Restrict to the minimum the number of messages printed by librmn rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) # Open Input file hour = 48 fdate = datetime.date.today().strftime('%Y%m%d') + '00_0' + str(hour) CMCGRIDF = os.getenv('CMCGRIDF').strip() fileNameOut = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate) try: fileIdIn = rmn.fstopenall(fileNameOut, rmn.FST_RO) except: sys.stderr.write("Problem opening the input file: %s\n" % fileNameOut) sys.exit(1) try: # Get the vgrid def present in the file # and the full list of ip1 # and the surface reference field name for the coor vIn = vgd.vgd_read(fileIdIn) ip1listIn0 = vgd.vgd_get(vIn, 'VIPT') rfldNameIn = vgd.vgd_get(vIn, 'RFLD') vkind = vgd.vgd_get(vIn, 'KIND') vver = vgd.vgd_get(vIn, 'VERS') VGD_KIND_VER_INV = dict( (v, k) for k, v in vgd.VGD_KIND_VER.items()) vtype = VGD_KIND_VER_INV[(vkind, vver)] print( "CB51: Found vgrid type=%s (kind=%d, vers=%d) with %d levels, RFLD=%s" % (vtype, vkind, vver, len(ip1listIn0), rfldNameIn)) # Trim the list of thermo ip1 to actual levels in files for TT # since the vgrid in the file is a super set of all levels # and get their "key" ip1Keys = [] rshape = None for ip1 in ip1listIn0: (lval, lkind) = rmn.convertIp(rmn.CONVIP_DECODE, ip1) key = rmn.fstinf(fileIdIn, nomvar='TT', ip2=hour, ip1=rmn.ip1_all(lval, lkind)) if key is not None: print("CB51: Found TT at ip1=%d, ip2=%d" % (ip1, hour)) ip1Keys.append((ip1, key['key'])) if rshape is None: rshape = key['shape'] rshape = (rshape[0], rshape[1], len(ip1Keys)) # Read every level for TT at ip2=hour, re-use 2d array while reading # and store the data in a 3d array # with lower level at nk, top at 0 as in the model r2d = {'d': None} r3d = None k = 0 gIn = None for ip1, key in ip1Keys: try: r2d = rmn.fstluk(key, dataArray=r2d['d']) print("CB51: Read TT at ip1=%d, ip2=%d" % (ip1, hour)) if r3d is None: r3d = r2d.copy() r3d['d'] = np.empty(rshape, dtype=r2d['d'].dtype, order='FORTRAN') r3d['d'][:, :, k] = r2d['d'][:, :] k += 1 if gIn is None: gIn = rmn.readGrid(fileIdIn, r2d) print("CB51: Read the horizontal grid descriptors") except: pass # Add the vgrid and the actual ip1 list in the r3d dict, update shape and nk r3d['vgd'] = vIn r3d['ip1list'] = [x[0] for x in ip1Keys] r3d['shape'] = rshape r3d['nk'] = rshape[2] # Read the Input reference fields rfldIn = None if rfldNameIn: rfldIn = rmn.fstlir(fileIdIn, nomvar=rfldNameIn, ip2=hour) if rfldNameIn.strip() == 'P0': rfldIn['d'][:] *= MB2PA print( "CB51: Read input RFLD=%s at ip2=%d [min=%7.0f, max=%7.0f]" % (rfldNameIn, hour, rfldIn['d'].min(), rfldIn['d'].max())) except: raise # pass finally: # Close file even if an error occured above rmn.fstcloseall(fileIdIn) # Define the destination vertical grid/levels try: lvlsOut = (500., 850., 1000.) vOut = vgd.vgd_new_pres(lvlsOut) ip1listOut = vgd.vgd_get(vOut, 'VIPT') rfldNameOut = vgd.vgd_get(vIn, 'RFLD') rfldOut = None # in this case, Pressure levels, there are no RFLD print("CB51: Defined a Pres vgrid with lvls=%s" % str(lvlsOut)) except: sys.stderr.write("Problem creating a new vgrid\n") sys.exit(1) # Get input and output 3d pressure cubes try: ## if rfldIn is None: ## rfldIn = pIn = vgd.vgd_levels(vIn, ip1list=r3d['ip1list'], rfld=rfldIn['d']) print( "CB51: Computed input pressure cube, k0:[min=%7.0f, max=%7.0f], nk:[min=%7.0f, max=%7.0f]" % (pIn[:, :, 0].min(), pIn[:, :, 0].max(), pIn[:, :, -1].min(), pIn[:, :, -1].max())) if rfldOut is None: rfldOut = rfldIn # provide a dummy rfld for array shape pOut = vgd.vgd_levels(vOut, ip1list=ip1listOut, rfld=rfldOut['d']) print( "CB51: Computed output pressure cube, k0:[min=%7.0f, max=%7.0f], nk:[min=%7.0f, max=%7.0f]" % (pOut[:, :, 0].min(), pOut[:, :, 0].max(), pOut[:, :, -1].min(), pOut[:, :, -1].max())) except: raise sys.stderr.write("Problem computing pressure cubes\n") sys.exit(1) # Use scipy.interpolate.interp1d to vertically interpolate try: ## f = scipy_interp1d(fromLvls, toLvls, kind='cubic', ## assume_sorted=True, bounds_error=False, ## fill_value='extrapolate', copy=False) ## # Unfortunately, looks like interp1d take colomn data ## f = scipy_interp1d(pIn, r3d['d'], kind='cubic', ## bounds_error=False, ## fill_value='extrapolate', copy=False) ## r3dout = f(pOut) ## # Unfortunately, assume_sorted, 'extrapolate' not support in my version ## extrap_value = 'extrapolate' # -99999. ## # Way too slow, needs a C implementation extrap_value = -999. ## for j in range(rshape[1]): ## for i in range(rshape[0]): ## f = scipy_interp1d(pIn[i,j,:], r3d['d'][i,j,:], ## kind='cubic', ## bounds_error=False, ## fill_value=extrap_value, copy=False) ## r1d = f(pOut[i,j,:]) ## #print i,j,r1d except: raise sys.stderr.write("Problem Interpolating data\n") sys.exit(1)
def testNewReadGetInt1D(self): vgd0ptr = self._newReadBcmk() v1 = vgd.vgd_get(vgd0ptr, 'VIPM') self.assertEqual(len(v1),158) self.assertEqual(v1[0:3],[97642568, 97690568, 97738568])
def testNewReadGetFloat1D(self): vgd0ptr = self._newReadBcmk() v1 = vgd.vgd_get(vgd0ptr, 'VCDM') self.assertEqual(len(v1),158) self.assertEqual([int(x*10000000) for x in v1[0:3]],[1250, 1729, 2209])
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 testNewReadGetDouble3D(self): vgd0ptr = self._newReadBcmk() v1 = vgd.vgd_get(vgd0ptr, 'VTBL') self.assertEqual([int(x*100.) for x in v1[:,0:3,0].T.flatten()], [500, 100, 300, 1000, 8000000, 160, 0, 0, 0])
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