Skip to content

Commit d2f6c03

Browse files
authored
Merge pull request #56 from HYPERNETS/setup_cfg
Setup cfg
2 parents c9659f9 + 8a6d575 commit d2f6c03

15 files changed

+424
-352
lines changed

examples/create_sample_file.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
from os.path import join as pjoin
88
from hypernets_processor import HypernetsDSBuilder
99
from hypernets_processor.data_io.hypernets_writer import HypernetsWriter
10-
from hypernets_processor.data_io.filename_util import FilenameUtil
10+
from hypernets_processor.data_io.product_name_util import ProductNameUtil
1111

1212

13-
'''___Authorship___'''
13+
"""___Authorship___"""
1414
__author__ = "Sam Hunt"
1515
__created__ = "12/2/2020"
1616
__version__ = "0.0"
@@ -24,11 +24,15 @@
2424

2525
def create_sample_W_L1B_file(output_directory, n_sequence, site):
2626
dsb = HypernetsDSBuilder()
27-
dataset = dsb.create_ds_template({"wavelength": N_WAVELENGTHS, "sequence": n_sequence}, "W_L1B")
27+
dataset = dsb.create_ds_template(
28+
{"wavelength": N_WAVELENGTHS, "sequence": n_sequence}, "W_L1B"
29+
)
2830

2931
# wavelength data
3032
# todo - fix issue with wavelength assignment as coordinate
31-
dataset["wavelength"].data = np.concatenate((np.arange(400, 1000, 3), np.arange(1000, 1700+10, 10)))
33+
dataset["wavelength"].data = np.concatenate(
34+
(np.arange(400, 1000, 3), np.arange(1000, 1700 + 10, 10))
35+
)
3236
dataset["bandwidth"].data = np.random.normal(1.0, 0.5, N_WAVELENGTHS)
3337

3438
# geometry data
@@ -38,23 +42,25 @@ def create_sample_W_L1B_file(output_directory, n_sequence, site):
3842
dataset["solar_zenith_angle"].data = np.linspace(30, 60, n_sequence)
3943

4044
# observation data
41-
dataset["upwelling_radiance"].data = np.round(np.random.rand(N_WAVELENGTHS, n_sequence), 3)
45+
dataset["upwelling_radiance"].data = np.round(
46+
np.random.rand(N_WAVELENGTHS, n_sequence), 3
47+
)
4248
# dataset["u_random_reflectance"].data = np.random.normal(1.0, 0.5, (N_WAVELENGTHS, n_sequence))
4349
# dataset["u_systematic_reflectance"].data = np.random.normal(1.0, 0.5, (N_WAVELENGTHS,n_sequence))
4450
# dataset["cov_random_reflectance"].data = np.random.normal(1.0, 0.5, (N_WAVELENGTHS, N_WAVELENGTHS))
4551
# dataset["cov_systematic_reflectance"].data = np.random.normal(1.0, 0.5, (N_WAVELENGTHS, N_WAVELENGTHS))
4652

4753
# time data
48-
dataset["acquisition_time"].data = np.arange(10000, 10000+n_sequence, dtype=int)
54+
dataset["acquisition_time"].data = np.arange(10000, 10000 + n_sequence, dtype=int)
4955

5056
# make file name
51-
fu = FilenameUtil()
57+
fu = ProductNameUtil()
5258
filename = fu.create_file_name_l1b("w", site, datetime.datetime.today(), "0.00")
5359

5460
# write file
5561
hw = HypernetsWriter()
5662
hw.write(dataset, pjoin(output_directory, filename))
5763

5864

59-
if __name__ == '__main__':
60-
create_sample_W_L1B_file(".", 26, "BSBE")
65+
if __name__ == "__main__":
66+
create_sample_W_L1B_file(".", 26, "BSBE")

hypernets_processor/context.py

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,108 +6,105 @@
66
import dataset
77

88

9-
'''___Authorship___'''
9+
"""___Authorship___"""
1010
__author__ = "Sam Hunt"
1111
__created__ = "3/8/2020"
1212
__version__ = __version__
1313
__maintainer__ = "Sam Hunt"
1414
__email__ = "sam.hunt@npl.co.uk"
1515
__status__ = "Development"
1616

17+
PROCESSOR_CONFIG_PROTECTED_VALUES = []
18+
1719

1820
class Context:
1921
"""
2022
Class to determine and store processor state
23+
24+
:type processor_config: configparser.RawConfigParser
25+
:param processor_config: processor config data object
26+
27+
:type job_config: configparser.RawConfigParser
28+
:param job_config: job config data object
2129
"""
2230

23-
def __init__(self, job_config=None, processor_config=None, logger=None):
31+
def __init__(self, processor_config=None, job_config=None, logger=None):
2432

2533
# Initialise attributes
26-
self.logger = None
34+
self.config_values = {}
35+
self.logger = logger
2736
self.metadata_db = None
2837
self.anomoly_db = None
2938

30-
# File attributes
31-
self.time = None
32-
33-
# Processor Attributes
34-
# Processor
35-
self.version = None
39+
# Unpack processor_config to set relevant attributes
40+
if processor_config is not None:
41+
self.unpack_config(processor_config)
3642

37-
# Input
38-
self.metadata_db_url = None
43+
# Unpack processor_config to set relevant attributes
44+
if job_config is not None:
45+
self.unpack_config(
46+
job_config, protected_values=PROCESSOR_CONFIG_PROTECTED_VALUES
47+
)
3948

40-
# Output
41-
self.raw_data_directory = None
49+
# Connect to metadata database
50+
if "metadata_db_url" in self.get_config_names():
51+
self.metadata_db = dataset.connect(self.get_config_value("metadata_db_url"))
4252

43-
# Job attributes
44-
# Info
45-
self.network = None
46-
self.site = None
53+
# Connect to anomoly database
54+
if "anomoly_db_url" in self.get_config_names():
55+
self.anomoly_db = dataset.connect(self.get_config_value("anomoly_db_url"))
4756

48-
# Input
49-
self.raw_data_directory = None
50-
self.anomoly_db_url = None
57+
def unpack_config(self, config, protected_values=None):
58+
"""
59+
Unpacks config data, sets relevant entries to values instance attribute
5160
52-
# Processing Options
53-
self.l1_mf_name = None
54-
self.l2_mf_name = None
55-
self.write_l1a = None
61+
:type config: configparser.RawConfigParser
62+
:param config: config data
63+
"""
5664

57-
# Set logger attribute
58-
if logger is not None:
59-
self.logger = logger
65+
protected_values = [] if protected_values is None else protected_values
6066

61-
# Unpack job_config to set relevant attributes
62-
if job_config is not None:
63-
self._unpack_job_config(job_config)
67+
for section in config.sections():
68+
for name in config[section].keys():
6469

65-
# Connect to anomoly database
66-
self.anomoly_db = dataset.connect(self.anomoly_db_url)
70+
if name not in protected_values:
71+
value = get_config_value(config, section, name)
72+
self.set_config_value(name, value)
6773

68-
# Unpack processor_config to set relevant attributes
69-
if processor_config is not None:
70-
self._unpack_processor_config(processor_config)
74+
def set_config_value(self, name, value):
75+
"""
76+
Sets config data to values instance attribute
7177
72-
# Connect to metadata database
73-
self.metadata_db = dataset.connect(self.metadata_db_url)
78+
:type name: str
79+
:param name: config data name
7480
75-
def _unpack_job_config(self, job_config):
81+
:param value: config data value
7682
"""
77-
Unpacks processor_config, sets relevant object attributes
7883

79-
:type job_config: configparser.RawConfigParser
80-
:param job_config: job configuration information
84+
self.config_values[name] = value
8185

86+
def get_config_value(self, name):
8287
"""
88+
Get config value
8389
84-
# Unpack Info
85-
self.network = get_config_value(job_config, "Info", "network")
86-
self.site = get_config_value(job_config, "Info", "site")
90+
:type name: str
91+
:param name: config data name
8792
88-
# Unpack Input
89-
self.raw_data_directory = get_config_value(job_config, "Input", "raw_data_directory")
90-
91-
# Unpack Output
92-
self.anomoly_db_url = get_config_value(job_config, "Output", "anomoly_db_url")
93+
:return: config value
94+
"""
9395

94-
# Unpack processing options
95-
self.l1_mf_name = get_config_value(job_config, "Processing Options", "measurement_function_name")
96-
self.l2_mf_name = get_config_value(job_config, "Processing Options", "reflectance_protocol_name")
97-
self.write_l1a = get_config_value(job_config, "Processing Options", "write_l1a", dtype=bool)
96+
return self.config_values[name] if name in self.get_config_names() else None
9897

99-
def _unpack_processor_config(self, processor_config):
98+
def get_config_names(self):
10099
"""
101-
Unpacks processor_config, sets relevant object attributes
100+
Get available config value names
102101
103-
:type processor_config: configparser.RawConfigParser
104-
:param processor_config: processor configuration information
102+
:return: config value names
103+
:rtype: list
105104
"""
106105

107-
self.version = get_config_value(processor_config, "Processor", "version")
108-
self.metadata_db_url = get_config_value(processor_config, "Input", "metadata_db_url")
109-
self.archive_directory = get_config_value(processor_config, "Output", "archive_directory")
106+
return list(self.config_values.keys())
110107

111108

112-
if __name__ == '__main__':
109+
if __name__ == "__main__":
113110
pass

hypernets_processor/data_io/hypernets_db_builder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from hypernets_processor.data_io.format.databases import DB_DICT_DEFS
88

99

10-
'''___Authorship___'''
10+
"""___Authorship___"""
1111
__author__ = "Sam Hunt"
1212
__created__ = "24/7/2020"
1313
__version__ = __version__
@@ -47,5 +47,5 @@ def create_db_template(url, db_format, db_format_defs=DB_DICT_DEFS):
4747
return create_template_db(url, schema_dict=schema_dict, schema_sql=schema_sql)
4848

4949

50-
if __name__ == '__main__':
50+
if __name__ == "__main__":
5151
pass

hypernets_processor/data_io/hypernets_ds_builder.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from hypernets_processor.data_io.format.variables import VARIABLES_DICT_DEFS
1010

1111

12-
'''___Authorship___'''
12+
"""___Authorship___"""
1313
__author__ = "Sam Hunt"
1414
__created__ = "6/5/2020"
1515
__version__ = __version__
@@ -33,8 +33,12 @@ class HypernetsDSBuilder:
3333
:param metadata_defs: dictionary of metadata for each product format (default is Hypernets formats)
3434
"""
3535

36-
def __init__(self, context=None, variables_dict_defs=VARIABLES_DICT_DEFS, metadata_defs=METADATA_DEFS):
37-
36+
def __init__(
37+
self,
38+
context=None,
39+
variables_dict_defs=VARIABLES_DICT_DEFS,
40+
metadata_defs=METADATA_DEFS,
41+
):
3842
self.context = context
3943
self.variables_dict_defs = variables_dict_defs
4044
self.metadata_defs = metadata_defs
@@ -64,8 +68,12 @@ def create_ds_template(self, dim_sizes_dict, ds_format, propagate_ds=None):
6468
if ds_format in self.return_ds_formats():
6569
variables_dict = self.variables_dict_defs[ds_format]
6670
else:
67-
raise NameError("Invalid format name: " + ds_format + " - must be one of " +
68-
str(self.variables_dict_defs.keys()))
71+
raise NameError(
72+
"Invalid format name: "
73+
+ ds_format
74+
+ " - must be one of "
75+
+ str(self.variables_dict_defs.keys())
76+
)
6977

7078
# Find metadata def
7179
metadata = {}
@@ -88,11 +96,17 @@ def create_ds_template(self, dim_sizes_dict, ds_format, propagate_ds=None):
8896
metadata_db_query = {}
8997

9098
# Set product_name metadata
91-
pu = ProductNameUtil()
92-
metadata["product_name"] = pu.create_product_name(ds_format, context=self.context)
93-
94-
return create_template_dataset(variables_dict, dim_sizes_dict, metadata=metadata, propagate_ds=propagate_ds,
95-
metadata_db=metadata_db, metadata_db_query=metadata_db_query)
99+
pu = ProductNameUtil(context=self.context)
100+
metadata["product_name"] = pu.create_product_name(ds_format)
101+
102+
return create_template_dataset(
103+
variables_dict,
104+
dim_sizes_dict,
105+
metadata=metadata,
106+
propagate_ds=propagate_ds,
107+
metadata_db=metadata_db,
108+
metadata_db_query=metadata_db_query,
109+
)
96110

97111
def return_ds_formats(self):
98112
"""
@@ -171,5 +185,5 @@ def create_empty_dim_sizes_dict(self, ds_format):
171185
return dim_sizes_dict
172186

173187

174-
if __name__ == '__main__':
188+
if __name__ == "__main__":
175189
pass

0 commit comments

Comments
 (0)