ReFRACtor
value.py
Go to the documentation of this file.
1 import numpy as np
2 import netCDF4
3 
4 from .base import Creator
5 from .. import param
6 
7 from refractor import framework as rf
8 
10 
11  value = param.Choice(param.Array(dims=1), param.ArrayWithUnit(dims=1))
12  retrieved = param.Scalar(bool, required=False)
13 
14  def retrieval_flag(self, **kwargs):
15  val = self.value(**kwargs)
16  retrieved = self.retrieved()
17 
18  if hasattr(val, "value"):
19  # ArrayWithUnit
20  val_shape = val.value.shape
21  else:
22  val_shape = val.shape
23 
24  if retrieved is None or retrieved:
25  return np.ones(val_shape, dtype=bool)
26  else:
27  return np.zeros(val_shape, dtype=bool)
28 
30 
31  value = param.Choice(param.Array(dims=2), param.ArrayWithUnit(dims=2))
32  retrieved = param.Iterable(required=False)
33 
34  def retrieval_flag(self, **kwargs):
35  val = self.value(**kwargs)
36  retrieved = self.retrieved()
37 
38  if hasattr(val, "value"):
39  # ArrayWithUnit
40  val_shape = val.value.shape
41  else:
42  val_shape = val.shape
43 
44  if retrieved is None:
45  return np.ones(val_shape, dtype=bool)
46  else:
47  flags = np.empty(val_shape, dtype=bool)
48  for chan_idx, chan_flag in enumerate(retrieved):
49  flags[chan_idx, :] = chan_flag
50  return flags
51 
53  "Create an array with unit class from a numpy array and type string"
54 
55  value = param.Array()
56  units = param.Scalar(str)
57 
58  def create(self, **kwargs):
59 
60  value = self.value(**kwargs)
61  units = self.units()
62 
63  num_dims = len(value.shape)
64  if num_dims == 3:
65  return rf.ArrayWithUnit_double_3(value, units)
66  elif num_dims == 2:
67  return rf.ArrayWithUnit_double_2(value, units)
68  elif num_dims == 1:
69  return rf.ArrayWithUnit_double_1(value, units)
70  else:
71  raise param.ParamError("Unsupported number of dimensions %s for array" % (num_dims))
72 
74 
75  filename = param.Scalar(str)
76 
77  def create(self, **kwargs):
78 
79  values = {}
80 
81  contents = netCDF4.Dataset(self.filename(), "r")
82 
83  def extract_datasets(group, values):
84  for v_name, v_ds in group.variables.items():
85  v_path = (group.path + "/" + v_ds.name).lstrip("/")
86  values[v_path] = v_ds[:]
87  for g_name, child_group in group.groups.items():
88  extract_datasets(child_group, values)
89 
90  extract_datasets(contents, values)
91 
92  return values
93 
95 
96  name = param.Scalar(str)
97 
98  def create(self, **kwargs):
99  self.register_parameter(self.name(), param.AnyValue())
100  return self.param(self.name())
def register_parameter(self, param_name, param_def)
Definition: base.py:95
def param(self, param_name, kwargs)
Definition: base.py:111

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