|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Prepare templates for output files from offline radiative transfer calculations |
| 4 | +# suitable for publishing on the Earth System Grid |
| 5 | +# |
| 6 | +# Robert Pincus, Robert.Pincus@colorado.edu, 2016-2019 |
| 7 | +# |
| 8 | +# --------------------------------------------------------------------------------- |
| 9 | +from netCDF4 import Dataset |
| 10 | +import numpy as np |
| 11 | +import time, uuid, argparse |
| 12 | +import urllib.request, json |
| 13 | +# --------------------------------------------------------------------------------- |
| 14 | +# Copy a variable and all its attributes from one netCDF file to another |
| 15 | +# |
| 16 | +def copyVar(nc_in, nc_out, name, newname=None) : |
| 17 | + if newname is None : |
| 18 | + newname = name |
| 19 | + nc_out.createVariable(newname, nc_in.variables[name].dtype, nc_in.variables[name].dimensions) |
| 20 | + nc_out.variables[newname].setncatts(nc_in.variables[name].__dict__) |
| 21 | + nc_out.variables[newname][:] = nc_in.variables[name][:] |
| 22 | +# --------------------------------------------------------------------------------- |
| 23 | + |
| 24 | +atmos_file = Dataset('multiple_input4MIPs_radiation_RFMIP_UColorado-RFMIP-1-2_none.nc', mode='r') |
| 25 | +# Available from https://www.earthsystemcog.org/projects/rfmip/resources/ |
| 26 | +# or from https://esgf-node.llnl.gov/search/input4mips/ ; search for "RFMIP" |
| 27 | + |
| 28 | +parser = argparse.ArgumentParser(description='Create CMIP6/ESGF-compliant output files for RFMIP-IRF.') |
| 29 | +parser.add_argument('--source_id', type=str, \ |
| 30 | + default = "LBLRTM-12-8", |
| 31 | + help='Source ID, must match CMIP Controlled Vocabulary at https://github.com/WCRP-CMIP/CMIP6_CVs/blob/master/CMIP6_source_id.json') |
| 32 | +parser.add_argument('--forcing_index', type=int, \ |
| 33 | + default = 1, \ |
| 34 | + help='Forcing index (1 = all available greenhouse gases; 2 = CO2, CH4, N2O, CFC12, CFC11eq; 3 = CO2, CH4, N2O, CFC12eq, HFC-134eq)') |
| 35 | +parser.add_argument('--physics_index', type=int, \ |
| 36 | + default = 1, \ |
| 37 | + help='Physics index, e.g. for different approximations') |
| 38 | +args = parser.parse_args() |
| 39 | + |
| 40 | +# |
| 41 | +# Check that source_id is valid |
| 42 | +# Use source_id to obtain other text |
| 43 | +# |
| 44 | +with urllib.request.urlopen("https://raw.githubusercontent.com/PCMDI/cmip6-cmor-tables/master/Tables/CMIP6_CV.json") as url: |
| 45 | + cmip6 = json.loads(url.read().decode()) |
| 46 | + |
| 47 | +if(args.source_id in cmip6['CV']['source_id'].keys()): |
| 48 | + source_id = args.source_id |
| 49 | + source = cmip6['CV']['source_id'][source_id]['source'] |
| 50 | + institution_id = cmip6['CV']['source_id'][source_id]['institution_id'][0] |
| 51 | + institution = cmip6['CV']['institution_id'][institution_id] |
| 52 | + physics_index = np.int32(args.physics_index) |
| 53 | + forcing_index = np.int32(args.forcing_index) |
| 54 | +else: |
| 55 | + print("source_id {} is not in CMIP6 Controlled Vocabulary".format(args.source_id)) |
| 56 | + sys.exit(1) |
| 57 | + |
| 58 | +if (forcing_index < 1 ) or (forcing_index > 3): |
| 59 | + print('forcing_index must be 1, 2, or 3 (1 = all available greenhouse gases; 2 = CO2, CH4, N2O, CFC12, CFC11eq; 3 = CO2, CH4, N2O, CFC12eq, HFC-134eq)') |
| 60 | + sys.exit(1) |
| 61 | + |
| 62 | +if (physics_index < 1 ): |
| 63 | + print('physics_index must be positive') |
| 64 | + sys.exit(1) |
| 65 | + |
| 66 | +# |
| 67 | +# Model/institution specific attributes |
| 68 | +# |
| 69 | +variant_label = "r1i1p{0}f{1}".format(physics_index,forcing_index) |
| 70 | +model_attrs = { |
| 71 | + "institution_id" :institution_id, |
| 72 | + "institution" :institution, |
| 73 | + "source_id" :source_id, |
| 74 | + "source" :source_id, |
| 75 | + "further_info_url":"https://furtherinfo.es-doc.org/CMIP6." + institution_id + "." + source_id + ".rad-irf.none." + variant_label, |
| 76 | + "forcing_index" :np.int32(forcing_index), |
| 77 | + "license" :"CMIP6 model data produced by " + institution_id + " is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License (https://creativecommons.org/licenses). " + |
| 78 | + "Consult https://pcmdi.llnl.gov/CMIP6/TermsOfUse for terms of use governing CMIP6 output, including citation requirements and proper acknowledgment. " + |
| 79 | + "Further information about this data, including some limitations, can be found via the further_info_url (recorded as a global attribute in this file) and at https://pcmdi.llnl.gov/." + |
| 80 | + "The data producers and data providers make no warranty, either express or implied, including, but not limited to, warranties of merchantability and fitness for a particular purpose." + |
| 81 | + "All liabilities arising from the supply of the information (including any liability arising in negligence) are excluded to the fullest extent permitted by law." } |
| 82 | + |
| 83 | +# |
| 84 | +# Required attributes, uniform across submissions |
| 85 | +# |
| 86 | +std_attrs = { |
| 87 | + "data_specs_version":"01.00.29", |
| 88 | + "physics_index":np.int32(physics_index)} |
| 89 | + |
| 90 | +# Submission attrs |
| 91 | +sub_attrs = { |
| 92 | + 'creation_date':time.strftime("%Y-%m-%dT%H:%M:%SZ",time.gmtime()), |
| 93 | + 'tracking_id' : '/'.join(['hdl:21.14100',str(uuid.uuid4())]), |
| 94 | + "variant_label":variant_label} |
| 95 | + |
| 96 | +# Attributes are taken from https://docs.google.com/document/d/1h0r8RZr_f3-8egBMMh7aqLwy3snpD6_MrDz1q8n5XUk/edit |
| 97 | +# Data reference syntax attributes |
| 98 | +drs_attrs = { |
| 99 | + "activity_id" :"RFMIP", # (from CMIP6_activity_id.json) |
| 100 | + "product" :"model-output", |
| 101 | + "experiment_id":"rad-irf", # (from CMIP6_experiment_id.json) |
| 102 | + "table_id" :"Efx", # (per http://clipc-services.ceda.ac.uk/dreq/u/efc0de22-5629-11e6-9079-ac72891c3257.html) |
| 103 | + "frequency" :"fx", |
| 104 | + "sub_experiment_id":"none"} |
| 105 | + |
| 106 | +expt_attrs = { |
| 107 | + "Conventions" :"CF-1.7 CMIP-6.2", |
| 108 | + "mip_era" :"CMIP6", |
| 109 | + "experiment" :"offline assessment of radiative transfer parmeterizations in clear skies", |
| 110 | + "sub_experiment" :"none", |
| 111 | + "product" :"model-output", |
| 112 | + "realization_index" :np.int32(1), |
| 113 | + "initialization_index":np.int32(1), |
| 114 | + "source_type" :"RAD", |
| 115 | + "nominal_resolution" :"10 km", |
| 116 | + "realm" :"atmos", |
| 117 | + "grid_label" :"gn", |
| 118 | + "grid" :"columns sampled from ERA-Interim, radiative fluxes computed independently"} |
| 119 | + |
| 120 | +short_names = ['rlu','rsu', 'rld', 'rsd'] |
| 121 | +stand_names = ['upwelling_longwave_flux_in_air','upwelling_shortwave_flux_in_air', |
| 122 | + 'downwelling_longwave_flux_in_air','downwelling_shortwave_flux_in_air'] |
| 123 | + |
| 124 | +for short, std in zip(short_names, stand_names) : |
| 125 | + # File name is constructed per https://docs.google.com/document/d/1h0r8RZr_f3-8egBMMh7aqLwy3snpD6_MrDz1q8n5XUk/edit# |
| 126 | + # fixed strings are table_id, experiment_id, grid_label |
| 127 | + out_file_name = short + "_Efx_" + source_id + "_rad-irf_" + variant_label + "_gn" + ".nc" |
| 128 | + print('Creating ' + out_file_name) |
| 129 | + out_file = Dataset(out_file_name, mode='w', FORMAT='NETCDF4_CLASSIC') |
| 130 | + out_file.setncatts(drs_attrs) |
| 131 | + out_file.setncatts(std_attrs) |
| 132 | + out_file.setncatts(expt_attrs) |
| 133 | + out_file.setncatts(model_attrs) |
| 134 | + out_file.setncatts(sub_attrs) |
| 135 | + out_file.setncatts({'variable_id' :short}) |
| 136 | + d = out_file.createDimension('expt', atmos_file.dimensions['expt'].size) |
| 137 | + d = out_file.createDimension('site', atmos_file.dimensions['site'].size) |
| 138 | + d = out_file.createDimension('level', atmos_file.dimensions['level'].size) |
| 139 | + copyVar(atmos_file, out_file, 'lat') |
| 140 | + copyVar(atmos_file, out_file, 'lon') |
| 141 | + copyVar(atmos_file, out_file, 'time') |
| 142 | + copyVar(atmos_file, out_file, 'pres_level', 'plev') |
| 143 | + v = out_file.createVariable(short, 'f4', ('expt', 'site', 'level')) |
| 144 | + v.setncatts({'variable_id' :short, |
| 145 | + 'standard_name':std, |
| 146 | + 'units' :'W m-2', |
| 147 | + '_FillValue' :np.float32(-1.e+03), |
| 148 | + 'missing_value':np.float32(-1.e+03), |
| 149 | + "cell_methods" :"area: point", |
| 150 | + 'coordinates' :'lon lat time'}) |
| 151 | + copyVar(atmos_file, out_file, 'profile_weight') |
| 152 | + out_file.close() |
| 153 | + |
0 commit comments