Example #1
0
 def test_ezsetopt_ezgetopt(self):
     otplist = [
         (rmn.EZ_OPT_WEIGHT_NUMBER,2), #int
         (rmn.EZ_OPT_EXTRAP_VALUE,99.), #float
         (rmn.EZ_OPT_EXTRAP_DEGREE.lower(),rmn.EZ_EXTRAP_VALUE.lower()) #str
         ]
     for (o,v) in otplist:
         rmn.ezsetopt(o,v)
         v1 = rmn.ezgetopt(o,vtype=type(v))
         self.assertEqual(v1,v)
Example #2
0
 def test_ezsetopt_ezgetopt(self):
     otplist = [
         (rmn.EZ_OPT_WEIGHT_NUMBER,2), #int
         (rmn.EZ_OPT_EXTRAP_VALUE,99.), #float
         (rmn.EZ_OPT_EXTRAP_DEGREE.lower(),rmn.EZ_EXTRAP_VALUE.lower()) #str
         ]
     for (o,v) in otplist:
         rmn.ezsetopt(o,v)
         v1 = rmn.ezgetopt(o,vtype=type(v))
         self.assertEqual(v1,v)
Example #3
0
def ezsetopt(option, value):
    """Set the string representation of one of the internal ezscint options
        opt_val = Fstrc.ezsetopt(option, value)
        @type option, value: A string
        @param option: The ezscint option 
        @param value: The value to set
    """
    try:
        _rmn.ezsetopt(option, value)
    except:
        raise error('Problem setting ezopt value')
Example #4
0
def ezsetopt(option, value):
    """Set the string representation of one of the internal ezscint options
        opt_val = Fstrc.ezsetopt(option, value)
        @type option, value: A string
        @param option: The ezscint option 
        @param value: The value to set
    """
    try:
        _rmn.ezsetopt(option, value)
    except:
        raise error('Problem setting ezopt value')
Example #5
0
def ezsetval(option, value):
    """Set an internal ezscint float or integer value by keyword
        opt_val = Fstdc.ezgetval(option, value)
        @type option: A string
        @param option: The keyword of the option to retrieve
        @type value: Float or integer, as appropriate for the option
        @param value: The value to set
    """
    try:
        _rmn.ezsetopt(option, value)
    except:
        raise error('Problem setting ezopt value')
Example #6
0
def ezsetval(option, value):
    """Set an internal ezscint float or integer value by keyword
        opt_val = Fstdc.ezgetval(option, value)
        @type option: A string
        @param option: The keyword of the option to retrieve
        @type value: Float or integer, as appropriate for the option
        @param value: The value to set
    """
    try:
        _rmn.ezsetopt(option, value)
    except:
        raise error('Problem setting ezopt value')
Example #7
0
    def test_13(self):
        """
        Interpolating Data

        Interpolating data to/from known FSTD grids is made easy with the Ezscint package.
        There are a few exceptions though
        * you can only interpolate to a Y grid, not from it.
        * multi-parts grids (Yin-Yang, ...) have to be dealth with in a special way (see below)
        In this example we'll interpolate forecast data onto the analysis grid to make some computations
        
        See also:
        """
        import os
        import rpnpy.librmn.all as rmn
 
        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES')
        fileName0 = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_000')  #Analysis
        fileName1 = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_012')  #Forecast
 
        # Get data and grid definition for P0 in the 1st FSTD file
        try:
            fileId   = rmn.fstopenall(fileName0, rmn.FST_RO)
            p0Data1  = rmn.fstlir(fileId, nomvar='P0')  # Get the record data and metadata along with partial grid info
            p0Data1['iunit'] = fileId
            p0GridId = rmn.ezqkdef(p0Data1)             # use ezscint to retreive a grid id
            p0Grid1  = rmn.decodeGrid(p0GridId)         # Decode all the grid parameters values
            rmn.fstcloseall(fileId)
        except:
            raise rmn.FSTDError("Problem getting P0 record grid meta from file: %s" % fileName0)
 
        # Get data and grid definition for P0 in the 2nd FSTD file
        try:
            fileId   = rmn.fstopenall(fileName1, rmn.FST_RO)
            p0Data2  = rmn.fstlir(fileId, nomvar='P0', ip2=12)  # Get the record data and metadata along with partial grid info
            p0Data2['iunit'] = fileId
            p0GridId = rmn.ezqkdef(p0Data2)                     # use ezscint to retreive a grid id
            p0Grid2  = rmn.decodeGrid(p0GridId)                 # Decode all the grid parameters values
            rmn.fstcloseall(fileId)
        except:
            raise rmn.FSTDError("Problem getting P0 record grid meta from file: %s " % fileName1)
 
        # Make a cubic interpolation of p0Data2 onto p0Grid1 with extrapolated values set to Minvalue of the field
        rmn.ezsetopt(rmn.EZ_OPT_EXTRAP_DEGREE, rmn.EZ_EXTRAP_MIN)
        rmn.ezsetopt(rmn.EZ_OPT_INTERP_DEGREE, rmn.EZ_INTERP_LINEAR)
        p0Data2_onGrid1 = rmn.ezsint(p0Grid1['id'], p0Grid2['id'], p0Data2['d'])
 
        # Make some computation
        p0Diff = p0Data2_onGrid1 - p0Data1['d']
Example #8
0
 def test_41qd(self):
     import os, sys, datetime
     import rpnpy.librmn.all as rmn
     fdate       = datetime.date.today().strftime('%Y%m%d') + '00_048'
     CMCGRIDF    = os.getenv('CMCGRIDF').strip()
     fileNameOut = 'p0fstfileqd.fst'
     fileIdIn    = rmn.fstopenall(os.getenv('CMCGRIDF')+'/prog/regeta/'+fdate)
     fileIdOut   = rmn.fstopenall(fileNameOut, rmn.FST_RW)
     gOut = rmn.defGrid_ZE(90, 45, 35., 250., 0.5, 0.5, 0., 180., 1., 270.)
     r    = rmn.fstlir(fileIdIn, nomvar='P0')
     gIn  = rmn.readGrid(fileIdIn, r)
     rmn.ezsetopt(rmn.EZ_OPT_INTERP_DEGREE, rmn.EZ_INTERP_LINEAR)
     d  = rmn.ezsint(gOut, gIn, r)
     r2 = r.copy()
     r2.update(gOut)
     r2.update({'etiket':'my_etk', 'd':d})
     rmn.fstecr(fileIdOut, r2)
     rmn.writeGrid(fileIdOut, gOut)
     rmn.fstcloseall(fileIdIn)
     rmn.fstcloseall(fileIdOut)
     os.unlink(fileNameOut)  # Remove test file
Example #9
0
 def test_41qd(self):
     import os, sys, datetime
     import rpnpy.librmn.all as rmn
     fdate = datetime.date.today().strftime('%Y%m%d') + '00_048'
     CMCGRIDF = os.getenv('CMCGRIDF').strip()
     fileNameOut = 'p0fstfileqd.fst'
     fileIdIn = rmn.fstopenall(
         os.getenv('CMCGRIDF') + '/prog/regeta/' + fdate)
     fileIdOut = rmn.fstopenall(fileNameOut, rmn.FST_RW)
     gOut = rmn.defGrid_ZE(90, 45, 35., 250., 0.5, 0.5, 0., 180., 1., 270.)
     r = rmn.fstlir(fileIdIn, nomvar='P0')
     gIn = rmn.readGrid(fileIdIn, r)
     rmn.ezsetopt(rmn.EZ_OPT_INTERP_DEGREE, rmn.EZ_INTERP_LINEAR)
     d = rmn.ezsint(gOut, gIn, r)
     r2 = r.copy()
     r2.update(gOut)
     r2.update({'etiket': 'my_etk', 'd': d})
     rmn.fstecr(fileIdOut, r2)
     rmn.writeGrid(fileIdOut, gOut)
     rmn.fstcloseall(fileIdIn)
     rmn.fstcloseall(fileIdOut)
     os.unlink(fileNameOut)  # Remove test file
Example #10
0
    def test_41(self):
        """
        Horizontal Interpolation
        
        See also:
        """
        import os, sys, datetime
        import rpnpy.librmn.all as rmn
        fdate = datetime.date.today().strftime('%Y%m%d') + '00_048'
        CMCGRIDF = os.getenv('CMCGRIDF').strip()
        fileNameIn = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate)
        fileNameOut = 'p0fstfile.fst'

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

        try:
            # Create Destination grid
            # Note: Destination grid can also be read from a file
            gp = {
                'grtyp': 'Z',
                'grref': 'E',
                'ni': 90,
                'nj': 45,
                'lat0': 35.,
                'lon0': 250.,
                'dlat': 0.5,
                'dlon': 0.5,
                'xlat1': 0.,
                'xlon1': 180.,
                'xlat2': 1.,
                'xlon2': 270.
            }
            gOut = rmn.encodeGrid(gp)
            print("CB41: Defined a %s/%s grid of shape=%d, %d" %
                  (gOut['grtyp'], gOut['grref'], gOut['ni'], gOut['nj']))
        except:
            sys.stderr.write("Problem creating grid\n")
            sys.exit(1)

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

        try:
            # Find and read record to interpolate with its grid
            r = rmn.fstlir(fileIdIn, nomvar='P0')
            gIn = rmn.readGrid(fileIdIn, r)
            print("CB41: Read P0")

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

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

            # Write record data + meta + grid to file
            rmn.fstecr(fileIdOut, r2)
            rmn.writeGrid(fileIdOut, gOut)
            print("CB41: Wrote interpolated P0 and its grid")
        except:
            pass
        finally:
            # Properly close files even if an error occured above
            # This is important when editing to avoid corrupted files
            rmn.fstcloseall(fileIdIn)
            rmn.fstcloseall(fileIdOut)
            os.unlink(fileNameOut)  # Remove test file
Example #11
0
    def test_13(self):
        """
        Interpolating Data

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

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

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

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

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

        # Make some computation
        p0Diff = p0Data2_onGrid1 - p0Data1['d']
Example #12
0
        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)

    (ni, nj) = data.shape
    outfile = open(options.outfile, 'w')
    for n in range(lat.size):
        (lat2, lon2) = (np.asarray([lat[n]]), np.asarray([lon[n]]))
        lldata2 = rmn.gdllsval(grid, lat2, lon2, data)
        xypos2 = rmn.gdxyfll(grid, lat2, lon2)
        extrap = ''
        if (xypos2['x'][0] < 1. or xypos2['x'][0] > ni or xypos2['y'][0] < 1.
                or xypos2['y'][0] > nj):
            extrap = 'extrap'
        outfile.write("%9.5f, %9.5f, %9.5f, %s\n" %
                      (lon[n], lat[n], lldata2[0], extrap))
        del lldata2, lat2, lon2, xypos2
Example #13
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
Example #14
0
    def test_41(self):
        """
        Horizontal Interpolation
        
        See also:
        """
        import os, sys, datetime
        import rpnpy.librmn.all as rmn
        fdate       = datetime.date.today().strftime('%Y%m%d') + '00_048'
        CMCGRIDF    = os.getenv('CMCGRIDF').strip()
        fileNameIn  = os.path.join(CMCGRIDF, 'prog', 'regeta', fdate)
        fileNameOut = 'p0fstfile.fst'

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

        try:
            # Create Destination grid
            # Note: Destination grid can also be read from a file
            gp = {
                'grtyp' : 'Z',
                'grref' : 'E',
                'ni'    : 90,
                'nj'    : 45,
                'lat0'  : 35.,
                'lon0'  : 250.,
                'dlat'  : 0.5,
                'dlon'  : 0.5,
                'xlat1' : 0.,
                'xlon1' : 180.,
                'xlat2' : 1.,
                'xlon2' : 270.
                }
            gOut = rmn.encodeGrid(gp)
            print("CB41: Defined a %s/%s grid of shape=%d, %d" %
                  (gOut['grtyp'], gOut['grref'], gOut['ni'], gOut['nj']))
        except:
            sys.stderr.write("Problem creating grid\n")
            sys.exit(1)

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

        try:
            # Find and read record to interpolate with its grid 
            r = rmn.fstlir(fileIdIn, nomvar='P0')
            gIn = rmn.readGrid(fileIdIn, r)
            print("CB41: Read P0")

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

            # Create new record to write with interpolated data and 
            r2 = r.copy()    # Preserve meta from original record
            r2.update(gOut)  # update grid information
            r2.update({      # attach data and update specific meta
                'etiket': 'my_etk',
                'd'     : d
                })
            
            # Write record data + meta + grid to file
            rmn.fstecr(fileIdOut, r2)
            rmn.writeGrid(fileIdOut, gOut)
            print("CB41: Wrote interpolated P0 and its grid")
        except:
            pass
        finally:
            # Properly close files even if an error occured above
            # This is important when editing to avoid corrupted files
            rmn.fstcloseall(fileIdIn)
            rmn.fstcloseall(fileIdOut)
            os.unlink(fileNameOut)  # Remove test file
Example #15
0
    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)

    (ni,nj) = data.shape
    outfile = open(options.outfile, 'w')
    for n in xrange(lat.size):
        (lat2,lon2) = (np.asarray([lat[n]]),np.asarray([lon[n]]))
        lldata2 = rmn.gdllsval(grid, lat2, lon2, data)
        xypos2  = rmn.gdxyfll(grid, lat2, lon2)
        extrap  = ''
        if (xypos2['x'][0] < 1. or xypos2['x'][0] > ni or
            xypos2['y'][0] < 1. or xypos2['y'][0] > nj):
            extrap='extrap'
        outfile.write("%9.5f, %9.5f, %9.5f, %s\n" %
                      (lon[n], lat[n], lldata2[0], extrap))
        del lldata2, lat2, lon2, xypos2