ReFRACtor
absorber.py
Go to the documentation of this file.
1 import os
2 
3 import numpy as np
4 
5 from .base import Creator, ParamPassThru
6 from .value import CreatorFlaggedValue
7 from .util import ExtendedFormatter
8 from .. import param
9 
10 from refractor import framework as rf
11 
12 DEFAULT_REFERENCE_ATM_FILENAME = os.path.join(os.environ.get("REFRACTOR_INPUTS", "$ENV{REFRACTOR_INPUTS}"), "reference_atmosphere.h5")
13 
14 class ReferenceAtmFileMixin(object):
15 
16  def ref_atm_data(self):
17 
18  if self.reference_atm_file() is not None:
19  if not os.path.exists(self.reference_atm_file()):
20  raise param.ParamError("Could not find reference atmosphere file supplied through config: %s" % self.reference_atm_file())
21 
22  ref_atm_data = rf.HdfFile(self.reference_atm_file())
23  else:
24  if not os.path.exists(DEFAULT_REFERENCE_ATM_FILENAME):
25  raise param.ParamError("Could not find default reference atmosphere file: %s" % DEFAULT_REFERENCE_ATM_FILENAME)
26 
27  ref_atm_data = rf.HdfFile(DEFAULT_REFERENCE_ATM_FILENAME)
28 
29  return ref_atm_data
30 
32  "Creates a VMR apriori for a gas species using the TCCON method"
33 
34  l1b = param.InstanceOf(rf.Level1b)
35  met = param.InstanceOf(rf.Meteorology)
36  pressure = param.InstanceOf(rf.Pressure)
37  altitude = param.ObjectVector("altitude")
38  temp_avg_window = param.Scalar(int, default=11)
39 
40  # Normally passed from through the create method from the AbsorberGasDefinition creator
41  gas_name = param.Scalar(str, required=False)
42 
43  # Normally use the distributed version of this file
44  reference_atm_file = param.Scalar(str, required=False)
45 
46  def create(self, gas_name=None, **kwargs):
47 
48  if self.gas_name() is not None:
49  gas_name = self.gas_name()
50  elif gas_name is None:
51  raise param.ParamError("gas_name not supplied to creator %s" % self.__class__.__name__)
52 
53  apriori_obj = rf.GasVmrApriori(self.met(), self.l1b(), self.altitude()[0], self.ref_atm_data(), "/Reference_Atmosphere", gas_name, self.temp_avg_window())
54  return apriori_obj.apriori_vmr(self.pressure())
55 
57  "Creates a VMR apriori for a gas species using the TCCON method"
58 
59  pressure = param.InstanceOf(rf.Pressure)
60  temperature = param.InstanceOf(rf.Temperature)
61  latitude = param.ArrayWithUnit(dims=1)
62  time = param.Iterable(rf.Time)
63  altitude = param.ObjectVector("altitude")
64  reference_atm_file = param.Scalar(str)
65  temp_avg_window = param.Scalar(int, default=11)
66 
67  # Normally passed from through the create method from the AbsorberGasDefinition creator
68  gas_name = param.Scalar(str, required=False)
69 
70  # Normally use the distributed version of this file
71  reference_atm_file = param.Scalar(str, required=False)
72 
73  def create(self, gas_name=None, **kwargs):
74 
75  if self.gas_name() is not None:
76  gas_name = self.gas_name()
77  elif gas_name is None:
78  raise param.ParamError("gas_name not supplied to creator %s" % self.__class__.__name__)
79 
80  pressure_levels = self.pressure().pressure_grid.value.value
81  temperature_levels = self.temperature().temperature_grid(self.pressure()).value.value
82 
83  apriori_obj = rf.GasVmrApriori(pressure_levels, temperature_levels, self.latitude().value[0], self.time()[0], \
84  self.altitude()[0], self.ref_atm_data(), "/Reference_Atmosphere", gas_name, self.temp_avg_window())
85  return apriori_obj.apriori_vmr()
86 
88  "Creates a AbsorberVmrLevel that supplies a AbsorberVmr class for use in an creating an Atmosphere"
89 
90  pressure = param.InstanceOf(rf.Pressure)
91 
92  # Normally passed from through the create method from the AbsorberGasDefinition creator
93  gas_name = param.Scalar(str, required=False)
94 
95  def create(self, gas_name=None, **kwargs):
96 
97  if self.gas_name() is not None:
98  gas_name = self.gas_name()
99  elif gas_name is None:
100  raise param.ParamError("gas_name not supplied to creator %s" % self.__class__.__name__)
101 
102  return rf.AbsorberVmrLevel(self.pressure(), self.value(gas_name=gas_name), self.retrieval_flag(gas_name=gas_name), gas_name)
103 
105 
106  met = param.InstanceOf(rf.Meteorology)
107  pressure = param.InstanceOf(rf.Pressure)
108 
109  def create(self, gas_name=None, **kwargs):
110 
111  if gas_name is None:
112  raise param.ParamError("gas_name not supplied to creator %s" % self.__class__.__name__)
113 
114  return rf.AbsorberVmrMet(self.met(), self.pressure(), self.value(gas_name=gas_name)[0], bool(self.retrieval_flag(gas_name=gas_name)[0]), gas_name)
115 
117 
118  absco_base_path = param.Scalar(str)
119  filename = param.Scalar(str)
120  table_scale = param.Choice(param.Iterable(), param.Scalar(float), default=1.0)
121  spec_win = param.InstanceOf(rf.SpectralWindow)
122 
123  def create(self, gas_name=None, **kwargs):
124 
125  # Use ExtendedFormatter that allows using l and u conversion codes for upper/lower case conversion
126  fn_formatter = ExtendedFormatter()
127  try:
128  absco_filename = fn_formatter.format(self.filename(), gas=gas_name, **self.common_store)
129  except ValueError as exc:
130  raise param.ParamError('Error formatting absco filename template "%s": %s' % (self.filename(), exc))
131 
132  if not os.path.exists(absco_filename):
133  raise param.ParamError("HDF ABSCO filename does not exist: %s" % absco_filename)
134 
135  table_scale = self.table_scale()
136 
137  if np.isscalar(table_scale):
138  return rf.AbscoHdf(absco_filename, table_scale)
139  else:
140  spectral_bound = self.spec_win().spectral_bound
141 
142  # Convert to vector to match interface
143  table_scale_vector = rf.vector_double()
144  for val in table_scale:
145  table_scale_vector.push_back(val)
146 
147  return rf.AbscoHdf(absco_filename, spectral_bound, table_scale_vector)
148 
150  "Defines the interface expected for VMR config defnition blocks, values are pass through as a dictionary"
151 
152  vmr = param.InstanceOf(rf.AbsorberVmr)
153  absorption = param.InstanceOf(rf.GasAbsorption)
154 
156  "Creates an AbsorberAbsco object that statisfies the AtmosphereCreato;rs absorber value"
157 
158  gases = param.Iterable()
159  pressure = param.InstanceOf(rf.Pressure)
160  temperature = param.InstanceOf(rf.Temperature)
161  altitude = param.ObjectVector("altitude")
162  num_sub_layers = param.Scalar(int, required=False)
163  constants = param.InstanceOf(rf.Constant)
164  default_gas_definition = param.Dict(required=False)
165 
166  def create(self, **kwargs):
167 
168  vmrs = rf.vector_absorber_vmr()
169  absorptions = rf.vector_gas_absorption()
170 
171  for gas_name in self.gases():
172  if gas_name in self.config_def:
173  self.register_parameter(gas_name, param.Dict())
174  gas_def = self.param(gas_name, gas_name=gas_name)
175  else:
176  gas_def = self.default_gas_definition(gas_name=gas_name)
177 
178  if gas_def is None:
179  raise param.ParamError("No definition for gas %s and no default_gas_defintion block defined" % gas_name)
180 
181  if not "vmr" in gas_def:
182  raise param.ParamError("vmr value not in gas definition for gas: %s" % gas_name)
183 
184  if not "absorption" in gas_def:
185  raise param.ParamError("absorption value not in gas definition for gas: %s" % gas_name)
186 
187  vmrs.push_back(gas_def['vmr'])
188  absorptions.push_back(gas_def['absorption'])
189 
190  if self.num_sub_layers() is not None:
191  return rf.AbsorberAbsco(vmrs, self.pressure(), self.temperature(), self.altitude(), absorptions, self.constants(), self.number_sub_layers())
192  else:
193  return rf.AbsorberAbsco(vmrs, self.pressure(), self.temperature(), self.altitude(), absorptions, self.constants())
194 
def create(self, gas_name=None, kwargs)
Definition: absorber.py:46
def create(self, gas_name=None, kwargs)
Definition: absorber.py:95
def register_parameter(self, param_name, param_def)
Definition: base.py:95
def create(self, gas_name=None, kwargs)
Definition: absorber.py:73
def param(self, param_name, kwargs)
Definition: base.py:111
def create(self, gas_name=None, kwargs)
Definition: absorber.py:123
def create(self, gas_name=None, kwargs)
Definition: absorber.py:109

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:10