本文整理汇总了Python中netCDF4.Dataset.createDimension方法的典型用法代码示例。如果您正苦于以下问题:Python Dataset.createDimension方法的具体用法?Python Dataset.createDimension怎么用?Python Dataset.createDimension使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netCDF4.Dataset
的用法示例。
在下文中一共展示了Dataset.createDimension方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _generate_mask_of_domain_of_interest
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createDimension [as 别名]
def _generate_mask_of_domain_of_interest( x_indices, y_indices):
path = "mask_of_quebec_basins_on_amno_ps_grid.nc"
ds = Dataset(path, mode = "w")
the_shape = polar_stereographic.lons.shape
mask = np.zeros(the_shape, dtype=int)
mask[x_indices, y_indices] = 1
ds.createDimension("lon", the_shape[0])
ds.createDimension("lat", the_shape[1])
maskVar = ds.createVariable("mask", "b", dimensions=("lon", "lat"))
lonVar = ds.createVariable("lon", "f4", dimensions=("lon", "lat"))
latVar = ds.createVariable("lat", "f4", dimensions=("lon", "lat"))
maskVar[:] = mask[:]
lonVar[:] = polar_stereographic.lons
latVar[:] = polar_stereographic.lats
ds.close()
pass
示例2: create_netcdf
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createDimension [as 别名]
def create_netcdf(self):
#I'm going to name the output grid's
#netcdf file after the first file in the grid
#and add the total filelist as an attribute
newfilename = (self.filelist[0]).strip("'")
newfilename = newfilename.strip(".loa")
newfilename += ("_grid.nc")
if (len(self.filelist) > 8):
newfilename = "large_grid.nc"
newfile = Dataset(newfilename, mode='w', clobber=True)
newfile.createDimension('naxes0', self.naxes0)
newfile.createDimension('naxes1', self.naxes1)
newfile.createDimension('naxes2', self.naxes2)
var = newfile.createVariable('otfmap', numpy.dtype(numpy.float32), (('naxes2', 'naxes1', 'naxes0')))
var[:] = self.T
var.__setattr__('filenames', self.filelist)
var.__setattr__('xmax', self.xmax)
var.__setattr__('ymax', self.ymax)
var.__setattr__('xmin', self.xmin)
var.__setattr__('ymin', self.ymin)
newfile.close()
示例3: output_file
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createDimension [as 别名]
def output_file(fname, varname, dat, lat_variable = None, lon_variable = None):
if len(dat.shape) == 2: dat = np.reshape(dat, [1, dat.shape[0], dat.shape[1]])
if lat_variable is None: lat_variable = coor_range(-90 , 90 , dat.shape[1])
if lon_variable is None: lon_variable = coor_range(0, 360, dat.shape[2])
rootgrp = Dataset(fname, "w", format="NETCDF4")
time = rootgrp.createDimension("time", dat.shape[0])
lat = rootgrp.createDimension("lat", dat.shape[1])
lon = rootgrp.createDimension("lon", dat.shape[2])
times = rootgrp.createVariable("time","f8",("time",))
latitudes = rootgrp.createVariable("lat","f4",("lat",))
longitudes = rootgrp.createVariable("lon","f4",("lon",))
longitudes.lon_name = 'Longitude'
longitudes.axis = "X"
longitudes.standard_name = "longitude"
longitudes.units = "degrees_east"
latitudes.lon_name = 'Latitude'
latitudes.axis = "Y"
latitudes.standard_name = "latitude"
latitudes.units = "degrees_north"
dims = ("time","lat","lon",)
var = rootgrp.createVariable(varname, "f4", dims)
latitudes [:] = lat_variable
longitudes[:] = lon_variable
var[:,:,:] = dat
rootgrp.close()
return dat
示例4: save_alts_to_netcdf_file
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createDimension [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 createDimension [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: construct_prtm
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createDimension [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()
示例7: create_temp_nc
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createDimension [as 别名]
def create_temp_nc(filename):
''' Creates a temporary NetCDF file to store v between
tasks.
'''
# If .nc on end of filename assume from 'filepsplit'
if filename[-3:] == '.nc' :
tempdir = filename[:-22]+'monthly_means/temp_files/'
tempfile = filename[-22:-5]+'.temp.v.nc'
else : # Else from monthly average
tempdir = filename[:-15]+'monthly_means/temp_files/'
tempfile = filename[-15:]+'.temp.v.nc'
f = Dataset(tempdir+tempfile,'w')
# Create dimensions
z_hybrid_height = f.createDimension('z_hybrid_height',180)
latitude = f.createDimension('latitude',768)
longitude = f.createDimension('longitude',1024)
bound = f.createDimension('bound',2)
# Create u variable
u = f.createVariable('v','f4',('z_hybrid_height','latitude',
'longitude',),zlib=True)
f.close()
示例8: create_grd
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createDimension [as 别名]
def create_grd(grdname,lat,lon,str):
dimy,dimx,units=setstr(str)
ff = NF(grdname, 'w', format='NETCDF3_CLASSIC')
ff.createDimension(dimy, np.size(lat))
ff.createDimension(dimx, np.size(lon))
ff.createVariable(dimy, 'd',(dimy, ) )
tmp=ff.variables[dimy]
if str=='l':
setattr(tmp, "long_name", "Latitude")
elif str=='x':
setattr(tmp, "long_name", "y dist")
setattr(tmp, "units", units)
ff.createVariable(dimx, 'd',(dimx, ) )
tmp=ff.variables[dimx]
if str=='l':
setattr(tmp, "long_name", "Longitude")
elif str=='x':
setattr(tmp, "long_name", "x dist")
setattr(tmp, "units", units)
ff.createVariable('D', 'd',(dimy,dimx, ) )
tmp=ff.variables['D']
setattr(tmp, "long_name", "Depth")
setattr(tmp, "units", "meter")
ff.close()
示例9: write_file
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createDimension [as 别名]
def write_file(td, filename='/home/shiraiwa/test.nc',
ignore_changes=False, format = 'NETCDF3_CLASSIC'):
import numpy as np
nm = td.getvar0()
dtype2str = {np.dtype('float64'): 'f8',
np.dtype('float32'): 'f4',
np.dtype('int64'): 'i8',
np.dtype('int32'): 'i4',
np.dtype('int16'): 'i2',
np.dtype('int8'): 'i1',
np.dtype('uint64'): 'u8',
np.dtype('uint32'): 'u4',
np.dtype('uint16'): 'u2',
np.dtype('uint8'): 'u1',
np.dtype('S1'): 'S1', }
rootgrp = Dataset(filename, 'w', format=format)
for key in nm.var:
setattr(rootgrp, key, nm.var[key])
for key in nm['dimensions'].var:
rootgrp.createDimension(key, nm['dimensions'].var[key])
for key in nm["variables"]:
dim = nm["variables"][key].var['dimensions']
datatype = dtype2str[nm["variables"][key].var['dtype']]
variable = rootgrp.createVariable(key,datatype,dim)
for attr in nm["variables"][key].var:
if not attr in ["dimensions", "dtype", "ndim", "shape"]:
setattr(variable, attr, nm["variables"][key].var[attr])
if ignore_changes or not nm["variables"][key]._data_loaded:
variable[:] = nm["variables"][key].nc_eval(td)
else:
variable[:] = nm["variables"][key]._data
rootgrp.close()
print('write NC file completed. ' + filename)
示例10: test_variable_units
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createDimension [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)
示例11: convertAllDataToNetcdf
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createDimension [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()
示例12: test_variable_attributes
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createDimension [as 别名]
def test_variable_attributes(self):
'''
Tests that the platform variable attributes check is working
'''
# 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('platform', 'S1', ())
platform = nc_obj.variables['platform']
results = self.ioos.check_variable_attributes(nc_obj)
for result in results:
self.assert_result_is_bad(result)
platform.long_name = 'platform'
platform.short_name = 'platform'
platform.source = 'glider'
platform.ioos_name = 'urn:ioos:station:glos:leorgn'
platform.wmo_id = '1234'
platform.comment = 'test'
results = self.ioos.check_variable_attributes(nc_obj)
for result in results:
self.assert_result_is_good(result)
示例13: setUp
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createDimension [as 别名]
def setUp(self):
self.file = file_name
f = Dataset(file_name,'w')
f.createDimension('x',None)
f.createDimension('y',ydim)
f.createDimension('z',zdim)
v = f.createVariable('data','i2',('x','y','z'))
v[:] = data
v1 = f.createVariable('data1','i2','x')
self.data1 = data1
self.data = data
# test __setitem___
v[0:xdim] = self.data
# integer array slice.
v[:,i,:] = -100
self.data[:,i,:] = -100
# boolen array slice.
v[ib2] = -200
self.data[ib2] = -200
v[ib3,:,:] = -300
self.data[ib3,:,:] = -300
# same as above, for 1d array
v1[0:xdim] = self.data1
v1[i] = -100
self.data1[i] = -100
v1[ib2] = -200
self.data1[ib2] = -200
v1[ib3] = -300
self.data1[ib3] = -300
f.close()
示例14: writeCDF
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createDimension [as 别名]
def writeCDF():
## fidOut = Dataset( 'aquaQuartBigEurope.nc', 'w', format = \
fidOut = Dataset( 'terraQuartBigEurope.nc', 'w', format = \
'NETCDF3_CLASSIC')
fidOut.createDimension( 'y', p.nYOut )
fidOut.createDimension( 'x', p.nXOut )
fidOut.createDimension( 't', p.nTimesIn )
##
fidOut.createVariable( 'lst', np.float32, ( 't', 'y', 'x' ) )
fidOut.variables[ 'lst'][:] = outputGrid
fidOut.createVariable( 'n', np.float32, ( 't', 'y', 'x' ) )
fidOut.variables[ 'n'][:] = outputCount
##
fidOut.createVariable( 'lstF', np.float32, ( 't', 'y', 'x' ) )
fidOut.variables[ 'lstF'][:] = forestGrid
fidOut.createVariable( 'nF', np.float32, ( 't', 'y', 'x' ) )
fidOut.variables[ 'nF'][:] = forestCount
##
fidOut.createVariable( 'lstU', np.float32, ( 't', 'y', 'x' ) )
fidOut.variables[ 'lstU'][:] = urbanGrid
fidOut.createVariable( 'nU', np.float32, ( 't', 'y', 'x' ) )
fidOut.variables[ 'nU'][:] = urbanCount
##
fidOut.createVariable( 'lstC', np.float32, ( 't', 'y', 'x' ) )
fidOut.variables[ 'lstC'][:] = cropGrid
fidOut.createVariable( 'nC', np.float32, ( 't', 'y', 'x' ) )
fidOut.variables[ 'nC'][:] = cropCount
##
fidOut.createVariable( 'lstO', np.float32, ( 't', 'y', 'x' ) )
fidOut.variables[ 'lstO'][:] = otherGrid
fidOut.createVariable( 'nO', np.float32, ( 't', 'y', 'x' ) )
fidOut.variables[ 'nO'][:] = otherCount
##
fidOut.close()
示例15: construct_loc
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createDimension [as 别名]
def construct_loc(
gg,
x_0,
out_f,
):
shape = ( len(x_0[0][1]), len(x_0[1][1]) )
# open files
old_loc = Dataset(gg+"loc.nc", 'r')
new_path = out_f['simulated_inputs'] + param_string(x_0) + "/"
make_sure_path_exists(new_path)
new_loc = Dataset(new_path+"simulated.loc.nc", 'w', format=old_loc.data_model)
# create dimensions
new_loc.createDimension("nx_loc", shape[0])
new_loc.createDimension("ny_loc", shape[1])
# create variables
lat = new_loc.createVariable( "lat", "f4", ("ny_loc","nx_loc"), fill_value=-999.0 )
lon = new_loc.createVariable( "lon", "f4", ("ny_loc","nx_loc"), fill_value=-999.0 )
# write data
lat[:] = np.tile( old_loc.variables["lat"][:], shape )
lon[:] = np.tile( old_loc.variables["lon"][:], shape )
# close files
new_loc.close()
old_loc.close()