}, ] axisIds = list() # Create list of axes for axis in axes: axisId = cmor.axis(**axis) axisIds.append(axisId) #pdb.set_trace() ; # Debug statement # Setup units and create variable to write using cmor - see https://cmor.llnl.gov/mydoc_cmor3_api/#cmor_set_variable_attribute d1.units = outputUnits[fi] varid = cmor.variable(outputVarName[fi], d1.units, axisIds, missing_value=d1.missing) values = np.array(d1[:], np.float32) # Append valid_min and valid_max to variable before writing using cmor - see https://cmor.llnl.gov/mydoc_cmor3_api/#cmor_set_variable_attribute #cmor.set_variable_attribute(varid,'valid_min','f',2.0) #cmor.set_variable_attribute(varid,'valid_max','f',3.0) # Prepare variable for writing, then write and close file - see https://cmor.llnl.gov/mydoc_cmor3_api/#cmor_set_variable_attribute cmor.set_deflate(varid, 1, 1, 1) # shuffle=1,deflate=1,deflate_level=1 - Deflate options compress file data cmor.write(varid, values, time_vals=time[:], time_bnds=time.getBounds()) # Write variable with time axis f.close() cmor.close()
}, {'table_entry': 'latitude', 'units': 'degrees_north', 'coord_vals': lat[:], 'cell_bounds': lat.getBounds()}, {'table_entry': 'longitude', 'units': 'degrees_east', 'coord_vals': lon[:], 'cell_bounds': lon.getBounds()}, ] axisIds = list() ; # Create list of axes for axis in axes: axisId = cmor.axis(**axis) axisIds.append(axisId) #pdb.set_trace() ; # Debug statement # Setup units and create variable to write using cmor - see https://cmor.llnl.gov/mydoc_cmor3_api/#cmor_set_variable_attribute d.units = outputUnits varid = cmor.variable(outputVarName,d.units,axisIds,missing_value=d.missing) values = np.array(d[:],np.float32) # Append valid_min and valid_max to variable before writing using cmor - see https://cmor.llnl.gov/mydoc_cmor3_api/#cmor_set_variable_attribute cmor.set_variable_attribute(varid,'valid_min','f',2.0) cmor.set_variable_attribute(varid,'valid_max','f',3.0) # Prepare variable for writing, then write and close file - see https://cmor.llnl.gov/mydoc_cmor3_api/#cmor_set_variable_attribute cmor.set_deflate(varid,1,1,1) ; # shuffle=1,deflate=1,deflate_level=1 - Deflate options compress file data cmor.write(varid,values,time_vals=time[:],time_bnds=time.getBounds()) ; # Write variable with time axis f.close() cmor.close()
myaxes[2] = cmor.axis(table_entry = 'time', units = s.getTime().units, coord_vals = s.getTime()[:],cell_bounds=s.getTime().getBounds()) pass_axes = [myaxes[2],myaxes[0],myaxes[1]] myvars[0] = cmor.variable( table_entry = varout, units = s.units, axis_ids = pass_axes, original_name = s.id, history = 'no history', comment = 'testing speed' ) cmor.set_deflate(myvars[0], shuffle, 1, level) import time,MV2 st = time.time() totcmor=0 totcdms=0 maxcmor=0 mincmor=1000 maxcdms=0 mincdms=1000 c0=st f=cdms2.open("Test/crap.nc","w") #print 'Time:',i print s.filled().shape cmor.write(myvars[0],s.filled(),ntimes_passed=ntimes) c=time.time()
#pdb.set_trace() ; # Debug statement # Setup units and create variable to write using cmor - see https://cmor.llnl.gov/mydoc_cmor3_api/#cmor_set_variable_attribute d.units = outputUnits if inputDict[key][var]['positive'] == 'down': varid = cmor.variable(outputVarName,d.units,axisIds,missing_value=d.missing,positive='down') else: varid = cmor.variable(outputVarName,d.units,axisIds,missing_value=d.missing) ; # This is not trapping the correct missing_value values = np.array(d[:],np.float32) # Append valid_min and valid_max to variable before writing - see https://cmor.llnl.gov/mydoc_cmor3_api/#cmor_set_variable_attribute #cmor.set_variable_attribute(varid,'valid_min','f',2.0) #cmor.set_variable_attribute(varid,'valid_max','f',3.0) # Prepare variable for writing, then write and close file - see https://cmor.llnl.gov/mydoc_cmor3_api/#cmor_set_variable_attribute cmor.set_deflate(varid,1,1,1) ; # shuffle=1,deflate=1,deflate_level=1 - Deflate options compress file data print 'Start CMOR write..' if key == 'OmonC': cmor.write(varid,values) ; # Write variable with time axis elif key == 'OyrC': cmor.write(varid,values) ; # Write variable with time axis elif key == 'LIyrC': cmor.write(varid,values) ; # Write variable with time axis elif key == 'Ofx': cmor.write(varid,values) ; # Write variable without time axis elif key == 'LIfx': cmor.write(varid,values) ; # Write variable without time axis else: cmor.write(varid,values,time_vals=time,time_bnds=time.getBounds()) ; # Write variable with time axis print 'End CMOR write..' # Alteratively write in append mode
def main(args, opts): print args print opts print 'python ./cmor4alma.gswp3.py ALAM_VarName' print '----------------------------------------' + '\n' almavn = args[0] alma = ALMA(almavn) # read json file & # Update frequency based on variable and write output to CMOR input file prjJson = json.load(open('./gswp3-do-input.json')) prjJson['frequency'] = alma.cmorjson('variable_entry')['frequency'] json.dump(prjJson, open('tmp.json', 'w'), ensure_ascii=True, encoding='utf-8', sort_keys=True) print '=' * 80 print 'CMOR Table Path:', alma.tblpath for srcpath in alma.fileList: print srcpath f = cdm.open(srcpath) d = f(alma.varname) # Or use temporal subset for testing below # Reset missing value d.setMissing(1e20) # This should also set fill_value, and suppress CMOR variable history being written # Get axes lat = d.getLatitude() lon = d.getLongitude() time = d.getTime() # Assumes variable is named 'time', for the demo file this is named 'months' #%% Initialize and run CMOR print 'Start CMORizing..' # For more information see https://cmor.llnl.gov/mydoc_cmor3_api/ cmor.setup( inpath='./', netcdf_file_action=cmor.CMOR_REPLACE_4) #,logfile='cmorLog.txt') cmor.dataset_json('tmp.json') cmor.load_table(alma.tblpath) if hasattr(alma, 'heightEntry'): axisIds = [ cmor.axis(**CMORAxes.get_timeAx(alma, time)), cmor.axis(**CMORAxes.get_heightAx(alma, cdm)), cmor.axis(**CMORAxes.get_latitudalAx(lat)), cmor.axis(**CMORAxes.get_longitudalAx(lon)), ] else: axisIds = [ cmor.axis(**CMORAxes.get_timeAx(alma, time)), cmor.axis(**CMORAxes.get_latitudalAx(lat)), cmor.axis(**CMORAxes.get_longitudalAx(lon)), ] print axisIds # Setup units and create variable to write using cmor - see https://cmor.llnl.gov/mydoc_cmor3_api/#cmor_set_variable_attribute d.units = alma.outputUnits varid = cmor.variable(alma.outputVarName, d.units, axisIds, missing_value=d.missing, positive=alma.positive) values = np.array(d[:], np.float32) cmor.set_deflate(varid, 1, 1, 1) # shuffle=1,deflate=1,deflate_level=1 - Deflate options compress file data # Write variable with time axis cmor.write(varid, values, time_vals=time, time_bnds=time.getBounds()) f.close() cmor.close() os.remove('tmp.json') return
{'table_entry': 'latitude', 'units': 'degrees_north', 'coord_vals': lat[:], 'cell_bounds': lat.getBounds()}, {'table_entry': 'longitude', 'units': 'degrees_east', 'coord_vals': lon[:], 'cell_bounds': lon.getBounds()}, ] axis_ids = list() for axis in axes: axis_id = cmor.axis(**axis) axis_ids.append(axis_id) varid = cmor.variable(var.id,var.units,axis_ids) values = np.array(d[:],np.float32) cmor.set_deflate(varid,1,1,1) ; # shuffle=1,deflate=1,deflate_level=1 ; CMOR 3.0.6+ cmor.write(varid,values,time_vals=time[:],time_bnds=time.getBounds()) f.close() cmor.close() # Cleanup del(outFile,var,f,d,lat,lon,time) ; gc.collect() # Write obs variables if 'amipobs' in filePath: cmor.setup(inpath='CMOR/input4MIPs-cmor-tables/Tables',netcdf_file_action=cmor.CMOR_REPLACE_4) cmor.dataset_json("CMOR/drive_input4MIPs_obs.json") f = cdm.open(outFile) d = f[var.id] lat = d.getLatitude() lon = d.getLongitude() time = d.getTime()