Skip to content

Commit

Permalink
Merge pull request dailyerosion#311 from akrherz/241225
Browse files Browse the repository at this point in the history
🐛 Significant CLI refactor to fix grid nav issues
  • Loading branch information
akrherz authored Dec 30, 2024
2 parents f27af4f + 6fc98b5 commit b8dc46c
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 217 deletions.
15 changes: 7 additions & 8 deletions scripts/RT/env2database.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import argparse
import datetime
import gzip
import os
import re
import sys
Expand All @@ -18,17 +17,15 @@
import geopandas as gpd
import numpy as np
import pandas as pd
from affine import Affine
import rasterio
from pyiem.grid.zs import CachingZonalStats
from pyiem.iemre import NORTH, WEST
from pyiem.util import get_dbconn, get_dbconnstr, logger
from tqdm import tqdm

from pydep.io.dep import read_env
from pydep.util import load_scenarios

LOG = logger()
PRECIP_AFF = Affine(0.01, 0.0, WEST, 0.0, -0.01, NORTH)
CONFIG = {"subset": False}

# Maximum precip value allowed, will alert otherwise, see dailyerosion/dep#65
Expand Down Expand Up @@ -147,21 +144,23 @@ def load_precip(dates, huc12s):
if CONFIG["subset"]:
huc12df = huc12df.loc[huc12s]

czs = CachingZonalStats(PRECIP_AFF)
czs = None
# 2. Loop over dates
res = {}
progress = tqdm(dates, disable=(not sys.stdout.isatty()))
for date in progress:
progress.set_description(date.strftime("%Y-%m-%d"))
fn = date.strftime("/mnt/idep2/data/dailyprecip/%Y/%Y%m%d.npy.gz")
fn = date.strftime("/mnt/idep2/data/dailyprecip/%Y/%Y%m%d.geotiff")
if not os.path.isfile(fn):
LOG.info("Missing precip: %s", fn)
for huc12 in huc12df.index.values:
d = res.setdefault(huc12, [])
d.append(0)
continue
with gzip.GzipFile(fn, "r") as fh:
pcp = np.flipud(np.load(file=fh))
with rasterio.open(fn, "r") as ds:
if czs is None:
czs = CachingZonalStats(ds.transform)
pcp = ds.read(1) * ds.scales[0]
# nodata here represents the value that is set to missing within the
# source dataset!, setting to zero has strange side affects
pcp[pcp < 0] = np.nan
Expand Down
Loading

0 comments on commit b8dc46c

Please sign in to comment.