Created
March 18, 2014 14:21
-
-
Save benbovy/9621033 to your computer and use it in GitHub Desktop.
pyHEMCO notebook examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"pyHEMCO (Python API for GEOS-Chem's HEMCO): usage examples" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"First, import the `pyhemco.emissions` Python module. The commands below allow to import the package without installing it." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import sys\n", | |
"sys.path.extend('/home/bovy/MyGitRepos/pyHEMCO') # replace with your path/to/pyHEMCO_git_repo\n", | |
"\n", | |
"from pyhemco import emissions" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"HEMCO Input file\n", | |
"----------------\n", | |
"\n", | |
"For all examples shown in this notebook, the following HEMCO input file is taken as a reference." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"input_content=!cat HEMCO_input_sample.txt\n", | |
"from IPython.display import HTML\n", | |
"HTML('<pre style=\"font-size:80%;\">' + '<br>'.join(input_content) + '</pre>')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": [ | |
"<pre style=\"font-size:80%;\">### HEMCO INPUT FILE ###<br>#<br># ==================================================================================================<br># This file contains source file information of all emission fields used by the Harvard emissions <br># component (HEMCO). <br># Base emissions, scale factors and masks are listed in three separate sections. HEMCO settings and <br># extensions are defined/activated at the end of the input file.<br>#<br># Required for all fields: <br># - Name: Descriptive field name.<br># - sourceFile: (netCDF) file name (incl. full path)<br># - sourceVar: file variable of interest<br># - sourceTime: time stamp of interest. Format is YYYY/MM/DD/HH. Ranges are allowed for temporally <br># changing data, e.g. 1990-2005/1/1/0 or 2002/1-12/1/0, in which case the time slices <br># closest to the simulation date is read and used (data will be updated automatically). <br># Use wildcard character (*) to read all time slices at once, e.g. 1990/1/1/*. This is <br># only recommended for quickly varying data, e.g. hourly data.<br># - srcDim: dimension of data: xy or xyz. This is mainly for compatibility with ESMF.<br># - srcUnit: unit of data. This is mainly for compatibility with ESMF.<br>#<br># Additional fields required for base emissions:<br># - ExtNr: Extension number. Must match the extension number set in the extensions toggle section. <br># Use 0 for data to be used by HEMCO core.<br># - Species: Model name of emitted species. Emissions will only be calculated if Species matches one <br># of the model species names defined during initialization of HEMCO.<br># - ScalIDs: Scale factor IDs to be applied to the base field. Must match one of the ScalID entries <br># given in the scale factor/mask section. Separate multiple scale factors by forwardslash <br># (/). Use dash (-) if none.<br># - Cat: Emission category. Fields of the same category will be assembled based upon hierarchies. <br># - Hier: Emission hierarchy. Higher hierarchy emissions will overwrite lower hierarchy emissions <br># (if same category).<br>#<br># Additional fields required for scale factors:<br># - ScalID: Scale factor ID. To identify scale factor IDs assigend to base emissions<br># - Oper: Mathematical operator.<br># 1: Multiply, i.e. E = B * S.<br># 2: Square, i.e. E = B * S^2.<br># -1: Divide, i.e. E = B * (1/S). Zeros will be ignored.<br># 3: Mirror, i.e. E = B * (1-S). Only applies to mask fields.<br># - Scalar: Instead of using gridded scale data from a (netCDF) file, a scalar value can be defined. <br># This value will be uniformly applied to each grid box (e.g. to divide lumped VOC <br># emissions into individual species). Leave all file information blank (-) if scalar value <br># shall be used. Scalars can be uniform in time (one entry), hourly scale factors (24 <br># entries, separated by forward slash), day-of-week factor (7 entries), or monthly scale <br># factors (12 entries).<br>#<br># Additional fields required for masks:<br># - Lon1/Lat1/Lon2/Lat2: Mask window. Give the approximate window of the mask field. Only used to <br># determine whether or not a field is important for a given CPU. Lon1/Lat1 <br># denote the lower left corner, Lon2/Lat2 is the upper right corner.<br>#<br># Settings:<br># - Verbose: Set to true to run HEMCO in verbose mode.<br>#<br># Extensions switches:<br># This section steers which extensions shall be used and the files associated with them.<br># - ExtName: extension name<br># - on/off: Set to on to activate extension<br># - ExtNr: Extensions number. Determines the base emission fields accessible within an extension<br># (and only in this extension!). Note that ExtNr 0 is reserved for HEMCO core!<br># ==================================================================================================<br><br>####################################################################################<br>### BASE EMISSIONS <br>####################################################################################<br><br># ExtNr\tName sourceFile\tsourceVar sourceTime SrcDim SrcUnit Species ScalIDs Cat Hier<br><br># --- GEIA ---<br>0 GEIA_NO /path/to/emis/GEIA/nc/GEIA_NOSO4.fullyear.geos.4x5.nc NO 1985/1-12/1/0 xy kg/m2/s NO 1/2/20/25 1 1<br><br># --- Bromocarbon emissions ---<br>0 LIANG_CHBR3 /path/to/data/emis/BROMINE/Bromocarb_Liang2010.nc CHBr3_emission 2000/1/1/0 xy kg/m2/s CHBr3 38 1 1<br>0 LIANG_CH2BR2 /path/to/data/emis/BROMINE/Bromocarb_Liang2010.nc CH2Br2_emission 2000/1/1/0 xy kg/m2/s CH2Br2 38 1 1<br><br># --- Seawater concentrations for oceanic emissions (Extension 101) ---<br>101 ACET_SEAWATER /path/to/data/emis/ACET/nc/ACET_seawater.generic.1x1.nc ACET 2005/1/1/0 xy ngL ACET - 1 1<br>101 DMS_SEAWATER /path/to/data/emis/DMS/nc/DMS_seawater.geos.4x5.nc DMS 1985/1-12/1/0 xy ngL DMS - 1 1<br>101 CH3I_SEAWATER /path/to/data/emis/CH3I/nc/ocean_ch3i.geos.4x5.nc CH3I 1985/1-12/1/0 xy ngL CH3I - 1 1<br><br># --- PARANOX emissions (Extension 102) ---<br># Ship NO emissions will be assembled from all inventories listed below based upon <br># the assigned hierarchies.<br>102 ICOADS_SHIP_NO /path/to/data/emis/ICOADS_SHIP/nc/ICOADS_ship.geos.1x1.nc NO 2002/1-12/1/0 xy kg/m2/s NO 1/3 1 1<br>102 EDGAR_SHIP_NO /path/to/data/emis/EDGAR/nc/EDGAR.NO.generic.1x1.nc NO.f5800 2000/1/1/0 xy kg/m2/s NO 1 1 2<br>102 EMEP_SHIP_NO /path/to/data/emis/EMEP/nc/EMEP.updt.ship.geos.1x1.nc NO 1990-2007/1/1/0 xy kg/m2/s NO 1000 1 10<br><br>#<br># END OF BASE EMISSIONS <br><br>####################################################################################<br>### SCALE FACTORS <br>####################################################################################<br><br># ScalID Name sourceFile sourceVar sourceTime SrcDim SrcUnit Oper Scalar<br><br># --- annual scale factors ---<br>1 TOTFUEL_THISYR /path/to/data/emis/AnnualScalar/nc/NOx-AnnualScalar.geos.1x1.nc NOXscalar 1985-2008/1/1/0 xy unitless 1 -<br>2 TOTFUEL_1985 /path/to/data/emis/AnnualScalar/nc/NOx-AnnualScalar.geos.1x1.nc NOXscalar 1985/1/1/0 xy unitless -1 -<br>3 TOTFUEL_2002 /path/to/data/emis/AnnualScalar/nc/NOx-AnnualScalar.geos.1x1.nc NOXscalar 2002/1/1/0 xy unitless -1 -<br><br># --- day-of-week scale factors --- <br>20 GEIA_DOW_NOX - - - xy unitless 1 1.0706/1.0706/1.0706/1.0706/1.0706/0.863/0.784 <br><br># --- diurnal scale factors --- <br>25 EDGAR_TODNOX /path/to/data/emis/EDGAR/nc/dailyScal.nc NOXscale 2000/1/1/1-24 xy unitless 1 -<br><br># Seasonal scale factors for bromocarbons:<br>38 BROMOCARB_SEASON /path/to/data/emis/BROMINE/BromoCarb_Season.nc CHXBRY_scale 2000/1-12/1/0 xy unitless 1 -<br><br>####################################################################################<br>### MASKS <br>####################################################################################<br>#<br># ScalID Name sourceFile sourceVar sourceTime SrcDim SrcUnit Oper Lon1/Lat1/Lon2/Lat2<br>1000 EMEP_MASK /path/to/data/emis/MASKS/nc/EMEP_mask.geos.1x1.nc MASK */*/*/* xy xxx 1 -30/30/45//70<br><br>#<br># END OF MASKS <br><br>####################################################################################<br>### SETTINGS<br>####################################################################################<br>#<br>Verbose: false<br>#<br># END OF SETTINGS <br><br>####################################################################################<br>### EXTENSIONS SWITCHES<br>####################################################################################<br># For extensions using gridded data organized by HEMCO, make sure that these files <br># are listed in the base emissions section and match the ExtNr of the respective <br># extension!<br>#<br># ExtName on/off ExtNr<br>SeaFlux off 101<br>PARANOX off 102<br>LightNOx off 103<br>SoilNOx off 104<br><br>### END OF HEMCO INPUT FILE ###</pre>" | |
], | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 2, | |
"text": [ | |
"<IPython.core.display.HTML at 0x2770750>" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Creating a new emission setup \"from scratch\"\n", | |
"--------------------------------------------\n", | |
"\n", | |
"Steps required for creating a new emission setup using pyhemco are detailed below.\n", | |
"\n", | |
"- Base emission fields, scale factors and masks are all GEOS-Chem data fields to which we add some specific metadata. It can be created from any existing `GCField` object. In this example, we first create new `GCField` objects for fields that will be included in the emission setup (a GEOS-Chem Python API currently in development will provide high-level functions for reading NetCDF files and importing the NetCDF variables as `GCField` objects). " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"noxscalar = emissions.GCField('NOXScalar',\n", | |
" var_name='NOXScalar',\n", | |
" filename='/path/to/data/emis/AnnualScalar/nc/NOx-AnnualScalar.geos.1x1.nc',\n", | |
" ndim=2,\n", | |
" unit='unitless')\n", | |
"\n", | |
"geia_dow_nox = emissions.GCField('GEIA_DOW_NOX',\n", | |
" ndim=1, # does all scalar-based scale factors have 'xy' dims in HEMCO input file ?\n", | |
" unit='unitless',\n", | |
" data=[1.0706, 1.0706, 1.0706, 1.0706, 1.0706, 0.863, 0.784])\n", | |
"\n", | |
"emep_mask = emissions.GCField('EMEP_MASK',\n", | |
" var_name='MASK',\n", | |
" filename='/path/to/data/emis/MASKS/nc/EMEP_mask.geos.1x1.nc',\n", | |
" ndim=2,\n", | |
" unit='xxx')\n", | |
"\n", | |
"geia_no = emissions.GCField('GIEA_NO',\n", | |
" var_name='NO',\n", | |
" filename='/path/to/emis/GEIA/nc/GEIA_NOSO4.fullyear.geos.4x5.nc',\n", | |
" ndim=2,\n", | |
" unit='kg/m2/s')\n", | |
"\n", | |
"emep_ship_no = emissions.GCField('EMEP_SHIP_NO',\n", | |
" var_name='NO',\n", | |
" filename='/path/to/data/emis/EMEP/nc/EMEP.updt.ship.geos.1x1.nc',\n", | |
" ndim=2,\n", | |
" unit='kg/m2/s')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- Datafields that will be used by the HEMCO core and its extensions are created using the `base_emission_field`, `scale_factor` and `mask` functions, which basically add the metadata required by HEMCO to `GCField` objects.\n", | |
"\n", | |
" Scale factors and masks IDs (`fid`) can be left undefined, although it must be set when saving the emission setup to a file. Note that mask fields are specific scale factor fields and therefore have some common metadata attributes.\n", | |
" \n", | |
" Scale factors and masks can be assigned to a base emission field with the `scale_factor` argument of the `base_emission_field` function. \n", | |
" \n", | |
" Emission metadata can be added to a `GCField` in place (`copy=False`, default) or to a copy of this `GCField` (`copy=True`). The latter option may be useful, for example, when we need to create multiple scale factors or masks from one `GCField`. " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": true, | |
"input": [ | |
"# copy GCField and add metadata\n", | |
"totfuel_thisyr = emissions.scale_factor(noxscalar, 'TOTFUEL_THISYR', '1985-2008/1/1/0', operator='*',\n", | |
" fid=1, copy=True)\n", | |
"totfuel_1985 = emissions.scale_factor(noxscalar, 'TOTFUEL_1985', '1985/1/1/0', operator='/',\n", | |
" fid=2, copy=True)\n", | |
"totfuel_2002 = emissions.scale_factor(noxscalar, 'TOTFUEL_2002', '2002/1/1/0', operator='/',\n", | |
" fid=3, copy=True)\n", | |
"\n", | |
"# add metadata in place\n", | |
"emissions.scale_factor(geia_dow_nox, geia_dow_nox.name, '*/*/*/*', operator='*', fid=None) # fid will be set later\n", | |
"\n", | |
"emissions.mask(emep_mask, emep_mask.name, '*/*/*/*', mask_window=[-30, 30, 45, 70], fid=1000)\n", | |
"\n", | |
"emissions.base_emission_field(geia_no, geia_no.name, '1985/1-12/1/0', 'NO', 1, 1,\n", | |
" scale_factors=[totfuel_thisyr, totfuel_1985, geia_dow_nox])\n", | |
"emissions.base_emission_field(emep_ship_no, emep_ship_no.name, '1990-2007/1/1/0', 'NO', 1, 10,\n", | |
" scale_factors=[emep_mask])\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- Creation of a new `EmissionExt` object for each HEMCO extension to include in the setup (must be done at least for HEMCO Core!). Currently, the `EmissionExt` class only stores basic information about the extension (extension name, on/off switch, extension number). Base emissions fields can eventually be attached to an extension. Similar to scale factors, User can optionally give a value for `eid` (i.e., the extension number) or leave it undefined (`None`, default), though it is higly recommended to define a value. When settings are written to a file, a `eid` value must be defined for each extension (it can be done automatically, see below). Note that HEMCO Core must have `eid` set to 0 ! " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"core_ext = emissions.EmissionExt('HEMCOCore', eid=0, base_emission_fields=[geia_no]) \n", | |
"seaflux_ext = emissions.EmissionExt('SeaFlux', enabled=False, eid=101)\n", | |
"paranox_ext = emissions.EmissionExt('PARANOX', enabled=False, eid=102, base_emission_fields=[emep_ship_no])\n", | |
"lightnox_ext = emissions.EmissionExt('LightNOx', enabled=True, eid=103)\n", | |
"soilnox_ext = emissions.EmissionExt('SoilNOx', enabled=True, eid=None) # eid will be set later." | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- Creating a new emission setup only requires providing the extensions (each extensions has reference to base emission fields and each emission field has references to scale factors and masks). Additional emission settings can be given with the `extra_settings` keyword argument. It is also possible to define an empty setup and add extensions later (see section 'Editing an existing emission setup' below). " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"example_setup = emissions.Emissions([core_ext, seaflux_ext, paranox_ext, lightnox_ext, soilnox_ext],\n", | |
" description='An emission setup example (not complete)',\n", | |
" extra_settings={'Verbose': False})\n", | |
"\n", | |
"empty_setup = emissions.Emissions([], description='An empty setup')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Accessing emission setup data\n", | |
"-----------------------------\n", | |
"\n", | |
"It is easy to access to any emission setting, either directly or through filtering. Iterating over settings is also straightforward. A few basic examples are given below.\n", | |
"\n", | |
"*Get all base emission fields (returns a collection of `GCField` objects, see class `pyhemco.datatypes.ObjectCollection`):*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"example_setup.base_emission_fields" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 7, | |
"text": [ | |
"<ObjectCollection: [<GCField: EMEP_SHIP_NO>, <GCField: GIEA_NO>]>" | |
] | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*Get all scale factors and masks that are assigned to base emission fields (read-only collection of `GCField` objects):*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"example_setup.scale_factors" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 8, | |
"text": [ | |
"<ObjectCollection: [<GCField: EMEP_MASK>, <GCField: GEIA_DOW_NOX>, <GCField: TOTFUEL_THISYR>, <GCField: TOTFUEL_1985>]>" | |
] | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*Get all HEMCO extensions involved in the setup (read-only collection of `EmissionExt` objects):*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"example_setup.extensions" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 9, | |
"text": [ | |
"<ObjectCollection: [<EmissionExt: HEMCOCore (0)>, <EmissionExt: SeaFlux (101)>, <EmissionExt: PARANOX (102)>, <EmissionExt: LightNOx (103)>, <EmissionExt: SoilNOx (None)>]>" | |
] | |
} | |
], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*Get the 'GIEA_NO' base emission field*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"giea_no = example_setup.base_emission_fields.get_object(name='GIEA_NO')\n", | |
"giea_no" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 10, | |
"text": [ | |
"<GCField: GIEA_NO>" | |
] | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*Base emission field metadata is stored as a dictionary in the 'emission_base' attribute of `GCField`:*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"giea_no.attributes['emission_base']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 11, | |
"text": [ | |
"{'category': 1,\n", | |
" 'hierarchy': 1,\n", | |
" 'name': 'GIEA_NO',\n", | |
" 'scale_factors': <ObjectCollection: [<GCField: TOTFUEL_THISYR>, <GCField: TOTFUEL_1985>, <GCField: GEIA_DOW_NOX>]>,\n", | |
" 'species': 'NO',\n", | |
" 'timestamp': '1985/1-12/1/0'}" | |
] | |
} | |
], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*Similarly, for scale factors and masks ('emission_scale_factor' attribute):*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"totfuel_thisyr = giea_no.emission_scale_factors.get_object(name='TOTFUEL_THISYR')\n", | |
"totfuel_thisyr.attributes['emission_scale_factor']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 12, | |
"text": [ | |
"{'fid': 1,\n", | |
" 'name': 'TOTFUEL_THISYR',\n", | |
" 'operator': '*',\n", | |
" 'timestamp': '1985-2008/1/1/0'}" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*Get all base emission fields that will be used by the 'PARANOX' extension:*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"paranox_ext = example_setup.extensions.get_object(name='PARANOX')\n", | |
"paranox_fields = paranox_ext.base_emission_fields\n", | |
"paranox_fields" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 13, | |
"text": [ | |
"<ObjectCollection: [<GCField: EMEP_SHIP_NO>]>" | |
] | |
} | |
], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*Get all masks:*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"filter_masks = lambda field: 'mask_window' in field.attributes['emission_scale_factor'].keys()\n", | |
"masks = example_setup.scale_factors.filter(filter_masks)\n", | |
"masks" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 14, | |
"text": [ | |
"<ObjectCollection (selection): [<GCField: EMEP_MASK>]>" | |
] | |
} | |
], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*Which extension will use the 'GIEA_NO' base emission field?*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"example_setup.extensions.get_object(lambda ext: giea_no in ext.base_emission_fields)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 15, | |
"text": [ | |
"<EmissionExt: HEMCOCore (0)>" | |
] | |
} | |
], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"There is more filtering / sorting possibilities, see docstring of class `pyhemco.datatypes.ObjectCollection`." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Editing an existing emission setup\n", | |
"----------------------------------\n", | |
"\n", | |
"Adding / removing emission fields or changing the value of any setting in a setup is as easy as accessing the setup data. Some examples below.\n", | |
"\n", | |
"*Add a new base emission field:*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"acet_seawater = emissions.GCField('ACET_SEAWATER',\n", | |
" var_name='ACET',\n", | |
" filename='/path/to/data/emis/ACET/nc/ACET_seawater.generic.1x1.nc',\n", | |
" ndim=2,\n", | |
" unit='ngL')\n", | |
"emissions.base_emission_field(acet_seawater, acet_seawater.name, '2005/1/1/0', 'ACET', 1, 1,\n", | |
" extension=seaflux_ext)\n", | |
"seaflux_ext = example_setup.extensions.get_object(name='SeaFlux')\n", | |
"seaflux_ext.base_emission_fields.add(acet_seawater)\n", | |
"seaflux_ext.base_emission_fields" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 16, | |
"text": [ | |
"<ObjectCollection: [<GCField: ACET_SEAWATER>]>" | |
] | |
} | |
], | |
"prompt_number": 16 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# base emission fields are automatically updated:\n", | |
"example_setup.base_emission_fields" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 17, | |
"text": [ | |
"<ObjectCollection: [<GCField: EMEP_SHIP_NO>, <GCField: GIEA_NO>, <GCField: ACET_SEAWATER>]>" | |
] | |
} | |
], | |
"prompt_number": 17 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*Create and assign a new scale factor to the 'GEIA_NO' base emission field:*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"edgar_todnox = emissions.GCField('EDGAR_TODNOX',\n", | |
" var_name='NOXscale',\n", | |
" filename='/path/to/data/emis/EDGAR/nc/dailyScal.nc',\n", | |
" ndim=2,\n", | |
" unit='unitless')\n", | |
"emissions.scale_factor(edgar_todnox, edgar_todnox.name, '2000/1/1/1-24', operator='*', fid=25)\n", | |
"\n", | |
"giea_no = example_setup.base_emission_fields.get_object(name='GIEA_NO')\n", | |
"giea_no.emission_scale_factors.add(edgar_todnox)\n", | |
"giea_no.emission_scale_factors" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 18, | |
"text": [ | |
"<ObjectCollection: [<GCField: TOTFUEL_THISYR>, <GCField: TOTFUEL_1985>, <GCField: GEIA_DOW_NOX>, <GCField: EDGAR_TODNOX>]>" | |
] | |
} | |
], | |
"prompt_number": 18 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# scale factors listed in setup are automatically updated:\n", | |
"example_setup.scale_factors" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 19, | |
"text": [ | |
"<ObjectCollection: [<GCField: EDGAR_TODNOX>, <GCField: EMEP_MASK>, <GCField: GEIA_DOW_NOX>, <GCField: TOTFUEL_THISYR>, <GCField: TOTFUEL_1985>]>" | |
] | |
} | |
], | |
"prompt_number": 19 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*Remove the mask assigned to the 'EMEP_SHIP_NO' base emission field:*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"emep_ship_no = example_setup.base_emission_fields.get_object(name='EMEP_SHIP_NO')\n", | |
"emep_ship_no.emission_scale_factors.get(name='EMEP_MASK').remove()\n", | |
"emep_ship_no.emission_scale_factors" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 20, | |
"text": [ | |
"<ObjectCollection: []>" | |
] | |
} | |
], | |
"prompt_number": 20 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"example_setup.scale_factors" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 21, | |
"text": [ | |
"<ObjectCollection: [<GCField: EDGAR_TODNOX>, <GCField: GEIA_DOW_NOX>, <GCField: TOTFUEL_THISYR>, <GCField: TOTFUEL_1985>]>" | |
] | |
} | |
], | |
"prompt_number": 21 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*Remove all base emission fields attached to the 'PARANOX' HEMCO extension:*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"paranox_ext = example_setup.extensions.get_object(name='PARANOX')\n", | |
"paranox_ext.base_emission_fields.get_all().remove()\n", | |
"example_setup.base_emission_fields" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 22, | |
"text": [ | |
"<ObjectCollection: [<GCField: GIEA_NO>, <GCField: ACET_SEAWATER>]>" | |
] | |
} | |
], | |
"prompt_number": 22 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*Change the ID (`fid`) of the 'EDGAR_TODNOX' scale factor:*" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"edgar_todnox = example_setup.scale_factors.get_object(name='EDGAR_TODNOX')\n", | |
"edgar_todnox.attributes['emission_scale_factor']['fid'] = 26\n", | |
"edgar_todnox.attributes" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 23, | |
"text": [ | |
"{'emission_scale_factor': {'fid': 26,\n", | |
" 'name': 'EDGAR_TODNOX',\n", | |
" 'operator': '*',\n", | |
" 'timestamp': '2000/1/1/1-24'}}" | |
] | |
} | |
], | |
"prompt_number": 23 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"giea_no.emission_scale_factors.get_object(name='EDGAR_TODNOX').attributes" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 24, | |
"text": [ | |
"{'emission_scale_factor': {'fid': 26,\n", | |
" 'name': 'EDGAR_TODNOX',\n", | |
" 'operator': '*',\n", | |
" 'timestamp': '2000/1/1/1-24'}}" | |
] | |
} | |
], | |
"prompt_number": 24 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Writing an emission setup to a text file\n", | |
"----------------------------------------\n", | |
"\n", | |
"(Not yet implemented)\n", | |
"\n", | |
"Writing a setup to a file requires consistent IDs for HEMCO extensions, scale factors and masks. The `Emission` class provides two methods for that:\n", | |
"\n", | |
"- `check_id` checks for missing or duplicate IDs" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# should raise an error because we left some IDs undefined above\n", | |
"example_setup.check_id()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "ValueError", | |
"evalue": "Missing or duplicate scale factor fid", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32m<ipython-input-25-e6fa640c767e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# should raise an error because we left some IDs undefined above\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mexample_setup\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcheck_id\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[1;32m/home/bovy/MyGitRepos/pyHEMCO/pyhemco/emissions.pyc\u001b[0m in \u001b[0;36mcheck_id\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 428\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mNone\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mfids\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfids\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfids\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 429\u001b[0m \u001b[1;31m# TODO: raise custom exception\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 430\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Missing or duplicate scale factor fid\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 431\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 432\u001b[0m \u001b[0meids\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mext\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0meid\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mext\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mextensions\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;31mValueError\u001b[0m: Missing or duplicate scale factor fid" | |
] | |
} | |
], | |
"prompt_number": 25 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- `resolve_id` allows to set automatically new values for missing or duplicate IDs " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"geia_dow_nox = example_setup.scale_factors.get_object(name='GEIA_DOW_NOX')\n", | |
"geia_dow_nox.attributes['emission_scale_factor']['fid'] is None # there is a missing ID here (should return True)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 26, | |
"text": [ | |
"True" | |
] | |
} | |
], | |
"prompt_number": 26 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"example_setup.resolve_id()\n", | |
"example_setup.check_id() # should not raise any error anymore\n", | |
"geia_dow_nox.attributes['emission_scale_factor']['fid'] # should return a valid ID" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 27, | |
"text": [ | |
"3" | |
] | |
} | |
], | |
"prompt_number": 27 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Reading an emission setup from a text file\n", | |
"------------------------------------------\n", | |
"\n", | |
"(Not yet implemented)\n", | |
"\n", | |
"Should implement the steps described in the section 'Creating a new emission setup from scratch'." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment