本文整理汇总了Python中netCDF4.Dataset.renameVariable方法的典型用法代码示例。如果您正苦于以下问题:Python Dataset.renameVariable方法的具体用法?Python Dataset.renameVariable怎么用?Python Dataset.renameVariable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netCDF4.Dataset
的用法示例。
在下文中一共展示了Dataset.renameVariable方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_scalar_vars_ismip6_conforming
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import renameVariable [as 别名]
def make_scalar_vars_ismip6_conforming(filename, ismip6_vars_dict):
'''
Make file ISMIP6 conforming
'''
# Open file
nc = CDF(filename, 'a')
pism_to_ismip6_dict = dict((v.pism_name, k) for k, v in ismip6_vars_dict.iteritems())
for pism_var in nc.variables:
nc_var = nc.variables[pism_var]
if pism_var in pism_to_ismip6_dict.keys():
ismip6_var = pism_to_ismip6_dict[pism_var]
print('Processing {} / {}'.format(pism_var, ismip6_var))
if not pism_var == ismip6_var:
print(' Renaming {pism_var} to {ismip6_var}'.format(pism_var=pism_var, ismip6_var=ismip6_var))
nc.renameVariable(pism_var, ismip6_var)
nc.sync()
if not nc_var.units == ismip6_vars_dict[ismip6_var].units:
o_units = ismip6_vars_dict[ismip6_var].units
i_units = nc_var.units
print(' Converting {pism_var} from {i_units} to {o_units}'.format(pism_var=pism_var, i_units=i_units, o_units=o_units))
i_f = cf_units.Unit(i_units)
o_f = cf_units.Unit(o_units)
nc_var[:] = i_f.convert(nc_var[:], o_f)
nc_var.units = o_units
nc_var.standard_name = ismip6_vars_dict[ismip6_var].standard_name
nc.close()
示例2: write_to_file
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import renameVariable [as 别名]
def write_to_file(nc_indice, data):
from netCDF4 import Dataset
from shutil import copy
from os.path import split, join
from flyingpigeon.utils import get_variable
from flyingpigeon.metadata import get_frequency
#path, nc_indice = split(indice_file)
var = get_variable(nc_indice)
fq = get_frequency(nc_indice)
agg = nc_indice.split('_')[-2]
nc = nc_indice.replace(var,'tree').replace(agg,fq)
copy(nc_indice, nc)
ds = Dataset(nc, mode= 'a')
vals = ds.variables[var]
ds.renameVariable(var,'tree')
vals[:,:,:] = data[:,:,:]
vals.long_name = 'Favourabilliy for tree species'
vals.standard_name = 'tree'
vals.units = '0-1'
ds.close()
return nc
示例3: modify_faimms_netcdf
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import renameVariable [as 别名]
def modify_faimms_netcdf(netcdf_file_path, channel_id_info):
""" Modify the downloaded netCDF file so it passes both CF and IMOS checker
input:
netcdf_file_path(str) : path of netcdf file to modify
channel_id_index(tupple) : information from xml for the channel
"""
modify_aims_netcdf(netcdf_file_path, channel_id_info)
netcdf_file_obj = Dataset(netcdf_file_path, 'a', format='NETCDF4')
netcdf_file_obj.aims_channel_id = int(channel_id_info['channel_id'])
if not (channel_id_info['metadata_uuid'] == 'Not Available'):
netcdf_file_obj.metadata_uuid = channel_id_info['metadata_uuid']
# some weather stations channels don't have a depth variable if sensor above water
if 'depth' in netcdf_file_obj.variables.keys():
var = netcdf_file_obj.variables['depth']
var.long_name = 'nominal depth'
var.positive = 'down'
var.axis = 'Z'
var.reference_datum = 'sea surface'
var.valid_min = -10.0
var.valid_max = 30.0
netcdf_file_obj.renameVariable('depth', 'NOMINAL_DEPTH')
if 'DEPTH' in netcdf_file_obj.variables.keys():
var = netcdf_file_obj.variables['DEPTH']
var.coordinates = "TIME LATITUDE LONGITUDE NOMINAL_DEPTH"
var.long_name = 'actual depth'
var.reference_datum = 'sea surface'
var.positive = 'down'
var.valid_min = -10.0
var.valid_max = 30.0
netcdf_file_obj.close()
netcdf_file_obj = Dataset(netcdf_file_path, 'a', format='NETCDF4') # need to close to save to file. as we call get_main_faimms_var just after
main_var = get_main_faimms_var(netcdf_file_path)
# DEPTH, LATITUDE and LONGITUDE are not dimensions, so we make them into auxiliary cooordinate variables by adding this attribute
if 'NOMINAL_DEPTH' in netcdf_file_obj.variables.keys():
netcdf_file_obj.variables[main_var].coordinates = "TIME LATITUDE LONGITUDE NOMINAL_DEPTH"
else:
netcdf_file_obj.variables[main_var].coordinates = "TIME LATITUDE LONGITUDE"
netcdf_file_obj.close()
if not convert_time_cf_to_imos(netcdf_file_path):
return False
remove_dimension_from_netcdf(netcdf_file_path) # last modification to do in this order!
return True
示例4: set_attributes
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import renameVariable [as 别名]
def set_attributes(resource, variable):
from netCDF4 import Dataset
if variable == 'tg':
new_variable = 'tas'
elif variable == 'tn':
new_variable = 'tasmin'
elif variable == 'tx':
new_variable = 'tasmax'
elif variable == 'rr':
new_variable = 'pr'
try :
ds = Dataset(resource, 'a')
# ds.renameDimension('Actual_latitude', 'latitude' )
# ds.renameDimension('Actual_longitude', 'longitude' )
ds.renameVariable(variable, new_variable)
#ds.setncatts(att_dict)
ds.close()
except Exception as e:
logger.error('could not set attributes in resouce %s', resource)
return resource
示例5: rename_variable
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import renameVariable [as 别名]
def rename_variable(resource, oldname=None, newname="newname"):
"""
Change the variable name of a netCDF variable
:param resource: path to netCDF input file
:param oldname: variable name to be changed
:param newname: variable name to be given
:retunrs str: path to resource
"""
try:
if oldname == None:
oldname = get_variable(resource)
if oldname != newname:
from netCDF4 import Dataset
ds = Dataset(resource, mode="a")
ds.renameVariable(oldname, newname)
ds.close()
logger.info("varname %s in netCDF renamed to %s" % (oldname, newname))
except Exception as e:
msg = "failed to rename variable in target files %s " % e
logger.debug(msg)
raise Exception(msg)
示例6: modify_soop_trv_netcdf
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import renameVariable [as 别名]
def modify_soop_trv_netcdf(netcdf_file_path, channel_id_info):
"""
Modify the downloaded NetCDF file so it passes both CF and IMOS checker
input:
netcdfFile_path(str) : path of netcdf file to modify
channel_id_index(tupple) : information from xml for the channel
"""
logger = logging_aims()
modify_aims_netcdf(netcdf_file_path, channel_id_info)
netcdf_file_obj = Dataset(netcdf_file_path, 'a', format='NETCDF4')
ship_code = netcdf_file_obj.platform_code
vessel_name = ship_callsign(ship_code)
if vessel_name is None:
logger.error(' UNKNOWN SHIP - channel %s' % str(channel_id_info['channel_id']))
netcdf_file_obj.close()
return False
# add gatts to net_cDF
netcdf_file_obj.cdm_data_type = 'Trajectory'
netcdf_file_obj.vessel_name = vessel_name
netcdf_file_obj.trip_id = channel_id_info['trip_id']
netcdf_file_obj.cdm_data_type = "Trajectory"
coordinates_att = "TIME LATITUDE LONGITUDE DEPTH"
# depth
depth = netcdf_file_obj.variables['depth']
depth.positive = 'down'
depth.axis = 'Z'
depth.reference_datum = 'sea surface'
depth.valid_max = 30.0
depth.valid_min = -10.0
netcdf_file_obj.renameVariable('depth', 'DEPTH')
# latitude longitude
latitude = netcdf_file_obj.variables['LATITUDE']
latitude.ancillary_variables = 'LATITUDE_quality_control'
longitude = netcdf_file_obj.variables['LONGITUDE']
longitude.ancillary_variables = 'LONGITUDE_quality_control'
latitude_qc = netcdf_file_obj.variables['LATITUDE_quality_control']
latitude_qc.long_name = 'LATITUDE quality control'
latitude_qc.standard_name = 'latitude status_flag'
longitude_qc = netcdf_file_obj.variables['LONGITUDE_quality_control']
longitude_qc.long_name = 'LONGITUDE quality control'
longitude_qc.standard_name = 'longitude status_flag'
netcdf_file_obj.close()
netcdf_file_obj = Dataset(netcdf_file_path, 'a', format='NETCDF4')
main_var = get_main_soop_trv_var(netcdf_file_path)
netcdf_file_obj.variables[main_var].coordinates = coordinates_att
netcdf_file_obj.close()
if not convert_time_cf_to_imos(netcdf_file_path):
return False
remove_dimension_from_netcdf(netcdf_file_path) # last modification to do !
return True
示例7: Dataset
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import renameVariable [as 别名]
"""Splits the time dimension into an reftime and a leadtime
so that multiple files can be concatenated more easily"""
import sys
from netCDF4 import Dataset, num2date, date2num
for f in sys.argv[1:]:
dataset = Dataset(f, 'a')
# rename record dimension to reftime
dataset.renameDimension('record', 'reftime')
# rename time dimension to leadtime
dataset.renameDimension('time', 'leadtime')
dataset.renameVariable('time', 'leadtime')
time = dataset.variables['leadtime']
reftime = dataset.createVariable('reftime', 'f8', ('reftime',))
reftime.units = time.units
reftime.calendar = time.calendar
reftime[0] = time[0]
reftime.standard_name = "forecast_reference_time"
reftime.long_name = "Time of model initialization"
dt = num2date(time[:], units=time.units, calendar=time.calendar)
lt = date2num(dt, units="hours since %s" % dt[0], calendar=time.calendar)
# use the existing time variable to hold lead time information
time.units = "hours"
示例8: Netcdf4Raster
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import renameVariable [as 别名]
class Netcdf4Raster(Netcdf4Envelope2D):
# Constants
DATAFILEXT = 'nc4';
_original_name = "yield_mai"
# Data attributes - assign some dummy values for the mean time
name = "dummy.nc4";
folder = os.getcwd();
_dataset = None;
_varname = "";
_currow = 0;
cellsize = 1;
nodatavalue = -9999.0;
def __init__(self, filepath):
# Retrieve the name from the filepath and assign - incl. extension
self.name = os.path.basename(filepath);
# Also derive the folder
self.folder = os.path.dirname(filepath);
def open(self, mode, ncols=1, nrows=1, xll=0, yll=0, cellsize=1, nodatavalue=-9999.0):
# If file does not exist and mode[0] = 'w', create it!
fpath = os.path.join(self.folder, self.name);
if mode[0] == 'a':
if os.path.exists(fpath):
print "About to open file " + fpath + " in append mode";
self._dataset = Dataset(fpath, 'a', format='NETCDF4');
Netcdf4Envelope2D.__init__(self, self._dataset);
return True;
else:
if os.path.exists(fpath):
# Open the netCDF4 file
ds = Dataset(fpath, 'r');
Netcdf4Envelope2D.__init__(self, ds);
self._dataset = ds;
self._varname = self._get_varname();
return True;
else: return False;
def _get_varname(self):
# Establish which variable is stored in it - assume it's only 1!
ds = self._dataset;
diff = set(ds.variables) - set(ds.dimensions);
if len(diff) > 0:
return diff.pop();
else:
return "";
def readheader(self):
pass;
def __iter__(self):
return self;
def next(self, parseLine=True):
# The netCDF4 format is not one that is read in a sequential manner. Is it a good idea
# that this method is part of the interface?
result = None;
if self._varname != "":
if parseLine:
# Indexing: 1. years 2. by rows 3. columns
result = self.getVariables(self._varname)[:, self._currow, :];
self._currow += 1; # row index is zero-based!
return result;
@staticmethod
def getDataFileExt(self):
return self.DATAFILEXT;
def writeheader(self, name, long_name, units):
v = self._dataset.variables[self._original_name]
v.setncattr("long_name", long_name)
v.setncattr("units", units)
if self._original_name != name:
self._dataset.renameVariable(self._original_name, name)
self._varname = name;
def writenext(self, sequence_with_data):
# Assume that the sequence is indexed 1. by year and 2. by column
key = self._varname;
if not hasattr(sequence_with_data, '__iter__'):
raise ValueError("Input value not an array")
if len(sequence_with_data.shape) != 2:
raise ValueError("Input array has unexpected shape")
if sequence_with_data.shape[1] != self.ncols:
raise ValueError("Input array has unexpected dimension")
self._dataset.variables[key][:, self._currow, :] = sequence_with_data
self._currow += 1;
def close(self):
if self._dataset:
self._dataset.close();
def reset(self):
self._currow = 0;
def getEnvelope(self):
# TODO: this cannot be solved by invoking super or so?
return GridEnvelope2D(self.ncols, self.nrows, self.xll, self.yll, self.dx, self.dy);
#.........这里部分代码省略.........
示例9: modify_aims_netcdf
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import renameVariable [as 别名]
def modify_aims_netcdf(netcdf_file_path, channel_id_info):
""" Modify the downloaded netCDF file so it passes both CF and IMOS checker
input:
netcdf_file_path(str) : path of netcdf file to modify
channel_id_index(dict) : information from xml for the channel
"""
imos_env_path = os.path.join(os.environ.get('DATA_SERVICES_DIR'), 'lib', 'netcdf', 'imos_env')
if not os.path.isfile(imos_env_path):
logger = logging_aims()
logger.error('%s is not accessible' % imos_env_path)
close_logger(logger)
sys.exit(1)
dotenv.load_dotenv(imos_env_path)
netcdf_file_obj = Dataset(netcdf_file_path, 'a', format='NETCDF4')
netcdf_file_obj.naming_authority = 'IMOS'
# add gatts to NetCDF
netcdf_file_obj.aims_channel_id = int(channel_id_info['channel_id'])
if not (channel_id_info['metadata_uuid'] == 'Not Available'):
netcdf_file_obj.metadata_uuid = channel_id_info['metadata_uuid']
if not netcdf_file_obj.instrument_serial_number:
del(netcdf_file_obj.instrument_serial_number)
# add CF gatts, values stored in lib/netcdf/imos_env
netcdf_file_obj.Conventions = os.environ.get('CONVENTIONS')
netcdf_file_obj.data_centre_email = os.environ.get('DATA_CENTRE_EMAIL')
netcdf_file_obj.data_centre = os.environ.get('DATA_CENTRE')
netcdf_file_obj.project = os.environ.get('PROJECT')
netcdf_file_obj.acknowledgement = os.environ.get('ACKNOWLEDGEMENT')
netcdf_file_obj.distribution_statement = os.environ.get('DISTRIBUTION_STATEMENT')
netcdf_file_obj.date_created = strftime("%Y-%m-%dT%H:%M:%SZ", gmtime())
netcdf_file_obj.quality_control_set = 1
imos_qc_convention = 'IMOS standard set using the IODE flags'
netcdf_file_obj.author = 'laurent besnard'
netcdf_file_obj.author_email = '[email protected]'
rename_netcdf_attribute(netcdf_file_obj, 'geospatial_LAT_max', 'geospatial_lat_max')
rename_netcdf_attribute(netcdf_file_obj, 'geospatial_LAT_min', 'geospatial_lat_min')
rename_netcdf_attribute(netcdf_file_obj, 'geospatial_LON_max', 'geospatial_lon_max')
rename_netcdf_attribute(netcdf_file_obj, 'geospatial_LON_min', 'geospatial_lon_min')
# variables modifications
time = netcdf_file_obj.variables['time']
time.calendar = 'gregorian'
time.axis = 'T'
time.valid_min = 0.0
time.valid_max = 9999999999.0
netcdf_file_obj.renameDimension('time', 'TIME')
netcdf_file_obj.renameVariable('time', 'TIME')
netcdf_file_obj.time_coverage_start = num2date(time[:], time.units, time.calendar).min().strftime('%Y-%m-%dT%H:%M:%SZ')
netcdf_file_obj.time_coverage_end = num2date(time[:], time.units, time.calendar).max().strftime('%Y-%m-%dT%H:%M:%SZ')
# latitude longitude
latitude = netcdf_file_obj.variables['LATITUDE']
latitude.axis = 'Y'
latitude.valid_min = -90.0
latitude.valid_max = 90.0
latitude.reference_datum = 'geographical coordinates, WGS84 projection'
latitude.standard_name = 'latitude'
latitude.long_name = 'latitude'
longitude = netcdf_file_obj.variables['LONGITUDE']
longitude.axis = 'X'
longitude.valid_min = -180.0
longitude.valid_max = 180.0
longitude.reference_datum = 'geographical coordinates, WGS84 projection'
longitude.standard_name = 'longitude'
longitude.long_name = 'longitude'
# handle masked arrays
lon_array = longitude[:]
lat_array = latitude[:]
if type(lon_array) != numpy.ma.core.MaskedArray or len(lon_array) == 1:
netcdf_file_obj.geospatial_lon_min = min(lon_array)
netcdf_file_obj.geospatial_lon_max = max(lon_array)
else:
netcdf_file_obj.geospatial_lon_min = numpy.ma.MaskedArray.min(lon_array)
netcdf_file_obj.geospatial_lon_max = numpy.ma.MaskedArray.max(lon_array)
if type(lat_array) != numpy.ma.core.MaskedArray or len(lat_array) == 1:
netcdf_file_obj.geospatial_lat_min = min(lat_array)
netcdf_file_obj.geospatial_lat_max = max(lat_array)
else:
numpy.ma.MaskedArray.min(lat_array)
netcdf_file_obj.geospatial_lat_min = numpy.ma.MaskedArray.min(lat_array)
netcdf_file_obj.geospatial_lat_max = numpy.ma.MaskedArray.max(lat_array)
# Change variable name, standard name, longname, untis ....
if 'Seawater_Intake_Temperature' in netcdf_file_obj.variables.keys():
var = netcdf_file_obj.variables['Seawater_Intake_Temperature']
var.units = 'Celsius'
netcdf_file_obj.renameVariable('Seawater_Intake_Temperature', 'TEMP')
netcdf_file_obj.renameVariable('Seawater_Intake_Temperature_quality_control', 'TEMP_quality_control')
var.ancillary_variables = 'TEMP_quality_control'
#.........这里部分代码省略.........
示例10: change_dataformat
# 需要导入模块: from netCDF4 import Dataset [as 别名]
# 或者: from netCDF4.Dataset import renameVariable [as 别名]
rootgrp.platform_code = '99999'
rootgrp.date_update = change_dataformat(rootgrp.date_modified) # should be converted from rootgrp.date_modified
rootgrp.data_mode = data_mode_dic[rootgrp.data_mode]
rootgrp.naming_authority = 'EGO'
rootgrp.id = outputfile.split('.')[0] # taken from file name... maybe something better to do
rootgrp.source = "Glider observation"
rootgrp.Conventions = "CF-1.4 EGO-1.0"
rootgrp.geospatial_lat_min = str(rootgrp.geospatial_lat_min)
rootgrp.geospatial_lat_max = str(rootgrp.geospatial_lat_max)
rootgrp.geospatial_lon_min = str(rootgrp.geospatial_lon_min)
rootgrp.geospatial_lon_max = str(rootgrp.geospatial_lon_max)
rootgrp.time_coverage_start = change_dataformat(rootgrp.time_coverage_start)
rootgrp.time_coverage_end = change_dataformat(rootgrp.time_coverage_end)
rootgrp.renameVariable('depth', 'DEPTH')
# Rename TIME variable and add attributes
rootgrp.renameVariable('time', 'TIME')
TIME = rootgrp.variables['TIME']
TIME.long_name = "Epoch time"
TIME.units = "seconds since 1970-01-01T00:00:00Z"
TIME.valid_min = "0." # problem to be mentionned
TIME.valid_max = "9000000000" # problem to be mentionned
TIME.QC_procedure, = "1"
TIME.comment, = " "
TIME.ancillary_variable = "TIME_QC"
TIME.sdn_parameter_urn = "SDN:P01::ELTMEP01"
TIME.sdn_uom_urn = "SDN:P061::UTBB"
# Create LATITUDE variable with its attributes