#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Wed May 26 19:43:41 EDT 2021 #get MDR/TROP sst time series from global sst data on regular lon/lat grids (lon range 0-360) if __name__ == '__main__': from misc.timer import Timer tt = Timer(f'start {__file__}') import sys, os.path, os, glob, datetime import xarray as xr, numpy as np, pandas as pd, matplotlib.pyplot as plt #more imports import geoxarray # if __name__ == '__main__': tt.check('end import') # #start from here def sst2mdrtrop(sst): """get MDR/TROP sst time series from global sst data on regular lon/lat grids (lon range 0-360)""" #mdr sst #print('MDR...') MDR = sst.sel(lat=slice(10,25), lon=slice(280,340)) \ .groupby('time.year').mean('time') \ .geo.fldmean() #MDR region: 10-25N, 80-20W if MDR.max() > 200: units = 'K' else: units = '$^\circ$C' MDR.attrs['units'] = units MDR.attrs['long_name'] = 'MDR SST' #trop sst #print('TROP...') TROP = sst.sel(lat=slice(-30,30)) \ .groupby('time.year').mean('time') \ .geo.fldmean() #TROP region: 30S-30N TROP.attrs['units'] = units TROP.attrs['long_name'] = 'TROP SST' #dataset ds = xr.Dataset(dict(MDR=MDR, TROP=TROP)) return ds if __name__ == '__main__': #from wyconfig import * #my plot settings ifile = sys.argv[1]#e.g. ts.ocean.past1000.0850-1849.maxmissing150.ens10.nc ofile = ifile.replace('ocean', 'mdrtrop') if os.path.exists(ofile): print('[exists]:', ofile) sys.exit() print('loading...') with xr.open_dataset(ifile) as ds: glbattrs = ds.attrs da = xr.open_dataarray(ifile).load() sst = da ds = sst2mdrtrop(sst) ds.attrs = glbattrs print('saving...') ds.to_netcdf(ofile) print('[saved]:', ofile) #savefig if 'savefig' in sys.argv: figname = __file__.replace('.py', f'.png') wysavefig(figname) tt.check(f'**Done**') plt.show()