|
| 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