本文整理汇总了Python中netCDF4.Dataset.createVariable方法的典型用法代码示例。如果您正苦于以下问题:Python Dataset.createVariable方法的具体用法?Python Dataset.createVariable怎么用?Python Dataset.createVariable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netCDF4.Dataset
的用法示例。
在下文中一共展示了Dataset.createVariable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_variable_units
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createVariable [as 别名]
def test_variable_units(self):
'''
Tests that the variable units test is working
'''
# this check tests that units attribute is present on EVERY variable
# Create an empty dataset that writes to /dev/null This acts as a
# temporary netCDF file in-memory that never gets written to disk.
nc_obj = Dataset(os.devnull, 'w', diskless=True)
self.addCleanup(nc_obj.close)
# The dataset needs at least one variable to check that it's missing
# all the required attributes.
nc_obj.createDimension('time', 1)
nc_obj.createVariable('sample_var', 'd', ('time',))
sample_var = nc_obj.variables['sample_var']
results = self.ioos.check_variable_units(nc_obj)
self.assert_result_is_bad(results)
sample_var.units = 'm'
sample_var.short_name = 'sample_var'
results = self.ioos.check_variable_units(nc_obj)
self.assert_result_is_good(results)
示例2: setUp
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createVariable [as 别名]
def setUp(self):
self.files = [tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name for nfile in range(2)]
for nfile,file in enumerate(self.files):
f = Dataset(file,'w',format='NETCDF4_CLASSIC')
f.createDimension('time',None)
f.createDimension('y',ydim)
f.createDimension('z',zdim)
f.history = 'created today'
time = f.createVariable('time', 'f', ('time', ))
#time.units = 'days since {0}-01-01'.format(1979+nfile)
yr = 1979+nfile
time.units = 'days since %s-01-01' % yr
time.calendar = 'standard'
x = f.createVariable('x','f',('time', 'y', 'z'))
x.units = 'potatoes per square mile'
nx1 = self.ninc*nfile;
nx2 = self.ninc*(nfile+1)
time[:] = np.arange(self.ninc)
x[:] = np.arange(nx1, nx2).reshape(self.ninc,1,1) * np.ones((1, ydim, zdim))
f.close()
示例3: netcdfSIT
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createVariable [as 别名]
def netcdfSIT(lats,lons,var):
directory = '/home/zlabe/Surtsey/seaice_obs/Thk/March/'
name = 'icesatG_regrid_March_20032008.nc'
filename = directory + name
ncfile = Dataset(filename,'w',format='NETCDF4')
ncfile.description = 'Sea ice thickness processed by NASA-G and now' \
'regridded on an EASE2.0 100 km grid for the' \
'period of March 2003-2008'
### Dimensions
ncfile.createDimension('years',var.shape[0])
ncfile.createDimension('lat',var.shape[1])
ncfile.createDimension('lon',var.shape[2])
### Variables
years = ncfile.createVariable('years','f4',('years'))
latitude = ncfile.createVariable('lat','f4',('lat','lat'))
longitude = ncfile.createVariable('lon','f4',('lon','lon'))
varns = ncfile.createVariable('sit','f4',('years','lat','lon'))
### Units
varns.units = 'meters'
ncfile.title = 'ICESat-G'
ncfile.instituion = 'Dept. ESS at University of California, Irvine'
ncfile.source = 'NASA-G'
ncfile.references = 'Donghui Yi, H. Zwally'
### Data
years[:] = list(xrange(var.shape[0]))
latitude[:] = lats
longitude[:] = lons
varns[:] = var
ncfile.close()
print 'Completed: Created netCDF4 File!'
示例4: save_alts_to_netcdf_file
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createVariable [as 别名]
def save_alts_to_netcdf_file(path="alt.nc",
data_path = "/home/huziy/skynet1_rech3/cordex/CORDEX_DIAG/NorthAmerica_0.44deg_MPI_B1",
year_range=None, coord_file=None):
year_range = range(1950, 2101) if year_range is None else year_range
ds = Dataset(path, mode="w", format="NETCDF3_CLASSIC")
if coord_file is None:
coord_file = os.path.join(data_path, "pmNorthAmerica_0.44deg_MPIHisto_B1_200009_moyenne")
b, lons2d, lats2d = draw_regions.get_basemap_and_coords(file_path=coord_file)
ds.createDimension('year', len(year_range))
ds.createDimension('lon', lons2d.shape[0])
ds.createDimension('lat', lons2d.shape[1])
lon_variable = ds.createVariable('longitude', 'f4', ('lon', 'lat'))
lat_variable = ds.createVariable('latitude', 'f4', ('lon', 'lat'))
year_variable = ds.createVariable("year", "i4", ("year",))
alt_variable = ds.createVariable("alt", "f4", ('year', 'lon', 'lat'))
lon_variable[:, :] = lons2d[:, :]
lat_variable[:, :] = lats2d[:, :]
year_variable[:] = year_range
dm = CRCMDataManager(data_folder=data_path)
dm_list = len(year_range) * [dm]
mean_types = len(year_range) * ["monthly"]
pool = Pool(processes=6)
alts = pool.map(get_alt_for_year, list(zip(year_range, dm_list, mean_types)))
alts = np.array(alts)
alt_variable[:, :, :] = alts[:, :, :]
ds.close()
示例5: wind_data
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createVariable [as 别名]
def wind_data(file_name, mode='netCDF'):
#every fifteen minutes maybe not necessary
frequency = 1/900
series_length = 1440
time = unit_conversion.generate_ms(1404647999870, series_length, frequency)
wind_direction = get_rand_circular_data(series_length, 15, 360)
wind_speed = get_rand_discrete_data(series_length, 2, 5, 0)
if mode == 'netCDF':
ds = Dataset(file_name, 'w', format="NETCDF4_CLASSIC")
ds.createDimension('time',len(time))
time_var = ds.createVariable('time','f8',('time'))
time_var[:] = time
wind_speed_var = ds.createVariable('wind_speed','f8',('time'))
wind_speed_var[:] = wind_speed
wind_direction_var = ds.createVariable('wind_direction','f8',('time'))
wind_direction_var[:] = wind_direction
ds.close()
else:
excelFile = pd.DataFrame({'Time': time,
'Wind Speed in m/s': wind_speed,
'Wind Direction in degrees': wind_direction,
})
excelFile.to_csv(path_or_buf= file_name)
print('total:', len(time), len(wind_direction), len(wind_speed))
示例6: write_stat_nc
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createVariable [as 别名]
def write_stat_nc(results):
ncfile = Dataset('ST' + results['info']['station'] + '_' + results['info']['year'] + '_' + results['info']['month'] + '.nc','w')
create_stat_global_att(ncfile,results['info'])
ncfile.createDimension('time',results['data']['time'].shape[0])
ncfile.createDimension('freq',results['data']['frequency'].shape[0])
ncfile.createDimension('direc',results['data']['direction'].shape[0])
# ncfile.createDimension('date',6)
for key,value in results['data'].items():
varshape = results['info']['outshape'][key]
if isinstance(varshape,dict):
if len(varshape) == 2:
dataset = ncfile.createVariable(key,results['info']['outtype'][key],(varshape[0],varshape[1]),zlib=True,complevel=4)
elif len(varshape) == 3:
dataset = ncfile.createVariable(key,results['info']['outtype'][key],(varshape[0],varshape[1],varshape[2]),fill_value=-999.9,zlib=True,complevel=4)
else:
dataset = ncfile.createVariable(key,results['info']['outtype'][key],(varshape,),fill_value=-999.9,zlib=True,complevel=4)
dataset[:] = value
dataset.Dimension = value.shape
longn, units = create_stat_var_att(key)
dataset.long_name = longn
dataset.units = units
ncfile.close()
示例7: runTest
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createVariable [as 别名]
def runTest(self):
f = Dataset(FILE_NAME, 'w', format='NETCDF3_CLASSIC')
f.createDimension('x', 1)
with self.assertRaisesRegexp(ValueError, 'strings are only supported'):
f.createVariable('foo', str, ('x',))
f.close()
os.remove(FILE_NAME)
示例8: netcdfPiomas
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createVariable [as 别名]
def netcdfPiomas(lats,lons,var,directory):
name = 'piomas_regrid_March_19792015.nc'
filename = directory + name
ncfile = Dataset(filename,'w',format='NETCDF4')
ncfile.description = 'PIOMAS Sea ice thickness reanalysis from 1979-2015 ' \
'interpolated on a 180x180 grid (latxlon)' \
'of NSIDC EASE100'
### Dimensions
ncfile.createDimension('years',var.shape[0])
ncfile.createDimension('lat',var.shape[1])
ncfile.createDimension('lon',var.shape[2])
### Variables
years = ncfile.createVariable('years','f4',('years'))
latitude = ncfile.createVariable('lat','f4',('lat','lat'))
longitude = ncfile.createVariable('lon','f4',('lon','lon'))
varns = ncfile.createVariable('thick','f4',('years','lat','lon'))
### Metrics
varns.units = 'meters'
ncfile.title = 'PIOMAS March SIT'
ncfile.instituion = 'Dept. ESS at University of California, Irvine'
ncfile.source = 'University of Washington'
ncfile.references = '[Zhang and Rothrock, 2003]'
### Data
years[:] = list(xrange(var.shape[0]))
latitude[:] = lats
longitude[:] = lons
varns[:] = var
ncfile.close()
print 'Completed: Created netCDF4 File!'
示例9: create_awips2_netcdf3
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createVariable [as 别名]
def create_awips2_netcdf3(filename, image, start_dt,
depictor_name, channel, source_name, satellite_name,
**grid_info):
nc_name = os.path.abspath(filename)
nc = Dataset(nc_name, mode='w', format="NETCDF3_CLASSIC")
y_dim = nc.createDimension("y", size=image.shape[0])
x_dim = nc.createDimension("x", size=image.shape[1])
time_var = nc.createVariable("validTime", "f8")
time_var.units = "seconds since 1970-1-1 00:00:00.00 0:00"
time_var[:] = float(calendar.timegm(start_dt.utctimetuple())) + float(start_dt.microsecond)/1e6
image_var = nc.createVariable("image", "i1", ("y", "x"))
image_var.set_auto_maskandscale(False)
image_var[:] = clip_to_data_type(image, DTYPE_UINT8)
nc.depictorName = depictor_name
nc.channel = channel
nc.source = source_name
nc.satelliteName = satellite_name
for k, v in grid_info.items():
attr_name = GRID_ATTR_NAME.get(k, k)
attr_type = GRID_ATTR_TYPE.get(k, None)
LOG.debug("Setting grid information for NetCDF file: %s -> %s", attr_name, v)
if attr_type is not None:
v = attr_type(v)
setattr(nc, attr_name, v)
nc.sync()
nc.close()
LOG.debug("Data transferred into NC file correctly")
示例10: main
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createVariable [as 别名]
def main(use_half_of_cols=True):
folder_path = "/home/huziy/skynet3_rech1/Global_terrain_slopes_30s"
out_filename = "slopes_30s.nc"
in_fname_pattern = "GloSlopesCl{0}_30as.asc"
nclasses = 1
params = {}
out_path = os.path.join(folder_path, out_filename)
ds = Dataset(out_path, "w")
for cl in range(1, nclasses + 1):
print("cl = {0}".format(cl))
inpath = os.path.join(folder_path, in_fname_pattern.format(cl))
if cl == 1: # generate lon/lat
params, lon2d, lat2d = _get_source_lon_lat(path=inpath)
nx, ny = lon2d.shape
ds.createDimension("x", nx)
ds.createDimension("y", ny)
lon_var = ds.createVariable("lon", "f4", ("y", "x"))
lat_var = ds.createVariable("lat", "f4", ("y", "x"))
lon_var[:] = lon2d
lat_var[:] = lat2d
data_var = ds.createVariable("class{0}".format(cl), np.uint8, ("y", "x"), chunksizes=(10000, 10000))
data_var.missing_value = params["NODATA_value"]
data_var[:] = _get_slope_data(path=inpath)
ds.close()
示例11: write2NC
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createVariable [as 别名]
def write2NC(ncfile,coords,output,nctime):
""" Writes the data to a netcdf file"""
print 'Writing to netcdf file: %s',ncfile
# Create an output netcdf file
#nc = Dataset(ncfile, 'w', format='NETCDF4_CLASSIC')
nc = Dataset(ncfile, 'w', format='NETCDF4')
# Define the dimensions
nc.createDimension('nt',0)
for vv in output.keys():
dimname = 'N'+vv
dimlength = np.size(coords['x_'+vv])
print '%s, %d' % (dimname, dimlength)
nc.createDimension(dimname,dimlength)
# Create the coordinate variables
tmpvar=nc.createVariable('Time','f8',('nt',))
# Write the data
tmpvar[:] = nctime
tmpvar.long_name = 'time'
tmpvar.units = 'seconds since 1990-01-01 00:00:00'
for vv in output.keys():
dimname = 'N'+vv
varx = 'x_'+vv
vary = 'y_'+vv
varz = 'z_'+vv
#print dimname, varx, coords[varx]
tmpvarx=nc.createVariable(varx,'f8',(dimname,))
tmpvary=nc.createVariable(vary,'f8',(dimname,))
tmpvarz=nc.createVariable(varz,'f8',(dimname,))
tmpvarx[:] = coords[varx]
tmpvary[:] = coords[vary]
tmpvarz[:] = coords[varz]
# Create the attributes
tmpvarx.setncattr('long_name','Longitude at '+vv)
tmpvarx.setncattr('units','degrees_north')
tmpvary.setncattr('long_name','Latitude at '+vv)
tmpvary.setncattr('units','degrees_east')
tmpvarz.setncattr('long_name','Elevation at '+vv)
tmpvarz.setncattr('units','m')
# Create the main variables
for vv in output.keys():
dimname = 'N'+vv
varx = 'x_'+vv
vary = 'y_'+vv
tmpvar = nc.createVariable(vv,'f8',('nt',dimname))
# Write the data
tmpvar[:] = output[vv]['Data']
# Write the attributes
for aa in output[vv].keys():
if aa != 'Data':
tmpvar.setncattr(aa,output[vv][aa])
# Create the all important coordinate attribute
tmpvar.setncattr('coordinates',varx+','+vary)
nc.close()
print 'Done.'
return
示例12: non_compliante_mesh
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createVariable [as 别名]
def non_compliante_mesh(fname):
"""
Dummy file based on:
https://gnome.orr.noaa.gov/py_gnome_testdata/COOPSu_CREOFS.nc
"""
nc = Dataset(fname, 'w')
nc.grid_type = 'Triangular'
nc.createDimension('nbi', 4)
nc.createDimension('three', 3)
nc.createDimension('nbnd', 5443)
nc.createDimension('node', 74061)
nc.createDimension('nele', 142684)
bnd = nc.createVariable('bnd', 'i4', dimensions=('nbnd', 'nbi'))
bnd[:] = np.random.random((5443, 4))
lon = nc.createVariable('lon', 'f4', dimensions=('node'))
lon[:] = np.random.random((74061))
lat = nc.createVariable('lat', 'f4', dimensions=('node'))
lat[:] = np.random.random((74061))
nbe = nc.createVariable('nbe', 'i4', dimensions=('three', 'nele'))
nbe.order = 'ccw'
nbe[:] = np.random.random((3, 142684))
nv = nc.createVariable('nv', 'i4', dimensions=('three', 'nele'))
nv[:] = np.random.random((3, 142684))
try:
yield nc
finally:
nc.close()
os.remove(fname)
示例13: create_bdy
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createVariable [as 别名]
def create_bdy(bdyname,grdname,str):
dimy,dimx,units=setstr(str)
grid=NF(grdname,'r')
lon=grid.variables[dimx][:]
lat=grid.variables[dimy][:]
grid.close()
ff = NF(bdyname, 'w', format='NETCDF3_CLASSIC')
ff.createDimension(dimy, np.size(lat))
ff.createDimension(dimx, np.size(lon))
ff.createDimension('interface', 3);
ff.createDimension('LAYER',2);
ff.createVariable(dimy, 'd',(dimy, ) )
ff.createVariable(dimx, 'd',(dimx, ) )
ff.createVariable('interface', 'd',('interface', ) )
ff.createVariable('LAYER', 'd',('LAYER', ) )
ff.createVariable('ETA', 'd',('interface',dimy,dimx, ) )
ff.createVariable('u', 'd',('LAYER',dimy,dimx, ) )
ff.createVariable('v', 'd',('LAYER',dimy,dimx, ) )
ff.close()
# copy lon/lat from grid
ff=NF(bdyname,'a')
ff.variables[dimx][:]=lon
ff.variables[dimy][:]=lat
ff.close()
示例14: convertAllDataToNetcdf
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createVariable [as 别名]
def convertAllDataToNetcdf(self, pathToNetCDF='data/swe_ross_brown/swe.nc4'):
ds = Dataset(pathToNetCDF, mode='w', format='NETCDF4_CLASSIC')
ds.createDimension('time', len(self.all_dates))
ds.createDimension('lon', self.lons2d.shape[0])
ds.createDimension('lat', self.lons2d.shape[1])
lonVariable = ds.createVariable('longitude', 'f4', ('lon', 'lat'))
latVariable = ds.createVariable('latitude', 'f4', ('lon', 'lat'))
sweVariable = ds.createVariable('SWE', 'f4', ('time', 'lon', 'lat'))
sweVariable.units = 'mm of equivalent water'
timeVariable = ds.createVariable('time', 'i4', ('time',))
ncSinceFormat = '%Y-%m-%d %H:%M:%S'
timeVariable.units = 'hours since ' + self.getStartDate().strftime(ncSinceFormat)
lonVariable[:] = self.lons2d
latVariable[:] = self.lats2d
nDates = len(self.all_dates)
for i, theDate in enumerate(self.all_dates):
filePath = os.path.join(self.root_path, theDate.strftime(self.file_name_format) + ".txt")
data = self._readFromTxtFile(filePath)
sweVariable[i, :, :] = data[:, :]
print ' {0} / {1} '.format(i, nDates)
timeVariable[:] = date2num(self.all_dates, units=timeVariable.units)
ds.close()
示例15: construct_prtm
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createVariable [as 别名]
def construct_prtm(
gg,
x_0,
out_f,
):
shape = ( len(x_0[0][1]), len(x_0[1][1]) )
# open files
old_prtm = Dataset(gg+"prtm.nc", 'r')
new_path = out_f['simulated_inputs'] + param_string(x_0) + "/"
make_sure_path_exists(new_path)
new_prtm = Dataset(new_path+"simulated.prtm.nc", 'w', format=old_prtm.data_model)
# create dimensions
for x in old_prtm.dimensions:
new_prtm.createDimension( x, len(old_prtm.dimensions[x]) )
# create variables and write data
for x in old_prtm.variables:
new_prtm.createVariable(
x,
old_prtm.variables[x].dtype,
old_prtm.variables[x].dimensions,
fill_value = -999.0,
)
new_prtm.variables[x][:] = old_prtm.variables[x][:]
# close files
new_prtm.close()
old_prtm.close()