Skip to content

Commit

Permalink
Refactoring and add create_initial_station_directories utility
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiggi committed Nov 29, 2023
1 parent 3fd6f38 commit 79d53e5
Show file tree
Hide file tree
Showing 32 changed files with 598 additions and 240 deletions.
1 change: 1 addition & 0 deletions .codebeatignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
disdrodb/l0/readers/*
4 changes: 2 additions & 2 deletions disdrodb/api/check_directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def _check_presence_metadata_directory(raw_dir):
If the 'metadata' does not exists, it create default metadata files
for each station present in the 'data' directory.
"""
from disdrodb.metadata.io import write_default_metadata
from disdrodb.metadata.writer import write_default_metadata

if not _is_metadata_directory_available(raw_dir):
# Create metadata directory
Expand Down Expand Up @@ -268,7 +268,7 @@ def _check_presence_all_metadata_files(raw_dir):
The function raise error if there is not a metadata file for each station
directory present in the 'data' directory.
"""
from disdrodb.metadata.io import write_default_metadata
from disdrodb.metadata.writer import write_default_metadata

# Get stations with available metadata
list_metadata_station_name = _get_available_stations_with_metadata_files(raw_dir)
Expand Down
113 changes: 103 additions & 10 deletions disdrodb/api/create_directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from disdrodb.api.info import infer_campaign_name_from_path, infer_data_source_from_path
from disdrodb.api.path import (
define_campaign_dir,
define_issue_dir,
define_issue_filepath,
define_metadata_dir,
Expand Down Expand Up @@ -208,6 +209,7 @@ def create_issue_directory(base_dir, data_source, campaign_name):
return str(issue_dir)


#### L0A processing
def create_initial_directory_structure(
raw_dir,
processed_dir,
Expand Down Expand Up @@ -258,6 +260,7 @@ def create_initial_directory_structure(
create_required_directory(os.path.join(processed_dir, product), dir_name=station_name)


#### > L0A processing
def create_directory_structure(processed_dir, product, station_name, force, verbose=False):
"""Create directory structure for L0B and higher DISDRODB products."""
from disdrodb.api.check_directories import check_presence_metadata_file, check_processed_dir
Expand Down Expand Up @@ -295,29 +298,116 @@ def create_directory_structure(processed_dir, product, station_name, force, verb
)


def create_test_archive(test_base_dir, data_source, campaign_name, station_name, force=False):
#### Initialization
def _create_station_directories(
data_source,
campaign_name,
station_name,
product="RAW",
base_dir=None,
):
"""Create the /metadata, /issue and /data/<station_name> directories of a station."""
# Create directory structure
_ = create_station_directory(
base_dir=base_dir,
product=product,
data_source=data_source,
campaign_name=campaign_name,
station_name=station_name,
)
_ = create_metadata_directory(
base_dir=base_dir, product=product, data_source=data_source, campaign_name=campaign_name
)

if product.upper() == "RAW":
_ = create_issue_directory(base_dir=base_dir, data_source=data_source, campaign_name=campaign_name)


def create_initial_station_structure(
data_source,
campaign_name,
station_name,
base_dir=None,
):
"""Create the DISDRODB Data Archive structure for a single station."""
from disdrodb.issue.writer import create_station_issue
from disdrodb.metadata.writer import create_station_metadata

# Check if already been defined
# - Check presence of metadata file
metadata_filepath = define_metadata_filepath(
base_dir=base_dir,
data_source=data_source,
campaign_name=campaign_name,
station_name=station_name,
product="RAW",
check_exists=False,
)
if os.path.exists(metadata_filepath):
raise ValueError(
f"A metadata file already exists at {metadata_filepath}. "
"The station is already part of the DISDRODB Archive or "
"or you already initialized the directory structure for the station !"
)

# Create directory structure (/metadata, /issue and /data/<station_name>)
_create_station_directories(
base_dir=base_dir,
data_source=data_source,
campaign_name=campaign_name,
station_name=station_name,
product="RAW",
)

# Add default station metadata file
create_station_metadata(
base_dir=base_dir,
data_source=data_source,
campaign_name=campaign_name,
station_name=station_name,
product="RAW",
)
# Add default station issue file
create_station_issue(
base_dir=base_dir,
data_source=data_source,
campaign_name=campaign_name,
station_name=station_name,
)

# Report location of the campaign directory
campaign_dir = define_campaign_dir(
base_dir=base_dir, data_source=data_source, campaign_name=campaign_name, product="RAW"
)
print(f"Initial station directory structure created at: {campaign_dir}")


#### Testing upload/download
def create_test_archive(test_base_dir, data_source, campaign_name, station_name, base_dir=None, force=False):
"""Create test DISDRODB Archive for a single station."""
# Check test_base_dir is not equal to true base_dir
if test_base_dir == get_base_dir():
raise ValueError("Provide a test_base_dir directory different from the true DISDRODB base directory !")

# Create test DISDRODB base directory
remove_if_exists(test_base_dir, force=force)
os.makedirs(test_base_dir, exist_ok=True)
# Create directory structure
_ = create_station_directory(

# Create directories (/metadata, /issue and /data/<station_name>)
_create_station_directories(
base_dir=test_base_dir,
product="RAW",
data_source=data_source,
campaign_name=campaign_name,
station_name=station_name,
)
_ = create_metadata_directory(
base_dir=test_base_dir, product="RAW", data_source=data_source, campaign_name=campaign_name
)
_ = create_issue_directory(base_dir=test_base_dir, data_source=data_source, campaign_name=campaign_name)

# Copy metadata and issue files in the test archive
src_metadata_fpath = define_metadata_filepath(
product="RAW", data_source=data_source, campaign_name=campaign_name, station_name=station_name
product="RAW",
data_source=data_source,
campaign_name=campaign_name,
station_name=station_name,
base_dir=base_dir,
)
dst_metadata_fpath = define_metadata_filepath(
product="RAW",
Expand All @@ -327,7 +417,10 @@ def create_test_archive(test_base_dir, data_source, campaign_name, station_name,
base_dir=test_base_dir,
)
src_issue_fpath = define_issue_filepath(
data_source=data_source, campaign_name=campaign_name, station_name=station_name
data_source=data_source,
campaign_name=campaign_name,
station_name=station_name,
base_dir=base_dir,
)
dst_issue_fpath = define_issue_filepath(
data_source=data_source, campaign_name=campaign_name, station_name=station_name, base_dir=test_base_dir
Expand Down
67 changes: 67 additions & 0 deletions disdrodb/api/scripts/disdrodb_initialize_station.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -----------------------------------------------------------------------------.
# Copyright (c) 2021-2023 DISDRODB developers
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------.
import sys

import click

from disdrodb.utils.scripts import click_station_arguments, click_base_dir_option

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

# -------------------------------------------------------------------------.
# Click Command Line Interface decorator


@click.command()
@click_station_arguments
@click_base_dir_option
def disdrodb_initialize_station(
# Station arguments
data_source: str,
campaign_name: str,
station_name: str,
# Processing options
base_dir: str = None,
):
"""Initialize the DISDRODB directory structure for a station.
It adds the relevant directories and the default issue and metadata YAML files..
Parameters \n
---------- \n
data_source : str \n
Institution name (when campaign data spans more than 1 country), or country (when all campaigns (or sensor
networks) are inside a given country).\n
Must be UPPER CASE.\n
campaign_name : str \n
Campaign name. Must be UPPER CASE.\n
station_name : str \n
Station name \n
base_dir : str \n
Base directory of DISDRODB \n
Format: <...>/DISDRODB \n
If not specified, uses path specified in the DISDRODB active configuration. \n
"""
from disdrodb.l0.routines import create_initial_station_structure

create_initial_station_structure(
base_dir=base_dir,
data_source=data_source,
campaign_name=campaign_name,
station_name=station_name,
)
return None
10 changes: 2 additions & 8 deletions disdrodb/data_transfer/download_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,10 @@ def click_download_options(function: object):
"--force",
type=bool,
show_default=True,
default=True,
default=False,
help="Force overwriting",
)(function)
function = click.option(
"--base_dir",
type=str,
show_default=True,
default=None,
help="DISDRODB base directory",
)(function)

return function


Expand Down
12 changes: 7 additions & 5 deletions disdrodb/data_transfer/scripts/disdrodb_download_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@
import click

from disdrodb.data_transfer.download_data import click_download_archive_options, click_download_options
from disdrodb.utils.scripts import click_base_dir_option

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


@click.command()
@click_download_archive_options
@click_base_dir_option
@click_download_options
def disdrodb_download_archive(
data_sources=None,
campaign_names=None,
station_names=None,
base_dir=None,
force=True,
data_sources: str = None,
campaign_names: str = None,
station_names: str = None,
base_dir: str = None,
force: bool = False,
):
from disdrodb.data_transfer.download_data import download_archive
from disdrodb.utils.scripts import parse_arg_to_list
Expand Down
16 changes: 9 additions & 7 deletions disdrodb/data_transfer/scripts/disdrodb_download_station.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@
import sys

import click
from disdrodb.utils.scripts import click_station_arguments

from disdrodb.data_transfer.download_data import click_download_options
from disdrodb.utils.scripts import click_base_dir_option, click_station_arguments

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


@click.command()
@click_station_arguments
@click_base_dir_option
@click_download_options
def disdrodb_download_station(
data_source,
campaign_name,
station_name,
base_dir=None,
force=False,
data_source: str,
campaign_name: str,
station_name: str,
base_dir: str = None,
force: bool = False,
):
from disdrodb.data_transfer.download_data import download_station

Expand All @@ -45,4 +47,4 @@ def disdrodb_download_station(
campaign_name=campaign_name,
station_name=station_name,
force=force,
)
)
14 changes: 8 additions & 6 deletions disdrodb/data_transfer/scripts/disdrodb_upload_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@
import click

from disdrodb.data_transfer.upload_data import click_upload_archive_options, click_upload_options
from disdrodb.utils.scripts import click_base_dir_option

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


@click.command()
@click_upload_archive_options
@click_upload_options
@click_base_dir_option
def disdrodb_upload_archive(
base_dir=None,
data_sources=None,
campaign_names=None,
station_names=None,
platform=None,
force=False,
base_dir: str = None,
data_sources: str = None,
campaign_names: str = None,
station_names: str = None,
platform: str = None,
force: bool = False,
):
from disdrodb.data_transfer.upload_data import upload_archive
from disdrodb.utils.scripts import parse_arg_to_list
Expand Down
15 changes: 8 additions & 7 deletions disdrodb/data_transfer/scripts/disdrodb_upload_station.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import click

from disdrodb.data_transfer.upload_data import click_upload_options
from disdrodb.data_transfer.upload_data import click_base_dir_option, click_upload_options
from disdrodb.utils.scripts import click_station_arguments

sys.tracebacklimit = 0 # avoid full traceback error if occur
Expand All @@ -31,13 +31,14 @@
@click.command()
@click_station_arguments
@click_upload_options
@click_base_dir_option
def disdrodb_upload_station(
data_source,
campaign_name,
station_name,
platform=None,
base_dir=None,
force=False,
data_source: str,
campaign_name: str,
station_name: str,
platform: str = None,
base_dir: str = None,
force: bool = False,
):
from disdrodb.data_transfer.upload_data import upload_station

Expand Down
Loading

0 comments on commit 79d53e5

Please sign in to comment.