def read_cmap(datafile, yearmin=None, yearmax=None, pentad_day=3): """Read CMAP pentad data for selected years. Parameters ---------- datafile : str File path for CMAP data in NetCDF format. yearmin, yearmax : ints, optional Min and max years (inclusive) to extract. pentad_day : int, optional Which day of pentad to use for day coordinate. Returns ------- precip : xray.DataArray CMAP pentad data for selected years, reshaped to [year, pentad, lat, lon]. """ NPYR = 73 # Number of pentads per year YRMIN = 1979 # First year of CMAP data if yearmin is None: yearmin = YRMIN # Read data with xray.open_dataset(datafile) as ds: precip = ds['precip'].load() # Discard incomplete year at end ny = precip.shape[0] // NPYR precip = precip[:ny*NPYR] # Split into individual years years = np.arange(YRMIN, YRMIN + ny) pentads = np.arange(1, NPYR + 1) precip = atm.split_timedim(precip, NPYR, time0_name='year', time0_vals=years, time1_name='pentad', time1_vals=pentads) # Extract selected years if yearmax is None: years = np.arange(yearmin, YRMIN + ny) else: years = np.arange(yearmin, yearmax + 1) precip = precip.sel(year=years) precip.coords['day'] = atm.pentad_to_jday(precip['pentad'], pmin=1, day=pentad_day) precip = precip.swap_dims({'pentad' : 'day'}) return precip
datadir = '/home/jwalker/eady/datastore/' #datadir = '/home/jennifer/datastore/' # ---------------------------------------------------------------------- # MERRA Daily filename = datadir + 'merra/daily/merra_u200_198601.nc' ds = atm.ncload(filename) u = ds['U'] lat = atm.get_coord(u, 'lat') lon = atm.get_coord(u, 'lon') # Number of time points per day n = 8 # Daily mean u_split = atm.split_timedim(u, n, time0_name='day') u_new = daily_from_subdaily(u, n, dayvals=np.arange(1,32)) print(np.array_equal(u_new, u_split.mean(axis=1))) # ndarray version u_new2 = daily_from_subdaily(u.values, n) print(np.array_equal(u_new, u_new2)) # Sub-sample version i = 2 u_new3 = daily_from_subdaily(u, n, method=i) print(np.array_equal(u_split[:,i], u_new3)) # Plot data to check d = 5
datadir = '/home/jwalker/eady/datastore/' #datadir = '/home/jennifer/datastore/' # ---------------------------------------------------------------------- # MERRA Daily filename = datadir + 'merra/daily/merra_u200_198601.nc' ds = atm.ncload(filename) u = ds['U'] lat = atm.get_coord(u, 'lat') lon = atm.get_coord(u, 'lon') # Number of time points per day n = 8 # Daily mean u_split = atm.split_timedim(u, n, time0_name='day') u_new = daily_from_subdaily(u, n, dayvals=np.arange(1, 32)) print(np.array_equal(u_new, u_split.mean(axis=1))) # ndarray version u_new2 = daily_from_subdaily(u.values, n) print(np.array_equal(u_new, u_new2)) # Sub-sample version i = 2 u_new3 = daily_from_subdaily(u, n, method=i) print(np.array_equal(u_split[:, i], u_new3)) # Plot data to check d = 5 plt.figure()
datadir = '/home/jwalker/eady/datastore/' #datadir = '/home/jennifer/datastore/' # ---------------------------------------------------------------------- # NCEP2 Monthly filename = datadir + 'atmos-tools/uwnd.mon.mean.nc' ds = atm.ncload(filename) u = ds['uwnd'] lat = atm.get_coord(u, 'lat') lon = atm.get_coord(u, 'lon') # Remove Jan-Jun of 2015 tlast = -7 u = u[:tlast] unew = split_timedim(u.values, 12) unew2 = split_timedim(u.values, 12, slowfast=False) # Check that reshaping worked properly m, y, k = 11, 20, 10 data1 = u[y*12 + m, k].values data2 = unew[y, m, k] data3 = unew2[m, y, k] print(np.array_equal(data1,data2)) print(np.array_equal(data2, data3)) # Plot data to check xi, yi = np.meshgrid(lon, lat) plt.figure() plt.subplot(211) plt.pcolormesh(xi, yi, data1, cmap='jet')