Skip to content

Instantly share code, notes, and snippets.

@aidanheerdegen
Last active August 8, 2022 15:47
Show Gist options
  • Save aidanheerdegen/4b40b0ab94d0059f3ca636eea801d69b to your computer and use it in GitHub Desktop.
Save aidanheerdegen/4b40b0ab94d0059f3ca636eea801d69b to your computer and use it in GitHub Desktop.
Using xarray to calculate daily averages over multiple years
import numpy as np
from netCDF4 import Dataset
import sys
import xarray
from xarray.ufuncs import *
from glob import glob
import os
import string
# Path to the files directories
directory = '/g/data1/v45/mom01_comparison/KDS75/'
start_index = 150
end_index = 158
file_paths=[]
for index in range(start_index, end_index+1):
file_paths.extend(glob(os.path.join(directory,'output{:03d}'.format(index),'rregionoceankerg__*.nc')))
print 'Found ',len(file_paths),' files'
# Open the list of files as a single dataset
ds = xarray.open_mfdataset(file_paths, engine='netcdf4', chunks={'time':1,'st_ocean':11,'yu_ocean_sub01':133,'xu_ocean_sub01':515},decode_times=False,mask_and_scale=True)
ds["time"].attrs["units"] = 'days since 1900-01-01'
ds["time"].attrs["calendar_type"] = string.lower(ds["time"].attrs["calendar_type"])
ds["time"].attrs["calendar"] = string.lower(ds["time"].attrs["calendar"])
ds = xarray.decode_cf(ds,decode_times=True)
print ds["time"]
# attrs = {'units': 'days since 1700-01-01'}
mld = ds["mld"]
mldave = mld.groupby("time.month").mean(dim='time')
print mldave.shape
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment