Skip to content

Commit 5ed2268

Browse files
committed
remove some redundancy from parsers
1 parent 6229f1f commit 5ed2268

1 file changed

Lines changed: 6 additions & 48 deletions

File tree

oceanval/parsers.py

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ def find_recipe(x, start = None, end = None):
1313
# check if there is only one key and one value
1414
if len(x.keys()) != 1:
1515
raise ValueError("Recipe dictionary must have exactly one key")
16-
if len(x.values()) != 1:
17-
raise ValueError("Recipe dictionary must have exactly one value")
1816

1917
valid_recipes = dict()
2018
valid_recipes["nitrate"] = "nsbc"
@@ -114,8 +112,6 @@ def find_recipe(x, start = None, end = None):
114112
output["obs_variable"] = 'TAlk'
115113
return output
116114

117-
118-
119115

120116
if value.lower() == "cobe2":
121117
if name.lower() == "temperature":
@@ -182,9 +178,7 @@ def find_recipe(x, start = None, end = None):
182178
url.append(f"https://www.ncei.noaa.gov/thredds-ocean/dodsC/woa23/DATA/silicate/netcdf/all/1.00/woa23_all_i{month_str}_01.nc")
183179
output["obs_path"] = url
184180
return output
185-
# todo
186-
# salinity
187-
# temperature
181+
# temperature/salinity
188182
if name.lower() in ["salinity", "temperature"]:
189183
# check if start and end are provided
190184
if start is None or end is None:
@@ -274,8 +268,6 @@ def find_recipe(x, start = None, end = None):
274268
output["climatology"] = False
275269
return output
276270

277-
278-
279271
if value.lower() == "nsbc":
280272
if name.lower() in ["ammonium", "nitrate", "phosphate", "silicate", "chlorophyll", "oxygen", "temperature", "salinity"]:
281273
if name.lower() == "chlorophyll":
@@ -305,21 +297,12 @@ def find_recipe(x, start = None, end = None):
305297
if name.lower() == "salinity":
306298
output["obs_variable"] = "salinity_mean"
307299

308-
309-
310300
return output
311301

312302
raise ValueError(f"Recipe value {value} is not valid for recipe name {name}")
313303

314-
315304
return x
316305

317-
def get_name(obj):
318-
namespace = globals()
319-
for name in namespace:
320-
if namespace[name] is obj:
321-
return name
322-
return None
323306

324307
# create a validator class
325308
# create a variable class to hold metadata
@@ -337,21 +320,10 @@ def __repr__(self):
337320
return '\n'.join("%s: %s" % item for item in attrs.items())
338321

339322

340-
341-
342323
class Validator:
343324
def __init__(self):
344325
self.rules = {}
345326

346-
def validate(self, data):
347-
errors = {}
348-
for field, rule in self.rules.items():
349-
if field not in data:
350-
errors[field] = "Field is missing"
351-
elif not rule(data[field]):
352-
errors[field] = "Validation failed"
353-
return errors
354-
# add chlorophyll
355327
def add_rule(self, field, rule):
356328
self.rules[field] = rule
357329

@@ -369,7 +341,6 @@ def remove(self, name):
369341
self.keys.remove(name)
370342
super().__delattr__(name)
371343

372-
373344
# add reset method
374345
def reset(self):
375346
for key in self.keys:
@@ -390,11 +361,8 @@ def __setattr__(self, name, value):
390361
# create a [] style accessor
391362
# make Validator subsettable, so that validator["chlorophyll"] returns the chlorophyll variable
392363
def __getitem__(self, key):
393-
# if key == "chlorophyll":
394-
# return
395364
return getattr(self, key, None)
396365

397-
398366

399367
#
400368
def add_gridded_comparison(self,
@@ -456,7 +424,6 @@ def add_gridded_comparison(self,
456424

457425
# maybe include an averaging option: daily, monthly, annual etc.
458426

459-
460427
if recipe is not None:
461428
recipe_info = find_recipe(recipe, start = start, end = end)
462429
obs_path = recipe_info["obs_path"]
@@ -486,7 +453,6 @@ def add_gridded_comparison(self,
486453
if not re.match("^[A-Za-z0-9]+$", name):
487454
raise ValueError("Name can only contain letters and numbers")
488455

489-
490456
if source is None:
491457
raise ValueError("Source must be supplied")
492458
if model_variable is None:
@@ -533,15 +499,14 @@ def add_gridded_comparison(self,
533499
short_title = name.title()
534500
assumed.append("short_title")
535501

536-
537502
if source_info is None:
538503
source_info = f"Source for {source}"
539504
assumed.append("source_info")
540505

541506
source_name = source
542507
source = {source: source_info}
543508
# ensure the sourc key does not included "_"
544-
if "_" in list(source_name)[0]:
509+
if "_" in source_name:
545510
raise ValueError("Source cannot contain '_'")
546511
if not isinstance(obs_variable, str):
547512
raise ValueError("obs_variable be provided")
@@ -580,9 +545,6 @@ def add_gridded_comparison(self,
580545
raise ValueError(f"Short title for {name} already exists as {session_info['short_title'][name]}, cannot change to {short_title}")
581546

582547

583-
584-
585-
586548
# Figure out if name is already
587549

588550
# figure out if self[name] exists already
@@ -761,6 +723,10 @@ def add_point_comparison(self,
761723
if name in session_info["short_title"]:
762724
if short_title != session_info["short_title"][name]:
763725
raise ValueError(f"Short title for {name} already exists as {session_info['short_title'][name]}, cannot change to {short_title}")
726+
727+
# check obs path exists
728+
if os.path.exists(obs_path) is False:
729+
raise ValueError(f"Observation path {obs_path} does not exist")
764730

765731
point_files = [f for f in glob.glob(os.path.join(obs_path, "*.csv"))]
766732
# if no files exists, raise error
@@ -800,7 +766,6 @@ def add_point_comparison(self,
800766
if not os.path.exists(point_dir):
801767
raise ValueError(f"Point directory {point_dir} does not exist")
802768

803-
804769
# figure out if self[name] exists already
805770
if getattr(self, name, None) is None:
806771
# add it
@@ -814,9 +779,7 @@ def add_point_comparison(self,
814779
self[name].thredds = None
815780
self[name].gridded_dir = None
816781
self[name].obs_variable = None
817-
818782
else:
819-
820783
# ensure short title is the same
821784
if short_title != session_info["short_title"][name]:
822785
raise ValueError(f"Short title for {name} already exists as {session_info['short_title'][name]}, cannot change to {short_title}")
@@ -831,7 +794,6 @@ def add_point_comparison(self,
831794
raise ValueError(f"Source {list(source.keys())[0]} already exists with a different value")
832795

833796
self[name].sources[source_name] = source_info
834-
835797
self[name].obs_multiplier_point= obs_multiplier
836798
self[name].obs_adder_point = obs_adder
837799
self[name].n_levels = 1
@@ -860,18 +822,14 @@ def add_point_comparison(self,
860822

861823
# figure out if var.binning exists
862824
self[name].binning = binning
863-
864825
#
865-
866826
for vv in assumed:
867827
print(f"Warning: The attribute {vv} was missing and was assumed for variable {name}")
868828
session_info["short_title"][name] = short_title
869829

870-
871830
definitions = Validator()
872831

873832

874-
875833
def generate_mapping(ds):
876834
"""
877835
Generate mapping of model and observational variables

0 commit comments

Comments
 (0)