def testwrap(lon,coord,index): ci = (coord[0],coord[1]) if len(coord)==2: indic = 'ccn' else: indic = coord[2] inter = lon.mapIntervalExt(ci,indic) if inter is not None: if inter[2]==-1: result = inter else: result = (inter[0],inter[1]) else: result = inter if result!=index: markError("%s ==> %s, not %s"%(`coord`,`result`,`index`))
#!/usr/bin/env python # Test dataset I/O print 'Test 1: Dataset I/O ... ', import cdms2,numpy,string,os,sys cdms2.setNetcdfUseParallelFlag(0) from cdms2.variable import WriteNotImplemented from cdms2.avariable import NotImplemented from markError import NTIME,NLAT,NLON,x,clearError,markError,reportError clearError() pth = os.path.dirname(os.path.abspath(__file__)) f = cdms2.open(os.path.join(pth,'test.xml')) if f.id!='test': markError('File id',f.id) u = f.variables['u'] try: scalar = u[0,0,0] except: markError("Scalar slice") try: s = u.size() except: markError("Size") else: if s!=1536: markError("Size",s) fullu = u[:] uslice = u[:,4:12,8:24] comp = x[0,:,4:12,8:24]
from markError import NTIME, NLAT, NLON, x, clearError, markError, reportError clearError() print 'Test 6: TransientVariables ...', f = cdms2.open(os.path.join(sys.prefix, 'sample_data', 'test.xml')) 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] try: xx = tv[1, 7, 15] except ValueError: markError("Scalar slice of transient variable") # Variable get: axis, grid, latitude, level, longitude, missing, order, time, len, typecode vaxis0 = v.getAxis(0) axis0 = tv.getAxis(0) if not numpy.ma.allequal(axis0[:], vaxis0[1:]): markError('getAxis: ' + ` axis0[:] `) taxis = tv.getTime() taxisarray = taxis[:] vaxisarray = vaxis0[1:] if not numpy.ma.allequal(taxisarray, vaxisarray): markError('getTime: ' + ` taxisarray `) vaxis1 = v.getAxis(1)
print 'Test 8: Regridding ...', ## lat = cdms2.createGaussianAxis(32) ## lon = cdms2.createUniformLongitudeAxis(0.0,64,360.0/64.) ## outgrid = cdms2.createRectGrid(lat,lon,'yx','gaussian') outgrid = cdms2.createGaussianGrid(32) pth = os.path.dirname(os.path.abspath(__file__)) f = cdms2.open(os.path.join(pth,'readonly.nc')) u = f.variables['u'] ingrid = u.getGrid() try: sh = ingrid.shape except: markError('Grid shape') regridf = Regridder(ingrid, outgrid) newu = regridf(u) if (abs(newu[0,0,-1]-488.4763488) > 1.e-3): markError('regrid',newu[0,0,-1]) newu = u.regrid(outgrid,regridTool='regrid2') if (abs(newu[0,0,-1]-488.4763488) > 1.e-3): markError('regrid',newu[0,0,-1]) # Regrid TV tv = u.subSlice(0) newtv = regridf(tv) if (abs(newtv[0,0,-1]-488.4763488) > 1.e-3): markError('regrid tv',newtv[0,0,-1]) newtv = tv.regrid(outgrid,regridTool='regrid2') if (abs(newtv[0,0,-1]-488.4763488) > 1.e-3): markError('regrid tv',newtv[0,0,-1])
print xmlpath, 'written' #-------------------------------------------------------------------------------------------------------------------------- if __name__ == '__main__': from cdms2 import CDMSError print 'Test 7: Database import ...', from markError import clearError, markError, reportError clearError() import os, sys argv = string.split( 'cdtest07.py -q -d test -x junk.xml u_2000.nc u_2001.nc u_2002.nc v_2000.nc v_2001.nc v_2002.nc' ) pth = os.path.dirname(os.path.abspath(__file__)) os.chdir(pth) try: main(argv) except CDMSError, e: markError(str(e)) except Exception, err: markError("Importing dataset") err = os.system("diff junk.xml test.xml") if err != 0: markError("Comparison with benchmark test.xml") reportError()
else: datasetnode.dump(xmlpath) if verbose: print xmlpath,'written' #-------------------------------------------------------------------------------------------------------------------------- if __name__ == '__main__': from cdms2 import CDMSError print 'Test 7: Database import ...', from markError import clearError,markError,reportError clearError() import os,sys argv = string.split('cdtest07.py -q -d test -x junk.xml u_2000.nc u_2001.nc u_2002.nc v_2000.nc v_2001.nc v_2002.nc') os.chdir(os.path.join(sys.prefix,'sample_data')) try: main(argv) except CDMSError,e: markError(str(e)) except Exception,err: markError("Importing dataset") err = os.system("diff junk.xml test.xml") if err!=0: markError("Comparison with benchmark test.xml") reportError()
# Can't write a _FillValue attribute to a Variable # J-Y Peterschmitt - LSCE - 07/2015 import cdms2, numpy, cdtime, os, sys import numpy as np from markError import NTIME, NLAT, NLON, x, clearError, markError, reportError clearError() data = np.random.random((10, 10)) data = np.ma.array(data, fill_value=1234.0) data = np.ma.masked_less(data, 0.5) dummy_grid = cdms2.createUniformGrid(10, 10, 1, 0, 10, 1) dummy_var = cdms2.createVariable(data, axes=[dummy_grid.getLatitude(), dummy_grid.getLongitude()], id="my_var") if dummy_var.fill_value != 1234.0: markError("createVariable fill_value failed") # Test if all fillvalue related attributes are changed dummy_var.fill_value = 2.0e20 if (dummy_var.fill_value != 2.0e20) or (dummy_var._FillValue != 2.0e20) or (dummy_var.missing_value != 2.0e20): markError("fill_value property failed (value 2.e20)") dummy_var._FillValue = 1.33 if (dummy_var.fill_value != 1.33) or (dummy_var._FillValue != 1.33) or (dummy_var.missing_value != 1.33): markError("fill_value property failed (value 1.33)") dummy_var.dummy_att = "Dummy att" dummy_var._dummy_att = "Dummy att starting with _" dummy_var.units = "10e3 dummy"
clearError() print 'Test 8: Regridding ...', ## lat = cdms2.createGaussianAxis(32) ## lon = cdms2.createUniformLongitudeAxis(0.0,64,360.0/64.) ## outgrid = cdms2.createRectGrid(lat,lon,'yx','gaussian') outgrid = cdms2.createGaussianGrid(32) f = cdms2.openDataset(os.path.join(get_sample_data_dir(), 'readonly.nc')) u = f.variables['u'] ingrid = u.getGrid() try: sh = ingrid.shape except: markError('Grid shape') regridf = Regridder(ingrid, outgrid) newu = regridf(u) if (abs(newu[0, 0, -1] - 488.4763488) > 1.e-3): markError('regrid', newu[0, 0, -1]) newu = u.regrid(outgrid) if (abs(newu[0, 0, -1] - 488.4763488) > 1.e-3): markError('regrid', newu[0, 0, -1]) # Regrid TV tv = u.subSlice(0) newtv = regridf(tv) if (abs(newtv[0, 0, -1] - 488.4763488) > 1.e-3): markError('regrid tv', newtv[0, 0, -1])
import MV2 from markError import clearError,markError,reportError print 'Test 15: reshape and mask and average ...', a=MV2.arange(100) try: failed = False a.shape=(10,10) except: failed = True a = MV2.reshape(a,(10,10)) if failed is True: markError('shape should not have worked (protected attribute)') if len(a.getAxisList())!=2: markError('reshape did not produce 2 axes') a=MV2.masked_greater(a,23) b=MV2.average(a,axis=0) c=a-b
f2.close() f3.close() os.system(cdat_info.get_prefix()+"/bin/cdscan -q --forecast -x 'test_fc.xml' test_fc?") # Read in the data. fcs = cdms2.forecast.forecasts( 'test_fc.xml', ("2010-08-25","2010-08-27") ) vin = fcs('var') fcaxis = vin._TransientVariable__domain[0][0] tinaxis = vin._TransientVariable__domain[1][0] xinaxis = vin._TransientVariable__domain[2][0] # Test vin against original data, looking especially at the forecast axis. # The following should be True, error otherwise: if not vin.id == v1.id : markError("wrong variable ID") if not numpy.alltrue( vin[0,:,:]==v1[:,:] ) : markError("wrong variable data for fc1") if not numpy.alltrue( vin[1,:,:]==v2[:,:] ) : markError("wrong variable data for fc2") if not vin.shape == (2,2,2) : markError("wrong variable shape") if not numpy.allclose( tinaxis._data_ , taxis._data_ ) : markError( "wrong t axis data" ) if not numpy.allclose( xinaxis._data_ , xaxis._data_ ) : markError( "wrong x axis data" ) tinaxis=vin.getAxis(1) print tinaxis print tinaxis.isTime() if not tinaxis.isTime()==True : markError( "time axis isn't time" ) if not xinaxis.isTime()==False : markError( "non-time axis is time" ) if not fcaxis.isTime()==False : markError( "forecast axis is time" ) if not fcaxis.isForecast()==True : markError( "forecast axis isn't forecast" ) if not fcaxis.shape == (3,) : markError( "forecast axis shape is wrong" ) #...Note that fcaxis is an Axis so its shape comes from len(fcaxis.node) which is the # number of forecasts described in test_fc.xml; in contrast for TransientAxis the shape
import cdms2,os,sys from cdms2.tvariable import TransientVariable as TV import MV2 print 'Test 11: MV module (transient variable arithmetic) ... ', from markError import NTIME,NLAT,NLON,x,clearError,markError,reportError from markError import get_sample_data_dir clearError() d = cdms2.open(os.path.join(get_sample_data_dir(),'test.xml')) ud = d['u'] vd = d['v'] udat = ud[:] vdat = vd[:] ulat = ud.getLatitude() if not isinstance(udat, TV): markError('Slice does not return TV') f = cdms2.open(os.path.join(get_sample_data_dir(),'u_2000.nc')) uf = f['u'] vel = MV2.sqrt(ud*ud + vd*vd) vel.id = 'velocity' vel2 = MV2.sqrt(udat*udat + vdat*vdat) vel2.id = 'velocity' vel2.units = ud.units if not MV2.allequal(vel,vel2): markError('Slice operators do not compare') x1 = uf+1.0 x2 = 1.0-ud x11 = -uf x12 = MV2.absolute(ud) x3 = uf+x2
## Automatically adapted for numpy.oldnumeric Aug 01, 2007 by #!/usr/bin/env python # Test dataset I/O print 'Test 1: Dataset I/O ... ', import cdms2,numpy,string,os,sys from cdms2.variable import WriteNotImplemented from cdms2.avariable import NotImplemented from markError import NTIME,NLAT,NLON,x,clearError,markError,reportError clearError() f = cdms2.open(os.path.join(sys.prefix,'sample_data','test.xml')) if f.id!='test': markError('File id',f.id) u = f.variables['u'] try: scalar = u[0,0,0] except: markError("Scalar slice") try: s = u.size() except: markError("Size") else: if s!=1536: markError("Size",s) fullu = u[:] uslice = u[:,4:12,8:24] comp = x[0,:,4:12,8:24]
print "Test 11: MV module (transient variable arithmetic) ... ", from markError import NTIME, NLAT, NLON, x, clearError, markError, reportError clearError() pth = os.path.dirname(os.path.abspath(__file__)) d = cdms2.open(os.path.join(pth, "test.xml")) ud = d["u"] vd = d["v"] udat = ud[:] vdat = vd[:] ulat = ud.getLatitude() if not isinstance(udat, TV): markError("Slice does not return TV") f = cdms2.open(os.path.join(pth, "u_2000.nc")) uf = f["u"] vel = MV2.sqrt(ud * ud + vd * vd) vel.id = "velocity" vel2 = MV2.sqrt(udat * udat + vdat * vdat) vel2.id = "velocity" vel2.units = ud.units if not MV2.allequal(vel, vel2): markError("Slice operators do not compare") x1 = uf + 1.0 x2 = 1.0 - ud x11 = -uf x12 = MV2.absolute(ud)
g.write(tchunk) g.close() imo += 12 os.unlink('cdtest10_t_2001.nc') os.unlink('cdtest10_t_2003.nc') os.unlink('cdtest10_uv_2003.nc') os.unlink('cdtest10_uv_2005.nc') # Test filemap access ... try: shutil.copy2(os.path.join(pth,"cdtest10.xml"),"cdtest10.xml") f = cdms2.open('cdtest10.xml') except Exception,err: print err markError('Opening cdtest10.xml using filemap: Version 3.0 or greater required') reportError() sys.exit(1) # Read u from 2001-7 to 2002-7 and compare u = f.getVariable('u') t = u.getTime() t1 = cdtime.comptime(2001,7).torel(t.units).value t2 = cdtime.comptime(2002,7).torel(t.units).value uar2 = u.getRegion(time=(t1,t2,'con')) uar2p = uar[18:30] if not numpy.ma.allclose(uar2,uar2p): markError('u.getRegion() from 2001-7 to 2002-7 failed',uar2[0,0,0]) # Read u from 2003-7 to 2004-7 and compare t1 = cdtime.comptime(2003,7).torel(t.units).value t2 = cdtime.comptime(2004,7).torel(t.units).value
f2.close() f3.close() os.system(sys.prefix + "/bin/cdscan -q --forecast -x 'test_fc.xml' test_fc?") # Read in the data. fcs = cdms2.forecast.forecasts('test_fc.xml', ("2010-08-25", "2010-08-27")) vin = fcs('var') fcaxis = vin._TransientVariable__domain[0][0] tinaxis = vin._TransientVariable__domain[1][0] xinaxis = vin._TransientVariable__domain[2][0] # Test vin against original data, looking especially at the forecast axis. # The following should be True, error otherwise: if not vin.id == v1.id: markError("wrong variable ID") if not numpy.alltrue(vin[0, :, :] == v1[:, :]): markError("wrong variable data for fc1") if not numpy.alltrue(vin[1, :, :] == v2[:, :]): markError("wrong variable data for fc2") if not vin.shape == (2, 2, 2): markError("wrong variable shape") if not numpy.allclose(tinaxis._data_, taxis._data_): markError("wrong t axis data") if not numpy.allclose(xinaxis._data_, xaxis._data_): markError("wrong x axis data") tinaxis = vin.getAxis(1) print tinaxis print tinaxis.isTime() if not tinaxis.isTime() == True: markError("time axis isn't time") if not xinaxis.isTime() == False: markError("non-time axis is time") if not fcaxis.isTime() == False: markError("forecast axis is time")
import MV2 from markError import clearError, markError, reportError clearError() print 'Test 15: reshape and mask and average ...', a = MV2.arange(100) try: failed = False a.shape = (10, 10) except: failed = True a = MV2.reshape(a, (10, 10)) if failed is False: markError('shape should not have worked (protected attribute)') if len(a.getAxisList()) != 2: markError('reshape did not produce 2 axes') a = MV2.masked_greater(a, 23) b = MV2.average(a, axis=0) c = a - b reportError()
os.unlink('cdtest10_t_2001.nc') os.unlink('cdtest10_t_2003.nc') os.unlink('cdtest10_uv_2003.nc') os.unlink('cdtest10_uv_2005.nc') # Copy cdtest10.xml to working directory shutil.copyfile(os.path.join(get_sample_data_dir(), 'cdtest10.xml'), 'cdtest10.xml') # Test filemap access ... try: f = cdms2.open('cdtest10.xml') except Exception, err: print err markError( 'Opening cdtest10.xml using filemap: Version 3.0 or greater required') reportError() sys.exit(1) # Read u from 2001-7 to 2002-7 and compare u = f.getVariable('u') t = u.getTime() t1 = cdtime.comptime(2001, 7).torel(t.units).value t2 = cdtime.comptime(2002, 7).torel(t.units).value uar2 = u.getRegion(time=(t1, t2, 'con')) uar2p = uar[18:30] if not numpy.ma.allclose(uar2, uar2p): markError('u.getRegion() from 2001-7 to 2002-7 failed', uar2[0, 0, 0]) # Read u from 2003-7 to 2004-7 and compare t1 = cdtime.comptime(2003, 7).torel(t.units).value
f = cdms2.createDataset("readwrite4.nc") pth = os.path.dirname(os.path.abspath(__file__)) h = cdms2.open(os.path.join(pth, "readonly.nc")) tobj = f.createAxis("time", numpy.ma.array([time[1]])) tobj.units = "days since 2000-1-1" latobj = f.createAxis("latitude", lat) latobj.units = "degrees_north" lonobj = f.createAxis("longitude", lon) lonobj.units = "degrees_east" var = f.createVariable("u", numpy.float, (tobj, latobj, lonobj)) var.units = "m/s" try: var[:] = u[0] except: markError("Setting a slice") try: var.assignValue(u[0]) except: markError("Assigning an array") try: var[0, 4:12] = -u[0, 4:12] except: markError("Setting an item") varattrs = var.attributes var.long_name = "Test variable" var.param = -99 if var.param != -99: markError("R/W param: " + var.param) if (not var.attributes.has_key("param")) or var.attributes["param"] != -99:
f2.close() f3.close() os.system("../Script/cdscan -q --forecast -x 'test_fc.xml' test_fc?") # Read in the data. fcs = cdms2.forecast.forecasts( 'test_fc.xml', ("2010-08-25","2010-08-27") ) vin = fcs('var') fcaxis = vin._TransientVariable__domain[0][0] tinaxis = vin._TransientVariable__domain[1][0] xinaxis = vin._TransientVariable__domain[2][0] # Test vin against original data, looking especially at the forecast axis. # The following should be True, error otherwise: if not vin.id == v1.id : markError("wrong variable ID") if not numpy.alltrue( vin[0,:,:]==v1[:,:] ) : markError("wrong variable data for fc1") if not numpy.alltrue( vin[1,:,:]==v2[:,:] ) : markError("wrong variable data for fc2") if not vin.shape == (2,2,2) : markError("wrong variable shape") if not numpy.alltrue( tinaxis._data_ == taxis._data_ ) : markError( "wrong t axis data" ) if not numpy.alltrue( xinaxis._data_ == xaxis._data_ ) : markError( "wrong x axis data" ) if not tinaxis.isTime()==True : markError( "time axis isn't time" ) if not xinaxis.isTime()==False : markError( "non-time axis is time" ) if not fcaxis.isTime()==False : markError( "forecast axis is time" ) if not fcaxis.isForecast()==True : markError( "forecast axis isn't forecast" ) if not fcaxis.shape == (3,) : markError( "forecast axis shape is wrong" ) #...Note that fcaxis is an Axis so its shape comes from len(fcaxis.node) which is the # number of forecasts described in test_fc.xml; in contrast for TransientAxis the shape # would be len(fcaxis._data_) which is the number of forecasts that have been read. if not fcaxis._data_ == [2010082500000L, 2010082600000L] : markError( "wrong forecast axis data" ) if not fcaxis.id == 'fctau0' : markError( "wrong forecast axis id" )
print 'Test 5: get/sub, time functions ...', pth = os.path.dirname(os.path.abspath(__file__)) f = cdms2.open(os.path.join(pth, 'test.xml')) v = f.variables['v'] vp = x[1, 1:, 4:12, 8:24] wp = x[1, 2, 4:12] xp = numpy.ma.concatenate((x[1, 1:, 4:12, 8:NLON], x[1, 1:, 4:12, 0:8]), axis=2) # getRegion - positional s = v.getRegion((366., 731., 'ccn'), (-42., 42., 'ccn'), (90., 270., 'con')) if not numpy.ma.allequal(vp, s): markError('getRegion/positional failed') # getRegion - keyword s = v.getRegion(latitude=(-42., 42., 'ccn'), longitude=(90., 270., 'con'), time=(366., 731., 'ccn')) if not numpy.ma.allequal(vp, s): markError('getRegion/keyword failed') # getRegion - wraparound s4 = v.getRegion(time=(366., 731., 'ccn'), latitude=(-42., 42., 'ccn'), longitude=(90., 450., 'con')) if not numpy.ma.allequal(xp, s4): markError('getRegion wraparound failed') # getSlice - positional s = v.getSlice(2, (4, 12), Ellipsis, squeeze=0)
print "Test 2: CdmsFile I/O ... ", import cdms2, numpy, string, os, sys from numpy.ma import masked from markError import NTIME, NLAT, NLON, x, clearError, markError, reportError clearError() f = cdms2.open(os.path.join(sys.prefix, "sample_data", "readonly.nc")) u = f.variables["u"] umasked = f.variables["umasked"] try: s = u.size() except: markError("Size") else: if s != 512: markError("Size", s) uslice = u[:, 4:12, 8:24] fullu = u[:] uslicemask = umasked[:, 0:4, 8:24] comp = x[0, 0, 4:12, 8:24] compmask = x[0, 0, 0:4, 8:24] compmask[1] = masked lonaxis = f["longitude"] if not numpy.ma.allequal(lonaxis.getValue(), lonaxis[:]): markError("Axis getValue()")
print 'Test 2: CdmsFile I/O ... ', import cdms2, numpy, string, os, sys from numpy.ma import masked from markError import NTIME, NLAT, NLON, x, clearError, markError, reportError clearError() f = cdms2.open(os.path.join(sys.prefix, 'sample_data', 'readonly.nc')) u = f.variables['u'] umasked = f.variables['umasked'] try: s = u.size() except: markError("Size") else: if s != 512: markError("Size", s) uslice = u[:, 4:12, 8:24] fullu = u[:] uslicemask = umasked[:, 0:4, 8:24] comp = x[0, 0, 4:12, 8:24] compmask = x[0, 0, 0:4, 8:24] compmask[1] = masked lonaxis = f['longitude'] if not numpy.ma.allequal(lonaxis.getValue(), lonaxis[:]): markError("Axis getValue()") if not numpy.ma.allequal(uslice, comp): markError("Slice read")
print 'Test 2: CdmsFile I/O ... ', import cdms2,numpy,string,os,sys from numpy.ma import masked from markError import NTIME,NLAT,NLON,x,clearError,markError,reportError from markError import get_sample_data_dir clearError() f = cdms2.open(os.path.join(get_sample_data_dir(),'readonly.nc')) u = f.variables['u'] umasked = f.variables['umasked'] try: s = u.size() except: markError("Size") else: if s!=512: markError("Size",s) uslice = u[:,4:12,8:24] fullu = u[:] uslicemask = umasked[:,0:4,8:24] comp = x[0,0,4:12,8:24] compmask = x[0,0,0:4,8:24] compmask[1]=masked lonaxis = f['longitude'] if not numpy.ma.allequal(lonaxis.getValue(), lonaxis[:]): markError("Axis getValue()") if not numpy.ma.allequal(uslice,comp): markError("Slice read") if not numpy.ma.allequal(uslicemask,compmask): markError("Slice read, masked")
latb = numpy.array([ -26.67690036, -30.99890917, ]) lonb = numpy.array([ 92.41822415, 94.4512163, ]) f = cdms2.open(os.path.join(sys.prefix, 'sample_data', 'sampleGenGrid3.nc')) # Slice a file variable on a curvilinear grid: by coordinates ... samp = f['sample'] x = samp(lat=(-32, -25), lon=(90, 95)) if not numpy.ma.allequal(x.data, datb): markError('slicing a file variable by coordinates') grid = x.getGrid() if grid.shape != (2, ): markError('grid shape is wrong') lat = grid.getLatitude() if not numpy.ma.allclose(lat.data, latb, atol=1.e-5): markError('latitude array is wrong') lon = grid.getLongitude() if not numpy.ma.allclose(lon.data, lonb, atol=1.e-5): markError('longitude array is wrong') # ... and by index y = samp[693:695] if not numpy.ma.allequal(y, datb): markError('slicing a file variable by index')
timestr = ['2000','2001','2002'] u = x[0] f = cdms2.createDataset('readwrite3.nc') tobj = f.createAxis('time',numpy.array([time[1]])) tobj.units = 'days since 2000-1-1' latobj = f.createAxis('latitude',lat) latobj.units = 'degrees_north' lonobj = f.createAxis('longitude',lon) lonobj.units = 'degrees_east' var = f.createVariable('u',cdms2.CdDouble,(tobj,latobj,lonobj)) var.units = 'm/s' try: var[:]=u[0] except: markError("Setting a slice") try: var.assignValue(u[0]) except: markError("Assigning an array") try: var[0,4:12] = -u[0,4:12] except: markError("Setting an item") var.long_name = 'Test variable' var.param = -99 if var.param!=-99: markError("R/W param: "+var.param) f.Conventions = 'CF1.0'
clearError() print 'Test 6: TransientVariables ...', pth = os.path.dirname(os.path.abspath(__file__)) f = cdms2.open(os.path.join(pth,'test.xml')) 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] try: xx = tv[1,7,15] except ValueError: markError("Scalar slice of transient variable") # Variable get: axis, grid, latitude, level, longitude, missing, order, time, len, typecode vaxis0 = v.getAxis(0) axis0 = tv.getAxis(0) if not numpy.ma.allequal(axis0[:],vaxis0[1:]): markError('getAxis: '+`axis0[:]`) taxis = tv.getTime() taxisarray = taxis[:] vaxisarray = vaxis0[1:] if not numpy.ma.allequal(taxisarray,vaxisarray): markError('getTime: '+`taxisarray`) vaxis1 = v.getAxis(1) lataxis = tv.getLatitude() if not numpy.ma.allequal(lataxis[:],vaxis1[4:12]): markError('getLatitude: '+`lataxis[:]`)
import numpy as np from markError import NTIME, NLAT, NLON, x, clearError, markError, reportError clearError() data = np.random.random((10, 10)) data = np.ma.array(data, fill_value=1234.0) data = np.ma.masked_less(data, 0.5) dummy_grid = cdms2.createUniformGrid(10, 10, 1, 0, 10, 1) dummy_var = cdms2.createVariable( data, axes=[dummy_grid.getLatitude(), dummy_grid.getLongitude()], id='my_var') if dummy_var.fill_value != 1234.0: markError("createVariable fill_value failed") # Test if all fillvalue related attributes are changed dummy_var.fill_value = 2.e+20 if (dummy_var.fill_value != 2.e20) or \ (dummy_var._FillValue != 2.e20) or \ (dummy_var.missing_value != 2.e20) : markError("fill_value property failed (value 2.e20)") dummy_var._FillValue = 1.33 if (dummy_var.fill_value != 1.33) or \ (dummy_var._FillValue != 1.33) or \ (dummy_var.missing_value != 1.33) : markError("fill_value property failed (value 1.33)") dummy_var.dummy_att = 'Dummy att'
tchunk = t.subSlice((imo,imo+12)) g.write(tchunk) g.close() imo += 12 os.unlink('cdtest10_t_2001.nc') os.unlink('cdtest10_t_2003.nc') os.unlink('cdtest10_uv_2003.nc') os.unlink('cdtest10_uv_2005.nc') # Test filemap access ... try: f = cdms2.open('cdtest10.xml') except Exception,err: print err markError('Opening cdtest10.xml using filemap: Version 3.0 or greater required') reportError() sys.exit(1) # Read u from 2001-7 to 2002-7 and compare u = f.getVariable('u') t = u.getTime() t1 = cdtime.comptime(2001,7).torel(t.units).value t2 = cdtime.comptime(2002,7).torel(t.units).value uar2 = u.getRegion(time=(t1,t2,'con')) uar2p = uar[18:30] if not numpy.ma.allclose(uar2,uar2p): markError('u.getRegion() from 2001-7 to 2002-7 failed',uar2[0,0,0]) # Read u from 2003-7 to 2004-7 and compare t1 = cdtime.comptime(2003,7).torel(t.units).value t2 = cdtime.comptime(2004,7).torel(t.units).value
[[0,0,0,0,0,0,], [0,0,0,0,0,0,], [0,0,0,0,0,0,], [0,0,0,0,0,0,], [1,1,1,1,1,0,]], 'b') f = cdms2.open(os.path.join(pth,'sampleCurveGrid4.nc')) #------------------------------------------------------------- # Slice a file variable on a curvilinear grid: by coordinates ... samp = f['sample'] x=samp() x = samp(lat=(-10,30), lon=(90,150)) if not numpy.ma.allequal(x.data, datb): markError('slicing a file variable by coordinates') if not numpy.ma.allequal(x.mask, maskb): markError('slicing a file variable by coordinates: invalid mask') grid = x.getGrid() if not (grid.shape==(5,6)): markError('grid shape is wrong') lat = grid.getLatitude() if not numpy.ma.allclose(lat.data, latb, atol=1.e-5): markError('latitude array is wrong') lon = grid.getLongitude() if not numpy.ma.allclose(lon.data, lonb, atol=1.e-5): markError('longitude array is wrong') # ... and by index y = samp[14:19, 25:31]
clearError() print 'Test 18: numpy types with NetCDF4 ...', for t in [numpy.byte,numpy.short,numpy.int,numpy.int32,numpy.float,numpy.float32,numpy.double,numpy.ubyte,numpy.ushort,numpy.uint,numpy.int64,numpy.uint64]: print 'Testing type:',t data = numpy.array([0], dtype=t) var = cdms2.createVariable(data) f = cdms2.open('test_%s.nc'%data.dtype.char, 'w') f.write(var, id='test') f.close() f=cdms2.open('test_%s.nc'%data.dtype.char) s=f("test") f.close() os.unlink("test_%s.nc" % data.dtype.char) print 'Done' cdms2.setNetcdfShuffleFlag(0) cdms2.setNetcdfDeflateFlag(0) cdms2.setNetcdfDeflateLevelFlag(0) var = cdms2.createVariable(numpy.array([0], dtype=numpy.int64)) f = cdms2.open('test.nc', 'w') try: f.write(var, id='test') except Exception,err: markError("Writing Netcdf4 type to NetCDF3 format") os.unlink("test.nc") f.close() reportError()
) maskb = numpy.array( [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0]], "b" ) f = cdms2.open(os.path.join(sys.prefix, "sample_data", "sampleCurveGrid4.nc")) # ------------------------------------------------------------- # Slice a file variable on a curvilinear grid: by coordinates ... samp = f["sample"] x = samp() x = samp(lat=(-10, 30), lon=(90, 150)) if not numpy.ma.allequal(x.data, datb): markError("slicing a file variable by coordinates") if not numpy.ma.allequal(x.mask, maskb): markError("slicing a file variable by coordinates: invalid mask") grid = x.getGrid() if not (grid.shape == (5, 6)): markError("grid shape is wrong") lat = grid.getLatitude() if not numpy.ma.allclose(lat.data, latb, atol=1.0e-5): markError("latitude array is wrong") lon = grid.getLongitude() if not numpy.ma.allclose(lon.data, lonb, atol=1.0e-5): markError("longitude array is wrong") # ... and by index y = samp[14:19, 25:31]
u = x[0] f = cdms2.createDataset('readwrite.nc') h = cdms2.open(os.path.join(get_sample_data_dir(),'readonly.nc')) tobj = f.createAxis('time',numpy.ma.array([time[1]])) tobj.units = 'days since 2000-1-1' latobj = f.createAxis('latitude',lat) latobj.units = 'degrees_north' lonobj = f.createAxis('longitude',lon) lonobj.units = 'degrees_east' var = f.createVariable('u',numpy.float,(tobj,latobj,lonobj)) var.units = 'm/s' try: var[:]=u[0] except: markError("Setting a slice") try: var.assignValue(u[0]) except: markError("Assigning an array") try: var[0,4:12] = -u[0,4:12] except: markError("Setting an item") varattrs = var.attributes var.long_name = 'Test variable' var.param = -99 if var.param!=-99: markError("R/W param: "+var.param) if (not var.attributes.has_key('param')) or var.attributes['param']!=-99: markError("Updating variable attributes")
import cdms2, numpy, os, sys from markError import clearError,markError,reportError from markError import get_sample_data_dir clearError() datb = numpy.array([ 693., 694.,]) latb = numpy.array([-26.67690036,-30.99890917,]) lonb = numpy.array([92.41822415, 94.4512163 ,]) f = cdms2.open(os.path.join(get_sample_data_dir(),'sampleGenGrid3.nc')) # Slice a file variable on a curvilinear grid: by coordinates ... samp = f['sample'] x = samp(lat=(-32,-25), lon=(90,95)) if not numpy.ma.allequal(x.data, datb): markError('slicing a file variable by coordinates') grid = x.getGrid() if grid.shape!=(2,): markError('grid shape is wrong') lat = grid.getLatitude() if not numpy.ma.allclose(lat.data, latb, atol=1.e-5): markError('latitude array is wrong') lon = grid.getLongitude() if not numpy.ma.allclose(lon.data, lonb, atol=1.e-5): markError('longitude array is wrong') # ... and by index y = samp[693:695] if not numpy.ma.allequal(y, datb): markError('slicing a file variable by index')
from markError import NTIME,NLAT,NLON,x,clearError,markError,reportError from markError import get_sample_data_dir clearError() print 'Test 5: get/sub, time functions ...', f = cdms2.open(os.path.join(get_sample_data_dir(),'test.xml')) v = f.variables['v'] vp = x[1,1:,4:12,8:24] wp = x[1,2,4:12] xp = numpy.ma.concatenate((x[1,1:,4:12,8:NLON],x[1,1:,4:12,0:8]),axis=2) # getRegion - positional s = v.getRegion((366.,731.,'ccn'),(-42.,42.,'ccn'),(90.,270.,'con')) if not numpy.ma.allequal(vp,s): markError('getRegion/positional failed') # getRegion - keyword s = v.getRegion(latitude=(-42.,42.,'ccn'),longitude=(90.,270.,'con'),time=(366.,731.,'ccn')) if not numpy.ma.allequal(vp,s): markError('getRegion/keyword failed') # getRegion - wraparound s4 = v.getRegion(time=(366.,731.,'ccn'),latitude=(-42.,42.,'ccn'),longitude=(90.,450.,'con')) if not numpy.ma.allequal(xp,s4): markError('getRegion wraparound failed') # getSlice - positional s = v.getSlice(2,(4,12),Ellipsis,squeeze=0) if not numpy.ma.allequal(wp,s): markError('getSlice/positional failed') # getSlice - keyword s = v.getSlice(latitude=(4,12), time=2)
def testCal(a,b,c,d,e,f,g,cal=cdtime.MixedCalendar): x = cdtime.comptime(d,e,f) units = "days since %d-%d-%d"%(a,b,c) r = x.torel(units,cal) if not isEqual(r.value,g): markError('component => relative failed: %s %s'%(`x`,`r`)) r2 = cdtime.reltime(g, units) x2 = r2.tocomp(cal) if not cmpYear(x,x2): markError('relative => component failed: %s %s'%(`r2`,`x2`)) units2 = "days since %d-%d-%d"%(d,e,f) r3 = cdtime.reltime(10.0,units2) r4 = r3.torel(units) if not isEqual(r4.value,(10.0+g)): markError('relative => relative: %s %s'%(`r3`,`r4`)) bb = cdtime.comptime(a,b,c) x2 = bb.add(g,cdtime.Day,cal) if not cmpYear(x,x2): markError('component add failed: %s %s'%(`bb`,`x2`)) x2 = x.sub(g,cdtime.Day,cal) if not cmpYear(bb,x2): markError('component sub failed: %s %s'%(`x`,`x2`)) r2 = cdtime.reltime(g, units) r3 = r2.add(1000.0,cdtime.Day,cal) if not isEqual(r3.value, g+1000.0): markError('relative add failed: %s %s'%(`r2`,`r3`)) r3 = r2.sub(1000.0,cdtime.Day,cal) if not isEqual(r3.value, g-1000.0): markError('relative sub failed: %s %s'%(`r2`,`r3`))
from cdms2.tvariable import TransientVariable as TV import MV2 print 'Test 11: MV module (transient variable arithmetic) ... ', from markError import NTIME, NLAT, NLON, x, clearError, markError, reportError clearError() pth = os.path.dirname(os.path.abspath(__file__)) d = cdms2.open(os.path.join(pth, 'test.xml')) ud = d['u'] vd = d['v'] udat = ud[:] vdat = vd[:] ulat = ud.getLatitude() if not isinstance(udat, TV): markError('Slice does not return TV') f = cdms2.open(os.path.join(pth, 'u_2000.nc')) uf = f['u'] vel = MV2.sqrt(ud * ud + vd * vd) vel.id = 'velocity' vel2 = MV2.sqrt(udat * udat + vdat * vdat) vel2.id = 'velocity' vel2.units = ud.units if not MV2.allequal(vel, vel2): markError('Slice operators do not compare') x1 = uf + 1.0 x2 = 1.0 - ud x11 = -uf x12 = MV2.absolute(ud) x3 = uf + x2
[[0,0,0,0,0,0,], [0,0,0,0,0,0,], [0,0,0,0,0,0,], [0,0,0,0,0,0,], [1,1,1,1,1,0,]], 'b') f = cdms2.open(os.path.join(get_sample_data_dir(),'sampleCurveGrid4.nc')) #------------------------------------------------------------- # Slice a file variable on a curvilinear grid: by coordinates ... samp = f['sample'] x=samp() x = samp(lat=(-10,30), lon=(90,150)) if not numpy.ma.allequal(x.data, datb): markError('slicing a file variable by coordinates') if not numpy.ma.allequal(x.mask, maskb): markError('slicing a file variable by coordinates: invalid mask') grid = x.getGrid() if not (grid.shape==(5,6)): markError('grid shape is wrong') lat = grid.getLatitude() if not numpy.ma.allclose(lat.data, latb, atol=1.e-5): markError('latitude array is wrong') lon = grid.getLongitude() if not numpy.ma.allclose(lon.data, lonb, atol=1.e-5): markError('longitude array is wrong') # ... and by index y = samp[14:19, 25:31]