-
Notifications
You must be signed in to change notification settings - Fork 6
/
generate.py
272 lines (237 loc) · 11.4 KB
/
generate.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"""
Generate STAC Collections for Daymet from the Zarr Groups.
python generate.py hi daily examples/daymet/daily/hi.json
python generate.py hi monthly examples/daymet/monthly/hi.json
python generate.py hi annual examples/daymet/annual/hi.json
python generate.py na daily examples/daymet/daily/na.json
python generate.py na monthly examples/daymet/monthly/na.json
python generate.py na annual examples/daymet/annual/na.json
python generate.py pr daily examples/daymet/daily/pr.json
python generate.py pr monthly examples/daymet/monthly/pr.json
python generate.py pr annual examples/daymet/annual/pr.json
"""
import sys
import argparse
import json
import fsspec
import xarray as xr
from pathlib import Path
import xstac
import pystac
import shapely.geometry
BBOX = {
"hi": [-160.3056, 17.9539, -154.772, 23.5186],
"na": [-178.1333, 14.0749, -53.0567, 82.9143],
"pr": [-67.9927, 16.8444, -64.1196, 19.9382],
}
DESC = {
"daily": "Gridded estimates of daily weather parameters. Daymet Version 4 variables include the following parameters: minimum temperature, maximum temperature, precipitation, shortwave radiation, vapor pressure, snow water equivalent, and day length.", # noqa: E501
"monthly": "Monthly climate summaries derived from Daymet Version 4 daily data at a 1 km x 1 km spatial resolution for five Daymet variables: minimum and maximum temperature, precipitation, vapor pressure, and snow water equivalent. Monthly averages are provided for minimum and maximum temperature, vapor pressure, and snow water equivalent, and monthly totals are provided for the precipitation variable.", # noqa: E501
"annual": "Annual climate summaries derived from Daymet Version 4 daily data at a 1 km x 1 km spatial resolution for five Daymet variables: minimum and maximum temperature, precipitation, vapor pressure, and snow water equivalent. Annual averages are provided for minimum and maximum temperature, vapor pressure, and snow water equivalent, and annual totals are provided for the precipitation variable.", # noqa: E501
}
CITATION_URLS = {
"daily": "https://doi.org/10.3334/ORNLDAAC/1840",
"monthly": "https://doi.org/10.3334/ORNLDAAC/1855",
"annual": "https://doi.org/10.3334/ORNLDAAC/1852",
}
DOI_NAMES = {
"daily": "10.3334/ORNLDAAC/1840",
"monthly": "10.3334/ORNLDAAC/1855",
"annual": "10.3334/ORNLDAAC/1852",
}
CITATIONS = {
"daily": "Thornton, M.M., R. Shrestha, P.E. Thornton, S. Kao, Y. Wei, and B.E. Wilson. 2021. Daymet Version 4 Monthly Latency: Daily Surface Weather Data. ORNL DAAC, Oak Ridge, Tennessee, USA. https://doi.org/10.3334/ORNLDAAC/1904", # noqa: E501
"monthly": "Thornton, M.M., R. Shrestha, Y. Wei, P.E. Thornton, S. Kao, and B.E. Wilson. 2020. Daymet: Monthly Climate Summaries on a 1-km Grid for North America, Version 4. ORNL DAAC, Oak Ridge, Tennessee, USA. https://doi.org/10.3334/ORNLDAAC/1855", # noqa: E501
"annual": "Thornton, M.M., R. Shrestha, Y. Wei, P.E. Thornton, S. Kao, and B.E. Wilson. 2020. Daymet: Annual Climate Summaries on a 1-km Grid for North America, Version 4. ORNL DAAC, Oak Ridge, Tennessee, USA. https://doi.org/10.3334/ORNLDAAC/1852", # noqa: E501
}
FULL_REGIONS = {"hi": "Hawaii", "na": "North America", "pr": "Puerto Rico"}
ZARR_MEDIA_TYPE = "application/vnd+zarr"
FREQUENCIES = ["daily", "monthly", "annual"]
def parse_args(args=None):
parser = argparse.ArgumentParser(usage=__doc__)
parser.add_argument(
"--region", type=str, choices=["all", "hi", "pr", "na"], default="all"
)
parser.add_argument(
"--frequency",
type=str,
choices=["all", "daily", "monthly", "annual"],
default="all",
)
return parser.parse_args(args)
def generate(frequency, region):
short_desc_snippet = (
"surface weather data" if frequency == "daily" else "climate summaries"
)
collection_template = {
"id": f"daymet-{frequency}-{region}",
"stac_extensions": [
"https://stac-extensions.github.io/scientific/v1.0.0/schema.json"
],
"description": "{{ collection.description }}",
"type": "Collection",
"title": f"Daymet {frequency.title()} {FULL_REGIONS[region]}",
"license": "proprietary",
"keywords": [
"Daymet",
FULL_REGIONS[region],
"Temperature",
"Precipitation",
"Vapor Pressure",
"Weather" if frequency == "daily" else "Climate",
],
"stac_version": "1.0.0",
"links": [
{
"rel": "license",
"title": "EOSDIS Data Use Policy",
"href": "https://science.nasa.gov/earth-science/earth-science-data/data-information-policy",
},
{"rel": "cite-as", "href": CITATION_URLS[frequency]},
],
"extent": {
"spatial": {"bbox": [BBOX[region]]},
"temporal": {"interval": [[None, None]]},
},
"providers": [
{
"name": "Microsoft",
"roles": ["host", "processor"],
"url": "https://planetarycomputer.microsoft.com",
},
{
"name": "ORNL DAAC",
"roles": ["producer"],
"url": CITATION_URLS[frequency],
},
],
"assets": {
"zarr-https": {
"href": f"https://daymeteuwest.blob.core.windows.net/daymet-zarr/{frequency}/{region}.zarr",
"type": "application/vnd+zarr",
"title": f"{frequency.title()} {FULL_REGIONS[region]} Daymet HTTPS Zarr root",
"description": f"HTTPS URI of the {frequency} {FULL_REGIONS[region]} Daymet Zarr Group on Azure Blob Storage.", # noqa: E501
"roles": ["data", "zarr", "https"],
"xarray:open_kwargs": {"consolidated": True},
},
"zarr-abfs": {
"href": f"abfs://daymet-zarr/{frequency}/{region}.zarr",
"type": "application/vnd+zarr",
"title": f"{frequency.title()} {FULL_REGIONS[region]} Daymet Azure Blob File System Zarr root",
"description": f"Azure Blob File System of the {frequency} {FULL_REGIONS[region]} Daymet Zarr Group on Azure Blob Storage for use with adlfs.", # noqa: E501
"roles": ["data", "zarr", "abfs"],
"xarray:storage_options": {"account_name": "daymeteuwest"},
"xarray:open_kwargs": {"consolidated": True},
},
"thumbnail": {
"href": f"https://ai4edatasetspublicassets.blob.core.windows.net/assets/pc_thumbnails/daymet-{frequency}-{region}.png", # noqa: E501
"type": "image/png",
"title": f"Daymet {frequency} {FULL_REGIONS[region]} map thumbnail",
"roles": ["thumbnail"],
},
},
"msft:short_description": f"{frequency.title()} {short_desc_snippet} on a 1-km grid for {FULL_REGIONS[region]}",
"msft:storage_account": "daymeteuwest",
"msft:container": "daymet-zarr",
"msft:group_id": "daymet",
"msft:group_keys": [frequency, FULL_REGIONS[region].lower()],
"sci:doi": DOI_NAMES[frequency],
"sci:citation": CITATIONS[frequency],
}
store = fsspec.get_mapper(
f"az://daymet-zarr/{frequency}/{region}.zarr", account_name="daymeteuwest"
)
ds = xr.open_zarr(store, consolidated=True)
if "yearday" in ds:
ds.yearday.attrs["long_name"] = ds.yearday.attrs["long_name"].replace(
"Januaray", "January"
)
collection = xstac.xarray_to_stac(
ds,
collection_template,
temporal_dimension="time",
x_dimension="x",
y_dimension="y",
)
collection.remove_links(pystac.RelType.SELF)
collection.remove_links(pystac.RelType.ROOT)
collection_result = collection.to_dict(include_self_link=False)
# additional dimensions not implemented in xstac
collection_result["cube:dimensions"]["nv"] = {
"type": "count",
"description": "Size of the 'time_bnds' variable.",
"values": [0, 1],
}
item_template = {
"id": f"daymet-{frequency}-{region}",
"type": "Feature",
"links": [],
"bbox": BBOX[region],
"geometry": shapely.geometry.mapping(shapely.geometry.box(*BBOX[region])),
"stac_version": "1.0.0",
"properties": {"start_datetime": None, "end_datetime": None},
"assets": {
"zarr-https": {
"href": f"https://daymeteuwest.blob.core.windows.net/daymet-zarr/{frequency}/{region}.zarr",
"type": "application/vnd+zarr",
"title": f"{frequency.title()} {FULL_REGIONS[region]} Daymet HTTPS Zarr root",
"description": f"HTTPS URI of the {frequency} {FULL_REGIONS[region]} Daymet Zarr Group on Azure Blob Storage.", # noqa: E501
"roles": ["data", "zarr", "https"],
"xarray:open_kwargs": {"consolidated": True},
},
"zarr-abfs": {
"href": f"abfs://daymet-zarr/{frequency}/{region}.zarr",
"type": "application/vnd+zarr",
"title": f"{frequency.title()} {FULL_REGIONS[region]} Daymet Azure Blob File System Zarr root",
"description": f"Azure Blob File System of the {frequency} {FULL_REGIONS[region]} Daymet Zarr Group on Azure Blob Storage for use with adlfs.", # noqa: E501
"roles": ["data", "zarr", "abfs"],
"xarray:storage_options": {"account_name": "daymeteuwest"},
"xarray:open_kwargs": {"consolidated": True},
},
"thumbnail": {
"href": f"https://ai4edatasetspublicassets.blob.core.windows.net/assets/pc_thumbnails/daymet-{frequency}-{region}.png", # noqa: E501
"type": "image/png",
"title": f"Daymet {frequency} {FULL_REGIONS[region]} map thumbnail",
},
},
}
item = xstac.xarray_to_stac(
ds, item_template, temporal_dimension="time", x_dimension="x", y_dimension="y"
)
item_result = item.to_dict(include_self_link=False)
# additional dimensions not implemented in xstac
item_result["properties"]["cube:dimensions"]["nv"] = {
"type": "count",
"description": "Size of the 'time_bnds' variable.",
"values": [0, 1],
}
for link in item_result["links"]:
if link["rel"] == "root":
link["href"] = "../catalog.json"
link["rel"] = str(link["rel"].value)
link["type"] = str(link["type"].value)
return collection_result, item_result
def main(args=None):
args = parse_args(args)
region = args.region
frequency = args.frequency
if region == "all":
regions = list(FULL_REGIONS)
else:
regions = [region]
if frequency == "all":
frequencies = FREQUENCIES
else:
frequencies = [frequency]
for region in regions:
for frequency in frequencies:
collection, item = generate(frequency, region)
outfile = Path(__file__).parent / f"{frequency}/{region}/collection.json"
outfile.parent.mkdir(exist_ok=True, parents=True)
with open(outfile, "w") as f:
json.dump(collection, f, indent=2)
outfile = Path(__file__).parent / f"{frequency}/{region}/item.json"
with open(outfile, "w") as f:
json.dump(item, f, indent=2)
if __name__ == "__main__":
sys.exit(main())