コード例 #1
0
    def test_fsteff_list_rec(self):
        """fsteff accept list and dict as input"""
        rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)
        (la, lo) = self.create_basefile()  #wrote 2 recs in that order: la, lo
        funit = rmn.fstopenall(self.fname, rmn.FST_RW)
        keylist = rmn.fstinl(funit)
        rmn.fsteff(keylist)
        rmn.fstcloseall(funit)
        funit = rmn.fstopenall(self.fname, rmn.FST_RO)
        kla = rmn.fstinf(funit, nomvar='LA')
        klo = rmn.fstinf(funit, nomvar='LO')
        rmn.fstcloseall(funit)
        self.erase_testfile()
        self.assertEqual(kla, None, 'LA found after delete: ' + repr(kla))
        self.assertEqual(klo, None, 'LO found after delete: ' + repr(klo))

        (la, lo) = self.create_basefile()  #wrote 2 recs in that order: la, lo
        funit = rmn.fstopenall(self.fname, rmn.FST_RW)
        klo = rmn.fstinf(funit, nomvar='LO')
        rmn.fsteff(klo)
        rmn.fstcloseall(funit)
        funit = rmn.fstopenall(self.fname, rmn.FST_RO)
        klo = rmn.fstinf(funit, nomvar='LO')
        rmn.fstcloseall(funit)
        self.erase_testfile()
        self.assertEqual(klo, None, 'LO found after delete: ' + repr(klo))
コード例 #2
0
    def test_fstdread3d(self):
        """
        """
        import os, sys, datetime
        import rpnpy.librmn.all as rmn
        import rpnpy.utils.fstd3d as fstd3d
        fdate       = datetime.date.today().strftime('%Y%m%d') + '00_048'
        CMCGRIDF    = os.getenv('CMCGRIDF').strip()
        fileNameIn  = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate)

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

        try:
            fileId = rmn.fstopenall(fileNameIn)
        except:
            sys.stderr.write("Problem opening the files: %s, %s\n" % (fileNameIn, fileNameOut))
            sys.exit(1)

        try:
            rec3d = fstd3d.fst_read_3d(fileId, nomvar='TT', getPress=True, verbose=True)
        except:
            raise
        finally:
            # Properly close files even if an error occured above
            # This is important when editing to avoid corrupted files
            rmn.fstcloseall(fileId)
コード例 #3
0
    def test_fstdread3d(self):
        """
        """
        import os, sys, datetime
        import rpnpy.librmn.all as rmn
        import rpnpy.utils.fstd3d as fstd3d
        fdate = datetime.date.today().strftime('%Y%m%d') + '00_048'
        CMCGRIDF = os.getenv('CMCGRIDF').strip()
        fileNameIn = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate)

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

        try:
            fileId = rmn.fstopenall(fileNameIn)
        except:
            sys.stderr.write("Problem opening the files: %s, %s\n" %
                             (fileNameIn, fileNameOut))
            sys.exit(1)

        try:
            rec3d = fstd3d.fst_read_3d(fileId,
                                       nomvar='TT',
                                       getPress=True,
                                       verbose=True)
        except:
            raise
        finally:
            # Properly close files even if an error occured above
            # This is important when editing to avoid corrupted files
            rmn.fstcloseall(fileId)
コード例 #4
0
 def test_ezqkdef_file_ezgprm_ezgfstp(self):
     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/geophy.fst')
     funit = rmn.fstopenall(myfile,rmn.FST_RO)
     (ni,nj) = (201,100)
     gp = {
         'shape' : (ni,nj),
         'ni' : ni,
         'nj' : nj,
         'grtyp' : 'Z',
         'ig1'   : 2002,
         'ig2'   : 1000,
         'ig3'   : 0,
         'ig4'   : 0,
         'grref' : 'E',
         'ig1ref' : 900,
         'ig2ref' : 0,
         'ig3ref' : 43200,
         'ig4ref' : 43200,
         'iunit'  : funit
         }
     gid1 = rmn.ezqkdef(gp)
     a = rmn.ezgfstp(gid1)
     rmn.fstcloseall(funit)
     self.assertTrue(gid1>=0)
     gp['id'] = gid1
     gprm = rmn.ezgxprm(gid1)
     for k in gprm.keys():
         self.assertEqual(gp[k],gprm[k])
     self.assertEqual(a['nomvarx'].strip(),'>>')
     self.assertEqual(a['nomvary'].strip(),'^^')
     rmn.gdrls(gid1)
コード例 #5
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)
コード例 #6
0
def plotTwo(paramsBase=paramsBase, paramsAlt=paramsAlt, savePath = saveLoc):
    rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)
    filePath1, spc1, area1, title1 = paramsBase['filePath'], paramsBase['spc'], paramsBase['area'], paramsBase['title']
    filePath2, spc2, area2, title2 = paramsAlt['filePath'], paramsAlt['spc'], paramsAlt['area'], paramsAlt['title']
    fileID1= openFile(filePath1)
    print(filePath)
    print(spc1)
    gridKey1 = getKey(fileID1, spc1, 76696048)
    gridData1, lonData1, latData1 = getGridData(fileID1, gridKey1)
    try:
        areaType1 = paramsBase['type']
        if areaType1 == 'n':
            ni1, nj1 = area1[0], area1[1]
        else:
            ni1, nj1 = getFuzzyNiNj(area1[0], area1[1], gridData1)
    except: #default to ninj type, only will get if lon/lat failed to get ninj values
        ni1, nj1 = area1[0], area1[1]

    fileID2 = openFile(filePath2)
    gridKey2 = getKey(fileID2, spc2, 76696048)
    gridData2, lonData2, latData2 = getGridData(fileID2, gridKey2)
    try:
        areaType2 = paramsAlt['type']
        if areaType2 == 'n':
            ni2, nj2 = area2[0], area2[1]
        else:
            ni2, nj2 = getFuzzyNiNj(area2[0], area2[1], gridData2)
    except: #default to ninj type, only will get if lon/lat failed to get ninj values
        ni2, nj2 = area1[0], area1[1]

    hyList1, concList1= getPlotValues(fileID1, ip1List, spc1, ni1, nj1)
    hyList2, concList2= getPlotValues(fileID2, ip1List, spc2, ni2, nj2)
    plotStackVP(concList1, concList2, hyList1, hyList2, title1, title2, spc1, savePath)
コード例 #7
0
ファイル: test_cookbook.py プロジェクト: wk1984/python-rpn
    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)
コード例 #8
0
    def test_isfst_openall_fstnbr(self):
        """isfst_openall_fstnbr should give known result with known input"""
        rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)
        ## rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST,rmn.FSTOP_GET)

        HOME = os.getenv('HOME')
        a = rmn.isFST(os.path.join(HOME.strip(), '.profile'))
        self.assertFalse(a, 'isFST should return false on non FST files')

        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES')
        myfile = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_000')
        a = rmn.isFST(myfile)
        self.assertTrue(a, 'isFST should return true on FST files')

        funit = rmn.fstopenall(myfile, rmn.FST_RO)
        self.assertTrue(funit > 0,
                        'fstopenall should return a valid file unit')

        nrec = rmn.c_fstnbrv(funit)
        self.assertEqual(nrec, 1083, ' c_fstnbrv found %d/1083 rec ' % nrec)

        nrec = rmn.fstnbrv(funit)
        self.assertEqual(nrec, 1083, ' fstnbrv found %d/1083 rec ' % nrec)

        rmn.fstcloseall(funit)
コード例 #9
0
def getID(filePath):
    """ Gets the fileID and opens the file in read/write mode. """

    rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)
    # open file to process
    fileID = rmn.fstopenall(filePath, rmn.FST_RW)
    return fileID
コード例 #10
0
def diffPlot(fileRun=defaultFile, baseFile=defaultPathStnd+defaultFile, modelFile=defaultPathStnd+defaultFile, savePath=defaultSave, level=defaultIp1, species=defaultSpc, buffering = buff, mapType=defaultDiffCmap, reverse=False, extension='neither', projType='stere', vmin=None, vmax=None, totalBins=defaultBins,partName='test',removed=defaultClear):
    """ Execute difference plot creation. """

    logging.basicConfig(filename='ConcentrationPlotter.log', level=logging.DEBUG,format='%(asctime)s %(message)s')

    # print the min number of messages produced by librmn
    rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)
    # get the data for the standard first, close file to continue
    concDataStnd = getConc(baseFile,level,species)
    if concDataStnd == None:
       return None
    else:
       gridStnd = getGrid(concDataStnd['dataKey'],concDataStnd['fileID'])
       closeFST(concDataStnd['fileID'])
       # get the data for the alternative, close file to continue
       concDataAlt = getConc(modelFile,level,species)
       if concDataAlt == None:
          return None
       else:
          gridAlt = getGrid(concDataAlt['dataKey'],concDataAlt['fileID'])
          closeFST(concDataAlt['fileID'])
          # get the difference between the datasets
          diffData = concDiff(concDataStnd['concData'],concDataAlt['concData'])
          # check that the grid sizes make sense
          gridCheck(gridStnd['gridll'],gridAlt['gridll'])
          # minor cleanup
          cmapDetails = cmapType(mapType,reverse)
          makeDir(savePath)
          # plot and save the figure
          plotConc(diffData, gridStnd['lonData'], gridStnd['latData'], savePath, fileRun, projType, level, species,cmapDetails, totalBins, vmin, vmax, extension, partName,removed,buffering)
コード例 #11
0
    def test_fsteff_list_rec(self):
        """fsteff accept list and dict as input"""
        rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)
        (la,lo) = self.create_basefile() #wrote 2 recs in that order: la, lo
        funit   = rmn.fstopenall(self.fname,rmn.FST_RW)
        keylist = rmn.fstinl(funit)
        rmn.fsteff(keylist)
        rmn.fstcloseall(funit)
        funit = rmn.fstopenall(self.fname,rmn.FST_RO)
        kla = rmn.fstinf(funit,nomvar='LA')
        klo = rmn.fstinf(funit,nomvar='LO')
        rmn.fstcloseall(funit)
        self.erase_testfile()
        self.assertEqual(kla,None,'LA found after delete: '+repr(kla))
        self.assertEqual(klo,None,'LO found after delete: '+repr(klo))

        (la,lo) = self.create_basefile() #wrote 2 recs in that order: la, lo
        funit   = rmn.fstopenall(self.fname,rmn.FST_RW)
        klo     = rmn.fstinf(funit,nomvar='LO')
        rmn.fsteff(klo)
        rmn.fstcloseall(funit)
        funit = rmn.fstopenall(self.fname,rmn.FST_RO)
        klo = rmn.fstinf(funit,nomvar='LO')
        rmn.fstcloseall(funit)
        self.erase_testfile()
        self.assertEqual(klo,None,'LO found after delete: '+repr(klo))
コード例 #12
0
ファイル: test_cookbook.py プロジェクト: meteokid/python-rpn
 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()))
コード例 #13
0
 def test_ezqkdef_file_ezgprm_ezgfstp(self):
     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/geophy.fst')
     funit = rmn.fstopenall(myfile,rmn.FST_RO)
     (ni,nj) = (201,100)
     gp = {
         'shape' : (ni,nj),
         'ni' : ni,
         'nj' : nj,
         'grtyp' : 'Z',
         'ig1'   : 2002,
         'ig2'   : 1000,
         'ig3'   : 0,
         'ig4'   : 0,
         'grref' : 'E',
         'ig1ref' : 900,
         'ig2ref' : 0,
         'ig3ref' : 43200,
         'ig4ref' : 43200,
         'iunit'  : funit
         }
     gid1 = rmn.ezqkdef(gp)
     a = rmn.ezgfstp(gid1)
     rmn.fstcloseall(funit)
     self.assertTrue(gid1>=0)
     gp['id'] = gid1
     gprm = rmn.ezgxprm(gid1)
     for k in gprm.keys():
         self.assertEqual(gp[k],gprm[k])
     self.assertEqual(a['nomvarx'].strip(),'>>')
     self.assertEqual(a['nomvary'].strip(),'^^')
     rmn.gdrls(gid1)
コード例 #14
0
ファイル: bin12.py プロジェクト: elisadong/ECCC_Stuff
def getID(filePath):
    ''' Returns the ID of the fst file provided by the filePath. '''

    rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)
    # open file to process
    fileID = rmn.fstopenall(filePath, rmn.FST_RW)
    return fileID
コード例 #15
0
ファイル: test_cookbook.py プロジェクト: wk1984/python-rpn
    def test_12(self):
        """
        Queries: Get Data and Meta

        This example shows how to
        * get the data of all records matching selection creterions
        * then print statistics

        See also:
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstinl
        rpnpy.librmn.fstd98.fstprm
        rpnpy.librmn.fstd98.fstluk
        rpnpy.librmn.fstd98.fstlir
        rpnpy.librmn.fstd98.DecodeIp
        rpnpy.librmn.fstd98.kindToString
        rpnpy.librmn.fstd98.fstcloseall
        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 all RPNStd files in the $ATM_MODEL_DFILES/bcmk/ directory
        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
        fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk')
        try:
            fileId = rmn.fstopenall(fileName, rmn.FST_RO)
        except:
            sys.stderr.write("Problem opening the file: %s\n" % fileName)
            sys.exit(1)

        try:
            # Get the list of record keys matching nomvar='TT'
            keylist = rmn.fstinl(fileId, nomvar='TT', ip2=12)

            for k in keylist:

                # Get the record meta data
                r = rmn.fstluk(k)
                d = r['d']

                # Decode level info and Print statistics
                rp1 = rmn.DecodeIp(r['ip1'], r['ip2'], r['ip3'])[0]
                level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind))

                print(
                    "CB12: %s (level=%s, ip2=%d) mean=%f, std=%f, min=%f, max=%f"
                    % (r['nomvar'], level, r['ip2'], d.mean(), d.std(),
                       d.min(), d.max()))
        except:
            pass
        finally:
            # Close file even if an error occured above
            rmn.fstcloseall(fileId)
コード例 #16
0
ファイル: test_cookbook.py プロジェクト: meteokid/python-rpn
    def test_12(self):
        """
        Queries: Get Data and Meta

        This example shows how to
        * get the data of all records matching selection creterions
        * then print statistics

        See also:
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstinl
        rpnpy.librmn.fstd98.fstprm
        rpnpy.librmn.fstd98.fstluk
        rpnpy.librmn.fstd98.fstlir
        rpnpy.librmn.fstd98.DecodeIp
        rpnpy.librmn.fstd98.kindToString
        rpnpy.librmn.fstd98.fstcloseall
        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 all RPNStd files in the $ATM_MODEL_DFILES/bcmk/ directory
        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
        fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk')
        try:
            fileId = rmn.fstopenall(fileName, rmn.FST_RO)
        except:
            sys.stderr.write("Problem opening the file: %s\n" % fileName)
            sys.exit(1)

        try:
            # Get the list of record keys matching nomvar='TT'
            keylist = rmn.fstinl(fileId, nomvar='TT', ip2=12)
            
            for k in keylist:
                
                # Get the record meta data
                r = rmn.fstluk(k)
                d = r['d']

                # Decode level info and Print statistics
                rp1 = rmn.DecodeIp(r['ip1'], r['ip2'], r['ip3'])[0]
                level  = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind))
                
                print("CB12: %s (level=%s, ip2=%d) mean=%f, std=%f, min=%f, max=%f" %
                      (r['nomvar'], level, r['ip2'], d.mean(), d.std(), d.min(), d.max()))
        except:
            pass
        finally:
            # Close file even if an error occured above
            rmn.fstcloseall(fileId)
コード例 #17
0
ファイル: test_cookbook.py プロジェクト: meteokid/python-rpn
    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)
コード例 #18
0
ファイル: test_cookbook.py プロジェクト: meteokid/python-rpn
 def test_12qd(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', rmn.FST_RO)
     for k in rmn.fstinl(fileId, nomvar='TT', ip2=2):
         r = rmn.fstluk(k)
         print("CB12qd: %s (ip1=%s, ip2=%d) mean=%f, std=%f, min=%f, max=%f" %
               (r['nomvar'], r['ip1'], r['ip2'], r['d'].mean(), r['d'].std(), r['d'].min(), r['d'].max()))
     rmn.fstcloseall(fileId)
コード例 #19
0
ファイル: test_cookbook.py プロジェクト: wk1984/python-rpn
 def test_12qd(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', rmn.FST_RO)
     for k in rmn.fstinl(fileId, nomvar='TT', ip2=2):
         r = rmn.fstluk(k)
         print(
             "CB12qd: %s (ip1=%s, ip2=%d) mean=%f, std=%f, min=%f, max=%f" %
             (r['nomvar'], r['ip1'], r['ip2'], r['d'].mean(), r['d'].std(),
              r['d'].min(), r['d'].max()))
     rmn.fstcloseall(fileId)
コード例 #20
0
def plotTwo(paramsBase=paramsBase, paramsAlt=paramsAlt, savePath=saveLoc):
    rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)
    filePath1, spc1, area1, title1 = paramsBase['filePath'], paramsBase[
        'spc'], paramsBase['area'], paramsBase['title']
    filePath2, spc2, area2, title2 = paramsAlt['filePath'], paramsAlt[
        'spc'], paramsAlt['area'], paramsAlt['title']
    fileID1 = openFile(filePath1)
    gridKey1 = getKey(fileID1, spc1, 76696048)
    gridData1, lonData1, latData1 = getGridData(fileID1, gridKey1)
    try:
        areaType1 = paramsBase['type']
        if areaType1 == 'n':
            niRange1, njRange1 = [area1['LL'][0], area1['UR'][0]
                                  ], [area1['LL'][1], area1['UR'][1]]
        else:
            print('Getting the fuzzies!')
            LLni, LLnj = getFuzzyNiNj(area1['LL'][0], area1['LL'][1],
                                      gridData1)
            URni, URnj = getFuzzyNiNj(area1['UR'][0], area1['UR'][1],
                                      gridData1)
            niRange1, njRange1 = [LLni, URni], [LLnj, URnj]
            print(niRange1, njRange1)
    except:  #default to ninj type, only will get if lon/lat failed to get ninj values
        niRange1, njRange1 = [0, len(lonData1)], [0, len(lonData1[0])]

    fileID2 = openFile(filePath2)
    gridKey2 = getKey(fileID2, spc2, 76696048)
    gridData2, lonData2, latData2 = getGridData(fileID2, gridKey2)
    try:
        areaType2 = paramsAlt['type']
        if areaType2 == 'n':
            niRange2, njRange2 = [area2['LL'][0], area2['UR'][0]
                                  ], [area2['LL'][1], area2['UR'][1]]
        else:
            print('Getting the fuzzies again!')
            LLni, LLnj = getFuzzyNiNj(area2['LL'][0], area2['LL'][1],
                                      gridData2)
            URni, URnj = getFuzzyNiNj(area2['UR'][0], area2['UR'][1],
                                      gridData2)
            niRange2, njRange2 = [LLni, URni], [LLnj, URnj]
    except:  #default to ninj type, only will get if lon/lat failed to get ninj values
        niRange2, njRange2 = [0, len(lonData2)], [0, len(lonData2[0])]

    hyList1, concList1 = getPlotValues(fileID1, ip1List, spc1, niRange1,
                                       njRange1)
    hyList2, concList2 = getPlotValues(fileID2, ip1List, spc2, niRange2,
                                       njRange2)
    print('The ni and nj range are: [{},{}], [{},{}]'.format(
        niRange1, njRange1, niRange2, njRange2))
    plotStackVP(concList1, concList2, hyList1, hyList2, title1, title2, spc1,
                area1, savePath)
コード例 #21
0
 def test_fstecr_fstluk_order(self):
     rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)
     fname = '__rpnstd__testfile2__.fst'
     try:
         os.unlink(fname)
     except:
         pass
     funit = rmn.fstopenall(fname, rmn.FST_RW)
     (ig1, ig2, ig3, ig4) = rmn.cxgaig(self.grtyp, self.xg14[0],
                                       self.xg14[1], self.xg14[2],
                                       self.xg14[3])
     (ni, nj) = (90, 45)
     la = rmn.FST_RDE_META_DEFAULT.copy()
     la.update({
         'nomvar': 'LA',
         'typvar': 'C',
         'ni': ni,
         'nj': nj,
         'nk': 1,
         'grtyp': self.grtyp,
         'ig1': ig1,
         'ig2': ig2,
         'ig3': ig3,
         'ig4': ig4
     })
     lo = la.copy()
     lo['nomvar'] = 'LO'
     #Note: For the order to be ok in the FSTD file, order='FORTRAN' is mandatory
     la['d'] = np.empty((ni, nj), dtype=np.float32, order='FORTRAN')
     lo['d'] = np.empty((ni, nj), dtype=np.float32, order='FORTRAN')
     for j in range(nj):
         for i in range(ni):
             lo['d'][i, j] = 100. + float(i)
             la['d'][i, j] = float(j)
     rmn.fstecr(funit, la['d'], la)
     rmn.fstecr(funit, lo)
     rmn.fstcloseall(funit)
     funit = rmn.fstopenall(fname, rmn.FST_RW)
     kla = rmn.fstinf(funit, nomvar='LA')['key']
     la2 = rmn.fstluk(kla)  #,rank=2)
     klo = rmn.fstinf(funit, nomvar='LO')['key']
     lo2 = rmn.fstluk(klo)  #,rank=2)
     rmn.fstcloseall(funit)
     try:
         os.unlink(fname)
     except:
         pass
     self.assertTrue(np.isfortran(la2['d']))
     self.assertTrue(np.isfortran(lo2['d']))
     self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon))
     self.assertFalse(np.any(np.fabs(lo2['d'] - lo['d']) > self.epsilon))
コード例 #22
0
 def test_fstecr_fstluk_order(self):
     rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)
     fname = '__rpnstd__testfile2__.fst'
     try:
         os.unlink(fname)
     except:
         pass
     funit = rmn.fstopenall(fname,rmn.FST_RW)
     (ig1,ig2,ig3,ig4) = rmn.cxgaig(self.grtyp,self.xg14[0],self.xg14[1],self.xg14[2],self.xg14[3])
     (ni,nj) = (90,45)
     la = rmn.FST_RDE_META_DEFAULT.copy()
     la.update(
         {'nomvar' : 'LA',
          'typvar' : 'C',
          'ni' : ni,
          'nj' : nj,
          'nk' : 1,
          'grtyp' : self.grtyp,
          'ig1' : ig1,
          'ig2' : ig2,
          'ig3' : ig3,
          'ig4' : ig4
          }
         )
     lo = la.copy()
     lo['nomvar'] = 'LO'
     #Note: For the order to be ok in the FSTD file, order='FORTRAN' is mandatory
     la['d'] = np.empty((ni,nj),dtype=np.float32,order='FORTRAN')
     lo['d'] = np.empty((ni,nj),dtype=np.float32,order='FORTRAN')
     for j in xrange(nj):
         for i in xrange(ni):
             lo['d'][i,j] = 100.+float(i)        
             la['d'][i,j] = float(j)
     rmn.fstecr(funit,la['d'],la)
     rmn.fstecr(funit,lo)
     rmn.fstcloseall(funit)
     funit = rmn.fstopenall(fname,rmn.FST_RW)
     kla = rmn.fstinf(funit,nomvar='LA')['key']
     la2 = rmn.fstluk(kla)#,rank=2)
     klo = rmn.fstinf(funit,nomvar='LO')['key']
     lo2 = rmn.fstluk(klo)#,rank=2)
     rmn.fstcloseall(funit)
     try:
         os.unlink(fname)
     except:
         pass
     self.assertTrue(np.isfortran(la2['d']))
     self.assertTrue(np.isfortran(lo2['d']))
     self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon))
     self.assertFalse(np.any(np.fabs(lo2['d'] - lo['d']) > self.epsilon))
コード例 #23
0
ファイル: test_cookbook.py プロジェクト: wk1984/python-rpn
    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)
コード例 #24
0
ファイル: test_cookbook.py プロジェクト: wk1984/python-rpn
 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()))
コード例 #25
0
ファイル: test_cookbook.py プロジェクト: meteokid/python-rpn
    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)
コード例 #26
0
    def test_fstvoi_fstversion(self):
        """fstvoi, fst_version should give known result with known input"""
        rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)
        (la,lo) = self.create_basefile() #wrote 2 recs in that order: la, lo
        funit = rmn.fstopenall(self.fname,rmn.FST_RW)
        ## 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)
        rmn.fstvoi(funit)
        rmn.fstvoi(funit,'NINJNK+GRIDINFO')
        rmn.fstcloseall(funit)
        self.erase_testfile()

        a = rmn.fst_version()
        self.assertEqual(a,200001)
コード例 #27
0
    def test_fstvoi_fstversion(self):
        """fstvoi, fst_version should give known result with known input"""
        rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)
        (la, lo) = self.create_basefile()  #wrote 2 recs in that order: la, lo
        funit = rmn.fstopenall(self.fname, rmn.FST_RW)
        ## 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)
        rmn.fstvoi(funit)
        rmn.fstvoi(funit, 'NINJNK+GRIDINFO')
        rmn.fstcloseall(funit)
        self.erase_testfile()

        a = rmn.fst_version()
        self.assertEqual(a, 200001)
コード例 #28
0
def plotOne(params=paramsBase, savePath = saveLoc):
    rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)
    filePath, spc, area, title = params['filePath'], params['spc'], params['area'], params['title']
    fileID = openFile(filePath)
    gridKey = getKey(fileID, spc, 76696048)
    gridData, lonData, latData = getGridData(fileID, gridKey)
    try:
        areaType = params['type']
        if areaType == 'n':
            ni, nj = area[0], area[1]
        else:
            ni, nj = getFuzzyNiNj(area[0], area[1], gridData)
    except: #default to ninj type, only will get if lon/lat failed to get ninj values
        ni, nj = area[0], area[1]
    hyList, concList = getPlotValues(ip1List, spc, ni, nj)
    plotVP(concList, hyList, title, spc, savePath)
コード例 #29
0
ファイル: test_cookbook.py プロジェクト: meteokid/python-rpn
 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)
コード例 #30
0
    def test_fsteditdir_fsteff(self):
        """fst_edit_dir should give known result with known input"""
        rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)
        (la,lo) = self.create_basefile() #wrote 2 recs in that order: la, lo
        funit = rmn.fstopenall(self.fname,rmn.FST_RW)
        kla = rmn.fstinf(funit,nomvar='LA')['key']
        klo = rmn.fstinf(funit,nomvar='LO')['key']
        istat = rmn.fst_edit_dir(klo,nomvar='QW')        
        istat = rmn.fsteff(kla)
        rmn.fstcloseall(funit)

        funit = rmn.fstopenall(self.fname,rmn.FST_RO)
        kla = rmn.fstinf(funit,nomvar='LA')
        klo = rmn.fstinf(funit,nomvar='QW')['key']
        rmn.fstcloseall(funit)
        self.erase_testfile()
        self.assertEqual(kla,None,'LA found after delete: '+repr(kla))
        self.assertNotEqual(klo,None,'QW not found after rename: '+repr(klo))
コード例 #31
0
    def test_isfst_openall_dir_fstnbr(self):
        """isfst_openall__dir_fstnbr should give known result with known input"""
        rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)
        ## rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST,rmn.FSTOP_GET)

        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES')
        mydir = os.path.join(ATM_MODEL_DFILES.strip(),'bcmk/')    
        funit = rmn.fstopenall(mydir,rmn.FILE_MODE_RO)
        self.assertTrue(funit>0,'fstopenall should return a valid file unit')

        reclist = rmn.fstinl(funit,nomvar='VF')
        self.assertEqual(len(reclist),26,' fstinl of VF found %d/26 rec ' % len(reclist))

        #Note: fstnbr does not work on linked files...
        ## nrec = rmn.fstnbrv(funit)
        ## self.assertEqual(nrec,9999,' fstnbrv found %d/???? rec ' % nrec)
       
        rmn.fstcloseall(funit)
コード例 #32
0
ファイル: test_cookbook.py プロジェクト: wk1984/python-rpn
 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)
コード例 #33
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)
コード例 #34
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')
コード例 #35
0
    def test_fsteditdir_fsteff(self):
        """fst_edit_dir should give known result with known input"""
        rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)
        (la, lo) = self.create_basefile()  #wrote 2 recs in that order: la, lo
        funit = rmn.fstopenall(self.fname, rmn.FST_RW)
        kla = rmn.fstinf(funit, nomvar='LA')['key']
        klo = rmn.fstinf(funit, nomvar='LO')['key']
        istat = rmn.fst_edit_dir(klo, nomvar='QW')
        istat = rmn.fsteff(kla)
        rmn.fstcloseall(funit)

        funit = rmn.fstopenall(self.fname, rmn.FST_RO)
        kla = rmn.fstinf(funit, nomvar='LA')
        klo = rmn.fstinf(funit, nomvar='QW')['key']
        rmn.fstcloseall(funit)
        self.erase_testfile()
        self.assertEqual(kla, None, 'LA found after delete: ' + repr(kla))
        self.assertNotEqual(klo, None,
                            'QW not found after rename: ' + repr(klo))
コード例 #36
0
def concPlot(fileRun=defaultFile, modelFile=defaultPathStnd+defaultFile, savePath=defaultSave, level=defaultIp1, species=defaultSpc, buffering=buff, mapType=defaultConcCmap, reverse=False, extension='neither', projType='stere', vmin=None, vmax=None, totalBins=defaultBins, partName='test', removed=defaultClear):
    """ Execute the concentration plot creation. """

    logging.basicConfig(filename='ConcentrationPlotter.log', level=logging.DEBUG,format='%(asctime)s %(message)s')

    # print the min number of messages produced by librmn
    rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)
    # get concentration and grid data, then close file
    concData = getConc(modelFile,level,species)
    if concData == None:
       return None
    else:
       gridData = getGrid(concData['dataKey'],concData['fileID'])
       closeFST(concData['fileID'])
       # minor cleanup
       cmapDetails =cmapType(mapType, reverse)
       makeDir(savePath)
       # plot and save the figure
       plotConc(concData['concData'], gridData['lonData'], gridData['latData'], savePath, fileRun, projType, level, species, cmapDetails, totalBins, vmin, vmax, extension, partName, removed, buffering)
コード例 #37
0
    def test_isfst_openall_dir_fstnbr(self):
        """isfst_openall__dir_fstnbr should give known result with known input"""
        rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)
        ## rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST,rmn.FSTOP_GET)

        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES')
        mydir = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/')
        funit = rmn.fstopenall(mydir, rmn.FILE_MODE_RO)
        self.assertTrue(funit > 0,
                        'fstopenall should return a valid file unit')

        reclist = rmn.fstinl(funit, nomvar='VF')
        self.assertEqual(len(reclist), 26,
                         ' fstinl of VF found %d/26 rec ' % len(reclist))

        #Note: fstnbr does not work on linked files...
        ## nrec = rmn.fstnbrv(funit)
        ## self.assertEqual(nrec,9999,' fstnbrv found %d/???? rec ' % nrec)

        rmn.fstcloseall(funit)
コード例 #38
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')
コード例 #39
0
def plotOne(params=paramsBase, savePath=saveLoc):
    rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)
    filePath, spc, area, title = params['filePath'], params['spc'], params[
        'area'], params['title']
    fileID = openFile(filePath)
    gridKey = getKey(fileID, spc, 76696048)
    gridData, lonData, latData = getGridData(fileID, gridKey)
    try:
        areaType = params['type']
        if areaType == 'n':
            niRange, njRange = [area['LL'][0],
                                area['UR'][0]], [area['LL'][1], area['UR'][1]]
        else:
            LLni, LLnj = getFuzzyNiNj(area['LL'][0], area['LL'][1], gridData)
            URni, URnj = getFuzzyNiNj(area['UR'][0], area['UR'][1], gridData)
            niRange, njRange = [LLni, URni], [LLnj, URnj]
    except:  #default to ninj type, only will get if lon/lat failed to get ninj values
        niRange, njRange = [0, len(lonData)], [0, len(lonData[0])]
    hyList, concList = getPlotValues(ip1List, spc, niRange, njRange)
    print('The ni and nj range are: [{},{}]'.format(niRange, njRange))
    plotVP(concList, hyList, title, spc, area, savePath)
コード例 #40
0
    def test_fstecr_fstinf_fstluk(self):
        """fstinf, fstluk should give known result with known input"""
        rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)
        (la, lo) = self.create_basefile()  #wrote 2 recs in that order: la, lo
        a = rmn.isFST(self.fname)
        self.assertTrue(a)
        funit = rmn.fstopenall(self.fname, rmn.FST_RW)
        nrec = rmn.c_fstnbrv(funit)
        keylist = rmn.fstinl(funit)
        kla = rmn.fstinf(funit, nomvar='LA')['key']
        la2prm = rmn.fstprm(kla)  #,rank=2)
        la2 = rmn.fstluk(kla)  #,rank=2)
        klo = rmn.fstinf(funit, nomvar='LO')
        lo2 = rmn.fstluk(klo)  #,rank=2)
        rmn.fstcloseall(funit)
        self.erase_testfile()

        self.assertEqual(nrec, 2, ' c_fstnbrv found %d/2 rec ' % nrec)
        self.assertEqual(
            len(keylist), 2,
            'fstinl found %d/2 rec: %s' % (len(keylist), repr(keylist)))

        self.assertEqual(la2['nomvar'].strip(), la['nomvar'].strip())
        self.assertEqual(lo2['nomvar'].strip(), lo['nomvar'].strip())

        self.assertEqual(la2['d'].shape, la['d'].shape)
        self.assertEqual(lo2['d'].shape, lo['d'].shape)

        if np.any(np.fabs(la2['d'] - la['d']) > self.epsilon):
            print('la2:', la2['d'])
            print('la :', la['d'])
            print(np.fabs(la2['d'] - la['d']))
        self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon))
        if np.any(np.fabs(lo2['d'] - lo['d']) > self.epsilon):
            print('lo2:', lo2['d'])
            print('lo :', lo['d'])
            print(np.fabs(lo2['d'] - lo['d']))
        self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon))
コード例 #41
0
    def test_isfst_openall_fstnbr(self):
        """isfst_openall_fstnbr should give known result with known input"""
        rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)
        ## rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST,rmn.FSTOP_GET)

        HOME = os.getenv('HOME')
        a = rmn.isFST(os.path.join(HOME.strip(),'.profile'))
        self.assertFalse(a,'isFST should return false on non FST files')

        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES')
        myfile = os.path.join(ATM_MODEL_DFILES.strip(),'bcmk/2009042700_000')
        a = rmn.isFST(myfile)
        self.assertTrue(a,'isFST should return true on FST files')
    
        funit = rmn.fstopenall(myfile,rmn.FST_RO)
        self.assertTrue(funit>0,'fstopenall should return a valid file unit')

        nrec = rmn.c_fstnbrv(funit)
        self.assertEqual(nrec,1083,' c_fstnbrv found %d/1083 rec ' % nrec)
        
        nrec = rmn.fstnbrv(funit)
        self.assertEqual(nrec,1083,' fstnbrv found %d/1083 rec ' % nrec)
       
        rmn.fstcloseall(funit)
コード例 #42
0
    def test_fstecr_fstinf_fstluk(self):
        """fstinf, fstluk should give known result with known input"""
        rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)
        (la,lo) = self.create_basefile() #wrote 2 recs in that order: la, lo
        a = rmn.isFST(self.fname)
        self.assertTrue(a)
        funit = rmn.fstopenall(self.fname,rmn.FST_RW)
        nrec = rmn.c_fstnbrv(funit)
        keylist = rmn.fstinl(funit)
        kla = rmn.fstinf(funit,nomvar='LA')['key']
        la2prm = rmn.fstprm(kla)#,rank=2)
        la2 = rmn.fstluk(kla)#,rank=2)
        klo = rmn.fstinf(funit,nomvar='LO')
        lo2 = rmn.fstluk(klo)#,rank=2)
        rmn.fstcloseall(funit)
        self.erase_testfile()

        self.assertEqual(nrec,2,' c_fstnbrv found %d/2 rec ' % nrec)
        self.assertEqual(len(keylist),2,'fstinl found %d/2 rec: %s' % (len(keylist),repr(keylist)))

        self.assertEqual(la2['nomvar'].strip(),la['nomvar'].strip())
        self.assertEqual(lo2['nomvar'].strip(),lo['nomvar'].strip())
        
        self.assertEqual(la2['d'].shape,la['d'].shape)
        self.assertEqual(lo2['d'].shape,lo['d'].shape)

        if np.any(np.fabs(la2['d'] - la['d']) > self.epsilon):
                print('la2:',la2['d'])
                print('la :',la['d'])
                print(np.fabs(la2['d'] - la['d']))
        self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon))
        if np.any(np.fabs(lo2['d'] - lo['d']) > self.epsilon):
                print('lo2:',lo2['d'])
                print('lo :',lo['d'])
                print(np.fabs(lo2['d'] - lo['d']))
        self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon))
コード例 #43
0
def plotFSTs(season=season, spcs=spcs, spcsFiles=spcsFiles, outputName = outputName, saveDir=saveDir):
    # print minimum outputs
    rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)

    mInds = []
    for m in season:
        mInds += [monthList.index(m)]

    if os.path.exists(saveDir) == False:
        nu.makeDir(saveDir)

    for spcInd, nomvar in enumerate(spcs):
        try:
            filename = os.path.join(saveDir, 'output_file_{0}_{1}.fst'.format(outputName, nomvar))
            print('Creating and saving to {}'.format(filename))
            tmp = open(filename, 'w+'); tmp.close()

            output_file = filename
            file_id = rmn.fnom(output_file)
            open_fst = rmn.fstouv(file_id, rmn.FST_RW)
            open_file = spcsFiles[spcInd]
            print "Parameter: " + nomvar
            seaSpcData = get_var(pyg.open(open_file), nomvar, mInds)
            nc_lnsp = pyg.open(lnsp_file)
            pressures = get_pressures(nc_lnsp, mInds)

            timelen, levlen, latlen, lonlen = seaSpcData.shape
            #NOTE: uncomment the following three lines to prep data for basemap use
            #lonShiftSSData = shift_lon(seaSpcData)
            #vertInterpSSData = vert_interp(pressures, lonShiftSSData)
            #meanSSData = np.mean(vertInterpSSData, axis=0)
            #NOTE: uncommment the following four liness to use for fst plotting
            vertInterpSSData = vert_interp(pressures, seaSpcData)
            meanSSData = np.mean(vertInterpSSData, axis=0)  # temp
            for lvl, ray in enumerate(meanSSData):
                meanSSData[lvl] = np.flipud(ray)
            scaleFac = scaleSpcs[allSpcs.index(nomvar)]
            scaledSSData = meanSSData*scaleFac

            #define grid for this file - note that the MACC grid in the file is
            #defined for lons -180 to 180, but the python defGrid_L can't deal
            #with that and defines the grid from 0 to 360 so will have to reorder
            #the MACC fields a bit, or they end up 180 deg out of phase
            # Also necessary to add one more longitude to wrap around
            dlatlon = 360./lonlen   # this is equal to the resolution of the grid

            params0 = {
                    'grtyp' : 'Z',
                    'grref' : 'L',
                    'nj'    : latlen,
                    'ni'    : lonlen,
                    'lat0'  : -90.,
                    'lon0'  : 0.,
                    'dlat'  : dlatlon,
                    'dlon'  : dlatlon
                    }

            MACC_grid= rmn.encodeGrid(params0)
            print("Grids created.")
            print 'Grid Shape:' + str(MACC_grid['shape'])

            # copies the default record
            new_record = rmn.FST_RDE_META_DEFAULT.copy()
            tic_record = rmn.FST_RDE_META_DEFAULT.copy()
            tac_record = rmn.FST_RDE_META_DEFAULT.copy()

            try:
                rmn.writeGrid(file_id, MACC_grid)

                tac = rmn.fstinl(file_id, nomvar='>>')[0]
                tic = rmn.fstinl(file_id, nomvar='^^')[0]

                tic_record.update(rmn.fstprm(tic))
                tac_record.update(rmn.fstprm(tac))

                tic_record.update({'datyp' : rmn.FST_DATYP_LIST['float']})
                tac_record.update({'datyp' : rmn.FST_DATYP_LIST['float']})

                rmn.fsteff(tic)
                rmn.fsteff(tac)

                tic_record.update({'d': MACC_grid['ay']})
                tac_record.update({'d': MACC_grid['ax']})
                toc_record = vgd.vgd_new_pres(const_pressure, ip1=MACC_grid['ig1'], ip2=MACC_grid['ig2'])

                rmn.fstecr(file_id, tic_record)  # write the dictionary record to the file as a new record
                rmn.fstecr(file_id, tac_record)  # write the dictionary record to the file as a new record
                vgd.vgd_write(toc_record, file_id)

            except:
                raise

            for rp1 in xrange(len(const_pressure)):  # writes a record for every level (as a different ip1)
                try:
                    # converts rp1 into a ip1 with pressure kind
                    ip1 = rmn.convertIp(rmn.CONVIP_ENCODE, const_pressure[rp1], rmn.KIND_PRESSURE)
                    new_record.update(MACC_grid)
                    new_record.update({  # Update with specific meta
                        'nomvar': nomvar,
                        'typvar': 'C',
                        'etiket': 'MACCRean',
                        'ni'    : MACC_grid['ni'],
                        'nj'    : MACC_grid['nj'],
                        'ig1'   : tic_record['ip1'],
                        'ig2'   : tic_record['ip2'],
                        'ig3'   : tic_record['ip3'],
                        'ig4'   : tic_record['ig4'],
                        'dateo' : rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20120101, 0000000),
                        'deet'  : 0,  # Timestep in sec
                        'ip1'   : ip1
                        })

                    #tmp_nparray = np.asfortranarray(monthly_mean[rp1])
                    tmp = scaledSSData[rp1]
                    tmp = np.transpose(tmp)
                    # data array is structured as tmp = monthly_mean[level] where monthly_mean is [level, lat, lon]
                    new_record.update({'d': tmp.astype(np.float32)}) # Updates with data array in the form (lon x lat)

                    print "Defined a new record with dimensions ({0}, {1})".format(new_record['ni'], new_record['nj'])
                    rmn.fstecr(file_id, new_record)  # write the dictionary record to the file as a new record

                except:
                    #rmn.closeall(file_id)
                    rmn.fstfrm(file_id)
                    rmn.fclos(file_id)
                    raise
            rmn.fstfrm(file_id)
            rmn.fclos(file_id)
            print('{} complete~'.format(filename))
        except:
            rmn.fstfrm(file_id)
            rmn.fclos(file_id)
            raise
    print('Finished plotting all FSTs. ')
コード例 #44
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)
コード例 #45
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
コード例 #46
0
ファイル: test_cookbook.py プロジェクト: meteokid/python-rpn
    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()))
コード例 #47
0
ファイル: test_cookbook.py プロジェクト: meteokid/python-rpn
    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
コード例 #48
0
ファイル: test_cookbook2.py プロジェクト: guziy/python-rpn
    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
コード例 #49
0
ファイル: test_cookbook.py プロジェクト: wk1984/python-rpn
    def test_24(self):
        """
        Edit: New file from scratch

        This example shows how to
        * Create a record meta and data from scratch
        * Create grid descriptors for the data
        * Create vgrid descriptor for the data
        * write the recod data + meta
        * write the grid descriptors
        * write the vgrid descriptor

        See also:
        rpnpy.librmn.fstd98.fstopt
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.fstd98.fsrecr
        rpnpy.librmn.fstd98.dtype_fst2numpy
        rpnpy.librmn.grids.encodeGrid
        rpnpy.librmn.grids.defGrid_ZE
        rpnpy.librmn.grids.writeGrid
        rpnpy.librmn.base.newdate
        rpnpy.vgd.base.vgd_new
        rpnpy.vgd.base.vgd_new_pres
        rpnpy.vgd.base.vgd_new_get
        rpnpy.vgd.base.vgd_write
        rpnpy.librmn.const
        rpnpy.vgd.const
        """
        import os, sys
        import numpy as np
        import rpnpy.librmn.all as rmn
        import rpnpy.vgd.all as vgd

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

        # Create Record grid
        gp = {
            'grtyp': 'Z',
            'grref': 'E',
            'ni': 90,
            'nj': 45,
            'lat0': 35.,
            'lon0': 250.,
            'dlat': 0.5,
            'dlon': 0.5,
            'xlat1': 0.,
            'xlon1': 180.,
            'xlat2': 1.,
            'xlon2': 270.
        }
        g = rmn.encodeGrid(gp)
        print("CB24: Defined a %s/%s grid of shape=%d, %d" %
              (gp['grtyp'], gp['grref'], gp['ni'], gp['nj']))

        # Create Record vgrid
        lvls = (500., 850., 1000.)
        v = vgd.vgd_new_pres(lvls)
        ip1list = vgd.vgd_get(v, 'VIPT')
        print("CB24: Defined a Pres vgrid with lvls=%s" % str(lvls))

        # Create Record data + meta
        datyp = rmn.FST_DATYP_LIST['float_IEEE_compressed']
        npdtype = rmn.dtype_fst2numpy(datyp)
        rshape = (g['ni'], g['nj'], len(ip1list))
        r = rmn.FST_RDE_META_DEFAULT.copy()  # Copy default record meta
        r.update(g)  # Update with grid info
        r.update({  # Update with specific meta and data array
            'nomvar':
            'MASK',
            'dateo':
            rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20160302, 1800000),
            'nk':
            len(ip1list),
            'ip1':
            0,  # level, will be set later, level by level
            'ip2':
            6,  # Forecast of 6h
            'deet':
            3600,  # Timestep in sec
            'npas':
            6,  # Step number
            'etiket':
            'my_etk',
            'nbits':
            32,  # Keep full 32 bits precision for that field
            'datyp':
            datyp,  # datyp (above) float_IEEE_compressed
            'd':
            np.empty(rshape, dtype=npdtype, order='FORTRAN')
        })
        print("CB24: Defined a new record of shape=%d, %d" %
              (r['ni'], r['nj']))

        # Compute record values
        r['d'][:, :, :] = 0.
        r['d'][10:-11, 5:-6, :] = 1.

        # Open Files
        fileNameOut = 'newfromscratch.fst'
        try:
            fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW)
        except:
            sys.stderr.write("Problem opening the file: %s, %s\n" %
                             fileNameOut)
            sys.exit(1)

        # Write record data + meta + grid + vgrid to file
        try:
            r2d = r.copy()
            r2d['nk'] = 1
            for k in range(len(ip1list)):
                r2d['ip1'] = ip1list[k]
                r2d['d'] = np.asfortranarray(r['d'][:, :, k])
                rmn.fstecr(fileIdOut, r2d['d'], r2d)
                print("CB24: wrote %s at ip1=%d" % (r2d['nomvar'], r2d['ip1']))
            rmn.writeGrid(fileIdOut, g)
            print("CB24: wrote the grid descriptors")
            vgd.vgd_write(v, fileIdOut)
            print("CB24: wrote the vgrid descriptor")
        except:
            raise
        finally:
            # Properly close files even if an error occured above
            # This is important when editing to avoid corrupted files
            rmn.fstcloseall(fileIdOut)
            os.unlink(fileNameOut)  # Remove test file
コード例 #50
0
ファイル: test_cookbook2.py プロジェクト: guziy/python-rpn
    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)
コード例 #51
0
ファイル: test_cookbook.py プロジェクト: meteokid/python-rpn
    def test_24(self):
        """
        Edit: New file from scratch

        This example shows how to
        * Create a record meta and data from scratch
        * Create grid descriptors for the data
        * Create vgrid descriptor for the data
        * write the recod data + meta
        * write the grid descriptors
        * write the vgrid descriptor

        See also:
        rpnpy.librmn.fstd98.fstopt
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.fstd98.fsrecr
        rpnpy.librmn.fstd98.dtype_fst2numpy
        rpnpy.librmn.grids.encodeGrid
        rpnpy.librmn.grids.defGrid_ZE
        rpnpy.librmn.grids.writeGrid
        rpnpy.librmn.base.newdate
        rpnpy.vgd.base.vgd_new
        rpnpy.vgd.base.vgd_new_pres
        rpnpy.vgd.base.vgd_new_get
        rpnpy.vgd.base.vgd_write
        rpnpy.librmn.const
        rpnpy.vgd.const
        """
        import os, sys
        import numpy as np
        import rpnpy.librmn.all as rmn
        import rpnpy.vgd.all as vgd
        
        # Restrict to the minimum the number of messages printed by librmn
        rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)

        # Create Record grid
        gp = {
            'grtyp' : 'Z',
            'grref' : 'E',
            'ni'    : 90,
            'nj'    : 45,
            'lat0'  : 35.,
            'lon0'  : 250.,
            'dlat'  : 0.5,
            'dlon'  : 0.5,
            'xlat1' : 0.,
            'xlon1' : 180.,
            'xlat2' : 1.,
            'xlon2' : 270.
        }
        g = rmn.encodeGrid(gp)
        print("CB24: Defined a %s/%s grid of shape=%d, %d" %
              (gp['grtyp'], gp['grref'], gp['ni'], gp['nj']))
            
        # Create Record vgrid
        lvls = (500.,850.,1000.)
        v = vgd.vgd_new_pres(lvls)
        ip1list = vgd.vgd_get(v, 'VIPT')
        print("CB24: Defined a Pres vgrid with lvls=%s" % str(lvls))
        
        # Create Record data + meta
        datyp   = rmn.FST_DATYP_LIST['float_IEEE_compressed']
        npdtype = rmn.dtype_fst2numpy(datyp)
        rshape  = (g['ni'], g['nj'], len(ip1list))
        r = rmn.FST_RDE_META_DEFAULT.copy() # Copy default record meta
        r.update(g)                         # Update with grid info
        r.update({                          # Update with specific meta and data array
            'nomvar': 'MASK',
            'dateo' : rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20160302, 1800000),
            'nk'    : len(ip1list),
            'ip1'   : 0,     # level, will be set later, level by level
            'ip2'   : 6,     # Forecast of 6h
            'deet'  : 3600,  # Timestep in sec
            'npas'  : 6,     # Step number
            'etiket': 'my_etk',
            'nbits' : 32,    # Keep full 32 bits precision for that field
            'datyp' : datyp, # datyp (above) float_IEEE_compressed
            'd'     : np.empty(rshape, dtype=npdtype, order='FORTRAN')
            })
        print("CB24: Defined a new record of shape=%d, %d" % (r['ni'], r['nj']))

        
        # Compute record values
        r['d'][:,:,:] = 0.
        r['d'][10:-11,5:-6,:] = 1.
        
        # Open Files
        fileNameOut = 'newfromscratch.fst'
        try:
            fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW)
        except:
            sys.stderr.write("Problem opening the file: %s, %s\n" % fileNameOut)
            sys.exit(1)

        # Write record data + meta + grid + vgrid to file
        try:
            r2d = r.copy()
            r2d['nk']  = 1
            for k in range(len(ip1list)):
                r2d['ip1'] = ip1list[k]
                r2d['d'] = np.asfortranarray(r['d'][:,:,k])
                rmn.fstecr(fileIdOut, r2d['d'], r2d)
                print("CB24: wrote %s at ip1=%d" % (r2d['nomvar'], r2d['ip1']))
            rmn.writeGrid(fileIdOut, g)
            print("CB24: wrote the grid descriptors")
            vgd.vgd_write(v, fileIdOut)
            print("CB24: wrote the vgrid descriptor")
        except:
            raise
        finally:
            # Properly close files even if an error occured above
            # This is important when editing to avoid corrupted files
            rmn.fstcloseall(fileIdOut)
            os.unlink(fileNameOut)  # Remove test file
コード例 #52
0
    parser.add_argument("-v", "--verbose", dest="verbose",
                        action="count", default=0,
                        help="Increase output verbosity")
    parser.add_argument("-i", "--input", dest="inputFile",
                        nargs='+', required=True, type=str, default=[],
                        metavar='FILENAME.txt',
                        help="Input Text File name")
    parser.add_argument("-o", "--output", dest="outputFile",
                        required=True, type=str, default=None,
                        metavar='FILENAME.fst',
                        help="Output RPN Std File name")
    args = parser.parse_args()


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

    if args.verbose:
        sys.stdout.write("Opening the output file: {}\n".
                         format(args.outputFile))
    try:
        fileIdOut = rmn.fstopenall(args.outputFile, rmn.FST_RW)
    except:
        sys.stderr.write("ERROR: Problem opening the file: {}\n".
                         format(args.outputFile))
        sys.exit(1)

    status = 0
    for filename in args.inputFile:
        try:
            txtRec = txtFile2Rec(filename, args.verbose)
コード例 #53
0
ファイル: fst_to_lalo.py プロジェクト: guziy/python-rpn
    (options,args) = parser.parse_args()
    if not (options.varname and options.fstfile and options.outfile and options.lolafile and options.inttype):
        sys.stderr.write('Error: You need to specify a varname, an fst filename, an outfile name and a lolafile name.\n')
        parser.print_help()
        sys.exit(1)

    inttype = options.inttype[0].lower()
    if not (inttype in inttypelist.keys()):
        sys.stderr.write('Error: INTTYPE should be one of: nearest, linear or cubic.\n')
        parser.print_help()
        sys.exit(1)

    # Open and Read RPN STD file        
    try:
        rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)
        funit = rmn.fstopenall(options.fstfile,rmn.FST_RO)
        k = rmn.fstinf(funit,nomvar=options.varname)['key']
        data = rmn.fstluk(k)['d']
        meta = rmn.fstprm(k)
    except:
        raise rmn.RMNError('Problem opening/reading var=%s in File=%s' % (options.varname,options.fstfile))

    # Define input record grid
    try:
        meta['iunit'] = funit
        grid = rmn.ezqkdef(meta)
    except:
        raise rmn.RMNError('Problem defining input grid for var=%s in File=%s' % (options.varname,options.fstfile))

    # Read lat lon file