#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Wed Sep 18 21:10:30 EDT 2019 from datetime import datetime import os.path, sys, os #import matplotlib.pyplot as plt import xarray as xr, numpy as np, pandas as pd #import geoxarray import xaddon expnames = ['m6p0sol_CTL1860_tigercpu_intelmpi_18_576PE', 'p6p0sol_CTL1860_tigercpu_intelmpi_18_576PE', 'm6p0sol_CTL1860_recover0501_tigercpu_intelmpi_18_576PE', 'CTL1860_newdiag_tigercpu_intelmpi_18_576PE'] year_ranges = [range(101, 110+1), range(201, 400+1), range(451, 550+1), range(561, 590+1)] data_name = 't_surf' tag = 'lintrend' def do_lintrend(expname, data_name, years): '''get global mean of a variable from model output''' ofile = f'{expname}.{data_name}.{tag}.{years[0]:04d}-{years[-1]:04d}.nc' if os.path.exists(ofile): print('[exists]:', ofile) return ifiles = [f'/tigress/wenchang/MODEL_OUT/{expname}/POSTP/{year:04d}0101.atmos_month.nc' for year in years] # open dataset with xr.open_mfdataset(ifiles) as ds: da = ds[data_name] attrs = da.attrs # save the raw attrs # lintrend da = da.groupby('time.year').mean('time') ds_rg = da.linregress.on(da.year, dim='year') # save to netcdf encoding = None#{data_name_new: {'zlib': True, 'complevel': 1}} ds_rg.to_netcdf(ofile, encoding=encoding) print('[saved]:', ofile) if __name__ == '__main__': # record the start time tformat = '%Y-%m-%d %H:%M:%S' t0 = datetime.now() print('[time start]:', t0.strftime(tformat)) for expname in expnames[0:1]: for years in year_ranges: print(expname, data_name, tag) do_linregress(expname, data_name, years) # the end t1 = datetime.now() dt = t1 - t0 print('[time end]:', t1.strftime(tformat)) print('[time used]:', f'{dt.seconds:,} seconds')