예제 #1
0
def __myGetAxisWeights(x, i, axisoptions=None):
    """ Assume x is an MV2 duh! and get the weights associated with axis index i.
    index i can be any of the dimensions in x whose bounds are available.
    If bounds are not available then an error is raised.
    """
    if x.getAxis(i).isLatitude():
        Lataxis = x.getAxis(i)
        xgr = cdms2.createGenericGrid(Lataxis[:], numpy.array(range(1), numpy.float), latBounds=Lataxis.getBounds())
        xlatwt, xlonwt = xgr.getWeights()
        return xlatwt
    elif x.getAxis(i).isLongitude():
        Lonaxis = x.getAxis(i)
        xgr = cdms2.createGenericGrid(numpy.array(range(1), numpy.float), Lonaxis[:], lonBounds=Lonaxis.getBounds())
        xlatwt, xlonwt = xgr.getWeights()
        return xlonwt
    else:
        axis_bounds = x.getAxis(i).getBounds()
        if axis_bounds is None:
	    ax=x.getAxis(i)
            if axisoptions is not None:
                axo=cdms2.orderparse(axisoptions)
                if (i in axo ) or (ax.isTime() and 't' in axo) or (ax.isLevel() and 'z' in axo) or ('('+ax.id+')' in axo):
                    raise AveragerError, 'Bounds not available to compute weights on dimension: '+ax.id
                else:
                    axis_wts=numpy.ones(x.getAxis(i)[:].shape)
            else:
                raise AveragerError, 'Bounds not available to compute weights for dimension: '+ax.id
        else:
            axis_wts =  abs(axis_bounds[..., 1] - axis_bounds[..., 0])
        return axis_wts
예제 #2
0
파일: averager.py 프로젝트: AZed/uvcdat
def __myGetAxisWeights(x, i, axisoptions=None):
    """ Assume x is an MV2 duh! and get the weights associated with axis index i.
    index i can be any of the dimensions in x whose bounds are available.
    If bounds are not available then an error is raised.
    """
    if x.getAxis(i).isLatitude():
        Lataxis = x.getAxis(i)
        xgr = cdms2.createGenericGrid(Lataxis[:], numpy.array(range(1), numpy.float), latBounds=Lataxis.getBounds())
        xlatwt, xlonwt = xgr.getWeights()
        return xlatwt
    elif x.getAxis(i).isLongitude():
        Lonaxis = x.getAxis(i)
        xgr = cdms2.createGenericGrid(numpy.array(range(1), numpy.float), Lonaxis[:], lonBounds=Lonaxis.getBounds())
        xlatwt, xlonwt = xgr.getWeights()
        return xlonwt
    else:
        axis_bounds = x.getAxis(i).getBounds()
        if axis_bounds is None:
	    ax=x.getAxis(i)
            if axisoptions is not None:
                axo=cdms2.orderparse(axisoptions)
                if (i in axo ) or (ax.isTime() and 't' in axo) or (ax.isLevel() and 'z' in axo) or ('('+ax.id+')' in axo):
                    raise AveragerError, 'Bounds not available to compute weights on dimension: '+ax.id
                else:
                    axis_wts=numpy.ones(x.getAxis(i)[:].shape)
            else:
                raise AveragerError, 'Bounds not available to compute weights for dimension: '+ax.id
        else:
            axis_wts =  abs(axis_bounds[..., 1] - axis_bounds[..., 0])
        return axis_wts
예제 #3
0
def __myGetAxisWeightsByName(x, name):
    """ Get the weights associated with axis name.
    The name can be any of the dimensions in x whose bounds are available.
    If bounds are not available then an error is raised. 

    This is a slight modifications on the _myGetAxisWeight. The only difference
    is that the name is used to define the axis instead of the index.
    """
    if x.getAxisList(axes=name)[0].isLatitude():
        Lataxis = x.getAxisList(axes=name)[0]
        xgr = cdms2.createGenericGrid(Lataxis[:],
                                      numpy.array(range(1), numpy.float),
                                      latBounds=Lataxis.getBounds())
        xlatwt, xlonwt = xgr.getWeights()
        return xlatwt
    elif x.getAxisList(axes=name)[0].isLongitude():
        Lonaxis = x.getAxisList(axes=name)[0]
        xgr = cdms2.createGenericGrid(numpy.array(range(1), numpy.float),
                                      Lonaxis[:],
                                      lonBounds=Lonaxis.getBounds())
        xlatwt, xlonwt = xgr.getWeights()
        return xlonwt
    else:
        axis_bounds = x.getAxisList(axes=name)[0].getBounds()
        if not axis_bounds:
            raise AveragerError, 'Bounds not available to compute weights'
        else:
            axis_wts = abs(axis_bounds[..., 1] - axis_bounds[..., 0])
            return axis_wts
예제 #4
0
파일: averager.py 프로젝트: AZed/uvcdat
def __myGetAxisWeightsByName(x, name):
    """ Get the weights associated with axis name.
    The name can be any of the dimensions in x whose bounds are available.
    If bounds are not available then an error is raised. 

    This is a slight modifications on the _myGetAxisWeight. The only difference
    is that the name is used to define the axis instead of the index.
    """
    if x.getAxisList( axes = name )[0].isLatitude():
        Lataxis = x.getAxisList( axes = name )[0]
        xgr = cdms2.createGenericGrid(Lataxis[:], numpy.array(range(1), numpy.float), latBounds=Lataxis.getBounds())
        xlatwt, xlonwt = xgr.getWeights()
        return xlatwt
    elif x.getAxisList( axes = name )[0].isLongitude():
        Lonaxis = x.getAxisList( axes = name )[0]
        xgr = cdms2.createGenericGrid(numpy.array(range(1), numpy.float), Lonaxis[:], lonBounds=Lonaxis.getBounds())
        xlatwt, xlonwt = xgr.getWeights()
        return xlonwt
    else:
        axis_bounds = x.getAxisList( axes = name )[0].getBounds()
        if not axis_bounds:
            raise AveragerError, 'Bounds not available to compute weights'
        else:
            axis_wts =  abs(axis_bounds[..., 1] - axis_bounds[..., 0])
            return axis_wts
예제 #5
0
 def get(self):
     value=self.grid
     if value is None:
         if not self.latitude.n is None: # Ok we seem to have grid definition
             if self.latitude.type == 1: # Gaussian
                 if self.longitude.first is None:
                     value=cdms2.createGaussianGrid(self.latitude.n)
                 else:
                     value=cdms2.createGaussianGrid(self.latitude.n,self.longitude.first)
             elif self.latitude.type == 0: # Uniform
                 value=cdms2.createUniformGrid(self.latitude.first,
                                              self.latitude.n,
                                              self.latitude.delta,
                                              self.longitude.first,
                                              self.longitude.n,
                                              self.longitude.delta)
             elif self.latitude.type== 2: # Equalarea
                 lat=cdms2.createEqualAreaAxis(self.latitude.n)
                 lon=cdms2.createUniformLongitude(self.longitude.first,
                                                 self.longitude.n,
                                                 self.longitude.delta)
                 value=cdms2.createGenericGrid(lat[:],lon[:],lat.getBounds(),lon.getBounds())
         elif not self.file is None:
             f=cdms2.open(self.file)
             value=f[self.var].getGrid()
             ## Ok temporary stuff to try to be able to close the file
             lat=value.getLatitude()
             lon=value.getLongitude()
             value=cdms2.createRectGrid(lat,lon)
             f.close()
     return value
예제 #6
0
파일: ParaRMT.py 프로젝트: imclab/parallel
def getgrid(grid="coarse"):
    file1 =sys.prefix+'/sample_data/hadcrut2_sample.nc'
    a = cdms.open(file1) 
    data=a('temanom')
    if grid=="coarse":
        
        LATS=data.getLatitude()[::4]
        ub=data.getLatitude().getBounds()[1::4][:,0]
        lb=data.getLatitude().getBounds()[::4][:,1]
        latbounds=np.array(zip(ub,lb))
        
        LONS=data.getLongitude()[::4]
        ub=data.getLongitude().getBounds()[1::4][:,0]
        lb=data.getLongitude().getBounds()[::4][:,1]
        lonbounds=np.array(zip(ub,lb))
    else:
        
        LATS=data.getLatitude()[::2]
        ub=data.getLatitude().getBounds()[1::2][:,0]
        lb=data.getLatitude().getBounds()[::2][:,1]
        latbounds=np.array(zip(ub,lb))
        
        LONS=data.getLongitude()[::2]
        ub=data.getLongitude().getBounds()[1::2][:,0]
        lb=data.getLongitude().getBounds()[::2][:,1]
        lonbounds=np.array(zip(ub,lb))
    return cdms.createGenericGrid(LATS,LONS,latBounds=latbounds,lonBounds=lonbounds)
예제 #7
0
 def get(self):
     value = self.grid
     if value is None:
         if not self.latitude.n is None:  # Ok we seem to have grid definition
             if self.latitude.type == 1:  # Gaussian
                 if self.longitude.first is None:
                     value = cdms2.createGaussianGrid(self.latitude.n)
                 else:
                     value = cdms2.createGaussianGrid(
                         self.latitude.n, self.longitude.first)
             elif self.latitude.type == 0:  # Uniform
                 value = cdms2.createUniformGrid(self.latitude.first,
                                                 self.latitude.n,
                                                 self.latitude.delta,
                                                 self.longitude.first,
                                                 self.longitude.n,
                                                 self.longitude.delta)
             elif self.latitude.type == 2:  # Equalarea
                 lat = cdms2.createEqualAreaAxis(self.latitude.n)
                 lon = cdms2.createUniformLongitude(self.longitude.first,
                                                    self.longitude.n,
                                                    self.longitude.delta)
                 value = cdms2.createGenericGrid(lat[:], lon[:],
                                                 lat.getBounds(),
                                                 lon.getBounds())
         elif not self.file is None:
             f = cdms2.open(self.file)
             value = f[self.var].getGrid()
             ## Ok temporary stuff to try to be able to close the file
             lat = value.getLatitude()
             lon = value.getLongitude()
             value = cdms2.createRectGrid(lat, lon)
             f.close()
     return value
예제 #8
0
def makeGrid(xstart=0, xend=359, xstep=1, ystart=-90, yend=89, ystep=1):

    lon_bnds=[]
    lon=[]
    for ii in numpy.arange(xstart, xend, xstep):
        lon_bnds.append( [ii, ii+xstep] )
        lon.append(ii+0.5*xstep)
    lon_bnds=numpy.array(lon_bnds)
    lon=numpy.array(lon)

    lat_bnds=[]
    lat=[]
    for ii in numpy.arange(ystart, yend, ystep):
        lat_bnds.append([ii, ii+ystep])
        lat.append(ii+0.5*ystep)
    lat_bnds=numpy.array(lat_bnds)
    lat=numpy.array(lat)

    latAxis = cdms2.createAxis(lat, lat_bnds)
    latAxis.designateLatitude(True)
    latAxis.units='degrees_north'
    latAxis.long_name='Latitude'
    latAxis.id='latitude'

    lonAxis = cdms2.createAxis(lon, lon_bnds)
    lonAxis.designateLongitude(True, 360.0)
    lonAxis.units='degrees_east'
    lonAxis.id='longitude'
    lonAxis.long_name='Longitude'

    return((cdms2.createGenericGrid(latAxis, lonAxis, lat_bnds, lon_bnds), latAxis, lonAxis, lat_bnds, lon_bnds))
예제 #9
0
def makeGrid():
    xstart = 0
    xend = 360
    xstep = 0.5
    ystart = -85
    yend = 85
    ystep = 0.5

    lon_bnds = []
    lon = []
    for ii in numpy.arange(xstart, xend, xstep):
        lon_bnds.append([ii, ii + xstep])
        lon.append(ii + 0.5 * xstep)
    lon_bnds = numpy.array(lon_bnds)
    lon = numpy.array(lon)

    lat_bnds = []
    lat = []
    for ii in numpy.arange(ystart, yend, ystep):
        lat_bnds.append([ii, ii + ystep])
        lat.append(ii + 0.5 * ystep)
    lat_bnds = numpy.array(lat_bnds)
    lat = numpy.array(lat)

    latAxis = cdms2.createAxis(lat, lat_bnds)
    latAxis.designateLatitude(True)
    latAxis.units = "degree_north"
    latAxis.long_name = "Latitude"

    lonAxis = cdms2.createAxis(lon, lon_bnds)
    lonAxis.designateLongitude(True, 360.0)

    return cdms2.createGenericGrid(latAxis, lonAxis, lat_bnds, lon_bnds)
예제 #10
0
    def Xtest1(self):
        """
        Test from Kate Marvel
        As the following code snippet demonstrates, regridding a
        cdms2.tvariable.TransientVariable instance using regridTool='regrid2' 
        results in a new array that is masked everywhere.  regridTool='esmf' 
        and regridTool='libcf' both work as expected.

        This is similar to test0 but we only onterpolate over a single elevation,
        also fails. Likely GRID is not constructed correctly.
        """
        import cdms2 as cdms
        import numpy as np

        filename = cdat_info.get_sampledata_path() + '/clt.nc'
        a=cdms.open(filename)
        data=a('clt')[0,...]

        print data.mask #verify this data is not masked

        ## Now create a new grid using every other latitude and every 
        #  other longitude
        LATS=data.getLatitude()[::2]
        ub = data.getLatitude().getBounds()[1::2][:,0]
        lb = data.getLatitude().getBounds()[::2][:,1]
        latbounds = np.array(zip(ub,lb))

        self.assertEqual((ub != lb).all(), True)

        LONS=data.getLongitude()[::2]
        ub=data.getLongitude().getBounds()[1::2][:,0]
        lb=data.getLongitude().getBounds()[::2][:,1]
        lonbounds=np.array(zip(ub,lb))

        self.assertEqual((ub != lb).all(), True)
        

        GRID=cdms.createGenericGrid(LATS,LONS,
                                    latBounds=latbounds,
                                    lonBounds=lonbounds)

        test_data=data.regrid(GRID,regridTool='regrid2')

        # check that the mask does not extend everywhere...
        self.assertNotEqual(test_data.mask.sum(), test_data.size)
        
        if PLOT:
            pylab.subplot(2, 1, 1)
            pylab.pcolor(data[...])
            pylab.title('data')
            pylab.subplot(2, 1, 2)
            pylab.pcolor(test_data[...])
            pylab.title('test_data (interpolated data)')
            pylab.show()
예제 #11
0
    def Xtest1(self):
        """
        Test from Kate Marvel
        As the following code snippet demonstrates, regridding a
        cdms2.tvariable.TransientVariable instance using regridTool='regrid2' 
        results in a new array that is masked everywhere.  regridTool='esmf' 
        and regridTool='libcf' both work as expected.

        This is similar to test0 but we only onterpolate over a single elevation,
        also fails. Likely GRID is not constructed correctly.
        """
        import cdms2 as cdms
        import numpy as np

        filename = cdat_info.get_prefix() + '/sample_data/clt.nc'
        a = cdms.open(filename)
        data = a('clt')[0, ...]

        print data.mask  #verify this data is not masked

        ## Now create a new grid using every other latitude and every
        #  other longitude
        LATS = data.getLatitude()[::2]
        ub = data.getLatitude().getBounds()[1::2][:, 0]
        lb = data.getLatitude().getBounds()[::2][:, 1]
        latbounds = np.array(zip(ub, lb))

        self.assertEqual((ub != lb).all(), True)

        LONS = data.getLongitude()[::2]
        ub = data.getLongitude().getBounds()[1::2][:, 0]
        lb = data.getLongitude().getBounds()[::2][:, 1]
        lonbounds = np.array(zip(ub, lb))

        self.assertEqual((ub != lb).all(), True)

        GRID = cdms.createGenericGrid(LATS,
                                      LONS,
                                      latBounds=latbounds,
                                      lonBounds=lonbounds)

        test_data = data.regrid(GRID, regridTool='regrid2')

        # check that the mask does not extend everywhere...
        self.assertNotEqual(test_data.mask.sum(), test_data.size)

        if PLOT:
            pylab.subplot(2, 1, 1)
            pylab.pcolor(data[...])
            pylab.title('data')
            pylab.subplot(2, 1, 2)
            pylab.pcolor(test_data[...])
            pylab.title('test_data (interpolated data)')
            pylab.show()
예제 #12
0
 def testGenGrids2(self):
     latb = [62.47686472, 69.70600048]
     lonb = [102.87075526, 105.51598035]
     fn = self.getDataFile('sampleCurveGrid4.nc')
     s = fn("sample")
     g = s.getGrid()
     lat = g.getLatitude()
     lon = g.getLongitude()
     g2 = cdms2.createGenericGrid(lat, lon)
     datalat = g2.getLatitude().getBounds()[22, 25]
     datalon = g2.getLongitude().getBounds()[22, 25]
     self.assertTrue(numpy.ma.allclose(datalat, latb))
     self.assertTrue(numpy.ma.allclose(datalon, lonb))
예제 #13
0
파일: data_interim.py 프로젝트: hyma68/CAPT
    def __init__(self, state_xmlfile_dir, state_xmlfile_name):
    #---------------------------------------------------------
        """
        Initializes dataInterim class.

        state_files_dir  # TBD.
        state_file_name  #
        """

        self.state_xmlfile_dir  = state_xmlfile_dir
        self.state_xmlfile_name = state_xmlfile_name

        # These are to conform with MERRA names.
        #---------------------------------------
        self.var_dict = {'PHIS': 'var129', 'PS': 'var152', 'QV': 'var133',
                         'V':    'var132', 'U':  'var131', 'T':  'var130'}

        fin = \
            cdms2.open(self.state_xmlfile_dir + '/' + self.state_xmlfile_name)

        zsfc = fin.variables['var129']

        # Get coordinates.
        #-----------------
        lats = zsfc.getLatitude()[:]
        lons = zsfc.getLongitude()[:]

        self.lons = lons
        self.lats = lats

        # Create a CDMS grid object, so can use later for regridding.
        #------------------------------------------------------------
        self.grid = cdms2.createGenericGrid(self.lats, self.lons)

        # Get hybrid coordinate information.
        #-----------------------------------
        p0, hyam, hybm, hyai, hybi = get_ab_interim.get_ab_interim()

        self.p0 = p0

        self.hyam = hyam
        self.hybm = hybm
        self.hyai = hyai
        self.hybi = hybi

        self.fin = fin
예제 #14
0
def makeGrid():
    xstart=0
    xend=360
    xstep=0.5
    ystart=-85
    yend=85
    ystep=0.5

    lon_bnds=[]
    lon=[]
    for ii in numpy.arange(xstart, xend, xstep):
        lon_bnds.append( [ii, ii+xstep] )
        lon.append(ii+0.5*xstep)
    lon_bnds=numpy.array(lon_bnds)
    lon=numpy.array(lon)

    lat_bnds=[]
    lat=[]
    for ii in numpy.arange(ystart, yend, ystep):
        lat_bnds.append([ii, ii+ystep])
        lat.append(ii+0.5*ystep)
    lat_bnds=numpy.array(lat_bnds)
    lat=numpy.array(lat)

    latAxis = cdms2.createAxis(lat, lat_bnds)
    latAxis.designateLatitude(True)
    latAxis.units='degrees_north'
    latAxis.long_name='Latitude'
    latAxis.id='latitude'

    lonAxis = cdms2.createAxis(lon, lon_bnds)
    lonAxis.designateLongitude(True, 360.0)
    lonAxis.units='degrees_east'
    lonAxis.id='longitude'
    lonAxis.long_name='Longitude'

    lvl_bnds=numpy.array([[0,10], [10, 20], [20,30], [30,40], [40,50], [50,60], [60,70], [70,80], [80,90], [90,100], [100, 125], [125, 150], [150,175], [175,200], [200,250],[250,300],[300,400],[400,500], [500,600], [600,700], [700,800]])
    lvl = [ 0.5*(lvls[0] + lvls[1]) for lvls in lvl_bnds ]
#    lvl = numpy.zeros(len(lvl_bnds))
#    for ii in range(len(lvl_bnds)): lvl[ii]=0.5*(lvl_bnds[ii,0]+lvl_bnds[ii,1])
    return((cdms2.createGenericGrid(latAxis, lonAxis, lat_bnds, lon_bnds), latAxis, lonAxis, lat_bnds, lon_bnds, lvl_bnds, lvl))
예제 #15
0
def doRegrid(infile, varname, outfile, lon, lat, lon_bnds, lat_bnds):

    cdms2.setNetcdfShuffleFlag(1)
    cdms2.setNetcdfDeflateFlag(1)
    cdms2.setNetcdfDeflateLevelFlag(2)

    fh = cdms2.open(infile)
    if fh is None:
        exitMessage("Could not open file {0}. Exit 2.".format(infile), 2)

    if varname not in fh.variables.keys():
        exitMessage('variable named '+varname+' could not be found. Exit 4.', 4)

    yVar = fh(varname)

    latAxis = cdms2.createAxis(lat, lat_bnds)
    latAxis.designateLatitude(True)
    latAxis.units = 'degree_north'
    latAxis.long_name = 'Latitude'

    lonAxis = cdms2.createAxis(lon, lon_bnds)
    lonAxis.designateLongitude(True, 360.0)
    lonAxis.units = 'degree_east'
    lonAxis.long_name='Longitude'

    listAxisOrg = yVar.getAxisList()
    timeAxis = listAxisOrg[0]
    

    grid = cdms2.createGenericGrid(latAxis, lonAxis, lat_bnds, lon_bnds)
    regridded = yVar.regrid(grid)

    g=cdms2.open(outfile, 'w')
    
    #g.write(regridded, None, None, None, varname, None, 1.e20, None, cdms2.CdFloat)
    temp1 = cdms2.createVariable(regridded, typecode='f', id=varname, fill_value=1.e20, axes=[timeAxis, latAxis, lonAxis], copyaxes=0, attributes=dict(long_name=yVar.long_name, units=yVar.units) )
    g.write(temp1)
    g.close()
예제 #16
0
def makeGrid(thisStep=0.5):
    xstart=0
    xend=360
    xstep=thisStep
    ystart=-85
    yend=85
    ystep=thisStep

    lon_bnds=[]
    lon=[]
    for ii in numpy.arange(xstart, xend, xstep):
        lon_bnds.append( [ii, ii + xstep] )
        lon.append(ii+0.5*xstep)
    lon_bnds=numpy.array(lon_bnds)
    lon=numpy.array(lon)

    lat_bnds=[]
    lat=[]
    for ii in numpy.arange(ystart, yend, ystep):
        lat_bnds.append([ii, ii + ystep])
        lat.append(ii+0.5*ystep)
    lat_bnds=numpy.array(lat_bnds)
    lat=numpy.array(lat)

    latAxis = cdms2.createAxis(lat, lat_bnds)
    latAxis.designateLatitude(True)
    latAxis.units='degrees_north'
    latAxis.id='latitude'
    latAxis.long_name='Latitude'

    lonAxis = cdms2.createAxis(lon, lon_bnds)
    lonAxis.designateLongitude(True, xend)
    lonAxis.designateCircular(xend)
    lonAxis.units='degrees_east'
    lonAxis.id='longitude'
    lonAxis.long_name='Longitude'

    return((cdms2.createGenericGrid(latAxis, lonAxis, lat_bnds, lon_bnds), latAxis, lonAxis, lat_bnds, lon_bnds))
예제 #17
0
    def test0FixedByKindig(self):
        """
        Test from Kate Marvel
        As the following code snippet demonstrates, regridding a
        cdms2.tvariable.TransientVariable instance using regridTool='regrid2' 
        results in a new array that is masked everywhere.  regridTool='esmf' 
        and regridTool='libcf' both work as expected.

        Modified by Kindig to run.
        """
        import cdms2 as cdms
        import numpy as np

        filename = cdat_info.get_sampledata_path() + '/clt.nc'
        a=cdms.open(filename)
        data=a('clt')[0,...]

        print data.mask #verify this data is not masked

        ## Now create a new grid using every other latitude and every 
        #  other longitude
        LATS=data.getLatitude()[::2]
        lb = data.getLatitude().getBounds()[::2][:,0]
        ub = data.getLatitude().getBounds()[::2][:,1]
        latbounds = np.array(zip(lb, ub))

        self.assertEqual((ub != lb).all(), True)
        self.assertEqual(lb.min(), -90.0)

        LONS=data.getLongitude()[::2]
        lb=data.getLongitude().getBounds()[::2][:,0]
        ub=data.getLongitude().getBounds()[::2][:,1]
        lonbounds=np.array(zip(lb, ub))

        self.assertEqual((ub != lb).all(), True)
        self.assertEqual(lb.min(), -182.5)
        
        GRID=cdms.createGenericGrid(LATS,LONS,
                                    latBounds=latbounds,
                                    lonBounds=lonbounds)
        uniGrid = cdms.grid.createUniformGrid(-90, 23, 8, -180, 36, 10)

        test_gen = data.regrid(GRID,regridTool='regrid2')
        test_uni = data.regrid(uniGrid, regridTool = 'regrid2')
        

        # check that the mask does not extend everywhere...
        print data.mask, data.size
        
        print 'GENERIC', test_gen.mask.sum(), test_gen.size
        print 'UNIFORM', test_uni.mask.sum(), test_uni.size
        self.assertNotEqual(test_uni.mask.sum(), test_uni.size) 
        self.assertNotEqual(test_gen.mask.sum(), test_gen.size)
        
        if PLOT:
            pylab.subplot(2, 1, 1)
            pylab.pcolor(data[...])
            pylab.title('data')
            pylab.subplot(2, 1, 2)
            pylab.pcolor(test_gen[...])
            pylab.title('test_gen (interpolated data)')
            pylab.show()
예제 #18
0
    225.,
    236.25,
    247.5,
    258.75,
])
lat2 = numpy.ma.array([
    -42.,
    -30.,
    -18.,
    -6.,
    6.,
    18.,
    30.,
    42.,
])
grid2 = cdms2.createGenericGrid(lat2, lon2)
b1, b2 = grid2.getBounds()
grid2.setBounds(b1, b2)
latw, lonw = grid2.getWeights()

f.close()
# Test pressure regridder --------------------------------
import numpy

g = cdms2.createGaussianGrid(16)
levs = numpy.array([1.0, 3.0, 5.0])
lev = cdms2.createAxis(levs, id='level')
levsout = numpy.array([2.0, 4.0])
levout = cdms2.createAxis(levsout, id='level')
dat = numpy.zeros((3, 16, 32), numpy.float32)
dat2 = numpy.zeros((2, 16, 32), numpy.float32)
예제 #19
0
CTIminlati = np.argmin(np.abs(lats - (-6)))
CTImaxlati = np.argmin(np.abs(lats - 6))
CTIminloni = np.argmin(np.abs(lons - 0))
CTImaxloni = np.argmin(np.abs(lons - 90))

CTI = spatial_ave(sst[:, CTIminlati:CTImaxlati, CTIminloni:CTImaxloni],
                  lats[CTIminlati:CTImaxlati])

#SUBTRACT COLD TONGUE INDEX FROM SST

#coarse grid lat/lon spacing
cstep = 1
lats = np.arange(minlat, maxlat + cstep, cstep)
lons = np.arange(0, 360 + cstep, cstep)

cgrid = cdms2.createGenericGrid(lats, lons)
#regridfunc = Regridder(ingrid, cgrid)
sst = sst.regrid(cgrid, regridTool="esmf", regridMethod="linear")
field = field.regrid(cgrid, regridTool="esmf", regridMethod="linear")
u = u.regrid(cgrid, regridTool="esmf", regridMethod="linear")
v = v.regrid(cgrid, regridTool="esmf", regridMethod="linear")
ps = ps.regrid(cgrid, regridTool="esmf", regridMethod="linear")

sst = sst.subRegion(latitude=(latbounds[0], latbounds[1]),
                    longitude=(lonbounds[0], lonbounds[1]))
field = field.subRegion(latitude=(latbounds[0], latbounds[1]),
                        longitude=(lonbounds[0], lonbounds[1]))
u = u.subRegion(latitude=(latbounds[0], latbounds[1]),
                longitude=(lonbounds[0], lonbounds[1]))
v = v.subRegion(latitude=(latbounds[0], latbounds[1]),
                longitude=(lonbounds[0], lonbounds[1]))
def makeGrid4D():
    xstart=0
    xend=360
    xstep=0.5
    ystart=-85
    yend=85
    ystep=0.5

#	zlevels=[6, 17, 27, 37, 47, 57, 68.5, 82.5, 100, 122.5, 150, 182.5, 220, 262.5, 310, 362.5, 420, 485, 560, 645, 740, 845, 960, 1085]
    zlevels=[3.3, 10, 20, 30, 50, 75, 100, 125, 150, 200, 250, 300, 400, 500]
    lvl_bnds=[]
    for ii in range(0, len(zlevels)):
        if ii==0:
            xs = 0
        else:
            xs = 0.5 * ( zlevels[ii-1] + zlevels[ii] )
        if ii==(len(zlevels)-1):
            xe = zlevels[ii] + 0.5 * (zlevels[ii] - zlevels[ii-1])
        else:
            xe = 0.5 * ( zlevels[ii] + zlevels[ii+1] )
        lvl_bnds.append( [xs, xe] )

    zlevels=numpy.array(zlevels)
    lvl_bnds=numpy.array(lvl_bnds)
    print zlevels
    print lvl_bnds
    lvlAxis = cdms2.createAxis(zlevels, lvl_bnds)
    lvlAxis.units='m'
    lvlAxis.long_name='depth'
    lvlAxis.id='level'
    lvlAxis.designateLevel(True)

    lon_bnds=[]
    lon=[]
    for ii in numpy.arange(xstart, xend, xstep):
        lon_bnds.append( [ii, ii+xstep] )
        lon.append(ii+0.5*xstep)
    lon_bnds=numpy.array(lon_bnds)
    lon=numpy.array(lon)

    lat_bnds=[]
    lat=[]
    for ii in numpy.arange(ystart, yend, ystep):
        lat_bnds.append([ii, ii+ystep])
        lat.append(ii+0.5*ystep)
    lat_bnds=numpy.array(lat_bnds)
    lat=numpy.array(lat)

    latAxis = cdms2.createAxis(lat, lat_bnds)
    latAxis.designateLatitude(True)
    latAxis.units='degrees_north'
    latAxis.long_name='Latitude'
    latAxis.id='latitude'

    lonAxis = cdms2.createAxis(lon, lon_bnds)
    lonAxis.designateLongitude(True, 360.0)
    lonAxis.units='degrees_east'
    lonAxis.id='longitude'
    lonAxis.long_name='Longitude'

    return((cdms2.createGenericGrid(latAxis, lonAxis, lat_bnds, lon_bnds), latAxis, lonAxis, lat_bnds, lon_bnds, lvlAxis, lvl_bnds))
예제 #21
0
    def test0FixedByKindig(self):
        """
        Test from Kate Marvel
        As the following code snippet demonstrates, regridding a
        cdms2.tvariable.TransientVariable instance using regridTool='regrid2' 
        results in a new array that is masked everywhere.  regridTool='esmf' 
        and regridTool='libcf' both work as expected.

        Modified by Kindig to run.
        """
        import cdms2 as cdms
        import numpy as np

        filename = cdat_info.get_prefix() + '/sample_data/clt.nc'
        a = cdms.open(filename)
        data = a('clt')[0, ...]

        print data.mask  #verify this data is not masked

        ## Now create a new grid using every other latitude and every
        #  other longitude
        LATS = data.getLatitude()[::2]
        lb = data.getLatitude().getBounds()[::2][:, 0]
        ub = data.getLatitude().getBounds()[::2][:, 1]
        latbounds = np.array(zip(lb, ub))

        self.assertEqual((ub != lb).all(), True)
        self.assertEqual(lb.min(), -90.0)

        LONS = data.getLongitude()[::2]
        lb = data.getLongitude().getBounds()[::2][:, 0]
        ub = data.getLongitude().getBounds()[::2][:, 1]
        lonbounds = np.array(zip(lb, ub))

        self.assertEqual((ub != lb).all(), True)
        self.assertEqual(lb.min(), -182.5)

        GRID = cdms.createGenericGrid(LATS,
                                      LONS,
                                      latBounds=latbounds,
                                      lonBounds=lonbounds)
        uniGrid = cdms.grid.createUniformGrid(-90, 23, 8, -180, 36, 10)

        test_gen = data.regrid(GRID, regridTool='regrid2')
        test_uni = data.regrid(uniGrid, regridTool='regrid2')

        # check that the mask does not extend everywhere...
        print data.mask, data.size

        print 'GENERIC', test_gen.mask.sum(), test_gen.size
        print 'UNIFORM', test_uni.mask.sum(), test_uni.size
        self.assertNotEqual(test_uni.mask.sum(), test_uni.size)
        self.assertNotEqual(test_gen.mask.sum(), test_gen.size)

        if PLOT:
            pylab.subplot(2, 1, 1)
            pylab.pcolor(data[...])
            pylab.title('data')
            pylab.subplot(2, 1, 2)
            pylab.pcolor(test_gen[...])
            pylab.title('test_gen (interpolated data)')
            pylab.show()
예제 #22
0
    def testRegrid2(self):
        outgrid = cdms2.createGaussianGrid(32)

        pth = os.path.dirname(os.path.abspath(__file__))
        f = self.getDataFile('readonly.nc')
        u = f.variables['u']
        ingrid = u.getGrid()

        sh = ingrid.shape

        regridf = Horizontal(ingrid, outgrid)
        newu = regridf(u)

        self.assertLess(abs(newu[0, 0, -1] - 488.4763488), 1.e-3)
        newu = u.regrid(outgrid, regridTool='regrid2')
        self.assertLess(abs(newu[0, 0, -1] - 488.4763488), 1.e-3)

        # Regrid TV
        tv = u.subSlice(0)
        newtv = regridf(tv)
        self.assertLess(abs(newtv[0, 0, -1] - 488.4763488), 1.e-3)
        newtv = tv.regrid(outgrid, regridTool='regrid2')
        self.assertLess(abs(newtv[0, 0, -1] - 488.4763488), 1.e-3)

        # Regrid numpy.ma
        ma = u[0]
        newma = regridf(ma)
        # Force slice result to be a scalar
        self.assertLess(abs(newma[0][-1] - 488.4763488), 1.e-3)

        # Regrid numpy
        numar = numpy.ma.filled(u[0])
        newar = regridf(numar)
        self.assertLess(abs(newar[0][-1] - 488.4763488), 1.e-3)

        # Regrid masked Variable
        umasked = f.variables['umasked']
        newum = regridf(umasked)
        self.assertLess(abs(newum[0, 0, -1] - 488.4763488), 1.e-3)

        # Set explicit missing variable
        numar = numpy.ma.filled(umasked[0])
        newar = regridf(numar, missing=-99.9)
        self.assertLess(abs(newar[0][-1] - 488.4763488), 1.e-3)

        # Set explicit mask
        mask = umasked.subRegion().mask[0]
        newar = regridf(numar, mask=mask)
        self.assertLess(abs(newar[0][-1] - 488.4763488), 1.e-3)

        # Set the input grid mask
        ingrid.setMask(mask)
        regridf2 = Horizontal(ingrid, outgrid)
        newar = regridf2(numar)
        self.assertLess(abs(newar[0][-1] - 488.4763488), 1.e-3)

        # Dataset
        g = self.getDataFile('test.xml')
        u = g.variables['u']
        outgrid = cdms2.createGaussianGrid(24)
        regridf3 = Horizontal(u.getGrid(), outgrid)
        try:
            unew = regridf3(u)
        except BaseException:
            markError('regrid dataset variable')

        lon2 = numpy.ma.array([
            90.,
            101.25,
            112.5,
            123.75,
            135.,
            146.25,
            157.5,
            168.75,
            180.,
            191.25,
            202.5,
            213.75,
            225.,
            236.25,
            247.5,
            258.75,
        ])
        lat2 = numpy.ma.array([
            -42.,
            -30.,
            -18.,
            -6.,
            6.,
            18.,
            30.,
            42.,
        ])
        grid2 = cdms2.createGenericGrid(lat2, lon2)
        b1, b2 = grid2.getBounds()
        grid2.setBounds(b1, b2)
        latw, lonw = grid2.getWeights()

        g = cdms2.createGaussianGrid(16)
        levs = numpy.array([1.0, 3.0, 5.0])
        lev = cdms2.createAxis(levs, id='level')
        levsout = numpy.array([2.0, 4.0])
        levout = cdms2.createAxis(levsout, id='level')
        dat = numpy.zeros((3, 16, 32), numpy.float32)
        dat2 = numpy.zeros((2, 16, 32), numpy.float32)
        dat[0] = 2.0
        dat[1] = 4.0
        dat[2] = 6.0
        var = cdms2.createVariable(dat,
                                   axes=(lev, g),
                                   attributes={'units': 'N/A'},
                                   id='test')
        result = var.pressureRegrid(levout)

        self.assertLess(abs(result[0, 0, 0] - 3.26185), 1.e-4)
        # Test cross-section regridder --------------------------------
        latin = cdms2.createGaussianAxis(16)
        latout = cdms2.createGaussianAxis(24)
        levsin = numpy.array([1.0, 3.0, 5.0])
        lev = cdms2.createAxis(levsin, id='level')
        levsout = numpy.array([2.0, 4.0])
        levout = cdms2.createAxis(levsout, id='level')
        dat = numpy.zeros((3, 16), numpy.float32)
        dat[0] = 2.0
        dat[1] = 4.0
        dat[2] = 6.0
        var = cdms2.createVariable(dat,
                                   axes=(lev, latin),
                                   attributes={'units': 'N/A'},
                                   id='test')
        dat2 = var.crossSectionRegrid(levout, latout)
        self.assertLess(abs(dat2[0, 0] - 3.26185), 1.e-4)
예제 #23
0
if (abs(newar[0][-1]-488.4763488) > 1.e-3): markError('regrid numpy array with grid input mask',newar[0][-1])

# Dataset
g = cdms2.open(os.path.join(pth,'test.xml'))
u = g.variables['u']
outgrid = cdms2.createGaussianGrid(24)
regridf3 = Regridder(u.getGrid(), outgrid)
try:
    unew = regridf3(u)
except:
    markError('regrid dataset variable')

lon2 = numpy.ma.array([  90.  , 101.25, 112.5 , 123.75, 135.  , 146.25, 157.5 , 168.75, 180.  ,
       191.25, 202.5 , 213.75, 225.  , 236.25, 247.5 , 258.75,])
lat2 = numpy.ma.array([-42.,-30.,-18., -6.,  6., 18., 30., 42.,])
grid2 = cdms2.createGenericGrid(lat2,lon2)
b1, b2 = grid2.getBounds()
grid2.setBounds(b1,b2)
latw,lonw = grid2.getWeights()

f.close()
# Test pressure regridder --------------------------------
import numpy
g = cdms2.createGaussianGrid(16)
levs = numpy.array([1.0,3.0,5.0])
lev = cdms2.createAxis(levs, id='level')
levsout = numpy.array([2.0,4.0])
levout = cdms2.createAxis(levsout, id='level')
dat = numpy.zeros((3,16,32), numpy.float32)
dat2 = numpy.zeros((2,16,32), numpy.float32)
dat[0] = 2.0
f.close()
LWkernel = MV.masked_where(np.isnan(LWkernel), LWkernel)
SWkernel = MV.masked_where(np.isnan(SWkernel), SWkernel)
albcs = np.arange(
    0.0, 1.5, 0.5)  # the clear-sky albedos over which the kernel is computed

# Define the cloud kernel axis attributes
lats = cdms.createAxis(LWkernel.getLatitude()[:])
lats.id = "lat"
lats.units = "degrees_N"
lats.designateLatitude()
lons = cdms.createAxis(np.arange(1.25, 360, 2.5))
lons.id = "lon"
lons.units = "degrees_E"
lons.designateLongitude()
kern_grid = cdms.createGenericGrid(lats, lons)
kern_grid.getLatitude().id = 'lat'
kern_grid.getLongitude().id = 'lon'

##########################################################
##### Load in ISCCP HGG clisccp climo annual cycle  ######
##########################################################
f = cdms.open(datadir + 'AC_clisccp_ISCCP_HGG_198301-200812.nc', 'r')
#f.history='Written by /work/zelinka1/scripts/load_ISCCP_HGG.py on feedback.llnl.gov'
#f.comment='Monthly-resolved climatological annual cycle over 198301-200812'
obs_clisccp_AC = f('AC_clisccp')
f.close()

f = cdms.open(datadir + 'AC_clisccp_wap_ISCCP_HGG_198301-200812.nc', 'r')
#f.history='Written by /work/zelinka1/scripts/load_ISCCP_HGG.py on feedback.llnl.gov'
#f.comment='Monthly-resolved climatological annual cycle over 198301-200812, in omega500 space'