Skip to content

Commit 475dda8

Browse files
Merge pull request #244 from CIROH-UA/test_nrds_fp
add tests for full conus nrds forcingprocessing
2 parents 5cacfc6 + 2b1f0c2 commit 475dda8

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# This workflow will install Python dependencies, run tests and lint with a single version of Python
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
3+
4+
name: Test Forcing Processor for NRDS
5+
on:
6+
workflow_dispatch:
7+
push:
8+
branches:
9+
- main
10+
paths:
11+
- 'forcingprocessor/**'
12+
- '!forcingprocessor/README.md'
13+
pull_request:
14+
branches:
15+
- main
16+
paths:
17+
- 'forcingprocessor/**'
18+
- '!forcingprocessor/README.md'
19+
20+
permissions:
21+
contents: read
22+
jobs:
23+
test-forcingprocessor-nrds:
24+
runs-on: ubuntu-latest
25+
26+
steps:
27+
- uses: actions/checkout@v3
28+
29+
- name: Extract Python version from setup.cfg
30+
id: pyver
31+
run: |
32+
# Read python_requires from setup.cfg
33+
VERSION=$(awk -F'>=' '/^python_requires/ {print $2}' forcingprocessor/setup.cfg | tr -d ' ')
34+
echo "version=$VERSION" >> $GITHUB_OUTPUT
35+
36+
- name: Set up Python
37+
uses: actions/setup-python@v5
38+
with:
39+
python-version: ${{ steps.pyver.outputs.version }}
40+
41+
- name: Configure AWS
42+
run: |
43+
aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }}
44+
aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }}
45+
aws configure set region us-east-1
46+
47+
- name: Install dependencies
48+
run: |
49+
python -m pip install --upgrade pip
50+
pip install -e ./forcingprocessor
51+
pip install pytest
52+
53+
- name: Test with pytest
54+
run: |
55+
cd forcingprocessor
56+
python -m pytest -vv tests/test_nrds_fp.py

forcingprocessor/src/forcingprocessor/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
"DSWRF_surface",
2828
]
2929

30+
vpus = ["01","02","03W","03S","03N","04","05","06","07","08","09","10L","10U","11","12","13","14","15","16","17","18"]
31+
3032
def get_window(weights_df):
3133
"""
3234
Providing window on weights for which number of catchments is over 50,000

forcingprocessor/tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pytest, os
2+
3+
@pytest.fixture(scope="session")
4+
def clean_s3_test():
5+
yield
6+
os.system("aws s3 rm s3://ciroh-community-ngen-datastream/test/nrds_fp_test/ --recursive")
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# test_nrds_fp.py
2+
#
3+
# Author: Jordan Laser <jlaser@lynker.com>
4+
#
5+
#
6+
# Test the NRDS forcing processing by inputing all 21 VPU's weight files,
7+
# processing a single nwm forcing file, and the writing to a test location in the producting bucket.
8+
9+
import shutil, os
10+
from pathlib import Path
11+
from datetime import datetime, timedelta, timezone
12+
from forcingprocessor.processor import prep_ngen_data
13+
from forcingprocessor.nwm_filenames_generator import generate_nwmfiles
14+
import pytest
15+
from forcingprocessor.utils import vpus
16+
import boto3
17+
from botocore.exceptions import ClientError
18+
import re
19+
20+
HF_VERSION="v2.2"
21+
TODAY = datetime.now(timezone.utc)
22+
TODAY_YYMMDD = TODAY.strftime('%Y%m%d')
23+
hourminute = '0000'
24+
TODAY_YYMMDDHHMM = TODAY_YYMMDD + hourminute
25+
YESTERDAY = TODAY - timedelta(hours=24)
26+
YESTERDAY_YYMMDD = YESTERDAY.strftime('%Y%m%d')
27+
YESTERDAY_YYMMDDHHMM = YESTERDAY_YYMMDD + hourminute
28+
test_dir = Path(__file__).parent
29+
data_dir = (test_dir/'data').resolve()
30+
forcings_dir = (data_dir/'forcings').resolve()
31+
pwd = Path.cwd()
32+
data_dir = data_dir
33+
if os.path.exists(data_dir):
34+
os.system(f"rm -rf {data_dir}")
35+
os.system(f"mkdir {data_dir}")
36+
pwd = Path.cwd()
37+
filenamelist = str((pwd/"filenamelist.txt").resolve())
38+
39+
weight_files = [f"https://ciroh-community-ngen-datastream.s3.amazonaws.com/v2.2_resources/weights/nextgen_VPU_{x}_weights.json" for x in vpus]
40+
local_weight_files = [str((data_dir/f"nextgen_VPU_{x}_weights.json").resolve()) for x in vpus]
41+
42+
# download weight files
43+
for j, wf in enumerate(weight_files):
44+
local_file = local_weight_files[j]
45+
if not os.path.exists(local_file):
46+
os.system(f"wget {wf} -P {data_dir}")
47+
48+
conf = {
49+
"forcing" : {
50+
"nwm_file" : filenamelist,
51+
"gpkg_file" : local_weight_files
52+
},
53+
54+
"storage":{
55+
"output_path" : "s3://ciroh-community-ngen-datastream/test/nrds_fp_test",
56+
"output_file_type" : ["netcdf"]
57+
},
58+
59+
"run" : {
60+
"verbose" : False,
61+
"collect_stats" : False,
62+
"nprocs" : 1
63+
}
64+
}
65+
66+
nwmurl_conf = {
67+
"forcing_type" : "operational_archive",
68+
"start_date" : YESTERDAY_YYMMDDHHMM,
69+
"end_date" : YESTERDAY_YYMMDDHHMM,
70+
"runinput" : 1,
71+
"varinput" : 5,
72+
"geoinput" : 1,
73+
"meminput" : 0,
74+
"urlbaseinput" : 7,
75+
"fcst_cycle" : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18],
76+
"lead_time" : [1]
77+
}
78+
79+
s3 = boto3.client("s3")
80+
81+
@pytest.fixture(autouse=True)
82+
def clean_dir():
83+
if os.path.exists(forcings_dir):
84+
shutil.rmtree(forcings_dir)
85+
86+
def s3_object_exists(url: str) -> bool:
87+
m = re.match(r"s3://([^/]+)/(.+)", url)
88+
if not m:
89+
raise ValueError(f"Invalid S3 URL: {url}")
90+
bucket, key = m.groups()
91+
92+
try:
93+
s3.head_object(Bucket=bucket, Key=key)
94+
return True
95+
except ClientError as e:
96+
if e.response["Error"]["Code"] == "404":
97+
return False
98+
else:
99+
raise
100+
101+
def test_nrds_fp(clean_s3_test):
102+
generate_nwmfiles(nwmurl_conf)
103+
conf['run']['collect_stats'] = False
104+
prep_ngen_data(conf)
105+
106+
for vpu in vpus:
107+
url = f"s3://ciroh-community-ngen-datastream/test/nrds_fp_test/ngen.t01z.short_range.forcing.f001_f001.VPU_{vpu}.nc"
108+
print(f"Checking for {url}")
109+
assert s3_object_exists(url)
110+
111+
if __name__ == "__main__":
112+
test_nrds_fp()
113+

0 commit comments

Comments
 (0)