Exemplo n.º 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']
Exemplo n.º 2
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)
Exemplo n.º 3
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 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)
Exemplo n.º 4
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
Exemplo n.º 5
0
def ezinterp(arrayin, arrayin2,
             src_nij, src_grtyp, src_refparam, src_xyAxis, src_hasAxis, src_ij0,
             dst_nij, dst_grtyp, dst_refparam, dst_xyAxis, dst_hasAxis, dst_ij0,
             isVect):
    """Interpolate from one grid to another
    newArray = Fstdc.ezinterp(arrayin, arrayin2,
        (niS, njS), grtypS, (grrefS, ig1S, ig2S, ig3S, ig4S), (xsS, ysS), hasSrcAxis, (i0S, j0S),
        (niD, njD), grtypD, (grrefD, ig1D, ig2D, ig3D, ig4D), (xsD, ysD), hasDstAxis, (i0D, j0D),
        isVect)
    @param ...TODO...
    @return interpolated data (numpy.ndarray)
    @exception TypeError
    @exception Fstdc.error
    """
    try:
        src_gid = _getGridHandle(src_nij[0], src_nij[1], src_grtyp,
                                 src_refparam[0], src_refparam[1], src_refparam[2],
                                 src_refparam[3], src_refparam[4],
                                 src_ij0[0], src_ij0[1], src_xyAxis[0], src_xyAxis[1])
    except:
        raise error("ezgetlalo: Invalid Source Grid Desc")
    try:
        dst_gid = _getGridHandle(dst_nij[0], dst_nij[1], dst_grtyp,
                                 dst_refparam[0], dst_refparam[1], dst_refparam[2],
                                 dst_refparam[3], dst_refparam[4],
                                 dst_ij0[0], dst_ij0[1], dst_xyAxis[0], dst_xyAxis[1])
    except:
        raise error("ezgetlalo: Invalid Source Grid Desc")
    try:
        gridsetid = _rmn.ezdefset(dst_gid, src_gid)
    except:
        raise error("Problem defining a grid interpolation set")
    try:
        if isVect:
            dst_data = _rmn.ezuvint(dst_gid, src_gid, arrayin, arrayin2)
        else:
            dst_data = _rmn.ezsint(dst_gid, src_gid, arrayin)
        return dst_data
    except:
        raise error("Interpolation problem in ezscint")
Exemplo n.º 6
0
def ezinterp(arrayin, arrayin2,
             src_nij, src_grtyp, src_refparam, src_xyAxis, src_hasAxis, src_ij0,
             dst_nij, dst_grtyp, dst_refparam, dst_xyAxis, dst_hasAxis, dst_ij0,
             isVect):
    """Interpolate from one grid to another
    newArray = Fstdc.ezinterp(arrayin, arrayin2,
        (niS, njS), grtypS, (grrefS, ig1S, ig2S, ig3S, ig4S), (xsS, ysS), hasSrcAxis, (i0S, j0S),
        (niD, njD), grtypD, (grrefD, ig1D, ig2D, ig3D, ig4D), (xsD, ysD), hasDstAxis, (i0D, j0D),
        isVect)
    @param ...TODO...
    @return interpolated data (numpy.ndarray)
    @exception TypeError
    @exception Fstdc.error
    """
    try:
        src_gid = _getGridHandle(src_nij[0], src_nij[1], src_grtyp,
                                 src_refparam[0], src_refparam[1], src_refparam[2],
                                 src_refparam[3], src_refparam[4],
                                 src_ij0[0], src_ij0[1], src_xyAxis[0], src_xyAxis[1])
    except:
        raise error("ezgetlalo: Invalid Source Grid Desc")
    try:
        dst_gid = _getGridHandle(dst_nij[0], dst_nij[1], dst_grtyp,
                                 dst_refparam[0], dst_refparam[1], dst_refparam[2],
                                 dst_refparam[3], dst_refparam[4],
                                 dst_ij0[0], dst_ij0[1], dst_xyAxis[0], dst_xyAxis[1])
    except:
        raise error("ezgetlalo: Invalid Source Grid Desc")
    try:
        gridsetid = _rmn.ezdefset(dst_gid, src_gid)
    except:
        raise error("Problem defining a grid interpolation set")
    try:
        if isVect:
            dst_data = _rmn.ezuvint(dst_gid, src_gid, arrayin, arrayin2)
        else:
            dst_data = _rmn.ezsint(dst_gid, src_gid, arrayin)
        return dst_data
    except:
        raise error("Interpolation problem in ezscint")
Exemplo n.º 7
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
Exemplo n.º 8
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
Exemplo n.º 9
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']
Exemplo n.º 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'

        # 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