Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit d8c3f49

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 7480c15 commit d8c3f49

File tree

6 files changed

+34
-54
lines changed

6 files changed

+34
-54
lines changed

nwp/excarta/merge_excarta.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
# import libs
2-
import xarray as xr
3-
import pandas as pd
4-
import numpy as np
5-
import datetime
62
import os
7-
import pathlib as Path
8-
from datetime import datetime
9-
import zarr
10-
import ocf_blosc2
3+
4+
import xarray as xr
115

126

137
def merge_zarr_files(zarr_path, merged_zarr_path):
148
# Collect paths of Zarr files in the specified directory
159
zarr_files = [
16-
os.path.join(zarr_path, file)
17-
for file in os.listdir(zarr_path)
18-
if file.endswith(".zarr")
10+
os.path.join(zarr_path, file) for file in os.listdir(zarr_path) if file.endswith(".zarr")
1911
]
2012

2113
print("1")
@@ -30,7 +22,7 @@ def merge_zarr_files(zarr_path, merged_zarr_path):
3022

3123
# Iterate over the remaining Zarr files and merge them into the initial dataset
3224
for file in zarr_files[1:]:
33-
ds = xr.open_zarr(file)
25+
xr.open_zarr(file)
3426
print(file)
3527

3628
# ds_filt = ds.sel(x=slice(*x_range), y=slice(*y_range))

nwp/excarta/parse_excarta_monthly.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Low memory script
2+
import argparse
23
import os
4+
import pathlib
35
from datetime import datetime
6+
47
import pandas as pd
58
import xarray as xr
6-
import argparse
7-
import pathlib
89

910

1011
def _parse_args():
@@ -80,8 +81,7 @@ def pdtocdf(datasets):
8081
"""
8182

8283
datasets = [
83-
ds.set_index(index=["init_time", "step", "Latitude", "Longitude"])
84-
for ds in datasets
84+
ds.set_index(index=["init_time", "step", "Latitude", "Longitude"]) for ds in datasets
8585
]
8686

8787
ds = xr.concat(datasets, dim="index")
@@ -109,7 +109,9 @@ def main():
109109
raise RuntimeError(f'Output file "{args.output}" already exist')
110110

111111
PATH = "/mnt/storage_b/data/ocf/solar_pv_nowcasting/experimental/Excarta/sr_UK_Malta_full/solar_data"
112-
month_to_process = f"{args.year}{args.month:02d}" # combine year and month arguments into the required format
112+
month_to_process = (
113+
f"{args.year}{args.month:02d}" # combine year and month arguments into the required format
114+
)
113115
datasets = load_data_from_all_years(PATH, month_to_process)
114116
ds = pdtocdf(datasets)
115117

nwp/excarta/parse_excarta_to_output.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import xarray as xr
2-
import pandas as pd
3-
import numpy as np
1+
import argparse
42
import datetime
53
import os
64
import pathlib
75
from datetime import datetime
8-
import argparse
6+
7+
import pandas as pd
8+
import xarray as xr
99

1010

1111
def _parse_args():
@@ -71,9 +71,7 @@ def pdtocdf(dfs):
7171
merged_df = pd.concat(dfs, ignore_index=True)
7272

7373
ds = xr.Dataset.from_dataframe(merged_df)
74-
ds = ds.set_index(index=["init_time", "step", "Latitude", "Longitude"]).unstack(
75-
"index"
76-
)
74+
ds = ds.set_index(index=["init_time", "step", "Latitude", "Longitude"]).unstack("index")
7775
ds = ds.drop_vars(["LocationId", "DateTimeUTC"])
7876

7977
var_names = ds.data_vars

nwp/excarta/parse_excarta_to_output_low_mem.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Low memory script
2+
import argparse
23
import os
4+
import pathlib
35
from datetime import datetime
6+
47
import pandas as pd
58
import xarray as xr
6-
import argparse
7-
import pathlib
89

910

1011
def _parse_args():
@@ -70,8 +71,7 @@ def pdtocdf(datasets):
7071
# ds = xr.merge(datasets)
7172

7273
datasets = [
73-
ds.set_index(index=["init_time", "step", "Latitude", "Longitude"])
74-
for ds in datasets
74+
ds.set_index(index=["init_time", "step", "Latitude", "Longitude"]) for ds in datasets
7575
]
7676

7777
ds = xr.concat(datasets, dim="index")

nwp/icon/app.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,12 @@ def process_model_files(
8181
var_3d_list = GLOBAL_VAR3D_LIST
8282
var_2d_list = GLOBAL_VAR2D_LIST
8383
lon_ds = xr.open_dataset(
84-
list(
85-
glob(
86-
os.path.join(folder, run, f"{var_base}_time-invariant_*_CLON.grib2")
87-
)
88-
)[0],
84+
list(glob(os.path.join(folder, run, f"{var_base}_time-invariant_*_CLON.grib2")))[0],
8985
engine="cfgrib",
9086
backend_kwargs={"errors": "ignore"},
9187
)
9288
lat_ds = xr.open_dataset(
93-
list(
94-
glob(
95-
os.path.join(folder, run, f"{var_base}_time-invariant_*_CLAT.grib2")
96-
)
97-
)[0],
89+
list(glob(os.path.join(folder, run, f"{var_base}_time-invariant_*_CLAT.grib2")))[0],
9890
engine="cfgrib",
9991
backend_kwargs={"errors": "ignore"},
10092
)
@@ -156,9 +148,7 @@ def process_model_files(
156148
print(var_2d)
157149
try:
158150
ds = xr.open_mfdataset(
159-
os.path.join(
160-
folder, run, f"{var_base}_single-level_*_*_{var_2d.upper()}.grib2"
161-
),
151+
os.path.join(folder, run, f"{var_base}_single-level_*_*_{var_2d.upper()}.grib2"),
162152
engine="cfgrib",
163153
combine="nested",
164154
concat_dim="step",
@@ -203,9 +193,7 @@ def upload_to_hf(dataset_xr, folder, model="global", run="00", token=None):
203193
"longitude": 350,
204194
"isobaricInhPa": -1,
205195
}
206-
encoding = {
207-
var: {"compressor": Blosc2("zstd", clevel=9)} for var in dataset_xr.data_vars
208-
}
196+
encoding = {var: {"compressor": Blosc2("zstd", clevel=9)} for var in dataset_xr.data_vars}
209197
encoding["time"] = {"units": "nanoseconds since 1970-01-01"}
210198
with zarr.ZipStore(
211199
zarr_path,

scripts/convert_icon_archive.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
77
"""
88

9+
import multiprocessing as mp
910
import os
11+
import subprocess
1012
from glob import glob
13+
from pathlib import Path
1114

1215
import xarray as xr
1316
import zarr
@@ -18,10 +21,7 @@
1821
EU_VAR2D_LIST,
1922
EU_VAR3D_LIST,
2023
)
21-
import subprocess
2224

23-
from pathlib import Path
24-
import multiprocessing as mp
2525

2626
def decompress(full_bzip_filename: Path, temp_pth: Path) -> str:
2727
"""
@@ -38,7 +38,7 @@ def decompress(full_bzip_filename: Path, temp_pth: Path) -> str:
3838
base_nat_filename = os.path.splitext(base_bzip_filename)[0]
3939
full_nat_filename = os.path.join(temp_pth, base_nat_filename)
4040
if os.path.exists(full_nat_filename):
41-
return full_nat_filename # Don't decompress a second time
41+
return full_nat_filename # Don't decompress a second time
4242
with open(full_nat_filename, "wb") as nat_file_handler:
4343
process = subprocess.run(
4444
["pbzip2", "--decompress", "--keep", "--stdout", full_bzip_filename],
@@ -179,8 +179,8 @@ def upload_to_hf(dataset_xr, folder, model="eu", run="00", token=None):
179179
encoding = {var: {"compressor": Blosc2("zstd", clevel=9)} for var in dataset_xr.data_vars}
180180
encoding["time"] = {"units": "nanoseconds since 1970-01-01"}
181181
with zarr.ZipStore(
182-
zarr_path,
183-
mode="w",
182+
zarr_path,
183+
mode="w",
184184
) as store:
185185
dataset_xr.chunk(chunking).to_zarr(store, encoding=encoding, compute=True)
186186
done = False
@@ -189,10 +189,10 @@ def upload_to_hf(dataset_xr, folder, model="eu", run="00", token=None):
189189
api.upload_file(
190190
path_or_fileobj=zarr_path,
191191
path_in_repo=f"data/{dataset_xr.time.dt.year.values}/"
192-
f"{dataset_xr.time.dt.month.values}/"
193-
f"{dataset_xr.time.dt.day.values}/"
194-
f"{dataset_xr.time.dt.year.values}{str(dataset_xr.time.dt.month.values).zfill(2)}{str(dataset_xr.time.dt.day.values).zfill(2)}"
195-
f"_{str(dataset_xr.time.dt.hour.values).zfill(2)}.zarr.zip",
192+
f"{dataset_xr.time.dt.month.values}/"
193+
f"{dataset_xr.time.dt.day.values}/"
194+
f"{dataset_xr.time.dt.year.values}{str(dataset_xr.time.dt.month.values).zfill(2)}{str(dataset_xr.time.dt.day.values).zfill(2)}"
195+
f"_{str(dataset_xr.time.dt.hour.values).zfill(2)}.zarr.zip",
196196
repo_id="openclimatefix/dwd-icon-global"
197197
if model == "global"
198198
else "openclimatefix/dwd-icon-eu",

0 commit comments

Comments
 (0)