Skip to content

Instantly share code, notes, and snippets.

@raphaeldussin
Created December 3, 2020 02:45
Show Gist options
  • Save raphaeldussin/f40621068b6b5fc48e14d38ec50fb094 to your computer and use it in GitHub Desktop.
Save raphaeldussin/f40621068b6b5fc48e14d38ec50fb094 to your computer and use it in GitHub Desktop.
main code for roms2zarr conversion
import xarray as xr
import glob
# use glob to create a list of files to convert
filelist = glob.glob('data/CCS2_avg_*.nc')
# path to grid file
gridfile = 'grid/CCS2_grid.nc'
# output store name
store_name = 'CCS2_store'
# load the grid
ds_grid = xr.open_dataset(gridfile)
# drop some unnecessary variables
ds_grid = ds_grid.drop_vars(['hraw', 'spherical',
'lon_vert', 'lat_vert',
'x_vert', 'y_vert',
'Cs_r', 'Cs_w', 'hc'])
# select interior points for grid variables
ds_grid = select_interior(ds_grid)
first=True
# iterate on files
for ncfile in filelist:
ds = xr.open_dataset(ncfile)
ds = remove_all_small_variables(ds)
ds = select_interior(ds)
ds = xr.merge([ds, ds_grid])
ds = rename_dims(ds)
ds = add_coords(ds)
ds = ds.chunk({'time': 1})
print(f"working on {ncfile}")
if first:
ds.to_zarr(store_name, consolidated=True, mode='w')
else:
ds.to_zarr(store_name, consolidated=True, mode='a', append_dim='time')
first=False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment