Skip to content

Commit

Permalink
Qol bugfixes (#36)
Browse files Browse the repository at this point in the history
* update hydrolocation crs to match v20.1 gpkg

* format realization to be easier to read

* automatically subset before forcing or realization creation if needed

* add error message for watershed not found

* fix subset Always running
  • Loading branch information
JoshCu authored Aug 8, 2024
1 parent 5bf98cb commit 8f79335
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
2 changes: 1 addition & 1 deletion modules/data_processing/create_realization.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def make_ngen_realization_json(
realization["time"]["nts"] = nts

with open(config_dir / "realization.json", "w") as file:
json.dump(realization, file)
json.dump(realization, file, indent=4)


def create_realization(wb_id: str, start_time: datetime, end_time: datetime):
Expand Down
2 changes: 2 additions & 0 deletions modules/data_processing/gpkg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ def get_wbid_from_point(coords):
d = {"col1": ["point"], "geometry": [Point(coords["lng"], coords["lat"])]}
point = gpd.GeoDataFrame(d, crs="EPSG:4326")
df = gpd.read_file(q, format="GPKG", layer="divides", mask=point)
if df.empty:
raise IndexError(f"No watershed boundary found for {coords}")
return df["id"].values[0]


Expand Down
Binary file modified modules/data_sources/template.gpkg
Binary file not shown.
57 changes: 31 additions & 26 deletions modules/ngiab_data_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
class ColoredFormatter(logging.Formatter):
def format(self, record):
message = super().format(record)
if record.name == "root": # Only color messages from this script
return f"{Fore.GREEN}{message}{Style.RESET_ALL}"
# if debug level, color the message blue
if record.levelno == logging.DEBUG:
return f"{Fore.BLUE}{message}{Style.RESET_ALL}"
if record.levelno == logging.WARNING:
return f"{Fore.YELLOW}{message}{Style.RESET_ALL}"
if record.name == "root": # Only color info messages from this script green
return f"{Fore.GREEN}{message}{Style.RESET_ALL}"
return message


Expand Down Expand Up @@ -123,9 +124,9 @@ def validate_input(args: argparse.Namespace) -> None:
if not any([args.subset, args.forcings, args.realization]):
raise ValueError("At least one of --subset, --forcings, or --realization must be set.")

if args.subset and not args.input_file:
if not args.input_file:
raise ValueError(
"Input file or single wb-id/gage-id is required for subsetting. e.g. -i wb_ids.txt or -i wb-5173 or -i 01646500 -g"
"Input file or single wb-id/gage-id is required. e.g. -i wb_ids.txt or -i wb-5173 or -i 01646500 -g"
)

if (args.forcings or args.realization) and not (args.start_date and args.end_date):
Expand All @@ -136,6 +137,27 @@ def validate_input(args: argparse.Namespace) -> None:
if args.latlon and args.gage:
raise ValueError("Cannot use both --latlon and --gage options at the same time.")

input_file = Path(args.input_file)
if args.latlon:
waterbody_ids = get_wb_ids_from_lat_lon(input_file)
elif args.gage:
waterbody_ids = get_wb_ids_from_gage_ids(input_file)
else:
waterbody_ids = read_waterbody_ids(input_file)
logging.info(f"Read {len(waterbody_ids)} waterbody IDs from {input_file}")

wb_id_for_name = args.output_name or (waterbody_ids[0] if waterbody_ids else None)
if not wb_id_for_name:
raise ValueError("No waterbody input file or output folder provided.")

if not args.subset and (args.forcings or args.realization):
if not file_paths(wb_id_for_name).subset_dir().exists():
logging.warning(
"Forcings and realization creation require subsetting at least once. Automatically enabling subset for this run."
)
args.subset = True
return wb_id_for_name, waterbody_ids


def read_csv(input_file: Path) -> List[str]:
"""Read waterbody IDs from a CSV file."""
Expand Down Expand Up @@ -278,32 +300,15 @@ def main() -> None:

try:
args = parse_arguments()
validate_input(args)

if args.debug:
logging.getLogger("data_processing").setLevel(logging.DEBUG)

if args.input_file:
input_file = Path(args.input_file)
if args.latlon:
waterbody_ids = get_wb_ids_from_lat_lon(input_file)
elif args.gage:
waterbody_ids = get_wb_ids_from_gage_ids(input_file)
else:
waterbody_ids = read_waterbody_ids(input_file)
logging.info(f"Read {len(waterbody_ids)} waterbody IDs from {input_file}")
else:
waterbody_ids = []

wb_id_for_name = args.output_name or (waterbody_ids[0] if waterbody_ids else None)
if not wb_id_for_name:
raise ValueError("No waterbody input file or output folder provided.")

wb_id_for_name, waterbody_ids = validate_input(args)
paths = file_paths(wb_id_for_name)
output_folder = paths.subset_dir()
output_folder.mkdir(parents=True, exist_ok=True)
logging.info(f"Using output folder: {output_folder}")

if args.debug:
logging.getLogger("data_processing").setLevel(logging.DEBUG)

if args.subset:
logging.info(f"Subsetting hydrofabric for {len(waterbody_ids)} waterbody IDs...")
subset(waterbody_ids, subset_name=wb_id_for_name)
Expand Down

0 comments on commit 8f79335

Please sign in to comment.