Skip to content

Commit 42c6b11

Browse files
committed
Increase test coverage
1 parent d50d14d commit 42c6b11

25 files changed

+1071
-418
lines changed

disdrodb/api/create_directories.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,6 @@ def create_initial_station_structure(
415415
data_source=data_source,
416416
campaign_name=campaign_name,
417417
station_name=station_name,
418-
product="RAW",
419418
)
420419
# Add default station issue file
421420
create_station_issue(
@@ -440,7 +439,7 @@ def create_test_archive(test_base_dir, data_source, campaign_name, station_name,
440439
This enable to then test data download and DISDRODB processing.
441440
"""
442441
# Check test_base_dir is not equal to true base_dir
443-
if test_base_dir == get_base_dir():
442+
if test_base_dir == get_base_dir(base_dir):
444443
raise ValueError("Provide a test_base_dir directory different from the true DISDRODB base directory !")
445444

446445
# Create test DISDRODB base directory

disdrodb/data_transfer/scripts/disdrodb_upload_station.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
import click
2424

25-
from disdrodb.data_transfer.upload_data import click_base_dir_option, click_upload_options
26-
from disdrodb.utils.scripts import click_station_arguments
25+
from disdrodb.data_transfer.upload_data import click_upload_options
26+
from disdrodb.utils.scripts import click_base_dir_option, click_station_arguments
2727

2828
sys.tracebacklimit = 0 # avoid full traceback error if occur
2929

disdrodb/data_transfer/upload_data.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ def _filter_already_uploaded(metadata_filepaths: List[str], force: bool) -> List
111111
return filtered
112112

113113

114+
def _check_valid_platform(platform):
115+
"""Check upload platform validity."""
116+
valid_platform = ["zenodo", "sandbox.zenodo"]
117+
if platform not in valid_platform:
118+
raise NotImplementedError(f"Invalid platform {platform}. Valid platforms are {valid_platform}.")
119+
120+
114121
def upload_station(
115122
data_source: str,
116123
campaign_name: str,
@@ -145,6 +152,8 @@ def upload_station(
145152
The default is force=False.
146153
147154
"""
155+
_check_valid_platform(platform)
156+
148157
# Define metadata_filepath
149158
metadata_filepath = define_metadata_filepath(
150159
data_source=data_source,
@@ -162,11 +171,8 @@ def upload_station(
162171
if platform == "zenodo":
163172
upload_station_to_zenodo(metadata_filepath, sandbox=False)
164173

165-
elif platform == "sandbox.zenodo": # Only for testing purposes, not available through CLI
174+
else: # platform == "sandbox.zenodo": # Only for testing purposes, not available through CLI
166175
upload_station_to_zenodo(metadata_filepath, sandbox=True)
167-
else:
168-
valid_platform = ["zenodo", "sandbox.zenodo"]
169-
raise NotImplementedError(f"Invalid platform {platform}. Valid platforms are {valid_platform}.")
170176

171177

172178
def upload_archive(
@@ -206,6 +212,8 @@ def upload_archive(
206212
If not provided (None), all stations will be uploaded.
207213
The default is station_name=None.
208214
"""
215+
_check_valid_platform(platform)
216+
209217
# Get list metadata
210218
metadata_filepaths = get_list_metadata(
211219
**kwargs,

disdrodb/data_transfer/zenodo.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,20 @@ def _define_creators_list(metadata):
151151
list_names = re.split(";|,", metadata["authors"])
152152
list_identifier = re.split(";|,", metadata["authors_url"])
153153
list_affiliation = re.split(";|,", metadata["institution"])
154+
155+
# Check identifier fields match the number of specified authors
156+
# - If not, set identifier to ""
154157
if len(list_names) != len(list_identifier):
155-
# print("Impossible to automatically assign identifier to authors. Length mismatch.")
156158
list_identifier = [""] * len(list_names)
159+
160+
# If affiliation is only one --> Assume is the same for everyone
157161
if len(list_affiliation) == 1:
158162
list_affiliation = list_affiliation * len(list_names)
163+
# If more than one affiliation, but does not match list of authors, set to ""
164+
if len(list_affiliation) > 1 and len(list_affiliation) != len(list_names):
165+
list_affiliation = [""] * len(list_names)
166+
167+
# Create info dictionary of each author
159168
list_creator_dict = []
160169
for name, identifier, affiliation in zip(list_names, list_identifier, list_affiliation):
161170
creator_dict = {}
@@ -224,7 +233,7 @@ def upload_station_to_zenodo(metadata_filepath: str, sandbox: bool = True) -> st
224233
os.remove(station_zip_filepath)
225234
except Exception as e:
226235
os.remove(station_zip_filepath)
227-
raise ValueError(f"The upload on Zenodo has failed: {e}.")
236+
raise ValueError(f"{station_zip_filepath} The upload on Zenodo has failed: {e}.")
228237

229238
# Add the disdrodb_data_url information to the metadata
230239
print(" - The station metadata 'disdrodb_data_url' key has been updated with the remote url")

disdrodb/issue/writer.py

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _write_issue_docs(f):
5050
return None
5151

5252

53-
def _write_issue(filepath: str, timesteps: list = None, time_periods: list = None) -> None:
53+
def write_issue(filepath: str, timesteps: list = None, time_periods: list = None) -> None:
5454
"""Write the issue YAML file.
5555
5656
Parameters
@@ -92,35 +92,6 @@ def _write_issue(filepath: str, timesteps: list = None, time_periods: list = Non
9292
return None
9393

9494

95-
def write_issue_dict(filepath: str, issue_dict: dict) -> None:
96-
"""Write the issue YAML file.
97-
98-
Parameters
99-
----------
100-
filepath : str
101-
Filepath of the issue YAML to write.
102-
issue_dict : dict
103-
Issue dictionary
104-
"""
105-
_write_issue(
106-
filepath=filepath,
107-
timesteps=issue_dict.get("timesteps", None),
108-
time_periods=issue_dict.get("time_periods", None),
109-
)
110-
111-
112-
def write_default_issue(filepath: str) -> None:
113-
"""Write an empty issue YAML file.
114-
115-
Parameters
116-
----------
117-
filepath : str
118-
Filepath of the issue YAML to write.
119-
"""
120-
_write_issue(filepath=filepath)
121-
return None
122-
123-
12495
def create_station_issue(data_source, campaign_name, station_name, base_dir=None):
12596
"""Write an empty YAML issue YAML file for a DISDRODB station.
12697
@@ -155,6 +126,6 @@ def create_station_issue(data_source, campaign_name, station_name, base_dir=None
155126
issue_dir = os.path.dirname(issue_filepath)
156127
os.makedirs(issue_dir, exist_ok=True)
157128
# Write issue file
158-
write_default_issue(filepath=issue_filepath)
129+
write_issue(filepath=issue_filepath)
159130
print(f"An empty issue YAML file for station {station_name} has been created .")
160131
return None

disdrodb/l0/l0_processing.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import functools
2323
import logging
2424
import os
25+
import shutil
2526
import time
2627

2728
import dask
@@ -39,6 +40,7 @@
3940
define_l0a_filepath,
4041
define_l0b_filepath,
4142
define_l0b_station_dir,
43+
define_station_dir,
4244
get_disdrodb_path,
4345
)
4446
from disdrodb.configs import get_base_dir
@@ -785,7 +787,7 @@ def run_l0b_from_nc(
785787
return None
786788

787789

788-
def run_l0b_concat(processed_dir, station_name, remove=False, verbose=False):
790+
def run_l0b_concat(processed_dir, station_name, verbose=False):
789791
"""Concatenate all L0B netCDF files into a single netCDF file.
790792
791793
The single netCDF file is saved at <processed_dir>/L0B.
@@ -836,13 +838,6 @@ def run_l0b_concat(processed_dir, station_name, remove=False, verbose=False):
836838
ds.close()
837839
del ds
838840

839-
# -------------------------------------------------------------------------.
840-
# If remove = True, remove all the single files
841-
if remove:
842-
log_info(logger=logger, msg="Removal of single L0B files started.", verbose=verbose)
843-
_ = [os.remove(filepath) for filepath in filepaths]
844-
log_info(logger=logger, msg="Removal of single L0B files ended.", verbose=verbose)
845-
846841
# -------------------------------------------------------------------------.
847842
# Close the file logger
848843
close_logger(logger)
@@ -948,6 +943,7 @@ def run_l0b_station(
948943
verbose: bool = True,
949944
parallel: bool = True,
950945
debugging_mode: bool = False,
946+
remove_l0a: bool = False,
951947
base_dir: str = None,
952948
):
953949
"""
@@ -1005,6 +1001,18 @@ def run_l0b_station(
10051001
parallel=parallel,
10061002
)
10071003

1004+
if remove_l0a:
1005+
station_dir = define_station_dir(
1006+
base_dir=base_dir,
1007+
product="L0A",
1008+
data_source=data_source,
1009+
campaign_name=campaign_name,
1010+
station_name=station_name,
1011+
)
1012+
log_info(logger=logger, msg="Removal of single L0A files started.", verbose=verbose)
1013+
shutil.rmtree(station_dir)
1014+
log_info(logger=logger, msg="Removal of single L0A files ended.", verbose=verbose)
1015+
10081016

10091017
def run_l0b_concat_station(
10101018
# Station arguments
@@ -1053,9 +1061,20 @@ def run_l0b_concat_station(
10531061
run_l0b_concat(
10541062
processed_dir=processed_dir,
10551063
station_name=station_name,
1056-
remove=remove_l0b,
10571064
verbose=verbose,
10581065
)
10591066

1067+
if remove_l0b:
1068+
station_dir = define_station_dir(
1069+
base_dir=base_dir,
1070+
product="L0B",
1071+
data_source=data_source,
1072+
campaign_name=campaign_name,
1073+
station_name=station_name,
1074+
)
1075+
log_info(logger=logger, msg="Removal of single L0B files started.", verbose=verbose)
1076+
shutil.rmtree(station_dir)
1077+
log_info(logger=logger, msg="Removal of single L0B files ended.", verbose=verbose)
1078+
10601079

10611080
####---------------------------------------------------------------------------.

disdrodb/l0/routines.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
import datetime
2222
import logging
23-
import os
24-
import shutil
2523
import time
2624

2725
import click
@@ -107,6 +105,17 @@ def click_l0_processing_options(function: object):
107105
return function
108106

109107

108+
def click_remove_l0a_option(function: object):
109+
function = click.option(
110+
"--remove_l0a",
111+
type=bool,
112+
show_default=True,
113+
default=False,
114+
help="If true, remove the L0A files once the L0B processing is terminated.",
115+
)(function)
116+
return function
117+
118+
110119
def click_l0_archive_options(function: object):
111120
"""Click command line arguments for L0 processing archiving of a station.
112121
@@ -228,6 +237,7 @@ def run_disdrodb_l0b_station(
228237
debugging_mode: bool = False,
229238
parallel: bool = True,
230239
base_dir: str = None,
240+
remove_l0a: bool = False,
231241
):
232242
"""Run the L0B processing of a station calling disdrodb_run_l0b_station in the terminal."""
233243
# Define command
@@ -247,6 +257,8 @@ def run_disdrodb_l0b_station(
247257
str(debugging_mode),
248258
"--parallel",
249259
str(parallel),
260+
"--remove_l0a",
261+
str(remove_l0a),
250262
"--base_dir",
251263
str(base_dir),
252264
]
@@ -358,7 +370,6 @@ def run_disdrodb_l0_station(
358370
Base directory of DISDRODB. Format: <...>/DISDRODB
359371
If None (the default), the disdrodb config variable 'dir' is used.
360372
"""
361-
from disdrodb.api.path import get_disdrodb_path
362373

363374
# ---------------------------------------------------------------------.
364375
t_i = time.time()
@@ -394,20 +405,9 @@ def run_disdrodb_l0_station(
394405
verbose=verbose,
395406
debugging_mode=debugging_mode,
396407
parallel=parallel,
408+
remove_l0a=remove_l0a,
397409
)
398410

399-
# ------------------------------------------------------------------------.
400-
# Remove L0A station directory if remove_l0a = True and l0b_processing = True
401-
if l0b_processing and remove_l0a:
402-
campaign_dir = get_disdrodb_path(
403-
base_dir=base_dir,
404-
product="L0A",
405-
data_source=data_source,
406-
campaign_name=campaign_name,
407-
)
408-
station_product_dir = os.path.join(campaign_dir, "L0A", station_name)
409-
shutil.rmtree(station_product_dir)
410-
411411
# ------------------------------------------------------------------------.
412412
# If l0b_concat=True, concat the netCDF in a single file
413413
if l0b_concat:
@@ -656,6 +656,7 @@ def run_disdrodb_l0b(
656656
debugging_mode: bool = False,
657657
parallel: bool = True,
658658
base_dir: str = None,
659+
remove_l0a: bool = False,
659660
):
660661
run_disdrodb_l0(
661662
base_dir=base_dir,
@@ -666,7 +667,7 @@ def run_disdrodb_l0b(
666667
l0a_processing=False,
667668
l0b_processing=True,
668669
l0b_concat=False,
669-
remove_l0a=False,
670+
remove_l0a=remove_l0a,
670671
remove_l0b=False,
671672
# Processing options
672673
force=force,
@@ -676,6 +677,7 @@ def run_disdrodb_l0b(
676677
)
677678

678679

680+
####---------------------------------------------------------------------------.
679681
def run_disdrodb_l0b_concat(
680682
data_sources=None,
681683
campaign_names=None,

disdrodb/l0/scripts/disdrodb_run_l0b.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from disdrodb.l0.routines import (
2323
click_l0_processing_options,
2424
click_l0_stations_options,
25+
click_remove_l0a_option,
2526
)
2627
from disdrodb.utils.scripts import click_base_dir_option, parse_arg_to_list
2728

@@ -31,6 +32,7 @@
3132
@click.command()
3233
@click_l0_stations_options
3334
@click_l0_processing_options
35+
@click_remove_l0a_option
3436
@click_base_dir_option
3537
def disdrodb_run_l0b(
3638
# L0 disdrodb stations options
@@ -42,6 +44,7 @@ def disdrodb_run_l0b(
4244
verbose: bool = True,
4345
parallel: bool = True,
4446
debugging_mode: bool = False,
47+
remove_l0a: bool = False,
4548
base_dir: str = None,
4649
):
4750
"""
@@ -107,6 +110,7 @@ def disdrodb_run_l0b(
107110
verbose=verbose,
108111
debugging_mode=debugging_mode,
109112
parallel=parallel,
113+
remove_l0a=remove_l0a,
110114
)
111115

112116
return None

0 commit comments

Comments
 (0)