datadir = atm.homedir() + 'datastore/merra/daily/' year = 2014 subset = '_40E-120E_90S-90N' def get_var(datadir, varnm, subset, year): filenm = '%smerra_%s%s_%d.nc' % (datadir, varnm, subset, year) with xray.open_dataset(filenm) as ds: var = ds[varnm].load() return var uq_int = get_var(datadir, 'UFLXQV', subset, year) vq_int = get_var(datadir, 'VFLXQV', subset, year) mfc = atm.moisture_flux_conv(uq_int, vq_int, already_int=True) mfcbar = mfc.mean(dim='YDim').mean(dim='XDim') # Test atm.gradient a = atm.constants.radius_earth.values latdim, londim = 1, 2 lat = atm.get_coord(uq_int, 'lat') latrad = np.radians(lat) latrad[abs(lat) > 89] = np.nan coslat = xray.DataArray(np.cos(latrad), coords={'YDim': lat}) lon = atm.get_coord(uq_int, 'lon') lonrad = np.radians(lon) mfc_x = atm.gradient(uq_int, lonrad, londim) / (a * coslat) mfc_y = atm.gradient(vq_int * coslat, latrad, latdim) / (a * coslat) mfc_test = mfc_x + mfc_y
import matplotlib.pyplot as plt import pandas as pd import atmos as atm import precipdat import merra # ---------------------------------------------------------------------- #version, years = 'merra', np.arange(1979, 2015) version, years = 'merra2', np.arange(1980, 2016) datadir = atm.homedir() + 'datastore/%s/daily/' % version months = np.arange(1, 13) subset = '_40E-120E_90S-90N' def get_var(datadir, version, varnm, subset, year): filenm = '%s%s_%s%s_%d.nc' % (datadir, version, varnm, subset, year) with xray.open_dataset(filenm) as ds: var = ds[varnm].load() return var for year in years: print('Calculating MFC %d' % year) uq_int = get_var(datadir, version, 'UFLXQV', subset, year) vq_int = get_var(datadir, version, 'VFLXQV', subset, year) mfc = atm.moisture_flux_conv(uq_int, vq_int, already_int=True) mfc.attrs['long_name'] = mfc.name mfc.name = 'MFC' savefile = datadir + '%s_MFC%s_%d.nc' % (version, subset, year) print('Saving MFC to ' + savefile) atm.save_nc(savefile, mfc)
#monthstr='apr-sep_' def datafile(datadir, year, mon): filn = datadir + 'merra_vimt_%d%02d.nc' % (year, mon) return filn def savefile(datadir, varnm, year, monthstr, pmin): filn = datadir + 'merra_%s_ps-%.0fmb_%s%d.nc' filn = filn % (varnm, pmin/100, monthstr, year) return filn # Read daily data from each year and month and concatenate together for y, year in enumerate(years): files = [datafile(datadir, year, mon) for mon in months] ds = atm.load_concat(files, concat_dim='day') pmin = ds['uq_int'].attrs['pmin'] filn = savefile(datadir, 'vimt', year, monthstr, pmin) print('Saving VIMT to ' + filn) ds.to_netcdf(filn) # Compute moisture flux convergence and save to files print('Calculating MFC') mfc = atm.moisture_flux_conv(ds['uq_int'], ds['vq_int'], already_int=True) mfc.attrs['long_name'] = mfc.name mfc.name = 'MFC' for key in ds['uq_int'].attrs: mfc.attrs[key] = ds['uq_int'].attrs[key] filn = savefile(datadir, 'MFC', year, monthstr, pmin) print('Saving MFC to ' + filn) atm.save_nc(filn, mfc)