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)
def get_grid_descriptors(file_id):
    '''
    gets grid descriptors from a fst file
    '''

    tic_record = rmn.FST_RDE_META_DEFAULT.copy()
    tac_record = rmn.FST_RDE_META_DEFAULT.copy()
    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))

    return tic_record, tac_record
Exemple #3
0
def fstinf(iunit, nomvar, typvar, etiket, ip1, ip2, ip3, datev, inhandle):
    """Find a record matching provided criterias (Interface to fstinf, dsfsui, fstinfx)
    recParamDict = Fstdc.fstinf(iunit, nomvar, typvar, etiket, ip1, ip2, ip3, datev, inhandle)
    @param iunit file unit number handle returned by Fstdc_fstouv (int)
    @param nomvar select according to var name, blank==wildcard (string)
    @param typvar select according to var type, blank==wildcard (string)
    @param etiket select according to etiket, blank==wildcard (string)
    @param ip1 select according to ip1, -1==wildcard  (int)
    @param ip2 select according to ip2, -1==wildcard (int)
    @param ip3  select according to ip3, -1==wildcard (int)
    @param datev select according to date of validity, -1==wildcard (int)
    @param inhandle selcation criterion; inhandle=-2:search with criterion from start of file; inhandle=-1==fstsui, use previously provided criterion to find the next matching one; inhandle>=0 search with criterion from provided rec-handle (int)
    @returns python dict with record handle + record params keys/values
    @exception TypeError
    @exception Fstdc.error
    """
    if inhandle < -1:
        mymatch = _rmn.fstinf(iunit, datev, etiket, ip1, ip2, ip3, typvar, nomvar)
    elif inhandle == -1:
        mymatch = _rmn.fstsui(iunit)
    else:
        mymatch = _rmn.fstinfx(inhandle, iunit, datev, etiket, ip1, ip2, ip3, typvar, nomvar)
    if not mymatch:
        raise error('No matching record')
    recParams = _rmn.fstprm(mymatch['key'])
    recParams['handle'] = recParams['key']
    recParams['nom']    = recParams['nomvar']
    recParams['type']   = recParams['typvar']
    recParams['datev']  = recParams['xtra1']  #TODO: Keep Fstdc original bug?
    return recParams
Exemple #4
0
def fstinf(iunit, nomvar, typvar, etiket, ip1, ip2, ip3, datev, inhandle):
    """Find a record matching provided criterias (Interface to fstinf, dsfsui, fstinfx)
    recParamDict = Fstdc.fstinf(iunit, nomvar, typvar, etiket, ip1, ip2, ip3, datev, inhandle)
    @param iunit file unit number handle returned by Fstdc_fstouv (int)
    @param nomvar select according to var name, blank==wildcard (string)
    @param typvar select according to var type, blank==wildcard (string)
    @param etiket select according to etiket, blank==wildcard (string)
    @param ip1 select according to ip1, -1==wildcard  (int)
    @param ip2 select according to ip2, -1==wildcard (int)
    @param ip3  select according to ip3, -1==wildcard (int)
    @param datev select according to date of validity, -1==wildcard (int)
    @param inhandle selcation criterion; inhandle=-2:search with criterion from start of file; inhandle=-1==fstsui, use previously provided criterion to find the next matching one; inhandle>=0 search with criterion from provided rec-handle (int)
    @returns python dict with record handle + record params keys/values
    @exception TypeError
    @exception Fstdc.error
    """
    if inhandle < -1:
        mymatch = _rmn.fstinf(iunit, datev, etiket, ip1, ip2, ip3, typvar, nomvar)
    elif inhandle == -1:
        mymatch = _rmn.fstsui(iunit)
    else:
        mymatch = _rmn.fstinfx(inhandle, iunit, datev, etiket, ip1, ip2, ip3, typvar, nomvar)
    if not mymatch:
        raise error('No matching record')
    recParams = _rmn.fstprm(mymatch['key'])
    recParams['handle'] = recParams['key']
    recParams['nom']    = recParams['nomvar']
    recParams['type']   = recParams['typvar']
    recParams['datev']  = recParams['xtra1']  #TODO: Keep Fstdc original bug?
    return recParams
Exemple #5
0
def fstinl(iunit, nomvar, typvar, etiket, ip1, ip2, ip3, datev):
    """Find all records matching provided criterias (Interface to fstinl)
    Warning: list is limited to the first 50000 records in a file, subsequent matches raises Fstdc.tooManyRecError and are ignored.
    recList = Fstdc.fstinl(iunit, nomvar, typvar, etiket, ip1, ip2, ip3, datev)
    param iunit file unit number handle returned by Fstdc_fstouv (int)
    @param nomvar select according to var name, blank==wildcard (string)
    @param typvar select according to var type, blank==wildcard (string)
    @param etiket select according to etiket, blank==wildcard (string)
    @param ip1 select according to ip1, -1==wildcard  (int)
    @param ip2 select according to ip2, -1==wildcard (int)
    @param ip3  select according to ip3, -1==wildcard (int)
    @param datev select according to date of validity, -1==wildcard (int)
    @returns python dict with handles+params of all matching records
    @exception TypeError
    @exception Fstdc.error
    @exception Fstdc.tooManyRecError
    """
    try:
        keylist = _rmn.fstinl(iunit, datev, etiket, ip1, ip2, ip3, typvar, nomvar)
    except:
        raise error('Problem getting record list')
    recParamList = []
    for k in keylist:
        recParams = _rmn.fstprm(k)
        recParams['handle'] = k
        recParams['nom']    = recParams['nomvar']
        recParams['type']   = recParams['typvar']
        recParams['datev']  = recParams['xtra1'] #TODO: Keep Fstdc original bug?
        recParamList.append(recParams)
    return recParamList
def getGrid(dataKey, fileID):
    """ Get the gridID for an FST file. Can be used to get more grid details. """

    fileMeta = rmn.fstprm(dataKey)
    fileMeta['iunit'] = fileID
    gridID = rmn.ezqkdef(fileMeta)
    return gridID
Exemple #7
0
def fstinl(iunit, nomvar, typvar, etiket, ip1, ip2, ip3, datev):
    """Find all records matching provided criterias (Interface to fstinl)
    Warning: list is limited to the first 50000 records in a file, subsequent matches raises Fstdc.tooManyRecError and are ignored.
    recList = Fstdc.fstinl(iunit, nomvar, typvar, etiket, ip1, ip2, ip3, datev)
    param iunit file unit number handle returned by Fstdc_fstouv (int)
    @param nomvar select according to var name, blank==wildcard (string)
    @param typvar select according to var type, blank==wildcard (string)
    @param etiket select according to etiket, blank==wildcard (string)
    @param ip1 select according to ip1, -1==wildcard  (int)
    @param ip2 select according to ip2, -1==wildcard (int)
    @param ip3  select according to ip3, -1==wildcard (int)
    @param datev select according to date of validity, -1==wildcard (int)
    @returns python dict with handles+params of all matching records
    @exception TypeError
    @exception Fstdc.error
    @exception Fstdc.tooManyRecError
    """
    try:
        keylist = _rmn.fstinl(iunit, datev, etiket, ip1, ip2, ip3, typvar, nomvar)
    except:
        raise error('Problem getting record list')
    recParamList = []
    for k in keylist:
        recParams = _rmn.fstprm(k)
        recParams['handle'] = k
        recParams['nom']    = recParams['nomvar']
        recParams['type']   = recParams['typvar']
        recParams['datev']  = recParams['xtra1'] #TODO: Keep Fstdc original bug?
        recParamList.append(recParams)
    return recParamList
Exemple #8
0
    def test_2(self):
        """
        Find Record / Get Metadata

        Most librmn FSTD functions are supported to look for records matching the provided selection criterion.
        Criteria not specified are not used for selection (wildcard).
        The fstprm function can then be used to get the record metadata.

        See also:
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstinf
        rpnpy.librmn.fstd98.fstinfx
        rpnpy.librmn.fstd98.fstinl
        rpnpy.librmn.fstd98.fstsui
        rpnpy.librmn.fstd98.fstprm
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.fstd98.FSTDError
        rpnpy.librmn.RMNError
        rpnpy.librmn.const
        """
        import os
        import rpnpy.librmn.all as rmn

        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES')
        fileName = os.path.join(ATM_MODEL_DFILES.strip(),
                                'bcmk/2009042700_012')

        # Open
        try:
            fileId = rmn.fstopenall(fileName, rmn.FST_RO)
        except:
            raise rmn.FSTDError("File not found/readable: %s" % fileName)

        # Find
        try:
            k1 = rmn.fstinf(fileId)[
                'key']  #No criterion, this match the first rec in the file
            pr_key = rmn.fstinf(
                fileId,
                nomvar='PR')['key']  #this match the first record named PR
            # pr_key2 = rmn.fstinfx(pr_key, fileId, nomvar='PR')['key']  #this would match the next  record named PR
        except:
            raise rmn.FSTDError("Problem searching in File: %s" % fileName)

        if not pr_key:
            raise rmn.FSTDError("Record not found in File: %s" % fileName)

        # Read Metadata
        try:
            pr_meta = rmn.fstprm(pr_key)
            for k in ('nomvar', 'dateo', 'npas'):
                print("%s = %s" % (k, str(pr_meta[k])))
        except:
            raise rmn.FSTDError("Error: Problem getting record metadata")

        # ...

        # Close
        rmn.fstcloseall(fileId)
def data_from_key(key, file_id, lonarr, latarr):
    meta = rmn.fstprm(key)
    meta['iunit'] = file_id
    data = rmn.fstluk(key)['d']
    grid = rmn.ezqkdef(meta)
    xypos = rmn.gdxyfll(grid, latarr, lonarr)
    val = rmn.gdxysval(grid, xypos['x'], xypos['y'], data)
    return meta, grid, xypos, val
Exemple #10
0
def getGridData(fileID, key):
    fileMeta = rmn.fstprm(key)
    fileMeta['iunit'] = fileID
    gridMeta = rmn.ezqkdef(fileMeta)
    gridData = rmn.gdll(gridMeta)
    lonData = gridData['lon'].tolist()
    latData = gridData['lat'].tolist()
    return gridData, lonData, latData
Exemple #11
0
    def read_dateo_npas(self,fname,vname):
        'Read date of original analysis and time-step number'

        iunit = rmn.fstopenall(fname, rmn.FST_RO)
        key   = rmn.fstinf(iunit, nomvar=vname)['key']
        recmeta = rmn.fstprm(key)
        rmn.fstcloseall(iunit)

        return recmeta['datev'],recmeta['dateo'],recmeta['deet'],recmeta['npas']
Exemple #12
0
    def read_dateo_npas(self, fname, vname):
        'Read date of original analysis and time-step number'

        iunit = rmn.fstopenall(fname, rmn.FST_RO)
        key = rmn.fstinf(iunit, nomvar=vname)['key']
        recmeta = rmn.fstprm(key)
        rmn.fstcloseall(iunit)

        return recmeta['datev'], recmeta['dateo'], recmeta['deet'], recmeta[
            'npas']
Exemple #13
0
def getGrid(dataKey):

    fileMeta = rmn.fstprm(dataKey)
    fileMeta['iunit'] = fileID #add to dictionary as key,val pair
    gridData = rmn.ezqkdef(fileMeta)
    gridDecode = rmn.decodeGrid(gridData) #use this for information, not really needed if just getting all lat lon values, but can get bottom left corner if needed for something else
    llGridData = rmn.gdll(gridData) # is a dictionary of two arrays for lat and lon
    latData = llGridData['lat']
    lonData = llGridData['lon']
    return latData, lonData
    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)
Exemple #15
0
    def test_2(self):
        """
        Find Record / Get Metadata

        Most librmn FSTD functions are supported to look for records matching the provided selection criterion.
        Criteria not specified are not used for selection (wildcard).
        The fstprm function can then be used to get the record metadata.

        See also:
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstinf
        rpnpy.librmn.fstd98.fstinfx
        rpnpy.librmn.fstd98.fstinl
        rpnpy.librmn.fstd98.fstsui
        rpnpy.librmn.fstd98.fstprm
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.fstd98.FSTDError
        rpnpy.librmn.RMNError
        rpnpy.librmn.const
        """
        import os
        import rpnpy.librmn.all as rmn

        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES')
        fileName = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012')

        # Open
        try:
            fileId = rmn.fstopenall(fileName, rmn.FST_RO)
        except:
            raise rmn.FSTDError("File not found/readable: %s" % fileName)

        # Find
        try:
            k1      = rmn.fstinf(fileId)['key']                    #No criterion, this match the first rec in the file
            pr_key  = rmn.fstinf(fileId, nomvar='PR')['key']       #this match the first record named PR
            # pr_key2 = rmn.fstinfx(pr_key, fileId, nomvar='PR')['key']  #this would match the next  record named PR
        except:
            raise rmn.FSTDError("Problem searching in File: %s" % fileName)

        if not pr_key:
            raise rmn.FSTDError("Record not found in File: %s" % fileName)

        # Read Metadata
        try:
            pr_meta = rmn.fstprm(pr_key)
            for k in ('nomvar', 'dateo', 'npas'):
                print("%s = %s" % (k, str(pr_meta[k])))
        except:
            raise rmn.FSTDError("Error: Problem getting record metadata")

        # ...

        # Close
        rmn.fstcloseall(fileId)
Exemple #16
0
    def test_11(self):
        """
        Queries: Find records, Read meta
        
        This example shows how to
        * get the list of all records matching selection creterions
        * get the metadata and decode it
        * then print some values
        
        See also:
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstinl
        rpnpy.librmn.fstd98.fstprm
        rpnpy.librmn.fstd98.DecodeIp
        rpnpy.librmn.fstd98.kindToString
        rpnpy.librmn.base.newdate
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.const
        """
        import os, sys
        import rpnpy.librmn.all as rmn

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

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

            for k in keylist:

                # Get the record meta data
                m = rmn.fstprm(k)

                # Decode ip1, and dateo
                (rp1, rp2, rp3) = rmn.DecodeIp(m['ip1'], m['ip2'], m['ip3'])
                level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind))
                (yyyymmdd, hhmmsshh) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT,
                                                   m['dateo'])
                dateo = "%8.8d.%8.8d" % (yyyymmdd, hhmmsshh)
                nhours = float(m['npas'] * m['deet']) / 3600.

                print("CB11: %s (%d, %d) level=%s, dateo=%s + %s" %
                      (m['nomvar'], m['ip1'], m['ip2'], level, dateo, nhours))
        except:
            pass
        finally:
            # Close file even if an error occured above
            rmn.fstcloseall(fileId)
Exemple #17
0
    def test_11(self):
        """
        Queries: Find records, Read meta
        
        This example shows how to
        * get the list of all records matching selection creterions
        * get the metadata and decode it
        * then print some values
        
        See also:
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstinl
        rpnpy.librmn.fstd98.fstprm
        rpnpy.librmn.fstd98.DecodeIp
        rpnpy.librmn.fstd98.kindToString
        rpnpy.librmn.base.newdate
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.const
        """
        import os, sys
        import rpnpy.librmn.all as rmn

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

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

                # Decode ip1, and dateo
                (rp1, rp2, rp3) = rmn.DecodeIp(m['ip1'], m['ip2'], m['ip3'])
                level  = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind))
                (yyyymmdd,hhmmsshh) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT,m['dateo'])
                dateo  = "%8.8d.%8.8d" % (yyyymmdd,hhmmsshh)
                nhours = float(m['npas'] * m['deet']) / 3600.
                
                print("CB11: %s (%d, %d) level=%s, dateo=%s + %s" %
                      (m['nomvar'], m['ip1'], m['ip2'], level, dateo, nhours))
        except:
            pass
        finally:
            # Close file even if an error occured above
            rmn.fstcloseall(fileId)
Exemple #18
0
def getGrid(dataKey, fileID):
    try:
        fileMeta = rmn.fstprm(dataKey)
        fileMeta['iunit'] = fileID
        gridData = rmn.ezqkdef(fileMeta)
        llGridData = rmn.gdll(gridData)
        latData = llGridData['lat'].tolist()
        lonData = llGridData['lon'].tolist()
        return lonData, latData, gridData
    except:
        print('ERROR: Could not get grid. Please try again.')
Exemple #19
0
 def test_11bqd(self):
     import os, sys
     import rpnpy.librmn.all as rmn
     ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
     fileId = rmn.fstopenall(ATM_MODEL_DFILES+'/bcmk_p/anlp2015070706_000', rmn.FST_RO)
     for k in rmn.fstinl(fileId, nomvar='TT',
                         ip1=rmn.ip1_all(500., rmn.LEVEL_KIND_PMB),
                         datev=rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20150707, 6000000)):
             m = rmn.fstprm(k)
             print("CB11bqd: %s (%d, %d, %s)" % (m['nomvar'], m['ip1'], m['ip2'], m['datev']))
     rmn.fstcloseall(fileId)
Exemple #20
0
    def test_11b(self):
        """
        Queries: List/Find records

        This example shows how to
        * get the list of all records matching selection creterions
        * encode parameters (ip1, datev) values to use selection criteria
                
        See also:
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.ip1_all
        rpnpy.librmn.fstd98.ip2_all
        rpnpy.librmn.fstd98.fstinl
        rpnpy.librmn.fstd98.fstprm
        rpnpy.librmn.fstd98.DecodeIp
        rpnpy.librmn.fstd98.kindToString
        rpnpy.librmn.base.newdate
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.const
        """
        import os, sys
        import rpnpy.librmn.all as rmn

        # Open all RPNStd files in $ATM_MODEL_DFILES/bcmk_p/anlp2015070706_000
        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
        fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk_p',
                                'anlp2015070706_000')
        try:
            fileId = rmn.fstopenall(fileName, rmn.FST_RO)
        except:
            sys.stderr.write("Problem opening the file: %s\n" % fileName)
            sys.exit(1)

        try:
            # Encode selection criteria
            ip1 = rmn.ip1_all(500., rmn.LEVEL_KIND_PMB)
            datev = rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20150707, 6000000)

            # Get the list of record keys matching
            # nomvar='TT' at 500mb and valide date=20150707.06000000
            keylist = rmn.fstinl(fileId, nomvar='TT', ip1=ip1, datev=datev)
            print("CB11b: Found %d records matching TT, ip1=%d, datev=%d" %
                  (len(keylist), ip1, datev))

            # Get every record meta data
            for k in keylist:
                m = rmn.fstprm(k)
                print("CB11b: %s (%d, %d, %s)" %
                      (m['nomvar'], m['ip1'], m['ip2'], m['datev']))
        except:
            pass
        finally:
            # Close file even if an error occured above
            rmn.fstcloseall(fileId)
Exemple #21
0
    def test_12(self):
        """
        Defining Grids

        Grids can be defined from
        * a record in a FSTD file or
        * directly by providing parameters.
        These grids parameters can be used to geolocate the data points and
        for interpolation operations (see below).

        See also:
        """
        import os
        import rpnpy.librmn.all as rmn

        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES')
        fileName = os.path.join(ATM_MODEL_DFILES.strip(),
                                'bcmk/2009042700_012')

        # Define a grid from parameters, a cylindrical equidistant (LatLon) grid with 0.5 deg spacing.
        paramsL = {
            'grtyp': 'L',  # Cylindrical equidistant projection
            'ni': 90,  # Grid dimension: 90 by 45 points
            'nj': 45,
            'lat0':
            0.,  # Grid lower-left (south-west) corner (point 1,1) is located at 0N, 180E
            'lon0': 180.,
            'dlat':
            0.5,  # The grid has a resolution (grid spacing) of 0.5 deg. on both axes
            'dlon': 0.5
        }
        try:
            gridL = rmn.encodeGrid(paramsL)
        except:
            raise rmn.FSTDError(
                "Problem defining a grid with provided parameters: %s " %
                str(paramsL))

        # Get a grid definition from a record in a FSTD file
        try:
            fileId = rmn.fstopenall(fileName, rmn.FST_RO)
            prKey = rmn.fstinf(fileId, nomvar='PR')['key']
            prMeta = rmn.fstprm(
                prKey)  # Get the record metadata along with partial grid info
            prMeta['iunit'] = fileId
            prGrid0 = rmn.ezqkdef(
                prMeta)  # use ezscint to retreive full grid info
            prGrid = rmn.decodeGrid(
                prGrid0)  # Decode all the grid parameters values
            rmn.fstcloseall(fileId)
        except:
            raise rmn.FSTDError(
                "Problem getting PR record grid meta from file: %s" % fileName)
Exemple #22
0
    def test_11b(self):
        """
        Queries: List/Find records

        This example shows how to
        * get the list of all records matching selection creterions
        * encode parameters (ip1, datev) values to use selection criteria
                
        See also:
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.ip1_all
        rpnpy.librmn.fstd98.ip2_all
        rpnpy.librmn.fstd98.fstinl
        rpnpy.librmn.fstd98.fstprm
        rpnpy.librmn.fstd98.DecodeIp
        rpnpy.librmn.fstd98.kindToString
        rpnpy.librmn.base.newdate
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.const
        """
        import os, sys
        import rpnpy.librmn.all as rmn

        # Open all RPNStd files in $ATM_MODEL_DFILES/bcmk_p/anlp2015070706_000
        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
        fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk_p','anlp2015070706_000')
        try:
            fileId = rmn.fstopenall(fileName, rmn.FST_RO)
        except:
            sys.stderr.write("Problem opening the file: %s\n" % fileName)
            sys.exit(1)

        try:
            # Encode selection criteria
            ip1   = rmn.ip1_all(500., rmn.LEVEL_KIND_PMB)
            datev = rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20150707, 6000000)

            # Get the list of record keys matching
            # nomvar='TT' at 500mb and valide date=20150707.06000000
            keylist = rmn.fstinl(fileId, nomvar='TT', ip1=ip1, datev=datev)
            print("CB11b: Found %d records matching TT, ip1=%d, datev=%d" %
                  (len(keylist), ip1, datev))

            # Get every record meta data
            for k in keylist:
                m = rmn.fstprm(k)
                print("CB11b: %s (%d, %d, %s)" % (m['nomvar'], m['ip1'], m['ip2'], m['datev']))
        except:
            pass
        finally:
            # Close file even if an error occured above
            rmn.fstcloseall(fileId)
Exemple #23
0
    def test_9(self):
        """
        Decoding values

        When reading an FSTD record metadata, the ip1, ip2, ip3 contains the encoded time and levels values.
        In the old format, ip1 is used for the level value and ip2 is a none encoded time value in hours.
        In the new format, all ip1, ip2, ip3 can be used to specify time and level as well as ranges.
        
        See also:
        rpnpy.librmn.fstd98.convertIp
        rpnpy.librmn.fstd98.convertIPtoPK
        rpnpy.librmn.fstd98.DecodeIp
        rpnpy.librmn.fstd98.kindToString
        rpnpy.librmn.proto.FLOAT_IP
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstinf
        rpnpy.librmn.fstd98.fstinl
        rpnpy.librmn.fstd98.fstprm
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.fstd98.FSTDError
        rpnpy.librmn.RMNError
        rpnpy.librmn.const
        """
        import os
        import rpnpy.librmn.all as rmn

        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES')
        fileName = os.path.join(ATM_MODEL_DFILES.strip(),
                                'bcmk/2009042700_012')

        # Get list of records
        try:
            fileId = rmn.fstopenall(fileName, rmn.FST_RO)
            keyList = rmn.fstinl(fileId, nomvar='TT')
        except:
            raise rmn.FSTDError(
                "Problem getting list of TT records from file: %s" % fileName)

        # Get metadata and Decode level value
        try:
            for k in keyList:
                recMeta = rmn.fstprm(k)
                (level, ikind) = rmn.convertIp(rmn.CONVIP_DECODE,
                                               recMeta['ip1'])
                kindstring = rmn.kindToString(ikind)
                print("Found %s at level %f %s" %
                      (recMeta['nomvar'], level, kindstring))
        except:
            raise rmn.FSTDError(
                "Problem getting metadata for TT from file: %s " % fileName)

        rmn.fstcloseall(fileId)
Exemple #24
0
 def test_11qd(self):
     import os, sys
     import rpnpy.librmn.all as rmn
     ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
     fileId = rmn.fstopenall(ATM_MODEL_DFILES+'/bcmk', rmn.FST_RO)
     for k in rmn.fstinl(fileId, nomvar='TT', ip2=12):
         m = rmn.fstprm(k)
         rp1 = rmn.DecodeIp(m['ip1'], m['ip2'], m['ip3'])[0]
         level  = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind))
         dateo  = "%8.8d.%8.8d" % rmn.newdate(rmn.NEWDATE_STAMP2PRINT,m['dateo'])
         print("CB11qd: %s (%d, %d) level=%s, dateo=%s + %s" %
               (m['nomvar'], m['ip1'], m['ip2'], level, dateo, float(m['npas'] * m['deet']) / 3600.))
     rmn.fstcloseall(fileId)
Exemple #25
0
def getGrid(dataKey, fileID):
    """ Get the grid details in form of lon/lat of the FST file. """

    # Get the file metadata, then add on the keypair for 'iunit'
    fileMeta = rmn.fstprm(dataKey)
    fileMeta['iunit'] = fileID
    # Get the grid data and decode it
    gridData = rmn.ezqkdef(fileMeta)
    gridDecode = rmn.decodeGrid(gridData)
    llGridData = rmn.gdll(gridData)
    latData = llGridData['lat']
    lonData = llGridData['lon']
    return {'gridll':llGridData,'gridID':gridData, 'latData':latData,'lonData':lonData}
Exemple #26
0
def get_levels_keys(fileId, nomvar, datev=-1, ip2=-1, ip3=-1,
                    typvar=' ', etiket=' ',
                    vGrid=None, thermoMom='VIPT', verbose=False):
    """
    """
    #TODO: try to get the sorted ip1 list w/o vgrid, because vgrid doesn;t support 2 different vertical coor in the same file (or list of linked files)
    
    # Get the vgrid definition present in the file
    if vGrid is None:
        if verbose:
            print("Getting vertical grid description")
        _vgd.vgd_put_opt('ALLOW_SIGMA', _vgd.VGD_ALLOW_SIGMA)
        vGrid = _vgd.vgd_read(fileId)
        
    vip  = _vgd.vgd_get(vGrid, thermoMom)
    if verbose:
        vkind    = _vgd.vgd_get(vGrid, 'KIND')
        vver     = _vgd.vgd_get(vGrid, 'VERS')
        vtype    = _vgd.VGD_KIND_VER_INV[(vkind,vver)]
        print("Found %d %s levels of type %s" % (len(vip), thermoMom, vtype))

    # Trim the list of ip1 to actual levels in files for nomvar
    # since the vgrid in the file is a super set of all levels
    # and get their "key"
    vipkeys = []
    for ip1 in vip:
        (lval, lkind) = _rmn.convertIp(_rmn.CONVIP_DECODE, ip1)
        key = _rmn.fstinf(fileId, nomvar=nomvar, datev=datev, ip2=ip2, ip3=ip3,
                         ip1=_rmn.ip1_all(lval, lkind),
                         typvar=typvar, etiket=etiket)
        if key is not None:
            vipkeys.append((ip1, key['key']))
            if datev == -1 or ip2 == -1 or ip3 == -1 or typvar.strip() == '' or etiket.strip() == '':
                meta   = _rmn.fstprm(key)
                datev  = meta['datev']
                ip2    = meta['ip2']
                ip3    = meta['ip3']
                typvar = meta['typvar']
                etiket = meta['etiket']
    return {
        'nomvar' : nomvar,
        'datev'  : datev,
        'ip2'    : ip2,
        'ip3'    : ip3,
        'typvar' : typvar,
        'etiket' : etiket,
        'v'      : vGrid,
        'ip1keys': vipkeys
        }
Exemple #27
0
 def test_11qd(self):
     import os, sys
     import rpnpy.librmn.all as rmn
     ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
     fileId = rmn.fstopenall(ATM_MODEL_DFILES + '/bcmk', rmn.FST_RO)
     for k in rmn.fstinl(fileId, nomvar='TT', ip2=12):
         m = rmn.fstprm(k)
         rp1 = rmn.DecodeIp(m['ip1'], m['ip2'], m['ip3'])[0]
         level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind))
         dateo = "%8.8d.%8.8d" % rmn.newdate(rmn.NEWDATE_STAMP2PRINT,
                                             m['dateo'])
         print("CB11qd: %s (%d, %d) level=%s, dateo=%s + %s" %
               (m['nomvar'], m['ip1'], m['ip2'], level, dateo,
                float(m['npas'] * m['deet']) / 3600.))
     rmn.fstcloseall(fileId)
Exemple #28
0
 def test_11bqd(self):
     import os, sys
     import rpnpy.librmn.all as rmn
     ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
     fileId = rmn.fstopenall(
         ATM_MODEL_DFILES + '/bcmk_p/anlp2015070706_000', rmn.FST_RO)
     for k in rmn.fstinl(fileId,
                         nomvar='TT',
                         ip1=rmn.ip1_all(500., rmn.LEVEL_KIND_PMB),
                         datev=rmn.newdate(rmn.NEWDATE_PRINT2STAMP,
                                           20150707, 6000000)):
         m = rmn.fstprm(k)
         print("CB11bqd: %s (%d, %d, %s)" %
               (m['nomvar'], m['ip1'], m['ip2'], m['datev']))
     rmn.fstcloseall(fileId)
Exemple #29
0
    def test_9(self):
        """
        Decoding values

        When reading an FSTD record metadata, the ip1, ip2, ip3 contains the encoded time and levels values.
        In the old format, ip1 is used for the level value and ip2 is a none encoded time value in hours.
        In the new format, all ip1, ip2, ip3 can be used to specify time and level as well as ranges.
        
        See also:
        rpnpy.librmn.fstd98.convertIp
        rpnpy.librmn.fstd98.convertIPtoPK
        rpnpy.librmn.fstd98.DecodeIp
        rpnpy.librmn.fstd98.kindToString
        rpnpy.librmn.proto.FLOAT_IP
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstinf
        rpnpy.librmn.fstd98.fstinl
        rpnpy.librmn.fstd98.fstprm
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.fstd98.FSTDError
        rpnpy.librmn.RMNError
        rpnpy.librmn.const
        """
        import os
        import rpnpy.librmn.all as rmn

        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES')
        fileName = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012')

        # Get list of records
        try:
            fileId  = rmn.fstopenall(fileName, rmn.FST_RO)
            keyList = rmn.fstinl(fileId, nomvar='TT')
        except:
            raise rmn.FSTDError("Problem getting list of TT records from file: %s" % fileName)

        # Get metadata and Decode level value
        try:
            for k in keyList:
                recMeta = rmn.fstprm(k)
                (level, ikind) = rmn.convertIp(rmn.CONVIP_DECODE, recMeta['ip1'])
                kindstring     = rmn.kindToString(ikind)
                print("Found %s at level %f %s" % (recMeta['nomvar'], level, kindstring))
        except:
            raise rmn.FSTDError("Problem getting metadata for TT from file: %s " % fileName)

        rmn.fstcloseall(fileId)
Exemple #30
0
    def test_12(self):
        """
        Defining Grids

        Grids can be defined from
        * a record in a FSTD file or
        * directly by providing parameters.
        These grids parameters can be used to geolocate the data points and
        for interpolation operations (see below).

        See also:
        """
        import os
        import rpnpy.librmn.all as rmn
 
        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES')
        fileName = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012')
 
        # Define a grid from parameters, a cylindrical equidistant (LatLon) grid with 0.5 deg spacing.
        paramsL = {
            'grtyp' : 'L', # Cylindrical equidistant projection
            'ni'   :  90,  # Grid dimension: 90 by 45 points
            'nj'   :  45,
            'lat0' :   0., # Grid lower-left (south-west) corner (point 1,1) is located at 0N, 180E
            'lon0' : 180.,
            'dlat' :   0.5,# The grid has a resolution (grid spacing) of 0.5 deg. on both axes
            'dlon' :   0.5
            }
        try:
            gridL = rmn.encodeGrid(paramsL)
        except:
            raise rmn.FSTDError("Problem defining a grid with provided parameters: %s " % str(paramsL))
 
        # Get a grid definition from a record in a FSTD file
        try:
            fileId = rmn.fstopenall(fileName, rmn.FST_RO)
            prKey  = rmn.fstinf(fileId, nomvar='PR')['key']
            prMeta = rmn.fstprm(prKey)              # Get the record metadata along with partial grid info
            prMeta['iunit'] = fileId
            prGrid0 = rmn.ezqkdef(prMeta)           # use ezscint to retreive full grid info
            prGrid  = rmn.decodeGrid(prGrid0)       # Decode all the grid parameters values
            rmn.fstcloseall(fileId)
        except:
            raise rmn.FSTDError("Problem getting PR record grid meta from file: %s" % fileName)
Exemple #31
0
def getMeta(filePath, level, spc):
    try:
        fileID = rmn.fstopenall(filePath,rmn.FST_RO)
        dataKey = rmn.fstinf(fileID,nomvar=spc,ip1=level)['key']
        dataRec = rmn.fstluk(dataKey)
        # get the concentration data
        concData = dataRec['d']
        fileMeta = rmn.fstprm(dataKey)
        fileMeta['iunit'] = fileID
        gridData = rmn.ezqkdef(fileMeta)
        gridDecode = rmn.decodeGrid(gridData)
        # get the lonlat data
        llGridData = rmn.gdll(gridData)
        latData = llGridData['lat']
        lonData = llGridData['lon']
        return concData, lonData.tolist(), latData.tolist()
    except:
        print('Could not read file.')
        pass
Exemple #32
0
def fstsui(iunit):
    """Find next record matching criterions (Interface to fstsui)
        recParamDict = Fstdc.fstsui(iunit)
        @param iunit file unit number handle returned by Fstdc_fstouv (int)
        @returns python dict with record handle + record params keys/values
        @exception TypeError
        @exception Fstdc.error
    """
    try:
        mymatch = _rmn.fstsui(iunit)
    except:
        raise error('Problem getting record list')
    if not mymatch:
        raise error('No matching record')
    recParams = _rmn.fstprm(mymatch['key'])
    recParams['handle'] = recParams['key']
    recParams['nom']    = recParams['nomvar']
    recParams['type']   = recParams['typvar']
    recParams['datev']  = recParams['xtra1']  #TODO: Keep Fstdc original bug?
    return recParams
Exemple #33
0
def fstsui(iunit):
    """Find next record matching criterions (Interface to fstsui)
        recParamDict = Fstdc.fstsui(iunit)
        @param iunit file unit number handle returned by Fstdc_fstouv (int)
        @returns python dict with record handle + record params keys/values
        @exception TypeError
        @exception Fstdc.error
    """
    try:
        mymatch = _rmn.fstsui(iunit)
    except:
        raise error('Problem getting record list')
    if not mymatch:
        raise error('No matching record')
    recParams = _rmn.fstprm(mymatch['key'])
    recParams['handle'] = recParams['key']
    recParams['nom']    = recParams['nomvar']
    recParams['type']   = recParams['typvar']
    recParams['datev']  = recParams['xtra1']  #TODO: Keep Fstdc original bug?
    return recParams
Exemple #34
0
def getConc(filePath, level, spc):
    """ Get the concentration data for an FST file.

    Returns the data in an array, the datakey as a key and the fileID of the FST file.
    """

    try:
       fileID = rmn.fstopenall(filePath,rmn.FST_RO)
       dataKey = rmn.fstinf(fileID,nomvar=spc,ip1=level)['key']
       dataRec = rmn.fstluk(dataKey)
       concData = dataRec['d']
       fileMeta = rmn.fstprm(dataKey)
       fileMeta['iunit']=fileID
       gridData = rmn.ezqkdef(fileMeta)
       llGridData = rmn.gdll(gridData)
       print ('File {} recorded'.format(filePath))
       rmn.fstcloseall(fileID)
       return concData,llGridData
    except TypeError:
       # log an error into the log file
       #logging.warning('nomvar {} and ip1 {} could not be found for file {}.'.format(fileID, spc, level, filePath))
       return None
    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))
    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))
Exemple #37
0
from os import environ
from os.path import join

import rpnpy.librmn.all as rmn
print 'librmn=',rmn.librmn

data_file = join(environ['CMCGRIDF'], 'prog', 'gsloce', '2015070706_042')

funit = rmn.fstopenall(data_file,rmn.FST_RO)
#r = rmn.fstlir(funit, nomvar='UUW', typvar='P@')
k = rmn.fstinf(funit, nomvar='UUW', typvar='P@')
## print k
## print rmn.fstprm(k['key'])
r = rmn.fstluk(k['key'])

l = rmn.fstinl(funit)
print 'data_file=',data_file,len(l)
for k in l:
    p = rmn.fstprm(k)
    print p['nomvar'], p['typvar'], p['datyp']
    if p['datyp'] != 1344:
        r = rmn.fstluk(k)
        del(r['d'])
        del r
rmn.fstcloseall(funit)
Exemple #38
0
>>> for locx in range(dataRec['ni']):
...     for locy in range(dataRec['nj']):
...             xypos=rmn.gdxyfll(gridData,lat=latDic[locx][locy],lon=lonDic[locx][locy])
...             xlist+=[np.int(xypos['x'][0])]
...             ylist+=[np.int(xypos['y'][0])]
...

>>>
>>> len(xlist)
495624
>>> len(ylist)
495624
>>>


>>> fileMeta = rmn.fstprm(dataKey)
>>> fileMeta['iunit']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'iunit'
>>> fileMeta['iunit']=fileID
>>> gridData = rmn.ezqkdef(fileMeta)
Read(999) ^^   X  GM_DEV           1   642     1  363996800       60193   56056     1        0        0  E 32  E  1470   560 54400 46560
Read(999) >>   X  GM_DEV         772     1     1  363996800       60193   56056     1        0        0  E 32  E  1470   560 54400 46560
>>> gridDecode = rmn.decodeGrid(gridData)
>>> gridDecode
{'tag1': 40749, 'tag2': 67634, 'xlon1': 250.0, 'xlon2': 291.0, 'ig2ref': 560, 'shape': (772, 642), 'grref': 'E', 'xlat1': 57.0, 'ax': array([[ 148.05000305],
       [ 148.13999939],
       [ 148.22999573],
       [ 148.32000732],
       [ 148.41000366],
Exemple #39
0
>>> O3=rmn.fstinf(fileOpen,nomvar=defaultSpc,ip1=defaultIp1)
>>> O3
{'shape': (772, 642, 1), 'key': 6311937}

>>> O3Key=rmn.fstinf(fileOpen,nomvar=defaultSpc,ip1=defaultIp1)
>>> O3Key
{'shape': (772, 642, 1), 'key': 6311937}

>>> O3Key=O3Key['key']
>>> O3Key
6311937

>>> O3Key
6311937
>>> O3Meta=rmn.fstprm(O3Key)


>>> O3Meta
{'deet': 300, 'shape': (772, 642, 1), 'typvar': 'P ', 'lng': 143566, 'ni': 772, 'nj': 642, 'nk': 1, 'swa': 110332609, 'datyp': 134, 'xtra1': 363997700, 'xtra2': 0, 'xtra3': 0, 'ip2': 1, 'ip3': 0, 'nbits': 16, 'key': 6311937, 'ubc': 0, 'npas': 12, 'ig4': 0, 'ig3': 1, 'ig2': 56056, 'ig1': 60193, 'datev': 363997700, 'nomvar': 'TO3 ', 'ip1': 76696048, 'dltf': 0, 'etiket': 'GM_DEV      ', 'grtyp': 'Z', 'dateo': 363996800}


>>> O3Rec=rmn.fstluk(O3Key)
Read(999) TO3  P  GM_DEV         772   642     1  363996800    76696048       1     0      300       12  f 16  Z 60193 56056     1     0
>>> O3Rec['d']
array([[ 27.0703125 ,  27.0703125 ,  27.0703125 , ...,  31.35546875,
         31.35546875,  31.35546875],
       [ 27.0703125 ,  27.0703125 ,  27.0703125 , ...,  31.35546875,
         31.35546875,  31.35546875],
       [ 27.0703125 ,  27.0703125 ,  27.01171875, ...,  31.36328125,
         31.42578125,  31.42578125],
Exemple #40
0
from os import environ
from os.path import join

import rpnpy.librmn.all as rmn
print 'librmn=', rmn.librmn

data_file = join(environ['CMCGRIDF'], 'prog', 'gsloce', '2015070706_042')

funit = rmn.fstopenall(data_file, rmn.FST_RO)
#r = rmn.fstlir(funit, nomvar='UUW', typvar='P@')
k = rmn.fstinf(funit, nomvar='UUW', typvar='P@')
## print k
## print rmn.fstprm(k['key'])
r = rmn.fstluk(k['key'])

l = rmn.fstinl(funit)
print 'data_file=', data_file, len(l)
for k in l:
    p = rmn.fstprm(k)
    print p['nomvar'], p['typvar'], p['datyp']
    if p['datyp'] != 1344:
        r = rmn.fstluk(k)
        del (r['d'])
        del r
rmn.fstcloseall(funit)
Exemple #41
0
        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
    try:
Exemple #42
0
def getGrid(dataKey, fileID):
    fileMeta = rmn.fstprm(dataKey)
    fileMeta['iunit'] = fileID
    gridID = rmn.ezqkdef(fileMeta)
    return gridID
Exemple #43
0
def get_model(overpass,
              lonArray,
              latArray,
              fst_dir,
              ctrl_dir=None,
              var='AF',
              threshold=4,
              filestart=00):
    if filestart == 12:
        file_time = (overpass if overpass.minute < 30 else overpass +
                     datetime.timedelta(hours=1)) - datetime.timedelta(
                         hours=12)
        file_path = "{0}/{1}".format(os.path.abspath(fst_dir),
                                     file_time.strftime('%Y%m%d12_0%H'))
    elif filestart == 0:
        file_time = (overpass if overpass.minute < 30 else overpass +
                     datetime.timedelta(hours=1))
        file_path = "{0}/{1}".format(os.path.abspath(fst_dir),
                                     file_time.strftime('%Y%m%d00_0%H'))
    rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)
    rmn.ezsetopt(rmn.EZ_OPT_INTERP_DEGREE, rmn.EZ_INTERP_NEAREST)
    fid = rmn.fstopenall(file_path, rmn.FST_RO)
    lonarr = np.array(lonArray, dtype='float32')
    latarr = np.array(latArray, dtype='float32')
    keylist = rmn.fstinl(fid, nomvar=var)
    dir = os.path.abspath(fst_dir)
    ctrl_dir = os.path.abspath(ctrl_dir) if ctrl_dir else None
    # assuming this for now
    ref_lvl = 'sea'
    height = [float('-inf')] * len(lonarr)
    value = [float('-inf')] * len(lonarr)
    points = [{} for _ in lonarr]
    iplist = []
    for key in keylist:
        meta = rmn.fstprm(key)
        iplist.append(
            rmn.DecodeIp(meta['ip1'], meta['ip2'], meta['ip3'])[0].v1)
    sorted_keylist = (x for _, x in sorted(zip(iplist, keylist), reverse=True))
    next(sorted_keylist, None)
    before_val = [float('inf')] * len(lonarr)
    cur_meta, cur_grid, cur_xypos, cur_val = data_from_key(
        next(sorted_keylist), fid, lonarr, latarr)
    if ctrl_dir:
        if filestart == 12:
            ctrl_path = "{0}/{1}".format(os.path.abspath(ctrl_dir),
                                         file_time.strftime('%Y%m%d12_0%H'))
        elif filestart == 0:
            ctrl_path = "{0}/{1}".format(os.path.abspath(ctrl_dir),
                                         file_time.strftime('%Y%m%d00_0%H'))
        ctrl_fid = rmn.fstopenall(ctrl_path, rmn.FST_RO)
        ctrl_keylist = rmn.fstinl(ctrl_fid, nomvar=var)
        sorted_ctrl_keylist = (
            x for _, x in sorted(zip(iplist, ctrl_keylist), reverse=True))
        next(sorted_ctrl_keylist, None)
        _, _, _, ctrl_val = data_from_key(next(sorted_ctrl_keylist), ctrl_fid,
                                          lonarr, latarr)
        cur_val -= ctrl_val
    for progress_ind, after_key in enumerate(sorted_keylist):
        after_meta, after_grid, after_xypos, after_val = data_from_key(
            after_key, fid, lonarr, latarr)
        if ctrl_dir:
            after_ctrl_key = next(sorted_ctrl_keylist)
            _, _, _, after_ctrl_val = data_from_key(after_ctrl_key, ctrl_fid,
                                                    lonarr, latarr)
            after_val -= after_ctrl_val
        for ind, val in enumerate(cur_val):
            if ((val > before_val[ind]) and (val > after_val[ind])
                    and (val >= threshold) and (val > value[ind])):
                try:
                    if int(ind) <= 20:
                        print(
                            'Updating GZ, val: {}, existing val: {}'.format(
                                val, value[ind]), ind, cur_meta['ip1'])
                    gzkey = rmn.fstinf(fid, nomvar='GZ',
                                       ip1=cur_meta['ip1'])['key']
                    gzdata = rmn.fstluk(gzkey)['d']
                    meta = rmn.fstprm(gzkey)
                    meta['iunit'] = fid
                    gz_grid = rmn.ezqkdef(meta)
                    heightList = rmn.gdxysval(gz_grid, cur_xypos['x'],
                                              cur_xypos['y'], gzdata) * 10
                    height[ind] = float(heightList[ind])
                    value[ind] = float(val)
                    #print (height[ind], ind)
                    print(height, value)
                except TypeError:
                    continue
        before_val = cur_val
        cur_meta, cur_grid, cur_xypos, cur_val = after_meta, after_grid, after_xypos, after_val
        print height
    rmn.fstcloseall(fid)
    if ctrl_dir:
        rmn.fstcloseall(ctrl_fid)
    #print(height)
    return height, value
Exemple #44
0
def get_levels_keys(fileId,
                    nomvar,
                    datev=-1,
                    ip2=-1,
                    ip3=-1,
                    typvar=' ',
                    etiket=' ',
                    vGrid=None,
                    thermoMom='VIPT',
                    verbose=False):
    """
    Get from file the list of ip1 and fstd-record-key matching provided filters

    ipkeys = get_levels_keys(fileId, nomvar)

    Args:
        fileId  : unit number associated to the file
                  obtained with fnom+fstouv
        nomvar  : variable name
        datev   : valid date
        ip2     : forecast hour
        ip3     : user defined identifier
        typvar  : type of field
        etiket  : label
        vGrid   : vertical grid descriptor
        thermoMom : 'VIPT' to get Thermo levels, 'VIPT' for momentum levels
        verbose : Print some info when true
    Returns:
        {
        'nomvar' : nomvar,  # variable name
        'datev'  : datev,   # valid date
        'ip2'    : ip2,     # forecast hour
        'ip3'    : ip3,     # user defined identifier
        'typvar' : typvar,  # type of field
        'etiket' : etiket,  # label
        'vgrid'  : vGrid,   # vertical grid descriptor as returned by vgd_read
        'ip1keys': vipkeys  # list of ip1 and corresponding FSTD rec key as
                            # ((ip1,key1), (ip1b, key2), ...)
        }
    Raises:
        TypeError  on wrong input arg types
        ValueError on invalid input arg value
        FSTDError  on any other error

    Examples:
    >>> import os, os.path
    >>> import rpnpy.librmn.all as rmn
    >>> import rpnpy.utils.fstd3d as fstd3d
    >>> ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
    >>> filename = os.path.join(ATM_MODEL_DFILES,'bcmk')
    >>>
    >>> # Open existing file in Rear Only mode
    >>> fileId = rmn.fstopenall(filename, rmn.FST_RO)
    >>>
    >>> # Find ip1, key for all TT in file
    >>> ipkeys = fstd3d.get_levels_keys(fileId, 'TT', thermoMom='VIPT', verbose=True)
    Getting vertical grid description
    Found 158 VIPT levels of type hyb
    >>> print('# Found {} levels for TT'.format(len(ipkeys['ip1keys'])))
    # Found 80 levels for TT
    >>> rmn.fstcloseall(fileId)

    See Also:
        get_levels_press
        fst_read_3d
        rpnpy.librmn.fstd98.fstinf
        rpnpy.librmn.fstd98.fstprm
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.vgd.base.vgd_read
    """
    #TODO: try to get the sorted ip1 list w/o vgrid, because vgrid doesn;t support 2 different vertical coor in the same file (or list of linked files)

    # Get the vgrid definition present in the file
    if vGrid is None:
        if verbose:
            print("Getting vertical grid description")
        _vgd.vgd_put_opt('ALLOW_SIGMA', _vgd.VGD_ALLOW_SIGMA)
        vGrid = _vgd.vgd_read(fileId)

    vip = _vgd.vgd_get(vGrid, thermoMom)
    if verbose:
        vkind = _vgd.vgd_get(vGrid, 'KIND')
        vver = _vgd.vgd_get(vGrid, 'VERS')
        vtype = _vgd.VGD_KIND_VER_INV[(vkind, vver)]
        print("Found %d %s levels of type %s" % (len(vip), thermoMom, vtype))

    # Trim the list of ip1 to actual levels in files for nomvar
    # since the vgrid in the file is a super set of all levels
    # and get their "key"
    vipkeys = []
    for ip1 in vip:
        (lval, lkind) = _rmn.convertIp(_rmn.CONVIP_DECODE, ip1)
        key = _rmn.fstinf(fileId,
                          nomvar=nomvar,
                          datev=datev,
                          ip2=ip2,
                          ip3=ip3,
                          ip1=_rmn.ip1_all(lval, lkind),
                          typvar=typvar,
                          etiket=etiket)
        if key is not None:
            vipkeys.append((ip1, key['key']))
            if (datev == -1 or ip2 == -1 or ip3 == -1 or typvar.strip() == ''
                    or etiket.strip() == ''):
                meta = _rmn.fstprm(key)
                datev = meta['datev']
                ip2 = meta['ip2']
                ip3 = meta['ip3']
                typvar = meta['typvar']
                etiket = meta['etiket']
    return {
        'nomvar': nomvar,
        'datev': datev,
        'ip2': ip2,
        'ip3': ip3,
        'typvar': typvar,
        'etiket': etiket,
        'vgrid': vGrid,
        'ip1keys': vipkeys
    }
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. ')
Exemple #46
0
        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
    try:    
        (lon,lat) = np.loadtxt(options.lolafile, dtype=np.float32, unpack=True)
        ## lat = np.asfortranarray(lat, dtype=np.float32)
        ## lon = np.asfortranarray(lon, dtype=np.float32)