Last active
July 27, 2021 19:37
-
-
Save Zeitsperre/f799cb86f118b9f58d3e0b5e63fca1e3 to your computer and use it in GitHub Desktop.
Subsetting NetCDF4 with xarray and clisops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Install Anaconda3 https://www.anaconda.com/products/individual (Beginner-level, larger install) | |
or Miniconda3: https://docs.conda.io/en/latest/miniconda.html (Experienced, minimal install) | |
In a terminal / command prompt with `conda` installed: | |
$ conda create --name data_processing -c conda-forge python=3.7 xarray clisops | |
$ conda activate data_processing | |
<Modify paths to folders (source files, target_folder)> | |
$ python subset_clisops_xarray.py | |
""" | |
from pathlib import Path | |
import xarray as xr | |
from clisops.core import subset | |
vector = Path("/Path/to/a/shapefile.shp/or/geojson.json") # CRS should be WGS84 or NAD83 (decimal degrees). | |
source_file_folder = Path("/Path/to/the/top-level/folder/of/climate/data/") # Will subset all files within folder. | |
source_files = sorted([file for file in source_file_folder.rglob("*.nc")]) | |
target_folder = Path( | |
"/Path/to/another/folder/for/newly/subsetted/files/" | |
) # Folder must exist and have write access! | |
def subsetter_function(files, shape, output_folder): | |
for file in files: | |
# Subset with shapefile/geojson. | |
ds = subset.subset_shape( | |
xr.open_dataset(file, decode_times=False), # Open NetCDF4 with xarray. | |
shape=shape, # Supply a geometry to clip with. | |
) | |
# Name file based on location and filename of older file. | |
output_name = f"{file.stem}_subset.nc" | |
parent_folder = file.parts[-3: -1] # /scenario/variable/ | |
output_folder = output_folder.joinpath(parent_folder) # /output_folder/scenario/variable/ | |
output_folder.mkdir(exist_ok=True, parents=True) | |
# Write out object as a new NetCDF4 | |
ds.to_netcdf(output_folder.joinpath(output_name)) | |
if __name__ == "__main__": | |
subsetter_function(source_files, vector, target_folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment