本文整理汇总了Python中netCDF4.Dataset.createGroup方法的典型用法代码示例。如果您正苦于以下问题:Python Dataset.createGroup方法的具体用法?Python Dataset.createGroup怎么用?Python Dataset.createGroup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netCDF4.Dataset
的用法示例。
在下文中一共展示了Dataset.createGroup方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createGroup [as 别名]
def setUp(self):
self.testfile = tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name
f = Dataset(self.testfile, 'w')
grp1 = f.createGroup('Group1')
grp2 = f.createGroup('Group2')
f.createGroup('Group3') # empty group
f.createVariable('var0', "i2", ())
grp1.createVariable('var1', 'f8', ())
grp2.createVariable('var2', 'f4', ())
f.close()
示例2: setUp
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createGroup [as 别名]
def setUp(self):
self.testfile = tempfile.mktemp(".nc")
f = Dataset(self.testfile, 'w')
grp1 = f.createGroup('Group1')
grp2 = f.createGroup('Group2')
f.createGroup('Group3') # empty group
f.createVariable('var0', "i2", ())
grp1.createVariable('var1', 'f8', ())
grp2.createVariable('var2', 'f4', ())
f.close()
示例3: save
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createGroup [as 别名]
def save(self, out_file):
"""
Use netCDF4 format for data persistency
"""
from netCDF4 import Dataset
root = Dataset(out_file, 'w')
#------ event
event = root.createGroup("/event")
#------ station
stations = root.createGroup("/station")
for station in self.stations:
station.
示例4: makeEmptyNetcdf
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createGroup [as 别名]
def makeEmptyNetcdf(filename, vars, obsTypes, obsLimits):
## make daily empty netcdf file for vars
## include metadata of obsTypes and obsLimits
netcdf = Dataset(filename, 'w', format='NETCDF4')
for epoch, var in vars:
## add group for epoch if it's not already in the netcdf file
try:
grp = netcdf.groups[groupName(epoch)]
except:
grp = netcdf.createGroup(groupName(epoch))
## time dimension: allocate space in file based on sampling interval
dim = grp.createDimension('time', 24*60*60/epoch)
grp.createVariable('dates', 'f8', ('time',))
for v in var:
varID = grp.createVariable(v, 'f8', ('time',))
### attributes from metadata
u = vars.getUnits(v)
if u:
varID.units = u
if v in obsTypes:
obsName = obsTypes[v]
writeObsAttributes(varID, obsName, obsLimits[obsName])
elif vars.getOrig(v):
if vars.getOrig(v) in obsTypes:
obsName = obsTypes[vars.getOrig(v)]
writeObsAttributes(varID, obsName, obsLimits[obsName])
netcdf.close()
示例5: new
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createGroup [as 别名]
def new(self):
# create new file
rootgrp = Dataset(self.filename, 'w') # create new file
# create standard groups
fcstgrp = rootgrp.createGroup('forecasts')
obsgrp = rootgrp.createGroup('observations')
thmgrp = rootgrp.createGroup('themes')
# add group attributes
fcstgrp.long_name = 'weather_forecast'
obsgrp.long_name = 'interpolated_observations'
thmgrp.long_name = 'senorge_theme_layers'
self.rootgrp = rootgrp
self.fcstgrp = fcstgrp
self.obsgrp = obsgrp
self.thmgrp = thmgrp
示例6: local_min
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createGroup [as 别名]
def local_min(nc_input_file,nc_output_file,nc_comp,pres_level_list,num_procs):
#LOAD THE DATA:
data_root = Dataset(nc_input_file,'r')
#CREATE THE OUTPUT FILE
output_root = Dataset(nc_output_file,'w',format='NETCDF4')
output_root = netcdf_utils.replicate_netcdf_file(output_root,data_root)
#ADD DISTRIBUTION WEIGHT
#var_list = data.variables.keys()
var_list = data_root.groups.keys()
#DETERMINE WHICH VARIABLES TO DISTRIBUTE
for var in var_list:
output=output_root.createGroup(var+'_mask')
data=data_root.groups[var]
output = netcdf_utils.replicate_netcdf_file(output,data)
#CREATE OUTPUT VAR:
#FIND WHICH DIMENSIONS TO INCLUDE
var_dims = list(data.variables[var].dimensions)
if set(var_dims).issuperset({'lon','lat'}):
print(var)
output=netcdf_utils.replicate_netcdf_var_dimensions(output,data,var)
#CREATE OUTPUT VARIABLE
final_mask = output.createVariable(var+'_mask','f',tuple(var_dims),zlib=nc_comp)
#c_to_float=np.vectorize(np.float)
#slp = c_to_float(data.variables[var][:])
slp = data.variables[var][:]
#Add a random machine-precision perturbation:
slp*=(1.0+np.random.normal(size=slp.shape)*1e-10)
lat = data.variables['lat'][:]
lon_ind=var_dims.index('lon')
lat_ind=var_dims.index('lat')
#COMPUTE MINIMA MASK:
#Allow an asynchronous implementation:
print('Using {0} processors'.format(num_procs))
pool=mproc.Pool(processes=int(num_procs))
time_split=slp.shape[0]
#final_mask[:]=np.concatenate(pool.map(local_min_one_dt,
temp=np.concatenate(pool.map(local_min_one_dt,
zip(np.vsplit(slp,time_split),
[pres_level_list for x in range(time_split)],
[(2*np.pi/slp.shape[lon_ind])*(np.pi/slp.shape[lat_ind]) for x in range(time_split)]),
chunksize=1),axis=0)
final_mask[:]=np.where(temp>0.0,1,0)
pool.close()
output.sync()
data_root.close()
output_root.close()
示例7: setUp
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createGroup [as 别名]
def setUp(self):
self.file = FILE_NAME
f = Dataset(self.file, 'w')
d = f.createDimension(DIM_NAME,None)
g = f.createGroup(GROUP_NAME)
wind_vector_type = f.createCompoundType(dtype, TYPE_NAME)
wind_vectorunits_type = f.createCompoundType(dtypec, TYPE_NAMEC)
v = f.createVariable(VAR_NAME,wind_vector_type, DIM_NAME)
vv = g.createVariable(VAR_NAME2,wind_vector_type,DIM_NAME)
v.missing_values = missvals
v.units = windunits
vv.missing_values = missvals
vv.units = windunits
f.close()
示例8: setUp
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createGroup [as 别名]
def setUp(self):
self.file = FILE_NAME
f = Dataset(self.file, 'w')
d = f.createDimension(DIM_NAME,DIM_SIZE)
g = f.createGroup(GROUP_NAME)
# simple compound types.
cmptype1 = f.createCompoundType(dtype1, TYPE_NAME1)
cmptype2 = f.createCompoundType(dtype2, TYPE_NAME2)
# close and reopen the file to make sure compound
# type info read back in correctly.
f.close()
f = Dataset(self.file,'r+')
g = f.groups[GROUP_NAME]
# multiply nested compound types
cmptype3 = f.createCompoundType(dtype3, TYPE_NAME3)
cmptype4 = f.createCompoundType(dtype4, TYPE_NAME4)
cmptype5 = f.createCompoundType(dtype5, TYPE_NAME5)
v = f.createVariable(VAR_NAME,cmptype4, DIM_NAME)
vv = g.createVariable(VAR_NAME,cmptype5, DIM_NAME)
v[:] = data
vv[:] = datag
f.close()
示例9: setUp
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createGroup [as 别名]
def setUp(self):
self.file = FILE_NAME
f = Dataset(self.file, 'w')
d = f.createDimension(DIM_NAME,DIM_SIZE)
g = f.createGroup(GROUP_NAME)
# simple compound types.
cmptype1 = f.createCompoundType(dtype1, TYPE_NAME1)
cmptype2 = f.createCompoundType(dtype2, TYPE_NAME2)
# close and reopen the file to make sure compound
# type info read back in correctly.
f.close()
f = Dataset(self.file,'r+')
g = f.groups[GROUP_NAME]
# multiply nested compound types
cmptype3 = f.createCompoundType(dtype3, TYPE_NAME3)
cmptype4 = f.createCompoundType(dtype4, TYPE_NAME4)
cmptype5 = f.createCompoundType(dtype5, TYPE_NAME5)
v = f.createVariable(VAR_NAME,cmptype4, DIM_NAME)
vv = g.createVariable(VAR_NAME,cmptype5, DIM_NAME)
v[:] = data
vv[:] = datag
# try reading the data back before the file is closed
dataout = v[:]
dataoutg = vv[:]
assert (cmptype4 == dtype4a) # data type should be aligned
assert (dataout.dtype == dtype4a) # data type should be aligned
assert(list(f.cmptypes.keys()) ==\
[TYPE_NAME1,TYPE_NAME2,TYPE_NAME3,TYPE_NAME4,TYPE_NAME5])
assert_array_equal(dataout['xxx']['xx']['i'],data['xxx']['xx']['i'])
assert_array_equal(dataout['xxx']['xx']['j'],data['xxx']['xx']['j'])
assert_array_almost_equal(dataout['xxx']['yy']['x'],data['xxx']['yy']['x'])
assert_array_almost_equal(dataout['xxx']['yy']['y'],data['xxx']['yy']['y'])
assert_array_almost_equal(dataout['yyy'],data['yyy'])
assert_array_equal(dataoutg['x1']['i'],datag['x1']['i'])
assert_array_equal(dataoutg['x1']['j'],datag['x1']['j'])
assert_array_almost_equal(dataoutg['y1']['x'],datag['y1']['x'])
assert_array_almost_equal(dataoutg['y1']['y'],datag['y1']['y'])
f.close()
示例10: main
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createGroup [as 别名]
def main(fileName):
'''
The main function, reading the data and exporting netCDF file
'''
newData = Dataset("WavelengthExp.nc","w",format="NETCDF4")
dimensionBand, dimensionX, dimensionY = getDimension(fileName)
wavelength, hdrInfo = getWavelength(fileName), getHeaderInfo(fileName)
newData.createDimension('band', dimensionBand)
newData.createDimension('x', dimensionX)
newData.createDimension('y', dimensionY)
newData.createDimension('wavelength', len(wavelength))
mainDataHandler, tempVariable = open('/Users/jeromemao/Desktop/terraref/data'),\
newData.createVariable('exposure_2','f8',('band', 'x', 'y'))#('band', 'x', 'y')
fileSize = os.path.getsize(fileName)
dataNumber, dataType, dataSize = fileSize/DATATYPE[hdrInfo['data type']][-1], DATATYPE[hdrInfo['data type']][0],\
DATATYPE[hdrInfo['data type']][-1]
with TimeMeasurement("unpacking") as lineTiming: #measuring the time
value = struct.unpack(dataType*dataNumber,mainDataHandler.read(dataSize*dataNumber))#reading the data from the file
with TimeMeasurement("assigning value") as lineTiming:
tempVariable[:,:,:] = value #TODO need a better method to assign value to avoid "de-interleaving"
nestedWavelength = newData.createVariable('wavelength', 'f8',('wavelength',))
nestedWavelength[:] = wavelength
headerInfo = newData.createGroup("HeaderInfo")
for members in hdrInfo:
setattr(headerInfo,members,hdrInfo[members])
if isDigit(hdrInfo[members]):
tempVariable = headerInfo.createVariable(members,'i4')
tempVariable.assignValue(int(hdrInfo[members]))
newData.close()
示例11: len
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createGroup [as 别名]
root_grp_period.createDimension('diurnal', 24)
if timeres == 'H':
root_grp_period.createDimension('seasonal', 8766)
elif timeres == 'D':
root_grp_period.createDimension('seasonal',365)
elif timeres == 'M':
root_grp_period.createDimension('seasonal',12)
root_grp_period.createDimension('all', len(full_waveform[0]))
for i in range(len(obs_refs)):
# dimensions
site_ref = obs_refs[i]
ref_period = root_grp_period.createGroup('%s'%(site_ref.lower()))
ref_period.daily_harmonic3_amplitude = daily_h3_mag_array[i]
ref_period.daily_harmonic2_amplitude = daily_h2_mag_array[i]
ref_period.daily_harmonic1_amplitude = daily_h1_mag_array[i]
ref_period.original_daily_amplitude = orig_daily_mag_array[i]
ref_period.daily_amplitude = daily_mag_array[i]
ref_period.seasonal_harmonic3_amplitude = seasonal_h3_mag_array[i]
ref_period.seasonal_harmonic2_amplitude = seasonal_h2_mag_array[i]
ref_period.seasonal_harmonic1_amplitude = seasonal_h1_mag_array[i]
ref_period.annual_amplitude = annual_mag_array[i]
ref_period.seasonal_amplitude = seasonal_mag_array[i]
ref_period.daily_harmonic3_phase = daily_h3_phase_array[i]
ref_period.daily_harmonic2_phase = daily_h2_phase_array[i]
ref_period.daily_harmonic1_phase = daily_h1_phase_array[i]
ref_period.original_daily_phase = orig_daily_phase_array[i]
ref_period.daily_phase = daily_phase_max_array[i]
示例12: Dataset
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createGroup [as 别名]
'''
Slow but neater formatting for netcdf: seperates results into columns.
D. Ellis
'''
import numpy as np
import netCDF4,sys
from netCDF4 import Dataset
group_name = sys.argv[1]
nc = Dataset( group_name+'.nc' , mode='r')
ncf = Dataset('del.'+group_name,'w')
group = ncf.createGroup('del')
spec = group.createGroup('Spec')
time = ncf.createDimension('time', None)
rate = group.createGroup('Rate')
specs= [''.join(x).strip(' ') for x in nc.variables['species'][:]]
for i,s in enumerate(specs):
var = spec.createVariable( s , "f8" ,('time',))
var[:] = np.array(nc.variables['Spec'][i])
if (i%100 == 0):
print '%3d'%(float(i)/len(specs)*100), '% species from', group_name , 'saved'
reactions = [''.join(x).strip(' ') for x in nc.variables['reactions'][:]]
for i,r in enumerate(reactions):
var = rate.createVariable( r , "f8" ,('time',))
var[:] = np.array(nc.variables['Rate'][i])
示例13:
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createGroup [as 别名]
#Remove sites above 1000m from sea level
if np.float64(alt) >= 1000:
valid_write = False
print 'Site is over 1000m from sea level.'
if valid_write == True:
country = ''
while country == '':
try:
country = modules.get_country(np.float64(lat),np.float64(lon))
except:
pass
#save out netcdf file
net_ref = root_grp.createGroup('%s'%(ref.lower()))
#set variables
dates = net_ref.createVariable('date', 'i8', ('date',))
times = net_ref.createVariable('time', 'i8', ('time',))
spec = net_ref.createVariable(species.lower(), 'f8', ('species',))
#set group attributes
net_ref.latitude = np.float64(lat)
net_ref.longitude = np.float64(lon)
net_ref.altitude = np.float64(alt)
net_ref.process_group = 'CHILE'
net_ref.country = country
net_ref.data_completeness = data_complete
net_ref.anthrome_site_class = anth_class_name
net_ref.raw_site_class = raw_class
示例14: Dataset
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createGroup [as 别名]
from netCDF4 import Dataset
import numpy
rootgrp = Dataset('test.nc','w')
### Create some groups
group1 = rootgrp.createGroup('g1')
group2 = rootgrp.createGroup('g2')
group3 = rootgrp.createGroup('really_long group_name')
# a nested group
group2_1 = group2.createGroup('g2_1')
### Create some dimensions
# unlimited
dim1 = rootgrp.createDimension('bad_name', None)
dim2 = rootgrp.createDimension('good_name', None)
# limited
dim3 = rootgrp.createDimension('[email protected]#$%^&*()_-+""{}', 3)
dim4 = rootgrp.createDimension('dim4', 4)
# in other groups
g1_dim1 = group1.createDimension('same_dim', 1)
g1_dim2 = group1.createDimension('diff_dim', None)
# Should be allowed to use the same name in a different part of the tree?
g2_1_dim1 = group2_1.createDimension('same_dim', 3)
示例15: writePointData2Netcdf
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import createGroup [as 别名]
def writePointData2Netcdf(ncfile,data,globalatts):
"""
Function for writing point/observation data to a grouped netcdf file.
Each variable is written to a separate group to allow for variable time and/or
spatial coordinates.
Inputs:
ncfile - name of the output netcdf file (string)
data - list of dictionaries with netcdf data and attribute info
(see noaatools.py --> extractIOOS for an example of this array)
globalatts - dictionary with global attributes
"""
# Convert the dictionary array to a grouped netcdf file
print '################################################'
print ' Writing to file: %s...' % ncfile
nc = Dataset(ncfile, 'w', format='NETCDF4')
# Write the global attributes
for gg in globalatts.keys():
nc.setncattr(gg,globalatts[gg])
# Each listing in the dictionary is treated as a separate group
ctr=-1
for dd in data:
# Each variable in the group
for vv in dd:
# Create a group
ctr = ctr + 1
groupID = 'groupID_%04d' % ctr
grp = nc.createGroup(groupID)
# Work out the coordinates and create the dimensions
for cc in dd[vv]['coords']:
dimname = cc['Name']
dimlength = np.size(cc['Value'])
grp.createDimension(dimname,dimlength)
print dimname, dimlength
# Create the coordinate variables
tmpvar=grp.createVariable(cc['Name'],'f8',(dimname,))
tmpvar[:] = cc['Value']
# Create the attributes
for aa in cc.keys():
if aa !='Name' and aa !='Value':
tmpvar.setncattr(aa,cc[aa])
# Now create the varible and attribute data
# The dimension info is stored in the coordinates attribute
coordList = [str(x) for x in dd[vv]['coordinates'].split(', ')]
tmpvar = grp.createVariable(vv,'f8',(coordList))
# Write the data
print vv, np.size(dd[vv]['Data']), coordList
tmpvar[:] = dd[vv]['Data']
# Write the attriute data
for aa in dd[vv].keys():
if aa !='Data' and aa !='coords':
tmpvar.setncattr(aa,dd[vv][aa])
nc.close()
print 'Completed writing file.'
print '################################################'
return