Exemple #1
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
Exemple #2
0
    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)
Exemple #3
0
 def _newReadBcmk(self):
     ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
     fileName = os.path.join(ATM_MODEL_DFILES,'bcmk_toctoc','2009042700_000')
     fileId = rmn.fstopenall(fileName, rmn.FST_RO)
     vgd0ptr = vgd.vgd_read(fileId)
     rmn.fstcloseall(fileId)
     return vgd0ptr
Exemple #4
0
 def _newReadBcmk(self):
     ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
     fileName = os.path.join(ATM_MODEL_DFILES,'bcmk_toctoc','2009042700_000')
     fileId = rmn.fstopenall(fileName, rmn.FST_RO)
     vgd0ptr = vgd.vgd_read(fileId)
     rmn.fstcloseall(fileId)
     return vgd0ptr
Exemple #5
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
Exemple #6
0
 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()))
Exemple #7
0
    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)
Exemple #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.vgd_read(fileId)
     levels8 = vgd.vgd_levels(vgd0ptr, fileId, double_precision=True)
     rmn.fstcloseall(fileId)
     (ni,nj,nk) = levels8.shape
     self.assertEqual([int(x) for x in levels8[ni//2,nj//2,0:5]*10000.],
                      [100000, 138425, 176878, 241408, 305980])
     self.assertEqual(len(levels8.shape),3)
     self.assertEqual(levels8.dtype,np.float64)
Exemple #9
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.vgd_read(fileId)
     levels8 = vgd.vgd_levels(vgd0ptr, fileId, double_precision=True)
     rmn.fstcloseall(fileId)
     (ni,nj,nk) = levels8.shape
     self.assertEqual([int(x) for x in levels8[ni//2,nj//2,0:5]*10000.],
                      [100000, 138425, 176878, 241408, 305980])
     self.assertEqual(len(levels8.shape),3)
     self.assertEqual(levels8.dtype,np.float64)
Exemple #10
0
 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()))
Exemple #11
0
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
        }
Exemple #12
0
    def testWriteDesc(self):
        vgd0ptr = self._newReadBcmk()

        self._erase_testfile()
        fileName = self.fname
        fileId = rmn.fstopenall(fileName, rmn.FST_RW)
        vgd.vgd_write(vgd0ptr,fileId)
        rmn.fstcloseall(fileId)

        fileId = rmn.fstopenall(fileName, rmn.FST_RO)
        vgd1ptr = vgd.vgd_read(fileId)
        rmn.fstcloseall(fileId)
        self._erase_testfile()
        ok = vgd.vgd_cmp(vgd0ptr,vgd1ptr)
        self.assertTrue(ok)
Exemple #13
0
    def testWriteDesc(self):
        vgd0ptr = self._newReadBcmk()

        self._erase_testfile()
        fileName = self.fname
        fileId = rmn.fstopenall(fileName, rmn.FST_RW)
        vgd.vgd_write(vgd0ptr,fileId)
        rmn.fstcloseall(fileId)

        fileId = rmn.fstopenall(fileName, rmn.FST_RO)
        vgd1ptr = vgd.vgd_read(fileId)
        rmn.fstcloseall(fileId)
        self._erase_testfile()
        ok = vgd.vgd_cmp(vgd0ptr,vgd1ptr)
        self.assertTrue(ok)
Exemple #14
0
 def _newReadBcmk(self, vcode_name=None):
     fileName = None
     ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
     if vcode_name is None:
         fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk_toctoc',
                                 '2009042700_000')
     else:
         fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk_vgrid',
                                 vcode_name.strip())
     try:
         fileId = rmn.fstopenall(fileName, rmn.FST_RO)
     except Exception as e:
         print(e)
         raise RuntimeError("Invalid file name: {}".format(fileName))
     vgd0ptr = vgd.vgd_read(fileId)
     rmn.fstcloseall(fileId)
     return vgd0ptr
Exemple #15
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
Exemple #16
0
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
    }
Exemple #17
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)
Exemple #18
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
Exemple #19
0
##### MAIN #####
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)
Exemple #20
0
    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()))
Exemple #21
0
    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', '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 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 xrange(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()))
Exemple #22
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)
Exemple #23
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)
Exemple #24
0
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)
######
######

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']))