-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaprfc_qte_01h.py
More file actions
130 lines (100 loc) · 3.8 KB
/
Copy pathaprfc_qte_01h.py
File metadata and controls
130 lines (100 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
Acquire and Process APRFC QTE 01h
Returns
-------
Airflow DAG
Directed Acyclic Graph
"""
from datetime import datetime, timedelta, timezone
import json
from string import Template
from airflow.decorators import dag, task
from airflow.operators.python import get_current_context
from airflow.utils.task_group import TaskGroup
from helpers.downloads import s3_file_exists, trigger_download
import helpers.cumulus as cumulus
default_args = {
"owner": "airflow",
"depends_on_past": False,
"start_date": (datetime.now(timezone.utc) - timedelta(hours=72)).replace(
minute=0, second=0
),
"catchup": True,
"email_on_failure": False,
"email_on_retry": False,
"retries": 1,
"retry_delay": timedelta(minutes=30),
}
@dag(
default_args=default_args,
tags=["cumulus", "AIRTEMP", "QTE", "APRFC"],
schedule="45 * * * *",
max_active_runs=2,
max_active_tasks=4,
)
def cumulus_aprfc_qte_01h():
"""
# APRFC hourly estimated temps
This pipeline handles download, processing, and derivative product creation for APRFC hourly estimated temps
Raw data downloaded to S3 and notifies the Cumulus API of new product(s)
URLs:
- BASE - https://nomads.ncep.noaa.gov/pub/data/nccf/com/urma/prod/akurma.YYYYMMDD/
Filename/Dir Pattern:
URL Dir - https://nomads.ncep.noaa.gov/pub/data/nccf/com/urma/prod/akurma.YYYYMMDD/
Files matching akurma.tHHz.2dvaranl_ndfd_3p0.grb2 - 1 hour\n
"""
s3_bucket = cumulus.S3_BUCKET
key_prefix = cumulus.S3_ACQUIRABLE_PREFIX
URL_ROOT = "https://nomads.ncep.noaa.gov/pub/data/nccf/com/urma/prod/"
PRODUCT_SLUG = "aprfc-qte-01h"
LOOKBACK_HOURS = 12 # number of hours from runtime to look back for
filename_template = Template("akurma.t${hr_}z.2dvaranl_ndfd_3p0.grb2")
url_suffix_template = Template("akurma.${date_}")
@task()
def download_raw_qte():
logical_date = get_current_context()["logical_date"]
anchor = get_current_context()["data_interval_end"].replace(minute=0, second=0, microsecond=0)
results = []
for offset in range(LOOKBACK_HOURS):
ts = anchor - timedelta(hours=1 + offset) # last complete hour, then look back
date_only = ts.strftime("%Y%m%d")
hour_str = ts.strftime("%H")
url_suffix = url_suffix_template.substitute(
date_=date_only,
)
filename = filename_template.substitute(
hr_=hour_str,
)
file_dir = f"{URL_ROOT}{url_suffix}"
s3_filename = f"{date_only}_{filename}"
s3_key = f"{key_prefix}/{PRODUCT_SLUG}/{s3_filename}"
if s3_file_exists(cumulus.S3_BUCKET, s3_key):
print(f"Skipping existing S3 object: s3://{cumulus.S3_BUCKET}/{s3_key}")
continue # Skip to the next file
print(f"Downloading file: {url_suffix}/{filename}")
try:
trigger_download(
url=f"{file_dir}/{filename}", s3_bucket=s3_bucket, s3_key=s3_key
)
except:
print(f'Failed downloading {filename}')
results.append(
{
"execution": ts.isoformat(),
"s3_key": s3_key,
"filename": s3_filename,
}
)
return json.dumps(results)
@task()
def notify_cumulus(payload):
payload = json.loads(payload)
for item in payload:
print("Notifying Cumulus: " + item["filename"])
cumulus.notify_acquirablefile(
acquirable_id=cumulus.acquirables[PRODUCT_SLUG],
datetime=item["execution"],
s3_key=item["s3_key"],
)
notify_cumulus(download_raw_qte())
aprfc_qte_dag = cumulus_aprfc_qte_01h()