コード例 #1
0
ファイル: precipdat.py プロジェクト: jenfly/atmos-read
def save_gpcp_years(datafile, savedir, yearmin=1997, yearmax=2015):
    """Read GPCP daily data and save to individual file for each year."""

    savestr = savedir + '/gpcp_daily_%d.nc'

    with xray.open_dataset(datafile) as ds:
        pcp = ds['PREC']
        dates = ds['yyyyddd']
        yy = dates // 1000
        ddd = dates - 1000 * yy
        pcp.coords['year'] = yy
        pcp.coords['day'] = ddd

        years = np.arange(yearmin, yearmax + 1)
        for year in years:
            print(year)
            ind = np.where(yy.values == year)[0]
            precip = pcp[ind]
            days = precip['day'].values
            precip = precip.drop(['year', 'day'])
            precip = precip.rename({'time' : 'day'})
            precip['day'] = days
            savefile = savestr % year
            print('Saving to ' + savefile)
            atm.save_nc(savefile, precip)

    return None
コード例 #2
0
ファイル: merra-calc-mfc.py プロジェクト: jenfly/atmos-read
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)
コード例 #3
0
ファイル: save-dailyrel.py プロジェクト: jenfly/monsoon-onset
    d0 = int(index[ind_nm][y].values)
    dmin, dmax = d0 - npre, d0 + npost
    coords = {'year' : [year]}
    onset_var = xray.DataArray([d_onset], name='D_ONSET', coords=coords)
    retreat_var = xray.DataArray([d_retreat], name='D_RETREAT', coords=coords)
    for varnm in files:
        print('Reading daily data for ' + varnm)
        varid, plev = get_info(varnm)
        var = get_data_rel(varid, plev, year, files.get(varnm), data, d0, npre,
                           npost)
        var.attrs = atm.odict_delete(var.attrs, 'd_onset')
        var.name = varid
        data[varnm] = var
        savefile = get_savefile(version, savedir, varnm, onset_nm, ind_nm, year)
        print('Saving to ' + savefile)
        atm.save_nc(savefile, var, onset_var, retreat_var)

# ----------------------------------------------------------------------
# Compute climatologies and save
relfiles = {}
for key in datafiles:
    relfiles[key] = [get_savefile(version, savedir, key, onset_nm, ind_nm, yr)
                     % yr for yr in years]

for varnm in relfiles:
    varid, _ = get_info(varnm)
    var, onset, retreat = load_dailyrel(relfiles[varnm])
    ds = xray.Dataset()
    ds[varid], ds['D_ONSET'], ds['D_RETREAT'] = var, onset, retreat
    print('Computing climatological mean')
    yearnm = atm.get_coord(ds, 'year', 'name')
コード例 #4
0
ファイル: calc-merra-tt.py プロジェクト: jenfly/monsoon-onset
lon1, lon2 = 40, 100
lat1, lat2 = -15, 35
varlist = ['T']
pmin, pmax = 200e2, 600e2
years = range(1979, 2015)
months = [4, 5, 6, 7, 8, 9]

g = atm.constants.g.values
nperday = 8

subset_dict = {'lon' : (lon1, lon2), 'lat' : (lat1, lat2)}
for year in years:
    for mon in months:
        dayvals = atm.season_days(atm.month_str(mon), atm.isleap(year))
        T = merra.read_daily(varlist, year, mon, subset_dict=subset_dict)

        # Daily means of 3-hourly data
        T = atm.daily_from_subdaily(T, nperday, dayvals=dayvals)

        # Vertical integral
        Tbar = atm.int_pres(T, pmin=pmin, pmax=pmax)
        Tbar = Tbar * g / (pmax - pmin)
        Tbar.name='Tbar'
        Tbar.attrs['long_name'] = 'Vertical mean atmospheric temperature'
        Tbar.attrs['pmin'] = pmin
        Tbar.attrs['pmax'] = pmax

        # Save to file
        atm.save_nc(savefile(year, mon, pmin, pmax), T, Tbar)
コード例 #5
0
# lat, lon range to extract
lon1, lon2 = 40, 120
lat1, lat2 = -60, 60
lons = atm.latlon_labels([lon1, lon2], 'lon', deg_symbol=False)
lats = atm.latlon_labels([lat1, lat2], 'lat', deg_symbol=False)
latlon = '%s-%s_%s-%s' % (lons[0], lons[1], lats[0], lats[1])

savefile = datadir + ('merra_precip_%s_days%d-%d_%d-%d.nc' %
                      (latlon, daymin, daymax, years.min(), years.max()))

subset_dict = {'day' : (daymin, daymax), 'lat' : (lat1, lat2),
               'lon' : (lon1, lon2)}
for y, year in enumerate(years):
    datafile = datadir + 'merra_precip_%d.nc' % year
    print('Loading ' + datafile)
    with xray.open_dataset(datafile) as ds:
        precip1 = atm.subset(ds['PRECTOT'], subset_dict)
        precip1 = precip1.load()
        precip1.coords['year'] = year
    if y == 0:
        precip = precip1
    else:
        precip = xray.concat((precip, precip1), dim='year')

print('Converting to mm/day')
precip.values = atm.precip_convert(precip, precip.units, 'mm/day')
precip.attrs['units'] = 'mm/day'

print('Saving to ' + savefile)
atm.save_nc(savefile, precip)
コード例 #6
0
#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)
コード例 #7
0
calc_kw = {'latlon' : latlon, 'plevs' : plevs, 'dp_vars' : dp_vars,
           'sector_lons' : sector_lons}
nc_kw = { 'merra2' : {'format' : 'NETCDF4_classic', 'engine' : 'netcdf4'},
          'merra' : {'format' : None, 'engine' : None}}[version]

# Read data and concatenate
for year in years:
    dailyfiles = collections.defaultdict(list)
    for month in months:
        url_dict = get_url_dict(year, month, version, vargroups)
        days = range(1, atm.days_this_month(year, month) + 1)
        jdays = atm.season_days(atm.month_str(month), atm.isleap(year))
        for day, jday in zip(days, jdays):
            files = read_groups(url_dict, vargroups, datadir, year, month, day,
                                jday, calc_kw, nc_kw)
            for nm in files:
                dailyfiles[nm] += [files[nm]]
    # Consolidate daily files into yearly files and delete daily files
    for nm in dailyfiles:
        data = atm.load_concat(dailyfiles[nm], concat_dim='day')
        for varnm in data.data_vars:
            var = data[varnm]
            filenm = get_filename(var, version, datadir, year)
            var.name = var.attrs.get('varnm', varnm)
            print('Saving to ' + filenm)
            atm.save_nc(filenm, var)
        print('Deleting daily files')
        for filenm in dailyfiles[nm]:
            print(filenm)
            os.remove(filenm)
コード例 #8
0
lat1, lat2 = -20, 30
varlist = ['u', 'v', 'q']
pmin = 300e2
years = range(1979, 2015)
#months = [5, 6, 7, 8]
months = [4]

subset_dict = {'lon' : (lon1, lon2), 'lat' : (lat1, lat2)}
for year in years:
    for mon in months:
        dayvals = atm.season_days(atm.month_str(mon), atm.isleap(year))
        ds = merra.read_daily(varlist, year, mon, subset_dict=subset_dict)
        uq = ds['U'] * ds['QV']
        vq = ds['V'] * ds['QV']

        # Daily means of 3-hourly data
        nperday = 8
        uq = atm.daily_from_subdaily(uq, nperday, dayvals=dayvals)
        vq = atm.daily_from_subdaily(vq, nperday, dayvals=dayvals)

        # Vertical integral
        uq_int = atm.int_pres(uq, pmin=pmin)
        vq_int = atm.int_pres(vq, pmin=pmin)
        uq_int.name='uq_int'
        uq_int.attrs['pmin'] = pmin
        vq_int.name='vq_int'
        vq_int.attrs['pmin'] = pmin

        # Save to file
        atm.save_nc(savefile(year, mon), uq_int, vq_int)
コード例 #9
0
ファイル: scratchpad.py プロジェクト: jenfly/atmos-read
import atmos as atm
import merra
            
# ----------------------------------------------------------------------
# Save 200mb u, v daily data for one month
year = 1979
month = 1
datestr = '%d%02d' % (year, month)
savedir = '/home/jennifer/datastore/merra/daily/'

def filename(varname, datestr, savedir):
    return savedir + 'merra_' + varname + '_' + datestr + '.nc'

u = merra.load_daily(year, month, 'u', subset_dict={'plev' : (200, 200)})
atm.save_nc(filename('u200', datestr, savedir), u)

v = merra.load_daily(year, month, 'v', subset_dict={'plev' : (200, 200)})
atm.save_nc(filename('v200', datestr, savedir), v)

# ----------------------------------------------------------------------

url = ('http://goldsmr3.sci.gsfc.nasa.gov/opendap/MERRA_MONTHLY/'
    'MAIMCPASM.5.2.0/1979/MERRA100.prod.assim.instM_3d_asm_Cp.197907.hdf')

url2 = ('http://goldsmr2.sci.gsfc.nasa.gov/opendap/MERRA_MONTHLY/'
    'MATMNXFLX.5.2.0/1979/MERRA100.prod.assim.tavgM_2d_flx_Nx.197907.hdf')

ds = xray.open_dataset(url)
ds2 = xray.open_dataset(url2)
コード例 #10
0
    index.load()
index = index.sel(year=years)
d0 = index[ind_nm].values

# Precip data
if pcp_nm == 'cmap':
    pcp = precipdat.read_cmap(pcpfiles, yearmin=min(years), yearmax=max(years))

    # Interpolate to daily resolution
    name, attrs, coords, dimnames = atm.meta(pcp)
    days = np.arange(3, 364)
    interp_func = scipy.interpolate.interp1d(pcp['day'], pcp, axis=1)
    vals = interp_func(days)
    coords['day'] = xray.DataArray(days, coords={'day' : days})
    pcp = xray.DataArray(vals, dims=dimnames, coords=coords, name=name,
                         attrs=attrs)
else:
    pcp = atm.combine_daily_years(None, pcpfiles, years, yearname='year',
                                  subset_dict=subset_dict)


# Wrap from following year to get extended daily range
daymin = min(d0) - npre
daymax = max(d0) + npost
pcp = utils.wrapyear_all(pcp, daymin=daymin, daymax=daymax)

# Daily relative to onset/withdrawal
pcp_rel = utils.daily_rel2onset(pcp, d0, npre, npost)
print('Saving to ' + savefile)
atm.save_nc(savefile, pcp_rel)