def setNetCDFDataYearAttribute(netCDF_File, dataYear): # open the file with read/write mode (r+) rootGrp = Dataset(netCDF_File, 'r+', format='NETCDF3_CLASSIC') rootGrp.start_year = dataYear # close the file rootGrp.close()
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 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])