|
| 1 | +# import libs |
| 2 | +import xarray as xr |
| 3 | +import pandas as pd |
| 4 | +import numpy as np |
| 5 | +import datetime |
| 6 | +import os |
| 7 | +import pathlib as Path |
| 8 | +from datetime import datetime |
| 9 | +import zarr |
| 10 | +import ocf_blosc2 |
| 11 | + |
| 12 | +def merge_zarr_files(zarr_path, merged_zarr_path): |
| 13 | + # Collect paths of Zarr files in the specified directory |
| 14 | + zarr_files = [os.path.join(zarr_path, file) for file in os.listdir(zarr_path) if file.endswith('.zarr')] |
| 15 | + |
| 16 | + print("1") |
| 17 | + # Open the first Zarr file to create the initial dataset |
| 18 | + merged_ds = xr.open_zarr(zarr_files[0]) |
| 19 | + |
| 20 | + print("2") |
| 21 | + |
| 22 | + # Define the specific range of x and y coordinates |
| 23 | + # x_range = (-10, 2) # Example x coordinate range |
| 24 | + # y_range = (49, 59) # Example y coordinate range |
| 25 | + |
| 26 | + # Iterate over the remaining Zarr files and merge them into the initial dataset |
| 27 | + for file in zarr_files[1:]: |
| 28 | + ds = xr.open_zarr(file) |
| 29 | + print(file) |
| 30 | + |
| 31 | + # ds_filt = ds.sel(x=slice(*x_range), y=slice(*y_range)) |
| 32 | + merged_ds = merged_ds.combine_first(ds_filt) |
| 33 | + |
| 34 | + print("3") |
| 35 | + |
| 36 | + # Rechunk the merged dataset |
| 37 | + merged_ds = merged_ds.chunk(chunks={"init_time": 10, "x": 100, "y": 100}) |
| 38 | + |
| 39 | + print("4") |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + print(merged_ds) |
| 45 | + |
| 46 | + # Save the merged dataset as a new Zarr file |
| 47 | + merged_ds.to_zarr(merged_zarr_path) |
| 48 | + |
| 49 | + print("5") |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +# Specify the path where the independent Zarr files are located |
| 55 | +zarr_path = "/mnt/storage_b/data/ocf/solar_pv_nowcasting/experimental/Excarta/sr_UK_Malta_full/zarr_format/r3" |
| 56 | + |
| 57 | +# Specify the path for the merged Zarr file |
| 58 | +merged_zarr_path = "/mnt/storage_b/data/ocf/solar_pv_nowcasting/experimental/Excarta/sr_UK_Malta_full/merged_excarta/merged_r3_UK_full_t1.zarr" |
| 59 | + |
| 60 | +# Merge the Zarr files |
| 61 | +merge_zarr_files(zarr_path, merged_zarr_path) |
| 62 | + |
0 commit comments