-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
2663 lines (2223 loc) · 102 KB
/
utils.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import concurrent.futures
import copy
import datetime
import io
import json
import logging
import contextlib
import math
import os
import pathlib
import re
import sys
import tempfile
import time
from calendar import monthrange
from concurrent.futures import ThreadPoolExecutor
from functools import partial
from typing import Any, Dict, List, Optional, Tuple, Iterable, Callable
import shutil
import os
import warnings
import dask
import fsspec
import gcsfs
import numpy as np
import pandas as pd
from distributed import get_worker
from google.auth import credentials
from google.cloud import storage
from google.oauth2 import service_account
from kerchunk.grib2 import grib_tree, scan_grib
import dask.dataframe as dd
from dask.distributed import Client, get_worker
import xarray as xr
import zarr
import gcsfs
import pathlib
import pandas as pd
import datatree
from enum import Enum, auto
from dynamic_zarr_store import (
AggregationType,
build_idx_grib_mapping,
map_from_index,
parse_grib_idx,
store_coord_var,
store_data_var,
strip_datavar_chunks,
_extract_single_group,
)
logger = logging.getLogger("utils-logs")
class JSONFormatter(logging.Formatter):
def format(self, record):
log_data = {
"timestamp": self.formatTime(record, self.datefmt),
"level": record.levelname,
"message": self.format_message(record.getMessage()),
"function": record.funcName,
"line": record.lineno,
}
try:
return json.dumps(log_data)
except (TypeError, ValueError):
# Handle cases where the logged message contains non-JSON-serializable data
return f"{{\"timestamp\": \"{self.formatTime(record, self.datefmt)}\", \"level\": \"{record.levelname}\", \"function\": \"{record.funcName}\", \"line\": {record.lineno}, \"message\": \"{self.format_message(record.getMessage())}\"}}"
def format_message(self, message):
# Replace any newline characters with a space
return message.replace('\n', ' ')
def setup_logging(log_level: int = logging.INFO, log_file: str = "gfs_processing.log"):
logger = logging.getLogger()
logger.setLevel(log_level)
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(JSONFormatter())
logger.addHandler(file_handler)
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logger.addHandler(console_handler)
def log_function_call(func):
def wrapper(*args, **kwargs):
logger = logging.getLogger()
func_name = func.__name__
logger.info(json.dumps({"event": "function_start", "function": func_name}))
# Capture print and stderr output
with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):
result = func(*args, **kwargs)
# Log the captured output
stdout = sys.stdout.getvalue()
stderr = sys.stderr.getvalue()
if stdout:
for line in stdout.splitlines():
logger.info(json.dumps({"event": "print_output", "function": func_name, "message": line}))
if stderr:
for line in stderr.splitlines():
logger.info(json.dumps({"event": "stderr_output", "function": func_name, "message": line}))
logger.info(json.dumps({"event": "function_end", "function": func_name}))
return result
return wrapper
def build_grib_tree(gfs_files: List[str]) -> Tuple[dict, dict]:
"""
Scan GFS files, build a hierarchical tree structure for the data, and strip unnecessary data.
Parameters:
- gfs_files (List[str]): List of file paths to GFS files.
Returns:
- Tuple[dict, dict]: Original and deflated GRIB tree stores.
"""
print("Building Grib Tree")
gfs_grib_tree_store = grib_tree([group for f in gfs_files for group in scan_grib(f)])
deflated_gfs_grib_tree_store = copy.deepcopy(gfs_grib_tree_store)
strip_datavar_chunks(deflated_gfs_grib_tree_store)
print(f"Original references: {len(gfs_grib_tree_store['refs'])}")
print(f"Stripped references: {len(deflated_gfs_grib_tree_store['refs'])}")
return gfs_grib_tree_store, deflated_gfs_grib_tree_store
def calculate_time_dimensions(axes: List[pd.Index]) -> Tuple[Dict, Dict, np.ndarray, np.ndarray, np.ndarray]:
"""
Calculate time-related dimensions and coordinates based on input axes.
Parameters:
- axes (List[pd.Index]): List of pandas Index objects containing time information.
Returns:
- Tuple[Dict, Dict, np.ndarray, np.ndarray, np.ndarray]: Time dimensions, coordinates, times, valid times, and steps.
"""
print("Calculating Time Dimensions and Coordinates")
axes_by_name: Dict[str, pd.Index] = {pdi.name: pdi for pdi in axes}
aggregation_type = AggregationType.BEST_AVAILABLE
time_dims: Dict[str, int] = {}
time_coords: Dict[str, tuple[str, ...]] = {}
if aggregation_type == AggregationType.BEST_AVAILABLE:
time_dims["valid_time"] = len(axes_by_name["valid_time"])
assert len(axes_by_name["time"]) == 1, "The time axes must describe a single 'as of' date for best available"
reference_time = axes_by_name["time"].to_numpy()[0]
time_coords["step"] = ("valid_time",)
time_coords["valid_time"] = ("valid_time",)
time_coords["time"] = ("valid_time",)
time_coords["datavar"] = ("valid_time",)
valid_times = axes_by_name["valid_time"].to_numpy()
times = np.where(valid_times <= reference_time, valid_times, reference_time)
steps = valid_times - times
times = valid_times
return time_dims, time_coords, times, valid_times, steps
def process_dataframe(df, varnames_to_process):
"""
Filter and process the DataFrame by specific variable names and their corresponding type of levels.
Parameters:
- df (pd.DataFrame): Input DataFrame to process.
- varnames_to_process (list): List of variable names to filter and process in the DataFrame.
Returns:
- pd.DataFrame: Processed DataFrame with duplicates removed based on the 'time' column and sorted by 'length'.
"""
conditions = {
'acpcp':'surface',
'cape': 'surface',
'cin': 'surface',
'pres': 'heightAboveGround',
'r': 'atmosphereSingleLayer',
'soill': 'atmosphereSingleLayer',
'soilw':'depthBelowLandLayer', # Handling multiple levels for 'soill'
'st': 'depthBelowLandLayer',
't': 'surface',
'tp': 'surface'
}
processed_df = pd.DataFrame()
for varname in varnames_to_process:
if varname in conditions:
level = conditions[varname]
if isinstance(level, list):
for l in level:
filtered_df = df[(df['varname'] == varname) & (df['typeOfLevel'] == l)]
filtered_df = filtered_df.sort_values(by='length', ascending=False).drop_duplicates(subset=['time'], keep='first')
processed_df = pd.concat([processed_df, filtered_df], ignore_index=True)
else:
filtered_df = df[(df['varname'] == varname) & (df['typeOfLevel'] == level)]
filtered_df = filtered_df.sort_values(by='length', ascending=False).drop_duplicates(subset=['time'], keep='first')
processed_df = pd.concat([processed_df, filtered_df], ignore_index=True)
return processed_df
def create_mapped_index(axes: List[pd.Index], mapping_parquet_file_path: str, date_str: str) -> pd.DataFrame:
"""
Create a mapped index from GFS files for a specific date, using the mapping from a parquet file.
Parameters:
- axes (List[pd.Index]): List of time axes to map.
- mapping_parquet_file_path (str): File path to the mapping parquet file.
- date_str (str): Date string for the data being processed.
Returns:
- pd.DataFrame: DataFrame containing the mapped index for the specified date.
"""
print(f"Creating Mapped Index for date {date_str}")
mapped_index_list = []
dtaxes = axes[0]
for idx, datestr in enumerate(dtaxes):
try:
fname = f"s3://noaa-gfs-bdp-pds/gfs.{date_str}/00/atmos/gfs.t00z.pgrb2.0p25.f{idx:03}"
idxdf = parse_grib_idx(
fs=fsspec.filesystem("s3"),
basename=fname
)
deduped_mapping = pd.read_parquet(f"{mapping_parquet_file_path}gfs-mapping-{idx:03}.parquet")
mapped_index = map_from_index(
datestr,
deduped_mapping,
idxdf.loc[~idxdf["attrs"].duplicated(keep="first"), :]
)
mapped_index_list.append(mapped_index)
except Exception as e:
logger.error(f"Error processing file {fname}: {str(e)}")
gfs_kind = pd.concat(mapped_index_list)
gfs_kind_var=gfs_kind.drop_duplicates('varname')
var_list=gfs_kind_var['varname'].tolist()
var_to_remove=['acpcp','cape','cin','pres','r','soill','soilw','st','t','tp']
var1_list = list(filter(lambda x: x not in var_to_remove, var_list))
gfs_kind1=gfs_kind.loc[gfs_kind.varname.isin(var1_list)]
#gfs_kind1 = gfs_kind.drop_duplicates('uri')
# Process the data that needs to be filtered and modified
to_process_df = gfs_kind[gfs_kind['varname'].isin(var_to_remove)]
processed_df = process_dataframe(to_process_df, var_to_remove)
# Concatenate the unprocessed and processed parts back together
final_df = pd.concat([gfs_kind1, processed_df], ignore_index=True)
# Optionally, you might want to sort or reorganize the DataFrame
final_df = final_df.sort_values(by=['time', 'varname'])
final_df_var=final_df.drop_duplicates('varname')
final_var_list=final_df_var['varname'].tolist()
print(f"Mapped collected multiple variables index info: {len(final_var_list)} and {final_var_list}")
return final_df
def cs_create_mapped_index(axes: List[pd.Index], gcs_bucket_name: str, date_str: str) -> pd.DataFrame:
"""
Create a mapped index from GFS files for a specific date, using the mapping from a parquet file stored in GCS.
Parameters:
- axes (List[pd.Index]): List of time axes to map.
- gcs_bucket_name (str): Name of the GCS bucket containing mapping files.
- date_str (str): Date string for the data being processed (format: YYYYMMDD).
Returns:
- pd.DataFrame: DataFrame containing the mapped index for the specified date.
"""
#logger = logging.getLogger()
mapped_index_list = []
dtaxes = axes[0]
# Convert date_str to first day of the month
first_day_of_month = pd.to_datetime(date_str).replace(day=1).strftime('%Y%m%d')
# Initialize GCS filesystem
gcs_fs = fsspec.filesystem('gcs')
for idx, datestr in enumerate(dtaxes):
try:
# S3 path for the GFS data
fname = f"s3://noaa-gfs-bdp-pds/gfs.{date_str}/00/atmos/gfs.t00z.pgrb2.0p25.f{idx:03}"
# Parse the idx file from S3
idxdf = parse_grib_idx(
fs=fsspec.filesystem("s3"),
basename=fname
)
# Construct GCS path for mapping file
gcs_mapping_path = f"gs://{gcs_bucket_name}/time_idx/20231201/agfs-time-20231201-rt{idx:03}.parquet"
# Read parquet directly from GCS using fsspec
deduped_mapping = pd.read_parquet(
gcs_mapping_path,
filesystem=gcs_fs
)
mapped_index = map_from_index(
datestr,
deduped_mapping,
idxdf.loc[~idxdf["attrs"].duplicated(keep="first"), :]
)
mapped_index_list.append(mapped_index)
logger.info(json.dumps({
"event": "file_processed",
"date": date_str,
"file_index": idx,
"mapping_file": gcs_mapping_path
}))
except Exception as e:
logger.error(json.dumps({
"event": "error_processing_file",
"date": date_str,
"file_index": idx,
"error": str(e)
}))
continue
if not mapped_index_list:
raise ValueError(f"No valid mapped indices created for date {date_str}")
# Combine all mapped indices
gfs_kind = pd.concat(mapped_index_list)
# Get unique variables
gfs_kind_var = gfs_kind.drop_duplicates('varname')
var_list = gfs_kind_var['varname'].tolist()
# Define variables to process separately
var_to_remove = ['acpcp', 'cape', 'cin', 'pres', 'r', 'soill', 'soilw', 'st', 't', 'tp']
# Filter variables
var1_list = list(filter(lambda x: x not in var_to_remove, var_list))
gfs_kind1 = gfs_kind.loc[gfs_kind.varname.isin(var1_list)]
# Process special variables
to_process_df = gfs_kind[gfs_kind['varname'].isin(var_to_remove)]
processed_df = process_dataframe(to_process_df, var_to_remove)
# Combine processed and unprocessed data
final_df = pd.concat([gfs_kind1, processed_df], ignore_index=True)
final_df = final_df.sort_values(by=['time', 'varname'])
# Get final variable list for logging
final_df_var = final_df.drop_duplicates('varname')
final_var_list = final_df_var['varname'].tolist()
return final_df
def prepare_zarr_store(deflated_gfs_grib_tree_store: dict, gfs_kind: pd.DataFrame) -> Tuple[dict, pd.DataFrame]:
"""
Prepare Zarr store and related data for chunk processing based on GFS kind DataFrame.
Parameters:
- deflated_gfs_grib_tree_store (dict): Deflated GRIB tree store containing reference data.
- gfs_kind (pd.DataFrame): DataFrame containing GFS data.
Returns:
- Tuple[dict, pd.DataFrame]: Zarr reference store and the DataFrame for chunk index.
"""
print("Preparing Zarr Store")
zarr_ref_store = deflated_gfs_grib_tree_store
#chunk_index = gfs_kind.loc[gfs_kind.varname.isin(["t2m"])]
chunk_index = gfs_kind
zstore = copy.deepcopy(zarr_ref_store["refs"])
return zstore, chunk_index
def process_unique_groups(zstore: dict, chunk_index: pd.DataFrame, time_dims: Dict, time_coords: Dict,
times: np.ndarray, valid_times: np.ndarray, steps: np.ndarray) -> dict:
"""
Process and update Zarr store by configuring data for unique variable groups. This involves setting time dimensions,
coordinates, and updating Zarr store paths with processed data arrays.
Parameters:
- zstore (dict): The initial Zarr store with references to original data.
- chunk_index (pd.DataFrame): DataFrame containing metadata and paths for the chunks of data to be stored.
- time_dims (Dict): Dictionary specifying dimensions for time-related data.
- time_coords (Dict): Dictionary specifying coordinates for time-related data.
- times (np.ndarray): Array of actual times from the data files.
- valid_times (np.ndarray): Array of valid forecast times.
- steps (np.ndarray): Time steps in seconds converted from time differences.
Returns:
- dict: Updated Zarr store with added datasets and metadata.
This function processes each unique combination of 'varname', 'stepType', and 'typeOfLevel' found in the chunk_index.
For each group, it determines appropriate dimensions and coordinates based on the unique levels present and updates
the Zarr store with the processed data. It removes any data references that do not match the existing unique groups.
"""
print("Processing Unique Groups and Updating Zarr Store")
unique_groups = chunk_index.set_index(["varname", "stepType", "typeOfLevel"]).index.unique()
for key in list(zstore.keys()):
lookup = tuple([val for val in os.path.dirname(key).split("/")[:3] if val != ""])
if lookup not in unique_groups:
del zstore[key]
for key, group in chunk_index.groupby(["varname", "stepType", "typeOfLevel"]):
try:
base_path = "/".join(key)
lvals = group.level.unique()
dims = time_dims.copy()
coords = time_coords.copy()
if len(lvals) == 1:
lvals = lvals.squeeze()
dims[key[2]] = 0
elif len(lvals) > 1:
lvals = np.sort(lvals)
dims[key[2]] = len(lvals)
coords["datavar"] += (key[2],)
else:
raise ValueError("Invalid level values encountered")
# Store coordinates and data variables in the Zarr store
store_coord_var(key=f"{base_path}/time", zstore=zstore, coords=time_coords["time"], data=times.astype("datetime64[s]"))
store_coord_var(key=f"{base_path}/valid_time", zstore=zstore, coords=time_coords["valid_time"], data=valid_times.astype("datetime64[s]"))
store_coord_var(key=f"{base_path}/step", zstore=zstore, coords=time_coords["step"], data=steps.astype("timedelta64[s]").astype("float64") / 3600.0)
store_coord_var(key=f"{base_path}/{key[2]}", zstore=zstore, coords=(key[2],) if lvals.shape else (), data=lvals)
store_data_var(key=f"{base_path}/{key[0]}", zstore=zstore, dims=dims, coords=coords, data=group, steps=steps, times=times, lvals=lvals if lvals.shape else None)
except Exception as e:
print(f"Skipping of processing group {key}: {str(e)}")
return zstore
def zstore_dict_to_df(zstore: dict):
"""
Helper function to convert dictionary to pandas DataFrame with columns 'key' and 'value'.
Parameters:
- zstore (dict): The dictionary representing the Zarr store.
Returns:
- pd.DataFrame: DataFrame with two columns: 'key' representing the dictionary keys, and 'value'
representing the dictionary values, which are encoded in UTF-8 if they are of type
dictionary, list, or numeric.
"""
data = []
for key, value in zstore.items():
# Convert dictionaries, lists, or numeric types to UTF-8 encoded strings
if isinstance(value, (dict, list, int, float, np.integer, np.floating)):
value = str(value).encode('utf-8')
data.append((key, value))
return pd.DataFrame(data, columns=['key', 'value'])
def create_parquet_file(zstore: dict, output_parquet_file: str):
"""
Converts a dictionary containing Zarr store data to a DataFrame and saves it as a Parquet file.
This function encapsulates the Zarr store data within a dictionary, converts this dictionary to a pandas DataFrame,
and then writes the DataFrame to a Parquet file. This is useful for persisting Zarr metadata and references
in a compressed and efficient format that can be easily reloaded.
Parameters:
- zstore (dict): The Zarr store dictionary containing all references and data needed for Zarr operations.
- output_parquet_file (str): The path where the Parquet file will be saved.
This function first creates an internal dictionary that includes versioning information, then iterates over
the items in the Zarr store. For each item, it checks if the value is a dictionary, list, or a numeric type,
and encodes it as a UTF-8 string if necessary. This encoded data is then used to create a DataFrame, which
is subsequently written to a Parquet file. The function logs both the beginning of the operation and its
successful completion, noting the location of the saved Parquet file.
"""
gfs_store = dict(refs=zstore, version=1) # Include versioning for the store structure
zstore_df = zstore_dict_to_df(gfs_store)
zstore_df.to_parquet(output_parquet_file)
print(f"Parquet file saved to {output_parquet_file}")
def create_parquet_df(zstore: dict, date_str: str, run_str: str, source: str = "aws_s3") -> pd.DataFrame:
"""
Converts a dictionary containing Zarr store data to a DataFrame with additional columns.
This function encapsulates the Zarr store data within a dictionary,
converts this dictionary to a pandas DataFrame, and returns the DataFrame.
Additional columns are added for metadata such as date, run, and source.
Parameters:
- zstore (dict): The Zarr store dictionary containing all references
and data needed for Zarr operations.
- date_str (str): A string representing the date to be added to the DataFrame.
- run_str (str): A string representing the run to be added to the DataFrame.
- source (str): A string representing the data source to be added to the DataFrame.
Defaults to "aws_s3".
Returns:
- pd.DataFrame: The resulting DataFrame representing the Zarr store data
with additional metadata columns.
"""
gfs_store = dict(refs=zstore, version=1) # Include versioning for the store structure
zstore_df = zstore_dict_to_df(gfs_store)
# Add additional metadata columns
zstore_df["date"] = date_str
zstore_df["run"] = run_str
zstore_df["source"] = source
return zstore_df
def generate_axes(date_str: str) -> List[pd.Index]:
"""
Generate temporal axes indices for a given forecast start date over a predefined forecast period.
This function creates two pandas Index objects: one for 'valid_time' and another for 'time'.
The 'valid_time' index represents a sequence of datetime stamps for each hour over a 5-day forecast period,
starting from the given start date. The 'time' index captures the single forecast initiation date.
Parameters:
- date_str (str): The start date of the forecast, formatted as 'YYYYMMDD'.
Returns:
- List[pd.Index]: A list containing two pandas Index objects:
1. 'valid_time' index with datetime stamps spaced one hour apart, covering a 5-day range from the start date.
2. 'time' index representing the single start date of the forecast as a datetime object.
Example:
For a given start date '20230101', this function will return two indices:
- The first index will span from '2023-01-01 00:00' to '2023-01-06 00:00' with hourly increments.
- The second index will contain just the single datetime '2023-01-01 00:00'.
These indices are typically used to set up time coordinates in weather or climate models and datasets,
facilitating data alignment and retrieval based on forecast times.
"""
start_date = pd.Timestamp(date_str)
end_date = start_date + pd.Timedelta(days=5) # Forecast period of 5 days
valid_time_index = pd.date_range(start_date, end_date, freq="60min", name="valid_time")
time_index = pd.Index([start_date], name="time")
return [valid_time_index, time_index]
def generate_gfs_dates(year: int, month: int) -> List[str]:
"""
Generate a list of dates for a specific month and year, formatted as 'YYYYMMDD', to cover the full range of days in the specified month.
This function computes the total number of days in the given month of the specified year and generates a complete list of dates.
This is particularly useful for scheduling tasks or simulations that require a complete temporal scope of a month for processes like
weather forecasting or data collection where daily granularity is needed.
Parameters:
- year (int): The year for which the dates are to be generated.
- month (int): The month for which the dates are to be generated, where 1 represents January and 12 represents December.
Returns:
- List[str]: A list of dates in the format 'YYYYMMDD' for every day in the specified month and year.
Example:
For inputs year=2023 and month=1, the output will be:
['20230101', '20230102', '20230103', ..., '20230130', '20230131']
This example demonstrates generating dates for January 2023, resulting in a list that includes every day from January 1st to 31st.
"""
# Get the last day of the month using the monthrange function from the calendar module
_, last_day = monthrange(year, month)
# Generate a date range for the entire month from the first to the last day
date_range = pd.date_range(start=f'{year}-{month:02d}-01',
end=f'{year}-{month:02d}-{last_day}',
freq='D')
# Convert the date range to a list of strings formatted as 'YYYYMMDD'
return date_range.strftime('%Y%m%d').tolist()
def process_gfs_data(date_str: str, mapping_parquet_file_path: str, output_parquet_file: str, log_level: int = logging.INFO):
"""
Orchestrates the end-to-end processing of Global Forecast System (GFS) data for a specific date. This function
integrates several steps including reading GFS files, calculating time dimensions, mapping data, preparing Zarr
stores, processing unique data groups, and finally saving the processed data to a Parquet file.
This function is designed to automate the workflow for daily GFS data processing, ensuring that each step is
logged and any issues are reported for troubleshooting.
Parameters:
- date_str (str): A date string in the format 'YYYYMMDD' representing the date for which GFS data is to be processed.
- mapping_parquet_file_path (str): Path to the parquet file that contains mapping information for the GFS data.
- output_parquet_file (str): Path where the output Parquet file will be saved after processing the data.
- log_level (int): Logging level to use for reporting within this function. Default is logging.INFO.
Workflow:
1. Set up logging based on the specified log level.
2. Generate time axes for the date specified by `date_str`.
3. List GFS file paths for the initial and subsequent model outputs.
4. Build a GFS grib tree for storing and managing data hierarchically.
5. Calculate the time dimensions and coordinates necessary for data processing.
6. Create a mapped index based on the generated axes and specified parquet mapping file.
7. Prepare the Zarr store for efficient data manipulation and storage.
8. Process unique data groups, organizing and formatting the data as required.
9. Save the processed data to a Parquet file for durable storage.
Exceptions:
- Raises an exception if an error occurs during the processing steps, with the error logged for diagnostic purposes.
Example:
To process GFS data for January 1st, 2021, call the function as follows:
process_gfs_data('20210101', '/path/to/mapping.parquet/', '/output/path/20210101_processed.parquet')
This will execute the entire data processing pipeline for the specified date and save the results in the designated output file.
"""
try:
print(f"Processing date: {date_str}")
axes = generate_axes(date_str)
gfs_files = [
f"s3://noaa-gfs-bdp-pds/gfs.{date_str}/00/atmos/gfs.t00z.pgrb2.0p25.f000",
f"s3://noaa-gfs-bdp-pds/gfs.{date_str}/00/atmos/gfs.t00z.pgrb2.0p25.f001"
]
_, deflated_gfs_grib_tree_store = build_grib_tree(gfs_files)
time_dims, time_coords, times, valid_times, steps = calculate_time_dimensions(axes)
gfs_kind = create_mapped_index(axes, mapping_parquet_file_path, date_str)
zstore, chunk_index = prepare_zarr_store(deflated_gfs_grib_tree_store, gfs_kind)
updated_zstore = process_unique_groups(zstore, chunk_index, time_dims, time_coords, times, valid_times, steps)
create_parquet_file(updated_zstore, output_parquet_file)
except Exception as e:
print(f"An error occurred during processing: {str(e)}")
raise
def get_details(url):
"""Extract date and time details from GFS URL."""
pattern = r"s3://noaa-gfs-bdp-pds/gfs\.(\d{8})/(\d{2})/atmos/gfs\.t(\d{2})z\.pgrb2\.0p25\.f(\d{3})"
match = re.match(pattern, url)
if match:
date = match.group(1) # Captures '20241010'
run = match.group(2) # Captures '00'
hour = match.group(4) # Captures '003'
return date, run, hour
else:
logger.warning(f"No match found for URL pattern: {url}")
return None, None, None
def gfs_s3_url_maker(date_str):
"""Create S3 URLs for GFS data."""
fs_s3 = fsspec.filesystem("s3", anon=True)
s3url_glob = fs_s3.glob(
f"s3://noaa-gfs-bdp-pds/gfs.{date_str}/00/atmos/gfs.t00z.pgrb2.0p25.f*"
)
s3url_only_grib = [f for f in s3url_glob if f.split(".")[-1] != "idx"]
fmt_s3og = sorted(["s3://" + f for f in s3url_only_grib])
print(f"Generated {len(fmt_s3og)} URLs for date {date_str}")
return fmt_s3og
def get_filename_from_path(file_path):
"""Extract filename from full path"""
return os.path.basename(file_path)
def nonclusterworker_upload_to_gcs(bucket_name, source_file_name, destination_blob_name, dask_worker_credentials_path):
"""Uploads a file to the GCS bucket using provided service account credentials."""
try:
# Get just the filename from the credentials path
#creds_filename = get_filename_from_path(credentials_path)
# Construct the worker-local path
#worker_creds_path = os.path.join(os.getcwd(), creds_filename)
#credentials_path = "/app/coiled-data-key.json"
#credentials_path = os.path.join(os.getcwd(), creds_filename)
#credentials_path = os.path.join(tempfile.gettempdir(), creds_filename)
print(f"Using credentials file at: {dask_worker_credentials_path}")
if not os.path.exists(dask_worker_credentials_path):
raise FileNotFoundError(f"Credentials file not found at {dask_worker_credentials_path}")
storage_client = storage.Client.from_service_account_json(dask_worker_credentials_path)
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print(f"File {source_file_name} uploaded to {destination_blob_name} in bucket {bucket_name}.")
except Exception as e:
print(f"Failed to upload file to GCS: {str(e)}")
raise
def get_worker_creds_path(dask_worker):
return str(pathlib.Path(dask_worker.local_directory) / 'coiled-data-key.json')
def upload_to_gcs(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the GCS bucket using provided service account credentials."""
try:
# Get the worker's local directory path for credentials
# Get current worker's credentials path
worker = get_worker()
worker_creds_path = get_worker_creds_path(worker)
print(f"Using credentials file at: {worker_creds_path}")
storage_client = storage.Client.from_service_account_json(worker_creds_path)
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print(f"File {source_file_name} uploaded to {destination_blob_name} in bucket {bucket_name}.")
except Exception as e:
print(f"Failed to upload file to GCS: {str(e)}")
raise
@dask.delayed
def process_gfs_time_idx_data(s3url, bucket_name):
"""Process GFS data and upload to GCS."""
# Ensure logger is set up in the worker process
worker_logger = setup_logger()
try:
worker_logger.info(f"Processing: {s3url}")
date_str, runz, runtime = get_details(s3url)
#worker_creds_path = os.path.join(dask_worker.local_directory, credentials_path)
#worker_logger.info(f"Using credentials from: {worker_creds_path}")
if not all([date_str, runz, runtime]):
worker_logger.error(f"Invalid URL format: {s3url}")
return False
# Build mapping for the specified runtime
mapping = build_idx_grib_mapping(
fs=fsspec.filesystem("s3"),
basename=s3url
)
deduped_mapping = mapping.loc[~mapping["attrs"].duplicated(keep="first"), :]
deduped_mapping.set_index('attrs', inplace=True)
# Save deduped mapping as Parquet
output_dir = f"gfs_mapping_{date_str}"
os.makedirs(output_dir, exist_ok=True)
parquet_path = os.path.join(output_dir, f"gfs-time-{date_str}-rt{int(runtime):03}.parquet")
deduped_mapping.to_parquet(parquet_path, index=True)
# Upload to GCS
destination_blob_name = f"time_idx/2023/{date_str}/{os.path.basename(parquet_path)}"
upload_to_gcs(bucket_name, parquet_path, destination_blob_name)
# Cleanup
os.remove(parquet_path)
worker_logger.info(f"Data for {date_str} runtime {runtime} has been processed and uploaded successfully.")
return True
except Exception as e:
worker_logger.error(f"Failed to process data for URL {s3url}: {str(e)}")
worker_logger.error(traceback.format_exc())
raise
#@dask.delayed
def logged_process_gfs_time_idx_data(s3url, bucket_name):
"""Process GFS data and upload to GCS, logging results to individual GCS logs."""
# Parse date_str and other details from the URL
date_str, runz, runtime = get_details(s3url)
year=date_str[0:3]
# Create a temporary log file for this specific URL with date_str in the name
with tempfile.NamedTemporaryFile(mode="w+", suffix=f"_{date_str}_{runtime}.log", delete=False) as log_file:
log_filename = log_file.name
worker_logger = setup_logger(log_file.name) # Log messages will go to this file
try:
worker_logger.info(f"Processing: {s3url}")
if not all([date_str, runz, runtime]):
worker_logger.error(f"Invalid URL format: {s3url}")
return False
# Build mapping for the specified runtime
mapping = build_idx_grib_mapping(
fs=fsspec.filesystem("s3"),
basename=s3url
)
deduped_mapping = mapping.loc[~mapping["attrs"].duplicated(keep="first"), :]
deduped_mapping.set_index('attrs', inplace=True)
# Save deduped mapping as Parquet
output_dir = f"gfs_mapping_{date_str}"
os.makedirs(output_dir, exist_ok=True)
parquet_path = os.path.join(output_dir, f"gfs-time-{date_str}-rt{int(runtime):03}.parquet")
deduped_mapping.to_parquet(parquet_path, index=True)
# Upload to GCS
destination_blob_name = f"time_idx/year/{date_str}/{os.path.basename(parquet_path)}"
upload_to_gcs(bucket_name, parquet_path, destination_blob_name)
# Cleanup
os.remove(parquet_path)
worker_logger.info(f"Data for {date_str} runtime {runtime} has been processed and uploaded successfully.")
process_success = True
except Exception as e:
worker_logger.error(f"Failed to process data for URL {s3url}: {str(e)}")
worker_logger.error(traceback.format_exc())
process_success = False
finally:
# Upload log file to GCS for later inspection
gcs_log_path = f"time_idx/2023/logs/{date_str}/{os.path.basename(log_filename)}"
upload_to_gcs(bucket_name, log_filename, gcs_log_path)
os.remove(log_filename) # Remove the temporary log file after uploading
return process_success
def old_s3_ecmwf_build_idx_grib_mapping(
fs: fsspec.AbstractFileSystem,
basename: str,
date_str: str,
idx:int,
suffix: str = "index",
mapper: Optional[Callable] = None,
tstamp: Optional[pd.Timestamp] = None,
validate: bool = False
) -> pd.DataFrame:
"""
Mapping method combines the idx and grib metadata to make a mapping from one to the other for a particular
model horizon file. This should be generally applicable to all forecasts for the given horizon.
:param fs: the file system to read metatdata from
:param basename: the full path for the grib2 file
:param suffix: the suffix for the index file
:param mapper: the mapper if any to apply (used for hrrr subhf)
:param tstamp: the timestamp to use for when the data was indexed
:param validate: assert mapping is correct or fail before returning
:return: the merged dataframe with the results of the two operations joined on the grib message group number
"""
#grib_file_index = _map_grib_file_by_group(fname=basename, mapper=mapper)
grib_file_index = pd.read_parquet(f'{date_str}/ecmwf_scangrib_metadata_table_{date_str}_{idx}.parquet')
idx_file_index = s3_parse_ecmwf_grib_idx(
fs=fs, basename=basename, suffix=suffix, tstamp=tstamp
)
result = idx_file_index.merge(
# Left merge because the idx file should be authoritative - one record per grib message
grib_file_index,
on="idx",
how="left",
suffixes=("_idx", "_grib"),
)
if validate:
# If any of these conditions fail - run the method in colab for the same file and inspect the result manually.
all_match_offset = (
(result.loc[:, "offset_idx"] == result.loc[:, "offset_grib"])
| pd.isna(result.loc[:, "offset_grib"])
| ~pd.isna(result.loc[:, "inline_value"])
)
all_match_length = (
(result.loc[:, "length_idx"] == result.loc[:, "length_grib"])
| pd.isna(result.loc[:, "length_grib"])
| ~pd.isna(result.loc[:, "inline_value"])
)
if not all_match_offset.all():
vcs = all_match_offset.value_counts()
raise ValueError(
f"Failed to match message offset mapping for grib file {basename}: {vcs[True]} matched, {vcs[False]} didn't"
)
if not all_match_length.all():
vcs = all_match_length.value_counts()
raise ValueError(
f"Failed to match message length mapping for grib file {basename}: {vcs[True]} matched, {vcs[False]} didn't"
)
if not result["attrs"].is_unique:
dups = result.loc[result["attrs"].duplicated(keep=False), :]
logger.warning(
"The idx attribute mapping for %s is not unique for %d variables: %s",
basename,
len(dups),
dups.varname.tolist(),
)
r_index = result.set_index(
["varname", "typeOfLevel", "stepType", "level", "valid_time"]
)
if not r_index.index.is_unique:
dups = r_index.loc[r_index.index.duplicated(keep=False), :]
logger.warning(
"The grib hierarchy in %s is not unique for %d variables: %s",
basename,
len(dups),
dups.index.get_level_values("varname").tolist(),
)
return result
def s3_parse_ecmwf_grib_idx(
fs: fsspec.AbstractFileSystem,
basename: str,
suffix: str = "index",
tstamp: Optional[pd.Timestamp] = None,
validate: bool = False,
) -> pd.DataFrame:
"""
Standalone method used to extract metadata from a grib2 index file
:param fs: the file system to read from
:param basename: the base name is the full path to the grib file
:param suffix: the suffix is the ending for the index file
:param tstamp: the timestamp to record for this index process
:return: the data frame containing the results
"""
fname = f"{basename.rsplit('.', 1)[0]}.{suffix}"
fs.invalidate_cache(fname)
fs.invalidate_cache(basename)
baseinfo = fs.info(basename)
with fs.open(fname, "r") as f:
splits = []
for idx, line in enumerate(f):
try:
# Removing the trailing characters if there's any at the end of the line
clean_line = line.strip().rstrip(',')
# Convert the JSON-like string to a dictionary
data = json.loads(clean_line)
# Extracting required fields using .get() method to handle missing keys
lidx = idx
offset = data.get("_offset", 0) # Default to 0 if missing
length = data.get("_length", 0)
date = data.get("date", "Unknown Date") # Default to 'Unknown Date' if missing
ens_number = data.get("number", -1) # Default to -1 if missing
# Append to the list as integers or the original data type
splits.append([int(lidx), int(offset),int(length), date, data, int(ens_number)])
except json.JSONDecodeError as e:
# Handle cases where JSON conversion fails
raise ValueError(f"Could not parse JSON from line: {line}") from e
result = pd.DataFrame(splits, columns=["idx", "offset", "length", "date", "attr", "ens_number"])
# Subtract the next offset to get the length using the filesize for the last value
result.loc[:, "idx_uri"] = fname
result.loc[:, "grib_uri"] = basename
if tstamp is None:
tstamp = pd.Timestamp.now()
#result.loc[:, "indexed_at"] = tstamp
result['indexed_at'] = result.apply(lambda x: tstamp, axis=1)
# Check for S3 or GCS filesystem instances to handle metadata
if "s3" in fs.protocol:
# Use ETag as the S3 equivalent to crc32c
result.loc[:, "grib_etag"] = baseinfo.get("ETag")
result.loc[:, "grib_updated_at"] = pd.to_datetime(
baseinfo.get("LastModified")
).tz_localize(None)
idxinfo = fs.info(fname)
result.loc[:, "idx_etag"] = idxinfo.get("ETag")
result.loc[:, "idx_updated_at"] = pd.to_datetime(
idxinfo.get("LastModified")
).tz_localize(None)
else:
# TODO: Fix metadata for other filesystems
result.loc[:, "grib_crc32"] = None
result.loc[:, "grib_updated_at"] = None
result.loc[:, "idx_crc32"] = None
result.loc[:, "idx_updated_at"] = None
if validate and not result["attrs"].is_unique:
raise ValueError(f"Attribute mapping for grib file {basename} is not unique)")
print(f'Completed index files and found {len(result.index)} entries in it')
return result.set_index("idx")
class KerchunkZarrDictStorageManager:
"""Manages storage and retrieval of Kerchunk Zarr dictionaries in Google Cloud Storage."""
def __init__(
self,
bucket_name: str,
service_account_file: Optional[str] = None,
service_account_info: Optional[Dict] = None
):
"""
Initialize the storage manager with GCP credentials.
Args:
bucket_name (str): Name of the GCS bucket
service_account_file (str, optional): Path to service account JSON file
service_account_info (dict, optional): Service account info as dictionary