Example #1
0
 def execute(self, parent, result):
     if result == 'Apply':
         try:
             v_name = self.name.get()
             expression = self.expression.get()
             if not __main__.__dict__.has_key('MV'):
                 exec "from cdms2 import MV" in __main__.__dict__
             __main__.__dict__[v_name] = eval(expression, __main__.__dict__)
             if type(__main__.__dict__[v_name]) in [
                     types.IntType, types.FloatType
             ]:
                 __main__.__dict__[v_name] = cdms.asVariable(
                     __main__.__dict__[v_name])
             __main__.__dict__[v_name].id = __main__.__dict__[
                 v_name].name = v_name
             gui_defined_variables.update_defined()
             gui_control.record_command(parent, "\n# Record the expression",
                                        1)
             gui_control.record_command(parent,
                                        "%s = %s" % (v_name, expression), 1)
         except:
             gui_message.error(
                 "Bad expression! Check expression and try again.")
     elif result == 'Clear':
         self.expression.clear()
         self.fself.doing_calculation_type = ''
         self.fself.saved_expression = ''
         self.fself.build_expression = 'no'
     else:
         self.dialog.destroy()
         self.fself.gui_expression = None
Example #2
0
def writenetcdf (slab, filename, mode="a"):
    """writenetcdf(slab, filename, mode="a") writes slab to the file.
       modes: 'a'  append
              'w'  replace
              'r'  replace (for legacy code only, deprecated)
       s can be anything asVariable will accept
    """
    if mode == 'r': mode = 'w'
    slab = cdms2.asVariable(slab, 0)
    f = cdms2.openDataset(filename, mode)
    f.write(slab)
    f.close()
Example #3
0
def writenetcdf(slab, filename, mode="a"):
    """
    writenetcdf(slab, filename, mode="a") writes slab to the file.
    :param mode: One of 'a' append, or 'w'  replace
    :type mode: str

    :param slab: Anything :py:func:`cdms2.asVariable` will accept
    :type: see :py:func:`cdms2.asVariable`
    """
    if mode == 'r':
        mode = 'w'
    slab = cdms2.asVariable(slab, 0)
    f = cdms2.openDataset(filename, mode)
    f.write(slab)
    f.close()
Example #4
0
def _get_phisfcr_in(ddi, rgfa2m):
    #--------------------------------
    """
    Returns era5 geopotential on E3SM ne120 grid.

    ddi     # era5 surface geopotential.
    rgfa2m  # era5 to E3SM SE regrid function.
    """

    phisfca = ddi.get_const_data(SURF_GEOPOTA)
    print('phisfca type: ', type(phisfca))
    print('phisfca shape: ', phisfca.shape)

    if not SURF_HT_IS_GEOPOT:
        phisfca = phisfca * GEE

    phisfcr_in = rgfa2m(cdms2.asVariable(phisfca))
    print('phisfcr_in type: ', type(phisfcr_in))
    print('phisfcr_in shape: ', phisfcr_in.shape)
    print(phisfcr_in.iscontiguous())

    return phisfcr_in
Example #5
0
 def execute(self, parent, result):
    if result == 'Apply':
       try:
          v_name = self.name.get()
          expression = self.expression.get()
          if not __main__.__dict__.has_key('MV'):
             exec "from cdms2 import MV" in __main__.__dict__
          __main__.__dict__[v_name] = eval( expression, __main__.__dict__ )
          if type(__main__.__dict__[v_name]) in [types.IntType,types.FloatType]:
             __main__.__dict__[v_name] = cdms.asVariable(__main__.__dict__[v_name])
          __main__.__dict__[v_name].id = __main__.__dict__[v_name].name = v_name
          gui_defined_variables.update_defined( )
          gui_control.record_command(parent, "\n# Record the expression", 1 )
          gui_control.record_command(parent, "%s = %s" % (v_name, expression), 1 )
       except:
          gui_message.error("Bad expression! Check expression and try again.")
    elif result == 'Clear':
       self.expression.clear()
       self.fself.doing_calculation_type = ''
       self.fself.saved_expression = ''
       self.fself.build_expression = 'no'
    else:
       self.dialog.destroy()
       self.fself.gui_expression = None
Example #6
0
    def testTV(self):
        f = self.getDataFile("test.xml")

        x = self.test_arr
        v = f.variables['v']
        vp = x[1, 1:, 4:12, 8:25]
        vp2 = vp[1, 1:-1, 1:]
        tv = v.subRegion((366., 731., 'ccn'), (-42., 42., 'ccn'), (90., 270.))
        tvv = v[0:2, 0:10, 30:40]

        # Make sure we retrieve a scalar
        xx = tv[1, 7, 15]
        self.assertFalse(isinstance(xx, numpy.ndarray))

        # Variable get: axis, grid, latitude, level, longitude, missing, order,
        # time, len, typecode

        vaxis0 = v.getAxis(0)
        axis0 = tv.getAxis(0)
        self.assertFalse(not numpy.ma.allequal(axis0[:], vaxis0[1:]))

        taxis = tv.getTime()
        taxisarray = taxis[:]
        vaxisarray = vaxis0[1:]
        self.assertFalse(not numpy.ma.allequal(taxisarray, vaxisarray))

        vaxis1 = v.getAxis(1)
        lataxis = tv.getLatitude()
        self.assertFalse(not numpy.ma.allequal(lataxis[:], vaxis1[4:12]))

        vaxis2 = v.getAxis(2)
        lonaxis = tv.getLongitude()

        #
        #  default is 'ccn' -- now it 8:25
        #
        self.assertFalse(not numpy.ma.allequal(lonaxis[:], vaxis2[8:25]))

        tv = v.subRegion((366., 731., 'ccn'), (-42., 42., 'ccn'), (90., 270.))
        missing_value = v.getMissing()
        self.assertEqual(missing_value, -99.9)

        tmv = tv.fill_value
        # TODO: Did the default value of fill_value/missing change? This is failing.
        #self.assertEqual(tmv, -99.9)

        grid = tv.getGrid()
        self.assertFalse(grid is None)

        order = tv.getOrder()
        self.assertEqual(order, 'tyx')

        self.assertEqual(len(tv), 2)

        # get TV domain
        domain = tv.getDomain()
        self.assertEqual(len(domain), 3)

        # getRegion of a TV
        tv2 = tv.getRegion(731., (-30., 30., 'ccn'), (101.25, 270.0))
        self.assertFalse(not numpy.ma.allequal(tv2, vp2))

        # Axis get: bounds, calendar, value, isXXX, len, subaxis, typecode
        axis1 = tv.getAxis(1)
        axis2 = tv.getAxis(2)
        bounds = axis1.getBounds()
        self.assertFalse(bounds is None)
        self.assertEqual(axis0.getCalendar(), cdtime.MixedCalendar)
        val = axis1.getValue()
        self.assertFalse(not numpy.ma.allequal(axis1.getValue(), axis1[:]))
        self.assertFalse(not axis0.isTime())
        self.assertFalse(not axis1.isLatitude())
        self.assertFalse(not axis2.isLongitude())
        self.assertTrue(axis2.isCircular())
        self.assertEqual(len(axis2), 17)

        saxis = axis2.subAxis(1, -1)
        self.assertFalse(not numpy.ma.allequal(saxis[:], axis2[1:-1]))
        self.assertEqual(axis1.typecode(), numpy.sctype2char(numpy.float))
        self.assertEqual(axis2.shape, (17, ))

        # Axis set: bounds, calendar
        savebounds = copy.copy(bounds)
        bounds[0, 0] = -90.0
        axis1.setBounds(bounds)
        nbounds = axis1.getBounds()
        self.assertFalse(not numpy.ma.allequal(bounds, nbounds))
        axis0.setCalendar(cdtime.NoLeapCalendar)
        self.assertEqual(axis0.getCalendar(), cdtime.NoLeapCalendar)
        gaussaxis = cdms2.createGaussianAxis(32)
        try:
            testaxis = cdms2.createGaussianAxis(31)
        except BaseException:
            markError('Gaussian axis with odd number of latitudes')

        # Grid get: axis, bounds, latitude, longitude, mask, order, type,
        # weights, subgrid, subgridRegion
        a1 = grid.getAxis(1)
        self.assertFalse(not numpy.ma.allequal(a1[:], axis2[:]))

        bounds[0, 0] = savebounds[0, 0]
        axis1.setBounds(bounds)
        latbounds, lonbounds = grid.getBounds()
        self.assertFalse(not numpy.ma.allequal(latbounds, savebounds))
        glat = grid.getLatitude()
        glon = grid.getLongitude()
        mask = grid.getMask()
        order = grid.getOrder()
        self.assertEqual(order, 'yx')
        gtype = grid.getType()
        weights = grid.getWeights()
        subg = grid.subGrid((1, 7), (1, 15))
        subg2 = grid.subGridRegion((-30., 30., 'ccn'), (101.25, 247.5, 'ccn'))
        self.assertFalse(not numpy.ma.allequal(subg.getLongitude()[:],
                                               subg2.getLongitude()[:]))
        self.assertEqual(grid.shape, (8, 17))

        # Grid set: bounds, mask, type
        latbounds[0, 0] = -90.0
        grid.setBounds(latbounds, lonbounds)
        nlatb, nlonb = grid.getBounds()
        self.assertFalse(not numpy.ma.allequal(latbounds, nlatb))
        grid.setType('uniform')
        self.assertEqual(grid.getType(), 'uniform')

        yy = numpy.ma.reshape(numpy.ma.arange(272.0), tv.shape)
        tv.assignValue(yy)
        self.assertFalse(not numpy.ma.allequal(tv, yy))
        tv3 = tv[0:-1]
        self.assertEqual(tv3.shape, (1, 8, 17))

        # Create a transient variable from scratch
        oldlat = tv.getLatitude()
        oldBounds = oldlat.getBounds()
        newlat = cdms2.createAxis(numpy.ma.array(oldlat[:]),
                                  numpy.ma.array(oldBounds))
        b = newlat.getBounds()
        b[0, 0] = -48.
        newlat.setBounds(b)

        tv4 = cdms2.createVariable(tv[:], copy=1, fill_value=255.)
        tv4[0, 1:4] = 20.0

        self.assertEqual(tv[:, ::-1, :].shape, tv.shape)

        # Test asVariable
        www = cdms2.asVariable(tv4)
        self.assertFalse(www is not tv4)
        www = cdms2.asVariable(v, 0)
        self.assertFalse(www is not v)
        www = cdms2.asVariable([1., 2., 3.])
        self.assertFalse(not cdms2.isVariable(www))

        # Check that createAxis allows an axis as an argument
        lon = f.axes['longitude']
        newlon = cdms2.createAxis(lon)
        self.assertFalse(newlon.typecode() == 'O')

        # Test take of axis without bounds
        newlat.setBounds(None)
        samp = cdms2.axis.take(newlat, (2, 4, 6))
Example #7
0
##    gui_control.record_command( parent, '# ' + s_coord, 1 )

   slab=MV2.transpose( slab, rank )

   # Return the appropriate slab
   if chk_type_ct == 0: # return the slab
      if parent.menu.squeeze_dim_flg == 1:
##          gui_control.record_command( parent, "\n# Squeeze the slab ", 1 )
##          gui_control.record_command( parent, "%s = %s( squeeze=1 )" % (var_name, var_name), 1 )
         try:
             slab = slab( squeeze=1 )
         except: # ok 0d slabs can not be squeezed anymore...
             pass
         if vcs.VCS_validation_functions.isNumber(slab):
            gui_control.record_command( parent, "%s = cdms2.asVariable( %s )" % (var_name, var_name), 1 )
            slab = cdms2.asVariable( slab )
            slab.id=slab.name=var_name
      return slab
   else:  # return the either average or sum slab
      if from_file == 1:
         var_name = var
      else:
         var_name = var.id
         if new_var is not None: slab.id = new_var
      i_index = 0
      for i in range(parent.panelDM.ndim):
       if this_dim[i] != -1:
         if (t[ this_dim[i] ] == 'sum'):   # return the summed dimension
            tempslab = cdutil.averager(slab, axis="(" + slab.getAxis(i_index).id  + ")", weight='equal', action='sum')
            # record sum command
            s = "\n%s=cdutil.averager( %s, axis='(%s)', weight='equal', action='sum' )" % (slab.id, var_name, slab.getAxis(i_index).id)
Example #8
0
         xM=blon[-1][1]
         ym=blat[0][0]
         yM=blat[-1][1]
       except Exception,err:
         ## No luck we have to generate bounds ourselves
         lat2[1:-1]=(lat[:-1]+lat[1:])/2.
         lat2[0]=lat[0]-(lat[1]-lat[0])/2.
         lat2[-1]=lat[-1]+(lat[-1]-lat[-2])/2.
         lon2[1:-1]=(lon[:-1]+lon[1:])/2.
         lon2[0]=lon[0]-(lon[1]-lon[0])/2.
         lon2[-1]=lat[-1]+(lat[-1]-lat[-2])/2.
       lat = lat2[:,numpy.newaxis]*numpy.ones(lon2.shape)[numpy.newaxis,:]
       lon = lon2[numpy.newaxis,:]*numpy.ones(lat2.shape)[:,numpy.newaxis]
 elif grid is None:
   ## No grid info from data, making one up
   data1=cdms2.asVariable(data1)
   lon=data1.getAxis(-1)
   lat=data1.getAxis(-2)
   xm=lon[0]
   xM=lon[-1]
   ym=lat[0]
   yM=lat[-1]
   lat2 = numpy.zeros(len(lat)+1)
   lon2 = numpy.zeros(len(lon)+1)
   # Ok let's try to get the bounds
   try:
     blat = lat.getBounds()
     blon = lon.GetBounds()
     lat2[:len(lat)]=blat[:][0]
     lat2[len(lat2)]=blat[-1][1]
     lon2[:len(lon)]=blon[:][0]
Example #9
0
# Create a transient variable from scratch
oldlat = tv.getLatitude()
oldBounds = oldlat.getBounds()
newlat = cdms2.createAxis(numpy.ma.array(oldlat[:]), numpy.ma.array(oldBounds))
b = newlat.getBounds()
b[0, 0] = -48.
newlat.setBounds(b)

tv4 = cdms2.createVariable(tv[:], copy=1, fill_value=255.)
tv4[0, 1:4] = 20.0

if tv[:, ::-1, :].shape != tv.shape: markError("Reversing axis direction")

# Test asVariable
www = cdms2.asVariable(tv4)
if www is not tv4: markError("asVariable failed, transient case.")
www = cdms2.asVariable(v, 0)
if www is not v: markError("asVariable failed, transient case.")
www = cdms2.asVariable([1., 2., 3.])
if not cdms2.isVariable(www): markError("as/is test failed.")

# Check that createAxis allows an axis as an argument
lon = f.axes['longitude']
newlon = cdms2.createAxis(lon)
if newlon.typecode() == 'O': markError("createAxis failed: allow axis arg")

# Test take of axis without bounds
newlat.setBounds(None)
samp = cdms2.axis.take(newlat, (2, 4, 6))
Example #10
0
# Create a transient variable from scratch
oldlat = tv.getLatitude()
oldBounds = oldlat.getBounds()
newlat = cdms2.createAxis(numpy.ma.array(oldlat[:]),numpy.ma.array(oldBounds))
b = newlat.getBounds()
b[0,0]=-48.
newlat.setBounds(b)

tv4 = cdms2.createVariable(tv[:],copy=1,fill_value=255.)
tv4[0,1:4]=20.0

if tv[:,::-1,:].shape != tv.shape: markError("Reversing axis direction")

# Test asVariable
www = cdms2.asVariable(tv4)
if www is not tv4: markError("asVariable failed, transient case.")
www = cdms2.asVariable (v, 0)
if www is not v:   markError("asVariable failed, transient case.")
www = cdms2.asVariable([1.,2.,3.])
if not cdms2.isVariable(www): markError("as/is test failed.")

# Check that createAxis allows an axis as an argument
lon = f.axes['longitude']
newlon = cdms2.createAxis(lon)
if newlon.typecode()=='O': markError("createAxis failed: allow axis arg")

# Test take of axis without bounds
newlat.setBounds(None)
samp = cdms2.axis.take(newlat,(2,4,6))
Example #11
0
    slab = MV2.transpose(slab, rank)

    # Return the appropriate slab
    if chk_type_ct == 0:  # return the slab
        if parent.menu.squeeze_dim_flg == 1:
            ##          gui_control.record_command( parent, "\n# Squeeze the slab ", 1 )
            ##          gui_control.record_command( parent, "%s = %s( squeeze=1 )" % (var_name, var_name), 1 )
            try:
                slab = slab(squeeze=1)
            except:  # ok 0d slabs can not be squeezed anymore...
                pass
            if vcs.VCS_validation_functions.isNumber(slab):
                gui_control.record_command(
                    parent,
                    "%s = cdms2.asVariable( %s )" % (var_name, var_name), 1)
                slab = cdms2.asVariable(slab)
                slab.id = slab.name = var_name
        return slab
    else:  # return the either average or sum slab
        if from_file == 1:
            var_name = var
        else:
            var_name = var.id
            if new_var is not None: slab.id = new_var
        i_index = 0
        for i in range(parent.panelDM.ndim):
            if this_dim[i] != -1:
                if (t[this_dim[i]] == 'sum'):  # return the summed dimension
                    tempslab = cdutil.averager(slab,
                                               axis="(" +
                                               slab.getAxis(i_index).id + ")",
Example #12
0
def writenetcdf (slab, filename, mode="a"):
    """writenetcdf(slab, filename, mode="a") writes slab to the file.
       modes: 'a'  append
              'w'  replace
              'r'  replace (for legacy code only, deprecated)
       s can be anything asVariable will accept
    """
    if mode == 'r': mode = 'w'
    slab = cdms2.asVariable(slab, 0)
    f = cdms2.openDataset(filename, mode)
    f.write(slab)
    f.close()

if __name__ == '__main__':
    from numpy.ma import allclose
    import pcmdi
    g = cdms2.openDataset('clt.nc','r')
    c = g.variables['clt']
    t = cdms2.asVariable([1.,2.,3.])
    t.id = 't'
    writenetcdf(c, 'test.nc', 'w')
    writenetcdf(t, 'test.nc', 'a')
    f = cdms2.open('test.nc')
    d = f.variables['clt']
    assert allclose(c,d)
    for name in ['clt', 't']:
        pcmdi.slabinfo(f.variables[name])


Example #13
0
             lat2[0] = lat[0] - (lat[1] - lat[0]) / 2.
             lat2[-1] = lat[-1] + (lat[-1] - lat[-2]) / 2.
             lon2[1:-1] = (lon[:-1] + lon[1:]) / 2.
             lon2[0] = lon[0] - (lon[1] - lon[0]) / 2.
             lon2[-1] = lat[-1] + (lat[-1] - lat[-2]) / 2.
         lat = lat2[:, numpy.newaxis] * numpy.ones(
             lon2.shape)[numpy.newaxis, :]
         lon = lon2[numpy.newaxis, :] * numpy.ones(
             lat2.shape)[:, numpy.newaxis]
     else:
         #Ok in this case it is a structured grid we have no bounds, using ponitData
         ## ??? We should probably revist and see if we can use the "bounds" attribute
         ## to generate cell instead of points
         cellData = False
 else:
     data1 = cdms2.asVariable(data1)
     lon = data1.getAxis(-1)
     lat = data1.getAxis(-2)
     xm = lon[0]
     xM = lon[-1]
     ym = lat[0]
     yM = lat[-1]
     lat2 = numpy.zeros(len(lat) + 1)
     lon2 = numpy.zeros(len(lon) + 1)
     # Ok let's try to get the bounds
     try:
         blat = lat.getBounds()
         blon = lon.GetBounds()
         lat2[:len(lat)] = blat[:][0]
         lat2[len(lat2)] = blat[-1][1]
         lon2[:len(lon)] = blon[:][0]
Example #14
0
def _do_one_timestep(ctime, ddi, hyam, hybm, levs, ncol, p0, phis, phisfcm_in,
                     phisfcr_in, plat, plevm, plevr, plevrp1, plon, ps, q,
                     rgfa2m, t, tidx, u, v):
    #------------------------------------------------------------------------------
    """
    Does one timestep of putting era5 raw reanalysis state variables
    onto E3SM ne120 grid.

    ctime  # cdtime component time.

    Suffix a=era5 raw reanalysis,
           m=E3SM ne120 model,
           r=era5 raw reanalysis regridded to E3SM ne120 model
             (but still era5).
    """

    psfca = ddi.get_state_data(SURF_PRESA, ctime)

    psfcr = rgfa2m(cdms2.asVariable(psfca))
    psfcr = psfcr * SURF_PRES_SCALE
    print('psfcr shape: ', psfcr.shape)
    print('psfcr min/max: ', psfcr.min(), psfcr.max())

    ta = ddi.get_state_data(VAR_DICT['T'], ctime)
    tr = rgfa2m(cdms2.asVariable(ta))
    print('tr shape: ', tr.shape)

    pressr = ddi.get_state_data('pressure_mid', ctime)
    pressr = rgfa2m(cdms2.asVariable(pressr))

    pressir = ddi.get_state_data('pressure_interface', ctime)
    pressir = rgfa2m(cdms2.asVariable(pressir))

    # In order to use the same Fortran, we need to dummy in two dimensions,
    # (lev, ncol, 1) analogous to (lev, lat, lon), that the Fortran is
    # expecting.
    #----------------------------------------------------------------------
    ncol_lat = ncol
    ncol_lon = 1

    # Fortran wants numpy arrays.
    #----------------------------
    phisfcm = phisfcm_in.filled()
    phisfcr = phisfcr_in.filled()

    psfcr = psfcr.filled()
    tr = tr.filled()

    nlevs = tr.shape[0]
    nlevsp1 = nlevs + 1

    phisfcm = phisfcm.reshape((ncol_lat, ncol_lon))
    phisfcr = phisfcr.reshape((ncol_lat, ncol_lon))

    psfcr = psfcr.reshape((ncol_lat, ncol_lon))
    tr = tr.reshape((nlevs, ncol_lat, ncol_lon))

    pressr = pressr.reshape((nlevs, ncol_lat, ncol_lon))
    pressr = pressr.filled()

    pressir = pressir.reshape((nlevsp1, ncol_lat, ncol_lon))
    pressir = pressir.filled()

    print('plevr, plevrp1, plat, plon: ', plevr, plevrp1, plat, plon)
    print ('tr pressr pressir shapes: ', \
          tr.shape, pressr.shape, pressir.shape)
    print ('phisfcr, phisfcm, psfcr shapes: ', \
          phisfcr.shape, phisfcm.shape, psfcr.shape)

    print('Executing vic.')

    # Adjust lower level temperature and pressue using hydrostatic approximation
    # to account for differences between ERA5 and E3SM topography.
    #
    # In Fortran, the arrays are defined:
    #   real*8 press_m(plon,plat,plev)   ! analysis pressures
    #   real*8 press_i(plon,plat,plevp1) ! analysis pressures (interfaces)
    # Since the lat, lon are arbitrary, we just want to get the vertical in
    # the correct place - transpose since lev in last index.
    #----------------------------------------------------------------------
    psfcm = vic.psadj(plevr, plevrp1, plat, plon,
                      n.transpose(tr[:, :, :], (2, 1, 0)),
                      n.transpose(pressr[:, :, :], (2, 1, 0)),
                      n.transpose(pressir[:, :, :], (2, 1, 0)),
                      n.transpose(phisfcr), n.transpose(phisfcm),
                      n.transpose(psfcr))

    print('psfcm shape: ', psfcm.shape)

    psfcm = n.transpose(psfcm)
    print('psfcm shape: ', psfcm.shape)

    # Write new surface pressure to file.
    #------------------------------------
    phis[tidx, :] = (phisfcm.astype(n.float32)).reshape((ncol, ))

    ps[tidx, :] = (psfcm.astype(n.float32)).reshape((ncol, ))

    # Now compute vertical pressures using new surface pressure.
    #-----------------------------------------------------------
    new_shape = (len(hyam), psfcm.shape[0], psfcm.shape[1])

    pressm = (hyam[:, n.newaxis] * p0) + n.outer(hybm, psfcm)
    pressm.shape = new_shape
    print('pressm shape: ', pressm.shape)

    # Need to flip 3D arrays to new.
    #-------------------------------
    print ('pressr, pressir, pressm shapes: ', \
          pressr.shape, pressir.shape, pressm.shape)

    pressir = n.transpose(pressir[:, :, :], (0, 2, 1))
    pressr = n.transpose(pressr[:, :, :], (0, 2, 1))

    pressm = n.transpose(pressm, (0, 2, 1))

    print ('pressr, pressir, pressm shapes: ', \
          pressr.shape, pressir.shape, pressm.shape)

    print('phisfcr, psfcr shapes: ', phisfcr.shape, psfcr.shape)

    phisfcr = n.transpose(phisfcr)
    psfcr = n.transpose(psfcr)

    print('phisfcr, psfcr shapes: ', phisfcr.shape, psfcr.shape)

    for var in VAR_LIST:
        ta = ddi.get_state_data(VAR_DICT[var], ctime)
        tr = rgfa2m(cdms2.asVariable(ta))

        nlevsa = tr.shape[0]

        fac = FAC_DICT[var]
        offset = OFFSET_DICT[var]

        tr = tr.reshape((nlevsa, ncol, 1)).filled()
        tr = n.transpose(tr[:, :, :], (0, 2, 1))

        if var == 'T':
            print('**T**')
            print ('tr, pressr, pressir shapes: ', \
                  tr.shape, pressr.shape, pressir.shape)
            print ('pressm, phisfcr, psfcr shapes: ', \
                  pressm.shape, phisfcr.shape, psfcr.shape)

            tint = vic.vert_quad_opt1(plevr, plevrp1, plevm, plat, plon, tr,
                                      pressr, pressir, pressm, phisfcr, psfcr,
                                      0)

        if var == 'Q':
            print('**Q**')
            print ('tr, pressr, pressm shapes: ', \
                  tr.shape, pressr.shape, pressm.shape)

            tint = vic.vert_int_opt1(plat, plon, plevr, plevm, pressr, pressm,
                                     tr, 1)

        if (var == 'U') or (var == 'V'):
            print('**U/V**')
            print ('tr, pressr, pressm shapes: ', \
                  tr.shape, pressr.shape, pressm.shape)

            tint = vic.vert_int_opt2(plat, plon, plevr, plevm, pressr, pressm,
                                     tr, 0)

        print('tint shape: ', tint.shape)

        tint = (fac * n.transpose(tint, (0, 2, 1))) + offset

        print('tint shape: ', tint.shape)

        if var == 'T':
            tint = (tint.astype(n.float32)).reshape((len(levs), ncol))
            t[tidx, :, :] = tint[:, :]

        if var == 'Q':
            # Ensure q is >= 0.0.
            #--------------------
            tint = n.where(n.less(tint, 0.0), 0.0, tint)
            tint = (tint.astype(n.float32)).reshape((len(levs), ncol))

            q[tidx, :, :] = tint[:, :]

        if var == 'U':
            tint = (tint.astype(n.float32)).reshape((len(levs), ncol))
            u[tidx, :, :] = tint[:, :]

        if var == 'V':
            tint = (tint.astype(n.float32)).reshape((len(levs), ncol))
            v[tidx, :, :] = tint[:, :]

    return
lat1 = cols1d_lat
lon1 = cols1d_lon

nlat = 192
nlon = 288
nlev = TSOI_r.shape[1]
print(nlat, nlon, nlev)

latS = 25.
latE = 40.
lonS = 70.
lonE = 104.

TSOI_new1 = np.copy(TSOI_r)
TSOI_new1 = cdms.asVariable(TSOI_new1)

# set the start and end col id to speed up the loop later
#icolS = 1 #21000
#icolE = 42572 #30000
icolS = 14700
icolE = 19500

for i in range(icolS, icolE):
    a = int((lat1(i) + 90) * nlat / 180)
    b = int(lon1(i) * nlon / 360)
    #print(i,lat1[i],lon1[i],'a=',a,'b=',b)

    if (lat1[i] < latE) and (lat1[i] > latS) and (lon1[i] < lonE) and (lon1[i]
                                                                       > lonS):
        #print('i=',i,'lat1=',lat1[i],'lon1=',lon1[i],'a=',a,'b=',b)