Example #1
0
    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)
Example #2
0
 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])))
Example #3
0
    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']
Example #4
0
 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)
Example #5
0
    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])
Example #6
0
 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
Example #7
0
 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
Example #8
0
    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])
Example #9
0
 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)
Example #10
0
    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)
Example #11
0
    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)
Example #12
0
    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')
Example #13
0
    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)
Example #14
0
    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')
Example #15
0
    def test_13(self):
        """
        Queries: Get Horizontal Grid info

        This example shows how to get the horizontal grid definition (including
        axes and ezscint id) from a previsouly read record

        See also:
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstlir
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.grids.readGrid
        rpnpy.librmn.const
        """
        import os, sys
        import rpnpy.librmn.all as rmn

        # Restrict to the minimum the number of messages printed by librmn
        rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)

        # Open file
        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
        fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk', 'geophy.fst')
        try:
            fileId = rmn.fstopenall(fileName, rmn.FST_RO)
        except:
            sys.stderr.write("Problem opening the file: %s\n" % fileName)
            sys.exit(1)

        try:
            # Get/read the MG record
            r = rmn.fstlir(fileId, nomvar='ME')

            # Get the grid definition of the record
            g = rmn.readGrid(fileId, r)

            print("CB13: %s grtyp/ref=%s/%s, ni/nj=%d,%d, gridID=%d" %
                  (r['nomvar'], g['grtyp'], g['grref'], g['ni'], g['nj'],
                   g['id']))
            print("     lat0/lon0  =%f, %f" % (g['lat0'], g['lon0']))
            print("     ldlat/dlon =%f, %f" % (g['dlat'], g['dlon']))
            print("     xlat`/xlon1=%f, %f; xlat2/xlon2=%f, %f" %
                  (g['xlat1'], g['xlon1'], g['xlat2'], g['xlon2']))
            print("     ax: min=%f, max=%f; ay: min=%f, max=%f" %
                  (g['ax'].min(), g['ax'].max(), g['ay'].min(), g['ay'].max()))
        except:
            pass
        finally:
            # Close file even if an error occured above
            rmn.fstcloseall(fileId)
Example #16
0
    def test_13(self):
        """
        Queries: Get Horizontal Grid info

        This example shows how to get the horizontal grid definition (including
        axes and ezscint id) from a previsouly read record

        See also:
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstlir
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.grids.readGrid
        rpnpy.librmn.const
        """
        import os, sys
        import rpnpy.librmn.all as rmn

        # Restrict to the minimum the number of messages printed by librmn
        rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)

        # Open file
        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
        fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk','geophy.fst')
        try:
            fileId = rmn.fstopenall(fileName, rmn.FST_RO)
        except:
            sys.stderr.write("Problem opening the file: %s\n" % fileName)
            sys.exit(1)

        try:
            # Get/read the MG record
            r = rmn.fstlir(fileId, nomvar='ME')

            # Get the grid definition of the record
            g = rmn.readGrid(fileId, r)

            print("CB13: %s grtyp/ref=%s/%s, ni/nj=%d,%d, gridID=%d" %
                  (r['nomvar'], g['grtyp'], g['grref'], g['ni'], g['nj'], g['id']))
            print("     lat0/lon0  =%f, %f" % (g['lat0'], g['lon0']))
            print("     ldlat/dlon =%f, %f" % (g['dlat'], g['dlon']))
            print("     xlat`/xlon1=%f, %f; xlat2/xlon2=%f, %f" %
                  (g['xlat1'], g['xlon1'], g['xlat2'], g['xlon2']))
            print("     ax: min=%f, max=%f; ay: min=%f, max=%f" %
                  (g['ax'].min(), g['ax'].max(), g['ay'].min(), g['ay'].max()))
        except:
            pass
        finally:
            # Close file even if an error occured above
            rmn.fstcloseall(fileId)
Example #17
0
    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])))
Example #18
0
 def test_13qd(self):
     import os, sys
     import rpnpy.librmn.all as rmn
     rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)
     ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
     fileId = rmn.fstopenall(ATM_MODEL_DFILES+'/bcmk/geophy.fst', rmn.FST_RO)
     r = rmn.fstlir(fileId, nomvar='ME')
     g = rmn.readGrid(fileId, r)
     print("CB13qd: %s grtyp/ref=%s/%s, ni/nj=%d,%d, gridID=%d" %
           (r['nomvar'], g['grtyp'], g['grref'], g['ni'], g['nj'], g['id']))
     print("     lat0/lon0  =%f, %f" % (g['lat0'], g['lon0']))
     print("     ldlat/dlon =%f, %f" % (g['dlat'], g['dlon']))
     print("     xlat`/xlon1=%f, %f; xlat2/xlon2=%f, %f" %
           (g['xlat1'], g['xlon1'], g['xlat2'], g['xlon2']))
     print("     ax: min=%f, max=%f; ay: min=%f, max=%f" %
           (g['ax'].min(), g['ax'].max(), g['ay'].min(), g['ay'].max()))
     rmn.fstcloseall(fileId)
Example #19
0
 def test_13qd(self):
     import os, sys
     import rpnpy.librmn.all as rmn
     rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)
     ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
     fileId = rmn.fstopenall(ATM_MODEL_DFILES + '/bcmk/geophy.fst',
                             rmn.FST_RO)
     r = rmn.fstlir(fileId, nomvar='ME')
     g = rmn.readGrid(fileId, r)
     print("CB13qd: %s grtyp/ref=%s/%s, ni/nj=%d,%d, gridID=%d" %
           (r['nomvar'], g['grtyp'], g['grref'], g['ni'], g['nj'], g['id']))
     print("     lat0/lon0  =%f, %f" % (g['lat0'], g['lon0']))
     print("     ldlat/dlon =%f, %f" % (g['dlat'], g['dlon']))
     print("     xlat`/xlon1=%f, %f; xlat2/xlon2=%f, %f" %
           (g['xlat1'], g['xlon1'], g['xlat2'], g['xlon2']))
     print("     ax: min=%f, max=%f; ay: min=%f, max=%f" %
           (g['ax'].min(), g['ax'].max(), g['ay'].min(), g['ay'].max()))
     rmn.fstcloseall(fileId)
Example #20
0
 def test_readGrid(self):
     ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES')
     myfile = os.path.join(ATM_MODEL_DFILES.strip(),'bcmk/2009042700_000')
     funit = rmn.fstopenall(myfile)
     rec   = rmn.fstlir(funit, nomvar='P0')
     grid  = rmn.readGrid(funit,rec)
     self.assertEqual(grid['grtyp'],'G')
     self.assertEqual(grid['ig1'],0)
     self.assertEqual(grid['ig2'],0)
     self.assertEqual(grid['ig3'],0)
     self.assertEqual(grid['ig4'],0)
     self.assertEqual(grid['xg1'],0.)
     self.assertEqual(grid['xg2'],0.)
     self.assertEqual(grid['xg3'],0.)
     self.assertEqual(grid['xg4'],0.)
     self.assertEqual(grid['ni'],200)
     self.assertEqual(grid['nj'],100)
     self.assertEqual(grid['shape'],(200,100))
     self.assertEqual(grid['glb'],True)
     self.assertEqual(grid['north'],True)
     self.assertEqual(grid['inverted'],False)
Example #21
0
 def test_41qd(self):
     import os, sys, datetime
     import rpnpy.librmn.all as rmn
     fdate       = datetime.date.today().strftime('%Y%m%d') + '00_048'
     CMCGRIDF    = os.getenv('CMCGRIDF').strip()
     fileNameOut = 'p0fstfileqd.fst'
     fileIdIn    = rmn.fstopenall(os.getenv('CMCGRIDF')+'/prog/regeta/'+fdate)
     fileIdOut   = rmn.fstopenall(fileNameOut, rmn.FST_RW)
     gOut = rmn.defGrid_ZE(90, 45, 35., 250., 0.5, 0.5, 0., 180., 1., 270.)
     r    = rmn.fstlir(fileIdIn, nomvar='P0')
     gIn  = rmn.readGrid(fileIdIn, r)
     rmn.ezsetopt(rmn.EZ_OPT_INTERP_DEGREE, rmn.EZ_INTERP_LINEAR)
     d  = rmn.ezsint(gOut, gIn, r)
     r2 = r.copy()
     r2.update(gOut)
     r2.update({'etiket':'my_etk', 'd':d})
     rmn.fstecr(fileIdOut, r2)
     rmn.writeGrid(fileIdOut, gOut)
     rmn.fstcloseall(fileIdIn)
     rmn.fstcloseall(fileIdOut)
     os.unlink(fileNameOut)  # Remove test file
Example #22
0
 def test_readGrid(self):
     ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES')
     myfile = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_000')
     funit = rmn.fstopenall(myfile)
     rec = rmn.fstlir(funit, nomvar='P0')
     grid = rmn.readGrid(funit, rec)
     self.assertEqual(grid['grtyp'], 'G')
     self.assertEqual(grid['ig1'], 0)
     self.assertEqual(grid['ig2'], 0)
     self.assertEqual(grid['ig3'], 0)
     self.assertEqual(grid['ig4'], 0)
     self.assertEqual(grid['xg1'], 0.)
     self.assertEqual(grid['xg2'], 0.)
     self.assertEqual(grid['xg3'], 0.)
     self.assertEqual(grid['xg4'], 0.)
     self.assertEqual(grid['ni'], 200)
     self.assertEqual(grid['nj'], 100)
     self.assertEqual(grid['shape'], (200, 100))
     self.assertEqual(grid['glb'], True)
     self.assertEqual(grid['north'], True)
     self.assertEqual(grid['inverted'], False)
Example #23
0
def readRec(fname, nomvar):
    """
    Read specified record data from file and get its grid definition

    Args:
       fname  (str): Filename to read from
       nomvar (str): Record varname to read
    Returns:
       rec : Record meta, data and grid definition
    """
    print("+ Read %s from: %s" % (nomvar, fname))

    # Open File
    try:
        funit = rmn.fstopenall(fname, rmn.FST_RO)
    except:
        raise rmn.FSTDError("Problem Opening file: %s" % fname)

    # Read Record data and meta
    try:
        rec = rmn.fstlir(funit, nomvar=nomvar)
    except:
        raise rmn.FSTDError("Problem Reading %s record" % nomvar)

    # Get associated grid
    try:
        rec['iunit'] = funit
        rec_gridid = rmn.ezqkdef(rec)  # use ezscint to get grid id
        rec['grid'] = rmn.decodeGrid(rec_gridid)
        del (rec['iunit']
             )  # iunit will be irrelevant after file is closed below
    except:
        sys.stderr.write('Error: Problem getting grid info for ' + nomvar +
                         ' in file: ' + fname + '\n')
        sys.exit(1)

    # Close File
    rmn.fstcloseall(funit)

    return rec
Example #24
0
 def test_41qd(self):
     import os, sys, datetime
     import rpnpy.librmn.all as rmn
     fdate = datetime.date.today().strftime('%Y%m%d') + '00_048'
     CMCGRIDF = os.getenv('CMCGRIDF').strip()
     fileNameOut = 'p0fstfileqd.fst'
     fileIdIn = rmn.fstopenall(
         os.getenv('CMCGRIDF') + '/prog/regeta/' + fdate)
     fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW)
     gOut = rmn.defGrid_ZE(90, 45, 35., 250., 0.5, 0.5, 0., 180., 1., 270.)
     r = rmn.fstlir(fileIdIn, nomvar='P0')
     gIn = rmn.readGrid(fileIdIn, r)
     rmn.ezsetopt(rmn.EZ_OPT_INTERP_DEGREE, rmn.EZ_INTERP_LINEAR)
     d = rmn.ezsint(gOut, gIn, r)
     r2 = r.copy()
     r2.update(gOut)
     r2.update({'etiket': 'my_etk', 'd': d})
     rmn.fstecr(fileIdOut, r2)
     rmn.writeGrid(fileIdOut, gOut)
     rmn.fstcloseall(fileIdIn)
     rmn.fstcloseall(fileIdOut)
     os.unlink(fileNameOut)  # Remove test file
Example #25
0
def readRec(fname, nomvar):
    """
    Read specified record data from file and get its grid definition

    Args:
       fname  (str): Filename to read from
       nomvar (str): Record varname to read
    Returns:
       rec : Record meta, data and grid definition
    """
    print("+ Read %s from: %s" % (nomvar, fname))
    
    # Open File
    try:
        funit = rmn.fstopenall(fname, rmn.FST_RO)
    except:
        raise rmn.FSTDError("Problem Opening file: %s" % fname)
    
    # Read Record data and meta
    try:
        rec = rmn.fstlir(funit, nomvar=nomvar)
    except:
        raise rmn.FSTDError("Problem Reading %s record" % nomvar)
    
    # Get associated grid
    try:
        rec['iunit'] = funit
        rec_gridid   = rmn.ezqkdef(rec)  # use ezscint to get grid id
        rec['grid']  = rmn.decodeGrid(rec_gridid)
        del(rec['iunit']) # iunit will be irrelevant after file is closed below
    except:
        sys.stderr.write('Error: Problem getting grid info for '+nomvar+' in file: '+fname+'\n')
        sys.exit(1)
        
    # Close File
    rmn.fstcloseall(funit)
    
    return rec
Example #26
0
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
        }
Example #27
0
    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)
Example #28
0
    def test_41(self):
        """
        Horizontal Interpolation
        
        See also:
        """
        import os, sys, datetime
        import rpnpy.librmn.all as rmn
        fdate = datetime.date.today().strftime('%Y%m%d') + '00_048'
        CMCGRIDF = os.getenv('CMCGRIDF').strip()
        fileNameIn = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate)
        fileNameOut = 'p0fstfile.fst'

        # Restrict to the minimum the number of messages printed by librmn
        rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)

        try:
            # Create Destination grid
            # Note: Destination grid can also be read from a file
            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.
            }
            gOut = rmn.encodeGrid(gp)
            print("CB41: Defined a %s/%s grid of shape=%d, %d" %
                  (gOut['grtyp'], gOut['grref'], gOut['ni'], gOut['nj']))
        except:
            sys.stderr.write("Problem creating grid\n")
            sys.exit(1)

        # 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:
            # Find and read record to interpolate with its grid
            r = rmn.fstlir(fileIdIn, nomvar='P0')
            gIn = rmn.readGrid(fileIdIn, r)
            print("CB41: Read P0")

            # Set interpolation options and interpolate
            rmn.ezsetopt(rmn.EZ_OPT_INTERP_DEGREE, rmn.EZ_INTERP_LINEAR)
            d = rmn.ezsint(gOut, gIn, r)
            print("CB41: Interpolate P0")

            # Create new record to write with interpolated data and
            r2 = r.copy()  # Preserve meta from original record
            r2.update(gOut)  # update grid information
            r2.update({  # attach data and update specific meta
                'etiket': 'my_etk',
                'd': d
            })

            # Write record data + meta + grid to file
            rmn.fstecr(fileIdOut, r2)
            rmn.writeGrid(fileIdOut, gOut)
            print("CB41: Wrote interpolated P0 and its grid")
        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
Example #29
0
    def test_41(self):
        """
        Horizontal Interpolation
        
        See also:
        """
        import os, sys, datetime
        import rpnpy.librmn.all as rmn
        fdate       = datetime.date.today().strftime('%Y%m%d') + '00_048'
        CMCGRIDF    = os.getenv('CMCGRIDF').strip()
        fileNameIn  = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate)
        fileNameOut = 'p0fstfile.fst'

        # Restric to the minimum the number of messages printed by librmn
        rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)

        try:
            # Create Destination grid
            # Note: Destination grid can also be read from a file
            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.
                }
            gOut = rmn.encodeGrid(gp)
            print("CB41: Defined a %s/%s grid of shape=%d, %d" %
                  (gOut['grtyp'], gOut['grref'], gOut['ni'], gOut['nj']))
        except:
            sys.stderr.write("Problem creating grid\n")
            sys.exit(1)

        # 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:
            # Find and read record to interpolate with its grid 
            r = rmn.fstlir(fileIdIn, nomvar='P0')
            gIn = rmn.readGrid(fileIdIn, r)
            print("CB41: Read P0")

            # Set interpolation options and interpolate
            rmn.ezsetopt(rmn.EZ_OPT_INTERP_DEGREE, rmn.EZ_INTERP_LINEAR)
            d = rmn.ezsint(gOut, gIn, r)
            print("CB41: Interpolate P0")

            # Create new record to write with interpolated data and 
            r2 = r.copy()    # Preserve meta from original record
            r2.update(gOut)  # update grid information
            r2.update({      # attach data and update specific meta
                'etiket': 'my_etk',
                'd'     : d
                })
            
            # Write record data + meta + grid to file
            rmn.fstecr(fileIdOut, r2)
            rmn.writeGrid(fileIdOut, gOut)
            print("CB41: Wrote interpolated P0 and its grid")
        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
Example #30
0
    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.

        # Restric 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.iteritems())
            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 xrange(rshape[1]):
            ##     for i in xrange(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)
Example #31
0
    def test_23(self):
        """
        Edit: Read, Edit, Write records with meta, grid and vgrid

        This example shows how to
        * select records in a RPNStd file
        * read the record data + meta
        * edit/use record data and meta (compute the wind velocity)
        * write the recod data + meta
        * copy (read/write) the record grid descriptors
        * copy (read/write) the file vgrid descriptor

        See also:
        rpnpy.librmn.fstd98.fstopt
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.fstd98.fsrinl
        rpnpy.librmn.fstd98.fsrluk
        rpnpy.librmn.fstd98.fsrlir
        rpnpy.librmn.fstd98.fsrecr
        rpnpy.librmn.grids.readGrid
        rpnpy.librmn.grids.writeGrid
        rpnpy.vgd.base.vgd_read
        rpnpy.vgd.base.vgd_write
        rpnpy.librmn.const
        rpnpy.vgd.const
        """
        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'
        CMCGRIDF = os.getenv('CMCGRIDF').strip()
        fileNameIn = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate)
        fileNameOut = 'uvfstfile.fst'

        # Restrict to the minimum the number of messages printed by librmn
        rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)

        # 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:
            # Copy the vgrid descriptor
            v = vgd.vgd_read(fileIdIn)
            vgd.vgd_write(v, fileIdOut)
            print("CB23: Copied the vgrid descriptor")

            # Loop over the list of UU records to copy
            uu = {'d': None}
            vv = {'d': None}
            uvarray = None
            copyGrid = True
            for k in rmn.fstinl(fileIdIn, nomvar='UU'):
                # Read the UU record data and meta from fileNameIn
                # Provide data array to re-use memory
                uu = rmn.fstluk(k, dataArray=uu['d'])

                # Read the corresponding VV
                # Provide data array to re-use memory
                vv = rmn.fstlir(fileIdIn,
                                nomvar='VV',
                                ip1=uu['ip1'],
                                ip2=uu['ip2'],
                                datev=uu['datev'],
                                dataArray=vv['d'])

                # Compute the wind modulus in m/s
                # Copy metadata from the UU record
                # Create / re-use memory space for computation results
                uv = uu.copy()
                if uvarray is None:
                    uvarray = np.empty(uu['d'].shape,
                                       dtype=uu['d'].dtype,
                                       order='FORTRAN')
                uv['d'] = uvarray
                uv['d'][:, :] = np.sqrt(uu['d']**2. + vv['d']**2.)
                uv['d'] *= KNOT2MS  # Convert from knot to m/s

                # Set new record name and Write it to fileNameOut
                uv['nomvar'] = 'WSPD'
                rmn.fstecr(fileIdOut, uv)

                print("CB23: Wrote %s ip1=%d, ip2=%d, dateo=%s : mean=%f" %
                      (uv['nomvar'], uv['ip1'], uv['ip2'], uv['dateo'],
                       uv['d'].mean()))

                # Read and Write grid (only once, all rec are on the same grid)
                if copyGrid:
                    copyGrid = False
                    g = rmn.readGrid(fileIdIn, uu)
                    rmn.writeGrid(fileIdOut, g)
                    print("CB23: Copied the grid descriptors")
        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
Example #32
0
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}
Example #33
0
import rpnpy.librmn.all as rmn
data_file = '/cnfs/dev/cmd/cmde/afsgbla/watroute2py/watroute/budget/io/test/input_test.fst'
funit = rmn.fstopenall(data_file,rmn.FST_RO)
k = rmn.fstinf(funit, nomvar='I1D')
r = rmn.fstlir(funit, nomvar='I1D')
print k
print r
rmn.fstcloseall(funit)
Example #34
0
import rpnpy.librmn.all as rmn

data_file = "/cnfs/dev/cmd/cmde/afsgbla/watroute2py/watroute/budget/io/test/input_test.fst"
funit = rmn.fstopenall(data_file, rmn.FST_RO)
k = rmn.fstinf(funit, nomvar="I1D")
r = rmn.fstlir(funit, nomvar="I1D")
print k
print r
rmn.fstcloseall(funit)
Example #35
0
    def test_23(self):
        """
        Edit: Read, Edit, Write records with meta, grid and vgrid

        This example shows how to
        * select records in a RPNStd file
        * read the record data + meta
        * edit/use record data and meta (compute the wind velocity)
        * write the recod data + meta
        * copy (read/write) the record grid descriptors
        * copy (read/write) the file vgrid descriptor

        See also:
        rpnpy.librmn.fstd98.fstopt
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.fstd98.fsrinl
        rpnpy.librmn.fstd98.fsrluk
        rpnpy.librmn.fstd98.fsrlir
        rpnpy.librmn.fstd98.fsrecr
        rpnpy.librmn.grids.readGrid
        rpnpy.librmn.grids.writeGrid
        rpnpy.vgd.base.vgd_read
        rpnpy.vgd.base.vgd_write
        rpnpy.librmn.const
        rpnpy.vgd.const
        """
        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'
        CMCGRIDF    = os.getenv('CMCGRIDF').strip()
        fileNameIn  = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate)
        fileNameOut = 'uvfstfile.fst'

        # Restrict to the minimum the number of messages printed by librmn
        rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)

        # 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:
            # Copy the vgrid descriptor
            v = vgd.vgd_read(fileIdIn)
            vgd.vgd_write(v, fileIdOut)
            print("CB23: Copied the vgrid descriptor")
            
            # Loop over the list of UU records to copy 
            uu = {'d': None}
            vv = {'d': None}
            uvarray = None
            copyGrid = True
            for k in rmn.fstinl(fileIdIn, nomvar='UU'):
                # Read the UU record data and meta from fileNameIn
                # Provide data array to re-use memory
                uu = rmn.fstluk(k, dataArray=uu['d'])
                
                # Read the corresponding VV
                # Provide data array to re-use memory
                vv = rmn.fstlir(fileIdIn, nomvar='VV',
                                ip1=uu['ip1'], ip2=uu['ip2'], datev=uu['datev'],
                                dataArray=vv['d'])

                # Compute the wind modulus in m/s
                # Copy metadata from the UU record
                # Create / re-use memory space for computation results
                uv = uu.copy()
                if uvarray is None:
                    uvarray = np.empty(uu['d'].shape, dtype=uu['d'].dtype, order='FORTRAN')
                uv['d'] = uvarray
                uv['d'][:,:] = np.sqrt(uu['d']**2. + vv['d']**2.)
                uv['d'] *= KNOT2MS  # Convert from knot to m/s

               # Set new record name and Write it to fileNameOut
                uv['nomvar'] = 'WSPD'
                rmn.fstecr(fileIdOut, uv)
                
                print("CB23: Wrote %s ip1=%d, ip2=%d, dateo=%s : mean=%f" %
                      (uv['nomvar'], uv['ip1'], uv['ip2'], uv['dateo'], uv['d'].mean()))

                # Read and Write grid (only once, all rec are on the same grid)
                if copyGrid:
                    copyGrid = False
                    g = rmn.readGrid(fileIdIn, uu)
                    rmn.writeGrid(fileIdOut, g)
                    print("CB23: Copied the grid descriptors")
        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
Example #36
0
    file = rmn.fstopenall(
        '/home/smc001/hall3/R2FC70H17V1_post/gridpt/prog/hyb/2017022812_084',
        rmn.FST_RO)
except:
    sys.stderr.write("Problem opening the file: %s\n" % fileName)

fhour = 84
var = 'TT'
level = 76696048

###Read in lat/lon data###

# Prefered method to get grid lat, lon. Works on any RPNSTD grid except 'x'
#####
try:
    rec = rmn.fstlir(file, nomvar=var)
except:
    sys.stderr.write('Error: Problem reading fields ' + var + ' in file: ' +
                     file + '\n')
    sys.exit(1)

try:
    rec['iunit'] = file
    gridid = rmn.ezqkdef(rec)  # use ezscint to retreive full grid
    gridLatLon = rmn.gdll(gridid)
    lat = gridLatLon['lat']
    lon = gridLatLon['lon']
except:
    sys.stderr.write('Error: Problem getting grid info in file')
    sys.exit(1)
######
Example #37
0
def vgd_levels(
    vgd_ptr, rfld=None, ip1list="VIPM", in_log=_vc.VGD_DIAG_PRES, dpidpis=_vc.VGD_DIAG_DPIS, double_precision=False
):
    """
    Compute level positions (pressure or log(p)) for the given ip1 list and surface field

    Args:
        vgd_ptr (VGridDescriptor ref):
                           Reference/Pointer to the VGridDescriptor
        rfld     (mixed) : Reference surface field
                           Possible values:
                           (int)    : RPNStd unit where to read the RFLD
                           (float)  : RFLD values
                           (list)   : RFLD values
                           (ndarray): RFLD values
        ip11list (mixed) : ip1 list of destination levels
                           (str) : get the ip1 list form the vgd object
                                   possible value: 'VIPM' or 'VIPT'
                           (int) or (list): ip1 values
        in_log   (int)   : VGD_DIAG_LOGP or VGD_DIAG_PRES
        dpidpis  (int)   : VGD_DIAG_DPI or VGD_DIAG_DPIS
        double_precision (bool) : True for double precision computations
    Returns:
        ndarray : numpy array with shape of...
                  if type(rfld) == float:   shape = [len(ip11list),]
                  if type(rfld) == list:    shape = [len(list), len(ip11list)]
                  if type(rfld) == ndarray: shape = rfld.shape + [len(ip11list)]
    Raises:
        TypeError
        VGDError

    Examples:
    >>> import sys
    >>> import rpnpy.vgd.all as vgd
    >>> lvls  = (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  = 1000.
    >>> try:
    >>>     myvgd = vgd.vgd_new_eta(lvls, ptop)
    >>> except vgd.VGDError:
    >>>     sys.stderr.write('There was a problem creating the VGridDescriptor')
    >>>     sys.exit(1)
    >>> try:
    >>>     levels = vgd.vgd_levels(myvgd, rfld=100130.)
    >>> except vgd.VGDError:
    >>>     sys.stderr.write("There was a problem computing VGridDescriptor levels")

    See Also:
        rpnpy.vgd.const.VGD_DIAG_LOGP
        rpnpy.vgd.const.VGD_DIAG_PRES
        rpnpy.vgd.const.VGD_DIAG_DPI
        rpnpy.vgd.const.VGD_DIAG_DPIS
        vgd_new
        vgd_read
        vgd_free
        vgd_get
    """
    if isinstance(ip1list, str):
        ip1list0 = vgd_get(vgd_ptr, ip1list)
        ip1list1 = _np.array(ip1list0, dtype=_np.int32, order="FORTRAN")
    elif isinstance(ip1list, _integer_types):
        ip1list1 = _np.array([ip1list], dtype=_np.int32, order="FORTRAN")
    elif isinstance(ip1list, (list, tuple)):
        ip1list1 = _np.array(ip1list, dtype=_np.int32, order="FORTRAN")
    elif isinstance(ip1list, _np.ndarray):
        ip1list1 = _np.array(ip1list.flatten(), dtype=_np.int32, order="FORTRAN")
    else:
        raise TypeError("ip1list should be string, list or int: {0}".format(str(type(ip1list))))
    nip1 = ip1list1.size
    ip1list = ip1list1.ctypes.data_as(_ct.POINTER(_ct.c_int))

    vkind = vgd_get(vgd_ptr, "KIND")
    vvers = vgd_get(vgd_ptr, "VERS")
    vcode = int(vkind) * 1000 + int(vvers)

    rank0 = False
    if isinstance(rfld, float):
        rfld = _np.array([rfld], dtype=_np.float32, order="FORTRAN")
        rank0 = True
    elif isinstance(rfld, (list, tuple)):
        rfld = _np.array(rfld, dtype=_np.float32, order="FORTRAN")
    elif isinstance(rfld, int):
        if _vc.VGD_VCODE_NEED_RFLD[vcode]:
            fileId = rfld
            rfld_name = vgd_get(vgd_ptr, "RFLD")
            if not rfld_name:
                raise VGDError("Problem getting RFLD to compute levels")
            rfld = _rmn.fstlir(fileId, nomvar=rfld_name.strip())["d"]
            MB2PA = 100.0
            rfld = rfld * MB2PA
        else:
            rfld = _np.array([float(fileId)], dtype=_np.float32, order="FORTRAN")
            rank0 = True
    elif rfld is None:
        if _vc.VGD_VCODE_NEED_RFLD[vcode]:
            raise TypeError("RFLD needs to be provided for vcode={0}".format(vcode))
        else:
            rfld = _np.array([1000.0], dtype=_np.float32, order="FORTRAN")
            rank0 = True
    elif not isinstance(rfld, _np.ndarray):
        raise TypeError("rfld should be ndarray, list or float: {0}".format(str(type(ip1list))))

    if double_precision:
        dtype = _np.float64
        rfld8 = _np.array(rfld, copy=True, dtype=dtype, order="FORTRAN")
    else:
        dtype = _np.float32
        rfld8 = rfld
    shape = list(rfld.shape) + [nip1]
    levels8 = _np.empty(shape, dtype=dtype, order="FORTRAN")

    ok = _vc.VGD_OK
    if vkind == 2:  # Workaround for pressure levels
        for k in xrange(nip1):
            (value, kind) = _rmn.convertIp(_rmn.CONVIP_DECODE, int(ip1list1[k]))
            levels8[:, :, k] = value * _MB2PA
    else:
        if double_precision:
            ok = _vp.c_vgd_diag_withref_8(vgd_ptr, rfld8.size, 1, nip1, ip1list, levels8, rfld8, in_log, dpidpis)
        else:
            ok = _vp.c_vgd_diag_withref(vgd_ptr, rfld8.size, 1, nip1, ip1list, levels8, rfld8, in_log, dpidpis)

    if ok != _vc.VGD_OK:
        raise VGDError("Problem computing levels.")
    if rank0:
        levels8 = levels8.flatten()
    return levels8
Example #38
0
    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']
Example #39
0
def read_fst(file, var, level, fhour, yy):
    global field
    global lat
    global lon
    print var
    print level
    print fhour
    print yy
    try:
        file = rmn.fstopenall(file, rmn.FST_RO)
    except:
        sys.stderr.write("Problem opening the file: %s\n" % file)

    ###Read in lat/lon data###

    # Prefered method to get grid lat, lon. Works on any RPNSTD grid except 'x'
    #####
    try:
        rec = rmn.fstlir(file, nomvar=var)
    except:
        sys.stderr.write('Error: Problem reading fields ' + var +
                         ' in file: ' + file + '\n')
        sys.exit(1)

    try:
        rec['iunit'] = file
        gridid = rmn.ezqkdef(rec)  # use ezscint to retreive full grid
        gridLatLon = rmn.gdll(gridid)
        lat = gridLatLon['lat']
        lon = gridLatLon['lon']
        if yy == True:
            print "This grid is Yin-Yang and the lat/lon arrays need to be constructed from the 2 subgrids"
            lat1 = gridLatLon['subgrid'][0]['lat']
            lat2 = gridLatLon['subgrid'][1]['lat']
            lon1 = gridLatLon['subgrid'][0]['lon']
            lon2 = gridLatLon['subgrid'][1]['lon']
            lat = np.append(lat1, lat2, axis=1)
            lon = np.append(lon1, lon2, axis=1)
    except:
        sys.stderr.write('Error: Problem getting grid info in file')
        sys.exit(1)
    ######
    ######

    try:
        #Get vertical grid definition from file
        v = vgd.vgd_read(file)

        #Get the list of ip1 on thermo levels in this file
        #tlvl = vgd.vgd_get(v, 'VIPT')
        tlvl = level

        # 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, level)
        key = rmn.fstinf(file,
                         nomvar=var,
                         ip2=fhour,
                         ip1=rmn.ip1_all(lval, lkind))
        if key is not None:
            tlvlkeys.append((level, key['key']))
            if rshape is None:
                rshape = key['shape']
        rshape = (rshape[0], rshape[1], len(tlvlkeys))

        r2d = {'d': None}
        r3d = None
        k = 0
        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:
            raise

    except:
        raise
    finally:
        # Close file even if an error occured above
        rmn.fstcloseall(file)

    # 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]

    field = r3d['d']

    return (field, lat, lon)
Example #40
0
        CMCGRIDF = os.environ['CMCGRIDF'].strip()
    except KeyError:
        sys.stderr.write('Error: CMCGRIDF environment variable undefined. Before Python, execute:\n')
        sys.stderr.write('. ssmuse-sh -d cmoi/base/20160901\n')
        sys.exit(1)        
    my_file = os.path.join(CMCGRIDF, 'prog', 'gsloce', forecast_name)

    try:
        funit = rmn.fstopenall(my_file, rmn.FST_RO)
    except:
        sys.stderr.write('Error: Unable to open file: '+my_file+'\n')
        sys.exit(1)

    varname = 'tm2'
    try:
        sst_rec      = rmn.fstlir(funit, nomvar=varname, typvar='P@')
        sst_mask_rec = rmn.fstlir(funit, nomvar=varname, typvar='@@')
    except:
        sys.stderr.write('Error: Problem reading fields '+varname+' in file: '+my_file+'\n')
        sys.exit(1)

    # Prefered method to get grid lat, lon. Works on any RPNSTD grid
    # type (except 'X')
    ## try:
    ##     sst_rec['iunit'] = funit
    ##     sst_gridid = rmn.ezqkdef(sst_rec)  # use ezscint to retreive full grid
    ##     gridLatLon = rmn.gdll(sst_gridid)
    ##     lat = gridLatLon['lat']
    ##     lon = gridLatLon['lon']
    ## except:
    ##     sys.stderr.write('Error: Problem getting grid info for '+varname+' in file: '+my_file+'\n')