#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Sun Sep 11 08:42:49 EDT 2022 if __name__ == '__main__': import sys from misc.timer import Timer tt = Timer('start ' + ' '.join(sys.argv)) import sys, os.path, os, glob, datetime import xarray as xr, numpy as np, pandas as pd, matplotlib.pyplot as plt #more imports from cftime import DatetimeNoLeap # if __name__ == '__main__': tt.check('end import') # #start from here #degK to degC for use in AM2.1 ifile = sys.argv[1] if '.nc' in ifile: ofile = ifile.replace('.nc', '.degC.nc') else: print('input must be nc file') sys.exit() if os.path.exists(ofile) and not 'o' in sys.argv: print('[exists]:', ofile) sys.exit() else: with xr.set_options(keep_attrs=True): ds = xr.open_dataset(ifile) ds['sst'] = ds['sst'] - 273.15 ds['sst'].attrs['units'] = 'degC' print('saving...') #nc_encoding = {'time':{'dtype': 'double', '_FillValue': None, 'calendar': 'noleap', 'units': f'days since {years.start}-01-16 00:00:00'}, nc_encoding = {'time':{'dtype': 'double', '_FillValue': None, 'calendar': 'noleap'}, 'lon':{'_FillValue': None}, 'lat':{'_FillValue': None}, 'sst':{'dtype': 'float32', '_FillValue': None}, } ds.to_netcdf(ofile, encoding=nc_encoding, unlimited_dims='time') print('[saved]:', ofile) if __name__ == '__main__': #from wyconfig import * #my plot settings #savefig if 'savefig' in sys.argv or 's' in sys.argv: figname = __file__.replace('.py', f'.png') if 'overwritefig' in sys.argv or 'o' in sys.argv: wysavefig(figname, overwritefig=True) else: wysavefig(figname) tt.check(f'**Done**') print() if 'notshowfig' in sys.argv: pass else: plt.show()