#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Wed Oct 17 09:45:40 EDT 2018 import os, os.path, sys, xarray as xr, numpy as np data_name = 'sst' units = 'degrees C' long_name = 'Daily sea surface temperature' dimx, dimy, dimz, dimt = 'lon', 'lat', 'zlev', 'time' years = range(1981, 2018+1) nc_encoding = {dimt: dict(_FillValue=None, dtype='double'), dimx: dict(_FillValue=None), dimy: dict(_FillValue=None), data_name: dict(_FillValue=np.nan, dtype='float32'), } for year in years: ifiles = f'raw/avhrr-only-v2.{year}*.nc' ofile = f'{data_name}/by_year/{data_name}.avhrr-only-v2.{year}.nc' if os.path.exists(ofile): print('[File exists]:', ofile) continue else: pass ds = xr.open_mfdataset(ifiles) da = ds[data_name].squeeze(dim=dimz, drop=True) da.attrs = dict(units=units, long_name=long_name) da.to_dataset(name=data_name).to_netcdf(ofile, unlimited_dims=dimt, encoding=nc_encoding) print('[File created]:', ofile)