Beispiel #1
0
    def test_13(self):
        """
        Interpolating Data

        Interpolating data to/from known FSTD grids is made easy with the Ezscint package.
        There are a few exceptions though
        * you can only interpolate to a Y grid, not from it.
        * multi-parts grids (Yin-Yang, ...) have to be dealth with in a special way (see below)
        In this example we'll interpolate forecast data onto the analysis grid to make some computations
        
        See also:
        """
        import os
        import rpnpy.librmn.all as rmn
 
        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES')
        fileName0 = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_000')  #Analysis
        fileName1 = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012')  #Forecast
 
        # Get data and grid definition for P0 in the 1st FSTD file
        try:
            fileId   = rmn.fstopenall(fileName0, rmn.FST_RO)
            p0Data1  = rmn.fstlir(fileId, nomvar='P0')  # Get the record data and metadata along with partial grid info
            p0Data1['iunit'] = fileId
            p0GridId = rmn.ezqkdef(p0Data1)             # use ezscint to retreive a grid id
            p0Grid1  = rmn.decodeGrid(p0GridId)         # Decode all the grid parameters values
            rmn.fstcloseall(fileId)
        except:
            raise rmn.FSTDError("Problem getting P0 record grid meta from file: %s" % fileName0)
 
        # Get data and grid definition for P0 in the 2nd FSTD file
        try:
            fileId   = rmn.fstopenall(fileName1, rmn.FST_RO)
            p0Data2  = rmn.fstlir(fileId, nomvar='P0', ip2=12)  # Get the record data and metadata along with partial grid info
            p0Data2['iunit'] = fileId
            p0GridId = rmn.ezqkdef(p0Data2)                     # use ezscint to retreive a grid id
            p0Grid2  = rmn.decodeGrid(p0GridId)                 # Decode all the grid parameters values
            rmn.fstcloseall(fileId)
        except:
            raise rmn.FSTDError("Problem getting P0 record grid meta from file: %s " % fileName1)
 
        # Make a cubic interpolation of p0Data2 onto p0Grid1 with extrapolated values set to Minvalue of the field
        rmn.ezsetopt(rmn.EZ_OPT_EXTRAP_DEGREE, rmn.EZ_EXTRAP_MIN)
        rmn.ezsetopt(rmn.EZ_OPT_INTERP_DEGREE, rmn.EZ_INTERP_LINEAR)
        p0Data2_onGrid1 = rmn.ezsint(p0Grid1['id'], p0Grid2['id'], p0Data2['d'])
 
        # Make some computation
        p0Diff = p0Data2_onGrid1 - p0Data1['d']
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
Beispiel #3
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)
 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)
Beispiel #5
0
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
Beispiel #6
0
 def test_ezsint(self):
     gp1 = self.getGridParams_L()
     gid1 = rmn.ezqkdef(gp1)
     self.assertTrue(gid1>=0)
     gp2 = self.getGridParams_L(0.25)
     gid2 = rmn.ezqkdef(gp2)
     self.assertTrue(gid2>=0)
     setid = rmn.ezdefset(gid2, gid1)
     self.assertTrue(setid>=0)
     zin = np.empty(gp1['shape'],dtype=np.float32,order='FORTRAN')
     for x in xrange(gp1['ni']):
         zin[:,x] = x
     zout = rmn.ezsint(gid2,gid1,zin)
     self.assertEqual(gp2['shape'],zout.shape)
     for j in xrange(gp2['nj']):
         for i in xrange(gp2['ni']):
             self.assertTrue(abs((zin[i,j]+zin[i+1,j])/2.-zout[i,j]) < self.epsilon)
 def test_ezsint(self):
     gp1 = self.getGridParams_L()
     gid1 = rmn.ezqkdef(gp1)
     self.assertTrue(gid1>=0)
     gp2 = self.getGridParams_L(0.25)
     gid2 = rmn.ezqkdef(gp2)
     self.assertTrue(gid2>=0)
     setid = rmn.ezdefset(gid2, gid1)
     self.assertTrue(setid>=0)
     zin = np.empty(gp1['shape'],dtype=np.float32,order='FORTRAN')
     for x in range(gp1['ni']):
         zin[:,x] = x
     zout = rmn.ezsint(gid2,gid1,zin)
     self.assertEqual(gp2['shape'],zout.shape)
     for j in range(gp2['nj']):
         for i in range(gp2['ni']):
             self.assertTrue(abs((zin[i,j]+zin[i+1,j])/2.-zout[i,j]) < self.epsilon)
Beispiel #8
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
 def test_ezqkdef_1subgrid(self):
     gp = self.getGridParams_L()
     gid1 = rmn.ezqkdef(gp)
     self.assertTrue(gid1>=0)
     ng = rmn.ezget_nsubgrids(gid1)
     self.assertEqual(ng,1)
     subgid = rmn.ezget_subgridids(gid1)
     self.assertEqual(subgid,[gid1])
     rmn.gdrls(gid1)
Beispiel #10
0
 def test_ezqkdef_ezgprm(self):
     gp = self.getGridParams_L()
     gid1 = rmn.ezqkdef(gp)
     self.assertTrue(gid1>=0)
     gp['id'] = gid1
     gprm = rmn.ezgprm(gid1)
     for k in gprm.keys():
         self.assertEqual(gp[k],gprm[k])
     rmn.gdrls(gid1)
Beispiel #11
0
 def test_ezqkdef_ezgprm(self):
     gp = self.getGridParams_L()
     gid1 = rmn.ezqkdef(gp)
     self.assertTrue(gid1>=0)
     gp['id'] = gid1
     gprm = rmn.ezgprm(gid1)
     for k in gprm.keys():
         self.assertEqual(gp[k],gprm[k])
     rmn.gdrls(gid1)
Beispiel #12
0
 def test_ezqkdef_1subgrid(self):
     gp = self.getGridParams_L()
     gid1 = rmn.ezqkdef(gp)
     self.assertTrue(gid1>=0)
     ng = rmn.ezget_nsubgrids(gid1)
     self.assertEqual(ng,1)
     subgid = rmn.ezget_subgridids(gid1)
     self.assertEqual(subgid,[gid1])
     rmn.gdrls(gid1)
Beispiel #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
Beispiel #14
0
 def test_ezqkdef_gdll(self):
     gp = self.getGridParams_L()
     gid1 = rmn.ezqkdef(gp)
     self.assertTrue(gid1>=0)
     gll = rmn.gdll(gid1)
     self.assertEqual(gp['shape'],gll['lat'].shape)
     self.assertEqual(gp['shape'],gll['lon'].shape)
     self.assertEqual(gp['lat0'],gll['lat'][0,0])
     self.assertEqual(gp['lon0'],gll['lon'][0,0])
     rmn.gdrls(gid1)
Beispiel #15
0
 def test_ezqkdef_gdll(self):
     gp = self.getGridParams_L()
     gid1 = rmn.ezqkdef(gp)
     self.assertTrue(gid1>=0)
     gll = rmn.gdll(gid1)
     self.assertEqual(gp['shape'],gll['lat'].shape)
     self.assertEqual(gp['shape'],gll['lon'].shape)
     self.assertEqual(gp['lat0'],gll['lat'][0,0])
     self.assertEqual(gp['lon0'],gll['lon'][0,0])
     rmn.gdrls(gid1)
Beispiel #16
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.')
Beispiel #17
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)
Beispiel #18
0
 def test_gdxysval(self):
     gp1 = self.getGridParams_L()
     gid1 = rmn.ezqkdef(gp1)
     self.assertTrue(gid1>=0)
     zin = np.empty(gp1['shape'],dtype=np.float32,order='FORTRAN')
     for x in xrange(gp1['ni']):
         zin[:,x] = x
     xx = np.array([1.5],dtype=np.float32,order='FORTRAN')
     yy = np.array([1.5],dtype=np.float32,order='FORTRAN')
     zout = rmn.gdxysval(gid1,xx,yy,zin)
     self.assertEqual(xx.shape,zout.shape)
     self.assertTrue(abs((zin[0,0]+zin[1,1])/2. - zout[0]) < self.epsilon)
     rmn.gdrls(gid1)
Beispiel #19
0
    def test_ezuvint(self):
        gp1 = self.getGridParams_L()
        gid1 = rmn.ezqkdef(gp1)
        self.assertTrue(gid1>=0)
        gp2 = self.getGridParams_L(0.25)
        gid2 = rmn.ezqkdef(gp2)
        self.assertTrue(gid2>=0)
        setid = rmn.ezdefset(gid2, gid1)
        self.assertTrue(setid>=0)
        uuin = np.empty(gp1['shape'],dtype=np.float32,order='FORTRAN')
        vvin = np.empty(gp1['shape'],dtype=np.float32,order='FORTRAN')
        for x in xrange(gp1['ni']):
            uuin[:,x] = x
        vvin = uuin*3.
        (uuout,vvout) = rmn.ezuvint(gid2,gid1,uuin,vvin)
        self.assertEqual(gp2['shape'],uuout.shape)
        self.assertEqual(gp2['shape'],vvout.shape)
        for j in xrange(gp2['nj']):
            for i in xrange(gp2['ni']):
                self.assertTrue(abs((uuin[i,j]+uuin[i+1,j])/2.-uuout[i,j]) < self.epsilon,'uvint, u: abs(%f-%f)=%f' % (((uuin[i,j]+uuin[i+1,j])/2),uuout[i,j],(uuin[i,j]+uuin[i+1,j])/2.-uuout[i,j]))

                self.assertTrue(abs((vvin[i,j]+vvin[i+1,j])/2.-vvout[i,j]) < self.epsilon,'uvint, v: abs(%f-%f)=%f' % (((vvin[i,j]+vvin[i+1,j])/2),vvout[i,j],(vvin[i,j]+vvin[i+1,j])/2.-vvout[i,j]))
Beispiel #20
0
 def test_gdxysval(self):
     gp1 = self.getGridParams_L()
     gid1 = rmn.ezqkdef(gp1)
     self.assertTrue(gid1>=0)
     zin = np.empty(gp1['shape'],dtype=np.float32,order='FORTRAN')
     for x in range(gp1['ni']):
         zin[:,x] = x
     xx = np.array([1.5],dtype=np.float32,order='FORTRAN')
     yy = np.array([1.5],dtype=np.float32,order='FORTRAN')
     zout = rmn.gdxysval(gid1,xx,yy,zin)
     self.assertEqual(xx.shape,zout.shape)
     self.assertTrue(abs((zin[0,0]+zin[1,1])/2. - zout[0]) < self.epsilon)
     rmn.gdrls(gid1)
Beispiel #21
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}
Beispiel #22
0
    def test_ezuvint(self):
        gp1 = self.getGridParams_L()
        gid1 = rmn.ezqkdef(gp1)
        self.assertTrue(gid1>=0)
        gp2 = self.getGridParams_L(0.25)
        gid2 = rmn.ezqkdef(gp2)
        self.assertTrue(gid2>=0)
        setid = rmn.ezdefset(gid2, gid1)
        self.assertTrue(setid>=0)
        uuin = np.empty(gp1['shape'],dtype=np.float32,order='FORTRAN')
        vvin = np.empty(gp1['shape'],dtype=np.float32,order='FORTRAN')
        for x in range(gp1['ni']):
            uuin[:,x] = x
        vvin = uuin*3.
        (uuout,vvout) = rmn.ezuvint(gid2,gid1,uuin,vvin)
        self.assertEqual(gp2['shape'],uuout.shape)
        self.assertEqual(gp2['shape'],vvout.shape)
        for j in range(gp2['nj']):
            for i in range(gp2['ni']):
                self.assertTrue(abs((uuin[i,j]+uuin[i+1,j])/2.-uuout[i,j]) < self.epsilon,'uvint, u: abs(%f-%f)=%f' % (((uuin[i,j]+uuin[i+1,j])/2),uuout[i,j],(uuin[i,j]+uuin[i+1,j])/2.-uuout[i,j]))

                self.assertTrue(abs((vvin[i,j]+vvin[i+1,j])/2.-vvout[i,j]) < self.epsilon,'uvint, v: abs(%f-%f)=%f' % (((vvin[i,j]+vvin[i+1,j])/2),vvout[i,j],(vvin[i,j]+vvin[i+1,j])/2.-vvout[i,j]))
Beispiel #23
0
 def test_gdllsval(self):
     gp1 = self.getGridParams_L()
     gid1 = rmn.ezqkdef(gp1)
     self.assertTrue(gid1>=0)
     zin = np.empty(gp1['shape'],dtype=np.float32,order='FORTRAN')
     for x in range(gp1['ni']):
         zin[:,x] = x
     lat = np.array([gp1['lat0']+gp1['dlat']/2.],dtype=np.float32,order='FORTRAN')
     lon = np.array([(gp1['lon0']+gp1['dlon'])/2.],dtype=np.float32,order='FORTRAN')
     zout = rmn.gdllsval(gid1,lat,lon,zin)
     self.assertEqual(lat.shape,zout.shape)
     self.assertTrue(abs((zin[0,0]+zin[1,1])/2. - zout[0]) < self.epsilon)
     rmn.gdrls(gid1)
Beispiel #24
0
 def test_gdllsval(self):
     gp1 = self.getGridParams_L()
     gid1 = rmn.ezqkdef(gp1)
     self.assertTrue(gid1>=0)
     zin = np.empty(gp1['shape'],dtype=np.float32,order='FORTRAN')
     for x in xrange(gp1['ni']):
         zin[:,x] = x
     lat = np.array([gp1['lat0']+gp1['dlat']/2.],dtype=np.float32,order='FORTRAN')
     lon = np.array([(gp1['lon0']+gp1['dlon'])/2.],dtype=np.float32,order='FORTRAN')
     zout = rmn.gdllsval(gid1,lat,lon,zin)
     self.assertEqual(lat.shape,zout.shape)
     self.assertTrue(abs((zin[0,0]+zin[1,1])/2. - zout[0]) < self.epsilon)
     rmn.gdrls(gid1)
Beispiel #25
0
 def test_ezqkdef_ezgxprm(self):
     gp = self.getGridParams_L()
     gid1 = rmn.ezqkdef(gp['ni'],gp['nj'],gp['grtyp'],
                        gp['ig1'],gp['ig2'],gp['ig3'],gp['ig4'])
     self.assertTrue(gid1>=0)
     gp['id'] = gid1
     gp['grref'] = ''
     gp['ig1ref'] = 0
     gp['ig2ref'] = 0
     gp['ig3ref'] = 0
     gp['ig4ref'] = 0
     gprm = rmn.ezgxprm(gid1)
     for k in gprm.keys():
         self.assertEqual(gp[k],gprm[k])
     rmn.gdrls(gid1)
Beispiel #26
0
 def test_ezqkdef_ezgxprm(self):
     gp = self.getGridParams_L()
     gid1 = rmn.ezqkdef(gp['ni'],gp['nj'],gp['grtyp'],
                        gp['ig1'],gp['ig2'],gp['ig3'],gp['ig4'])
     self.assertTrue(gid1>=0)
     gp['id'] = gid1
     gp['grref'] = ''
     gp['ig1ref'] = 0
     gp['ig2ref'] = 0
     gp['ig3ref'] = 0
     gp['ig4ref'] = 0
     gprm = rmn.ezgxprm(gid1)
     for k in gprm.keys():
         self.assertEqual(gp[k],gprm[k])
     rmn.gdrls(gid1)
Beispiel #27
0
def _getGridHandle(ni, nj, grtyp, grref, ig1, ig2, ig3, ig4, i0, j0, xs, ys):
    ## if (_isGridValid(ni, nj, grtyp, grref, ig1, ig2, ig3, ig4, i0, j0, xs, ys)>=0) #TODO
    gdid = -1
    i0b  = 0
    j0b  = 0
    grtypZ = "Z"
    grtypY = "Y"
    grtyp = grtyp.upper().strip()
    if grtyp == 'Z' or grtyp == '#':
        if grtyp == '#':
            i0b = i0-1
            j0b = j0-1
        gdid = _rmn.ezgdef_fmem(ni, nj, grtypZ, grref, ig1, ig2, ig3, ig4, xs[i0b:, 0], ys[0, j0b:]);
    elif grtyp == 'Y':
        gdid = _rmn.ezgdef_fmem(ni, nj, grtypY, grref, ig1, ig2, ig3, ig4, xs[i0b:, j0b:], ys[i0b:, j0b:]);
    else:
        gdid = _rmn.ezqkdef(ni, nj, grtyp, ig1, ig2, ig3, ig4);
    return gdid
Beispiel #28
0
def _getGridHandle(ni, nj, grtyp, grref, ig1, ig2, ig3, ig4, i0, j0, xs, ys):
    ## if (_isGridValid(ni, nj, grtyp, grref, ig1, ig2, ig3, ig4, i0, j0, xs, ys)>=0) #TODO
    gdid = -1
    i0b  = 0
    j0b  = 0
    grtypZ = "Z"
    grtypY = "Y"
    grtyp = grtyp.upper().strip()
    if grtyp == 'Z' or grtyp == '#':
        if grtyp == '#':
            i0b = i0-1
            j0b = j0-1
        gdid = _rmn.ezgdef_fmem(ni, nj, grtypZ, grref, ig1, ig2, ig3, ig4, xs[i0b:, 0], ys[0, j0b:]);
    elif grtyp == 'Y':
        gdid = _rmn.ezgdef_fmem(ni, nj, grtypY, grref, ig1, ig2, ig3, ig4, xs[i0b:, j0b:], ys[i0b:, j0b:]);
    else:
        gdid = _rmn.ezqkdef(ni, nj, grtyp, ig1, ig2, ig3, ig4);
    return gdid
Beispiel #29
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)
Beispiel #30
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
Beispiel #31
0
def readRec(fname, nomvar):
    """
    Read specified record data from file and get its grid definition

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

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

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

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

    # Close File
    rmn.fstcloseall(funit)

    return rec
Beispiel #32
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
Beispiel #33
0
def readRec(fname, nomvar):
    """
    Read specified record data from file and get its grid definition

    Args:
       fname  (str): Filename to read from
       nomvar (str): Record varname to read
    Returns:
       rec : Record meta, data and grid definition
    """
    print("+ Read %s from: %s" % (nomvar, fname))
    
    # Open File
    try:
        funit = rmn.fstopenall(fname, rmn.FST_RO)
    except:
        raise rmn.FSTDError("Problem Opening file: %s" % fname)
    
    # Read Record data and meta
    try:
        rec = rmn.fstlir(funit, nomvar=nomvar)
    except:
        raise rmn.FSTDError("Problem Reading %s record" % nomvar)
    
    # Get associated grid
    try:
        rec['iunit'] = funit
        rec_gridid   = rmn.ezqkdef(rec)  # use ezscint to get grid id
        rec['grid']  = rmn.decodeGrid(rec_gridid)
        del(rec['iunit']) # iunit will be irrelevant after file is closed below
    except:
        sys.stderr.write('Error: Problem getting grid info for '+nomvar+' in file: '+fname+'\n')
        sys.exit(1)
        
    # Close File
    rmn.fstcloseall(funit)
    
    return rec
Beispiel #34
0
 def test_ezqkdef_file_error(self):
     funit = -1
     (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
         }
     try:
         gid1 = rmn.ezqkdef(gp)
         self.assertTrue(False, 'ezqkdef should raise a error with ref grid and invalid file unit')
     except rmn.EzscintError:
         pass
Beispiel #35
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
Beispiel #36
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],
       [ 148.5       ],
       [ 148.58999634],
       [ 148.68000793],
       [ 148.77000427],
       [ 148.86000061],
       [ 148.94999695],
Beispiel #37
0
    # 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)
    except:
        raise IOError('Problem reading the lola file: %s' % (options.lolafile))
Beispiel #38
0
    def test_13(self):
        """
        Interpolating Data

        Interpolating data to/from known FSTD grids is made easy with the Ezscint package.
        There are a few exceptions though
        * you can only interpolate to a Y grid, not from it.
        * multi-parts grids (Yin-Yang, ...) have to be dealth with in a special way (see below)
        In this example we'll interpolate forecast data onto the analysis grid to make some computations
        
        See also:
        """
        import os
        import rpnpy.librmn.all as rmn

        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES')
        fileName0 = os.path.join(ATM_MODEL_DFILES.strip(),
                                 'bcmk/2009042700_000')  #Analysis
        fileName1 = os.path.join(ATM_MODEL_DFILES.strip(),
                                 'bcmk/2009042700_012')  #Forecast

        # Get data and grid definition for P0 in the 1st FSTD file
        try:
            fileId = rmn.fstopenall(fileName0, rmn.FST_RO)
            p0Data1 = rmn.fstlir(
                fileId, nomvar='P0'
            )  # Get the record data and metadata along with partial grid info
            p0Data1['iunit'] = fileId
            p0GridId = rmn.ezqkdef(
                p0Data1)  # use ezscint to retreive a grid id
            p0Grid1 = rmn.decodeGrid(
                p0GridId)  # Decode all the grid parameters values
            rmn.fstcloseall(fileId)
        except:
            raise rmn.FSTDError(
                "Problem getting P0 record grid meta from file: %s" %
                fileName0)

        # Get data and grid definition for P0 in the 2nd FSTD file
        try:
            fileId = rmn.fstopenall(fileName1, rmn.FST_RO)
            p0Data2 = rmn.fstlir(
                fileId, nomvar='P0', ip2=12
            )  # Get the record data and metadata along with partial grid info
            p0Data2['iunit'] = fileId
            p0GridId = rmn.ezqkdef(
                p0Data2)  # use ezscint to retreive a grid id
            p0Grid2 = rmn.decodeGrid(
                p0GridId)  # Decode all the grid parameters values
            rmn.fstcloseall(fileId)
        except:
            raise rmn.FSTDError(
                "Problem getting P0 record grid meta from file: %s " %
                fileName1)

        # Make a cubic interpolation of p0Data2 onto p0Grid1 with extrapolated values set to Minvalue of the field
        rmn.ezsetopt(rmn.EZ_OPT_EXTRAP_DEGREE, rmn.EZ_EXTRAP_MIN)
        rmn.ezsetopt(rmn.EZ_OPT_INTERP_DEGREE, rmn.EZ_INTERP_LINEAR)
        p0Data2_onGrid1 = rmn.ezsint(p0Grid1['id'], p0Grid2['id'],
                                     p0Data2['d'])

        # Make some computation
        p0Diff = p0Data2_onGrid1 - p0Data1['d']
Beispiel #39
0
def getGrid(dataKey, fileID):
    fileMeta = rmn.fstprm(dataKey)
    fileMeta['iunit'] = fileID
    gridID = rmn.ezqkdef(fileMeta)
    return gridID
Beispiel #40
0
level = 76696048

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

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

try:
    rec['iunit'] = file
    gridid = rmn.ezqkdef(rec)  # use ezscint to retreive full grid
    gridLatLon = rmn.gdll(gridid)
    lat = gridLatLon['lat']
    lon = gridLatLon['lon']
except:
    sys.stderr.write('Error: Problem getting grid info in file')
    sys.exit(1)
######
######

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

    #Get the list of ip1 on thermo levels in this file
    #tlvl = vgd.vgd_get(v, 'VIPT')
Beispiel #41
0
def read_fst(file, var, level, fhour, yy):
    global field
    global lat
    global lon
    print var
    print level
    print fhour
    print yy
    try:
        file = rmn.fstopenall(file, rmn.FST_RO)
    except:
        sys.stderr.write("Problem opening the file: %s\n" % file)

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

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

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

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

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

        # Trim the list of thermo ip1 to actual levels in files for TT
        # since the vgrid in the file is a super set of all levels
        # and get their "key"
        tlvlkeys = []
        rshape = None
        #for ip1 in tlvl:
        (lval, lkind) = rmn.convertIp(rmn.CONVIP_DECODE, level)
        key = rmn.fstinf(file,
                         nomvar=var,
                         ip2=fhour,
                         ip1=rmn.ip1_all(lval, lkind))
        if key is not None:
            tlvlkeys.append((level, key['key']))
            if rshape is None:
                rshape = key['shape']
        rshape = (rshape[0], rshape[1], len(tlvlkeys))

        r2d = {'d': None}
        r3d = None
        k = 0
        try:
            r2d = rmn.fstluk(key, dataArray=r2d['d'])
            if r3d is None:
                r3d = r2d.copy()
                r3d['d'] = np.empty(rshape,
                                    dtype=r2d['d'].dtype,
                                    order='FORTRAN')
            r3d['d'][:, :, k] = r2d['d'][:, :]
            k += 1
        except:
            raise

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

    # Add the vgrid and the actual ip1 list in the r3d dict, update shape and nk
    r3d['vgd'] = v
    r3d['ip1list'] = [x[0] for x in tlvlkeys]
    r3d['shape'] = rshape
    r3d['nk'] = rshape[2]

    field = r3d['d']

    return (field, lat, lon)
Beispiel #42
0
        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)
    except:
        raise IOError('Problem reading the lola file: %s' % (options.lolafile))
    
    # Interpolate input data to lat lon and print
    rmn.ezsetopt(rmn.EZ_OPT_INTERP_DEGREE,inttypelist[inttype])
    #rmn.ezsetopt(rmn.EZ_OPT_EXTRAP_DEGREE,rmn.EZ_EXTRAP_MAX)