def setNetCDFDataVariableNameAttribute(netCDF_File, variableName): # open the file with read/write mode (r+) rootGrp = Dataset(netCDF_File, 'r+', format='NETCDF3_CLASSIC') rootGrp.data_variable_name = variableName #close the file rootGrp.close()
#open a new blank netcdf file to which we will be writting data outRootGrp = Dataset(outNetCDF_File, 'w', format='NETCDF3_CLASSIC') if (inRootGrp.start_year != startDate.year): raise Exception("Specified simulation start year ({0}) does not match with input vapor pressure data file data year.".format(startDate.year)) exit() # DEBUG: print global attributes of the original file: ## for gAttribute in inRootGrp.ncattrs(): ## print 'Global attribute name:', gAttribute, '=', getattr(inRootGrp,gAttribute) # add global file level attributes to the new netcdf file outRootGrp.start_year = inRootGrp.start_year outRootGrp.data_variable_name = inRootGrp.data_variable_name outRootGrp.data_time_step = inTimeStep outRootGrp.orginal_data_source = 'Daymet Software Version 2.0' outRootGrp.conventions = 'CF-1.0' outRootGrp.modified_data_source = 'CI Water System' outRootGrp.spatial_reference = 'NAD83_UTM_Zone_12N' outRootGrp.datum = 'D_North_America_1983' # get dimension values from the original netcdf file inputTimeVar = inRootGrp.variables['time'] inputXvar = inRootGrp.variables['x'] inputYvar = inRootGrp.variables['y'] inputVpVar = inRootGrp.variables[inRootGrp.data_variable_name] print(inputTimeVar.shape[0]) print(inputXvar.shape[0])
inRootGrp = Dataset(inNetCDF_File, 'r', format='NETCDF3_CLASSIC') # open a new blank netcdf file to which we will be writting data outRootGrp = Dataset(outNetCDF_File, 'w', format='NETCDF3_CLASSIC') if (inRootGrp.start_year != startDate.year): raise Exception("Specified simulation start year ({0}) does not match with input precp data file data year.".format(startDate.year)) exit() # DEBUG: print global attributes of the original file: ## for gAttribute in inRootGrp.ncattrs(): ## print 'Global attribute name:', gAttribute, '=', getattr(inRootGrp,gAttribute) # add global file level attributes to the new netcdf file outRootGrp.start_year = inRootGrp.start_year outRootGrp.data_variable_name = 'Prec' outRootGrp.data_time_step = inTimeStep outRootGrp.orginal_data_source = 'Daymet Software Version 2.0' outRootGrp.conventions = 'CF-1.0' outRootGrp.modified_data_source = 'CI Water System' outRootGrp.spatial_reference = 'NAD83_UTM_Zone_12N' outRootGrp.datum = 'D_North_America_1983' # get dimension values from the original netcdf file inputTimeVar = inRootGrp.variables['time'] inputXvar = inRootGrp.variables['x'] inputYvar = inRootGrp.variables['y'] inputPrecVar = inRootGrp.variables['Prec'] print(inputTimeVar.shape[0]) print(inputXvar.shape[0])
inputTaYVar = inRootGrpTa.variables['y'] inputTaTimeVar = inRootGrpTa.variables['time'] inputVpVar = inRootGrpVp.variables[inRootGrpVp.data_variable_name] inputVpTimeVar = inRootGrpVp.variables['time'] #DEBUG: print netcdf variable diemensions print('Dimension of Temp var: ' + str(inputTaVar.shape)) print('Dimension of Vp var: ' + str(inputVpVar.shape)) #open a new blank netcdf file to which we will be writting data outRootGrp = Dataset(outRH_NetCDFFile, 'w', format='NETCDF3_CLASSIC') # add global file level attributes to the new netcdf file outRootGrp.start_year = inRootGrpTa.start_year outRootGrp.data_variable_name = outNetCDFDataVariableName outRootGrp.data_time_step = inRootGrpTa.data_time_step outRootGrp.orginal_data_source = 'Daymet Software Version 2.0' outRootGrp.conventions = 'CF-1.0' outRootGrp.modified_data_source = 'CI Water System' outRootGrp.spatial_reference = 'NAD83_UTM_Zone_12N' outRootGrp.datum = 'D_North_America_1983' # create 3 dimensions for the output netcdf file outTimeDimensionSize = inputVpVar.shape[0] outYvarDimensionSize = inputVpVar.shape[1] outXvarDimensionSize = inputVpVar.shape[2] outRootGrp.createDimension('time', outTimeDimensionSize) outRootGrp.createDimension('x', outXvarDimensionSize) outRootGrp.createDimension('y', outYvarDimensionSize)
inputTminVar = inRootGrpTmin.variables['tmin'] inputTmaxVar = inRootGrpTmax.variables['tmax'] inputXminVar = inRootGrpTmin.variables['x'] inputYminVar = inRootGrpTmin.variables['y'] # open a new blank netcdf file to which we will be writting data # TODO: date:11/12/2013 change the format for writting to netcdf to # NETCDF4_CLASSIC in order to be able to create a nc file size greater # than 2 GB # Ref: http://www.ncl.ucar.edu/Support/talk_archives/2011/0599.html outNetCDF_File = os.path.join(outNetCDFFilePath, outNetCDF_FileName) outRootGrp = Dataset(outNetCDF_File, 'w', format='NETCDF3_CLASSIC') # add global file level attributes to the new netcdf file outRootGrp.start_year = inRootGrpTmin.start_year outRootGrp.data_variable_name = outDataVariableName outRootGrp.data_time_step = inTimeStep outRootGrp.orginal_data_source = 'Daymet Software Version 2.0' outRootGrp.conventions = 'CF-1.0' outRootGrp.modified_data_source = 'CI Water System' outRootGrp.spatial_reference = 'NAD83_UTM_Zone_12N' outRootGrp.datum = 'D_North_America_1983' print(inputTminVar.shape) # Create 3 dimensions for the 3 variables: time, x and y dataPointsPerDayNeeded = 24/inTimeStep outTimeDimensionSize = inputTminVar.shape[0] * dataPointsPerDayNeeded outYvarDimensionSize = inputTminVar.shape[1] outXvarDimensionSize = inputTminVar.shape[2] outRootGrp.createDimension('time', outTimeDimensionSize)