Esempio n. 1
0
def read(dirname, track, warnings=[], step=1. / 24.):
    dd_index = ds.readdir(dirname,
                          variables=['time0', 'latitude', 'longitude'],
                          jd=True)
    start_time = track['time'][0]
    end_time = track['time'][1]
    dd = []
    for d_index in dd_index:
        time = d_index['time0']
        lon = d_index['longitude']
        lon = np.where(lon < 0., 360. + lon, lon)
        lat = d_index['latitude']
        filename = d_index['filename']
        ii = np.where((time >= start_time - GRACE_TIME)
                      & (time <= end_time + GRACE_TIME))[0]
        for i in ii:
            i2 = np.argmin(np.abs(track['time'] - time[i]))
            lon0 = track['lon'][i2]
            lat0 = track['lat'][i2]
            l = np.argmin((lon - lon0)**2 + (lat - lat0)**2)
            j, k = np.unravel_index(l, lon.shape)
            # print('<- %s' % filename)
            d = ds.read(filename,
                        variables=VARIABLES,
                        sel={
                            'time0': i,
                            'rlat': j,
                            'rlon': k
                        })
            if not set(VARIABLES).issubset(d.keys()):
                continue
            clw = d['model_qcl']
            cli = d['model_qcf']
            cl = 100. * np.ones(len(clw), dtype=np.float64)
            ps = 2 * d['model_press'][0] - d['model_press'][1]
            orog = max(0., 2 * d['hybridt32'][0] - d['hybridt32'][1])
            pfull = d['model_press']
            zfull = d['hybridt32']
            ta = d['theta_lev_temp']
            newshape4 = (1, len(clw))
            newshape3 = (1, )
            d_new = {
                'clw': clw.reshape(newshape4),
                'cli': cli.reshape(newshape4),
                'ta': ta.reshape(newshape4),
                'cl': cl.reshape(newshape4),
                'pfull': pfull.reshape(newshape4),
                'zfull': zfull.reshape(newshape4),
                'ps': [ps],
                'orog': [orog],
                'lon': np.array([lon[j, k]]),
                'lat': np.array([lat[j, k]]),
                'time': np.array([time[i]]),
                '.': META,
            }
            dd.append(d_new)
    d = ds.op.merge(dd, 'time')
    if 'time' in d:
        d['time_bnds'] = misc.time_bnds(d['time'], step)
    return d
Esempio n. 2
0
def read(dirname, track, warnings=[], step=3. / 24.):
    dd_index = ds.readdir(dirname, variables=['time', 'lat', 'lon'], jd=True)
    start_time = track['time'][0]
    end_time = track['time'][-1]
    dd = []
    for d_index in dd_index:
        time = d_index['time']
        lat = d_index['lat']
        lon = d_index['lon']
        lon = np.where(lon < 0., 360. + lon, lon)
        filename = d_index['filename']
        ii = np.nonzero((time >= start_time) & (time < end_time))[0]
        for i in ii:
            t = time[i]
            i2 = np.argmin(np.abs(track['time'] - time[i]))
            lat0 = track['lat'][i2]
            lon0 = track['lon'][i2]
            j = np.argmin(np.abs(lat - lat0))
            k = np.argmin(np.abs(lon - lon0))
            d = ds.read(filename,
                        variables=VARIABLES,
                        sel={
                            'time': i,
                            'lat': j,
                            'lon': k
                        })
            clw = d['QL'][::-1]
            cli = d['QI'][::-1]
            cl = d['CLOUD'][::-1] * 100.
            ps = d['PS']
            orog = d['PHIS'] / 9.80665
            pfull = d['PL'][::-1]
            zfull = d['H'][::-1]
            ta = d['T'][::-1]
            nlev = len(clw)
            newshape4 = (1, nlev)
            newshape3 = (1, )
            d_new = {
                'clw': clw.reshape(newshape4),
                'cli': cli.reshape(newshape4),
                'ta': ta.reshape(newshape4),
                'cl': cl.reshape(newshape4),
                'pfull': pfull.reshape(newshape4),
                'zfull': zfull.reshape(newshape4),
                'ps': ps.reshape(newshape3),
                'orog': orog.reshape(newshape3),
                'lat': np.array([lat[j]]),
                'lon': np.array([lon[k]]),
                'time': np.array([t]),
                '.': META,
            }
            dd.append(d_new)
    d = ds.op.merge(dd, 'time')
    if 'time' in d:
        d['time_bnds'] = misc.time_bnds(d['time'], step)
    d['.'] = META
    return d
Esempio n. 3
0
def read(dirname, track, warnings=[], step=3. / 24.):
    dd_index = ds.readdir(dirname, variables=['XTIME'], jd=True)
    start_time = track['time'][0]
    end_time = track['time'][-1]
    dd = []
    for d_index in dd_index:
        time = d_index['XTIME'][0]
        time0 = d_index['.']['.']['SIMULATION_START_DATE']
        time0 = aq.from_iso(time0.replace('_', 'T'))
        if time < 2000000.:
            time = time0 + time / (24. * 60.)
        filename = d_index['filename']
        if (time >= start_time - step * 0.5) & (time < end_time + step * 0.5):
            k = np.argmin(np.abs(track['time'] - time))
            lon0 = track['lon'][k]
            lat0 = track['lat'][k]
            d = ds.read(filename, variables=VARS, sel={'Time': 0})
            lon = np.where(d['XLONG'] < 0., 360. + d['XLONG'], d['XLONG'])
            lat = d['XLAT']
            l = np.argmin((lon - lon0)**2 + (lat - lat0)**2)
            i, j = np.unravel_index(l, lon.shape)
            clw = d['QCLOUD'][:, i, j]
            cli = d['QICE'][:, i, j]
            cl = 100. * np.ones(len(clw), dtype=np.float64)
            ps = d['PSFC'][i, j]
            orog = d['HGT'][i, j]
            pfull = d['PB'][:, i, j] + d['P'][:, i, j]
            zfull = (d['PHB'][:, i, j] + d['PH'][:, i, j]) / 9.81
            zfull = 0.5 * (zfull[1:] + zfull[:-1])
            theta = d['T'][:, i, j] + d['T00']
            ta = theta * (pfull / ps)**KAPPA
            newshape3 = [1] + list(clw.shape)
            newshape2 = [1] + list(ps.shape)
            d_new = {
                'clw': clw.reshape(newshape3),
                'cli': cli.reshape(newshape3),
                'ta': ta.reshape(newshape3),
                'cl': cl.reshape(newshape3),
                'pfull': pfull.reshape(newshape3),
                'zfull': zfull.reshape(newshape3),
                'ps': ps.reshape(newshape2),
                'orog': orog.reshape(newshape2),
                'lon': np.array([lon[i, j]]),
                'lat': np.array([lat[i, j]]),
                'time': np.array([time]),
                '.': META,
            }
            dd.append(d_new)
    d = ds.op.merge(dd, 'time')
    if 'time' in d:
        d['time_bnds'] = misc.time_bnds(d['time'], step, start_time, end_time)
        d['time'] = np.mean(d['time_bnds'], axis=1)
    d['.'] = META
    return d
Esempio n. 4
0
def read(dirname, track, warnings=[], step=1./24.):
	d_orog = ds.read(os.path.join(dirname, 'qrparm.orog.nc'), [
		'latitude',
		'longitude',
		'surface_altitude',
	])

	dd_idx = ds.readdir(dirname,
		variables=['TALLTS', 'latitude_t', 'longitude_t', 'DALLTH_zsea_theta'],
		jd=True,
		full=True,
		warnings=warnings,
	)
	start_time = track['time'][0]
	end_time = track['time'][-1]

	dd = []
	for d_idx in dd_idx:
		if 'TALLTS' not in d_idx:
			continue

		time = d_idx['TALLTS']
		lat = d_idx['latitude_t']
		lon = d_idx['longitude_t']
		filename = d_idx['filename']

		ii = np.nonzero(
			(time >= start_time - step*0.5) &
			(time < end_time + step*0.5)
		)[0]
		for i in ii:
			t = time[i]
			i2 = np.argmin(np.abs(track['time'] - time[i]))
			lat0 = track['lat'][i2]
			lon0 = track['lon'][i2]
			j = np.argmin(np.abs(lat - lat0))
			k = np.argmin(np.abs(lon - lon0))
			d = ds.read(filename, VARS,
				sel={'TALLTS': [i], 'latitude_t': j, 'longitude_t': k},
				jd=True,
			)
			for a, b in TRANS.items():
				if a in d.keys():
					ds.rename(d, a, b)
			d['lat'] = np.array([d['lat']])
			d['lon'] = np.array([d['lon']])
			d['.']['lat']['.dims'] = ['time']
			d['.']['lon']['.dims'] = ['time']
			orog = d_orog['surface_altitude'][j,k]
			d['zfull'] = d['eta']*85000. + orog*(1. - d['eta']/d['eta'][51])**2
			d['zfull'] = d['zfull'].reshape([1, len(d['zfull'])])
			d['.']['zfull'] = {'.dims': ['time', 'level']}
			d['orog'] = np.array([orog], np.float64)
			d['.']['orog'] = {'.dims': ['time']}
			del d['eta']
			dd.append(d)
	d = ds.op.merge(dd, 'time')
	d['cl'] *= 100.
	if 'time' in d:
		d['time_bnds'] = misc.time_bnds(d['time'], step, start_time, end_time)
		d['time'] = np.mean(d['time_bnds'], axis=1)
	d['.'] = META
	return d
Esempio n. 5
0
def read(dirname, track, warnings=[], step=6. / 24.):
    dd_index = ds.readdir(
        dirname,
        variables=['time', 'latitude', 'longitude', 'level_height'],
        jd=True,
        full=True,
        warnings=warnings,
    )
    start_time = track['time'][0]
    end_time = track['time'][-1]
    d_var = {}
    for var in VARIABLES:
        dd = []
        for d_index in dd_index:
            if var not in d_index['.']:
                continue
            time = d_index['time']
            time_half = misc.half(time)
            lat = d_index['latitude']
            lon = d_index['longitude']
            level_height = d_index['level_height']
            filename = d_index['filename']
            ii = np.nonzero((time_half[1:] >= start_time)
                            & (time_half[:-1] < end_time))[0]
            for i in ii:
                t = time[i]
                i2 = np.argmin(np.abs(track['time'] - time[i]))
                lat0 = track['lat'][i2]
                lon0 = track['lon'][i2]
                j = np.argmin(np.abs(lat - lat0))
                k = np.argmin(np.abs(lon - lon0))
                d = ds.read(filename,
                            variables=[var],
                            sel={
                                'time': i,
                                'latitude': j,
                                'longitude': k
                            })
                d_new = {
                    'lat': np.array([lat[j]]),
                    'lon': np.array([lon[k]]),
                    'time': np.array([t]),
                    'level_height': np.array([level_height]),
                    '.': META,
                }
                d_new['.']['level_height'] = {'.dims': ['level']}
                d_new[TRANS[var]] = d[var].reshape([1] + list(d[var].shape))
                if TRANS[var] == 'cl':
                    d_new[TRANS[var]] *= 100.
                dd.append(d_new)
        if len(dd) > 0:
            d_var[TRANS[var]] = ds.op.merge(dd, 'time')
    time_list = [set(d_var[var]['time']) for var in d_var.keys()]
    time = time_list[0].intersection(*time_list[1:]) \
     if len(time_list) > 0 \
     else set()
    d = {}
    d['.'] = {}
    if len(time) == 0:
        return None
    for var in d_var.keys():
        idx = [i for i, t in enumerate(d_var[var]['time']) if t in time]
        ds.select(d_var[var], {'time': idx})
        d[var] = d_var[var][var]
        d['lat'] = d_var[var]['lat']
        d['lon'] = d_var[var]['lon']
        d['time'] = d_var[var]['time']
        d['level_height'] = d_var[var]['level_height']
    if 'ps' not in d:
        n, m = d['pfull'].shape
        d['ps'] = np.full(n, np.nan, dtype=np.float64)
        for i in range(n):
            d['ps'][i] = 2 * d['pfull'][i, 0] - d['pfull'][i, 1]
    if 'zfull' not in d:
        n, m = d['pfull'].shape
        d['zfull'] = np.full((n, m), np.nan, dtype=np.float64)
        for i in range(n):
            d['zfull'][i, :] = d['level_height']
    del d['level_height']
    if 'orog' not in d:
        n, m = d['zfull'].shape
        d['orog'] = np.full(n, np.nan, dtype=np.float64)
        for i in range(n):
            d['orog'][i] = 2 * d['zfull'][i, 0] - d['zfull'][i, 1]
    if 'time' in d:
        d['time_bnds'] = misc.time_bnds(d['time'], step)
    d['.'] = META
    return d
Esempio n. 6
0
def stream(dd, state, dirname):
    if 'd_idx' not in state:
        d_idx = ds.readdir(dirname, ['time'], merge='time')
        state['d_idx'] = d_idx
    return misc.stream(dd, state, couple, d_idx=state['d_idx'])
Esempio n. 7
0
def read0(type_, dirname, track, warnings=[], step=1. / 24.):
    dd_idx = ds.readdir(
        dirname,
        variables=['time', 'latitude', 'longitude'],
        jd=True,
        full=True,
        warnings=warnings,
    )
    start_time = track['time'][0]
    end_time = track['time'][-1]

    vars = {
        'surf': VARS_SURF,
        'plev': VARS_PLEV,
    }[type_]

    trans = {
        'surf': TRANS_SURF,
        'plev': TRANS_PLEV,
    }[type_]

    dd = []
    for d_idx in dd_idx:
        time = d_idx['time']
        lat = d_idx['latitude']
        lon = d_idx['longitude']
        filename = d_idx['filename']

        ii = np.nonzero((time >= start_time - step * 0.5)
                        & (time < end_time + step * 0.5))[0]
        for i in ii:
            t = time[i]
            i2 = np.argmin(np.abs(track['time'] - time[i]))
            lat0 = track['lat'][i2]
            lon0 = track['lon'][i2]
            j = np.argmin(np.abs(lat - lat0))
            k = np.argmin(np.abs(lon - lon0))
            d = ds.read(
                filename,
                vars,
                sel={
                    'time': [i],
                    'latitude': j,
                    'longitude': k
                },
                jd=True,
            )
            for a, b in trans.items():
                if a in d.keys():
                    ds.rename(d, a, b)
            d['lat'] = np.array([d['lat']])
            d['lon'] = np.array([d['lon']])
            d['.']['lat']['.dims'] = ['time']
            d['.']['lon']['.dims'] = ['time']
            if type_ == 'plev':
                d['pfull'] = d['pfull'].reshape([1, len(d['pfull'])])
                d['.']['pfull']['.dims'] = ['time', 'level']
                d['cl'] = d['cl'][:, ::-1]
                d['clw'] = d['clw'][:, ::-1]
                d['cli'] = d['cli'][:, ::-1]
                d['ta'] = d['ta'][:, ::-1]
                d['zfull'] = d['zfull'][:, ::-1]
                d['pfull'] = d['pfull'][:, ::-1]
            dd.append(d)
    d = ds.op.merge(dd, 'time')
    if 'time' in d:
        d['time_bnds'] = misc.time_bnds(d['time'], step, start_time, end_time)
        d['time'] = np.mean(d['time_bnds'], axis=1)
    if 'pfull' in d:
        d['pfull'] = 1e2 * d['pfull']
    if 'zfull' in d:
        d['zfull'] /= 9.80665
    if 'orog' in d:
        d['orog'] /= 9.80665
    if 'cl' in d:
        d['cl'] *= 100.
    d['.'] = META
    return d
Esempio n. 8
0
def read(dirname, track, warnings=[], step=6. / 24.):
    d_ll = ds.read(os.path.join(dirname, 'LL125.nc'),
                   ['latitude', 'longitude', 'z'])
    lat_ll = d_ll['latitude']
    lon_ll = d_ll['longitude']
    orog_ll = d_ll['z'][0, :, :] / 9.80665

    dd_idx = ds.readdir(
        dirname,
        variables=['time', 'latitude', 'longitude'],
        jd=True,
        full=True,
        warnings=warnings,
    )
    start_time = track['time'][0]
    end_time = track['time'][-1]
    d_out = {}
    for var in VARS:
        dd = []
        var2 = TRANS[var]
        for d_idx in dd_idx:
            if var not in d_idx['.']:
                continue
            time = d_idx['time']
            lat = d_idx['latitude']
            lon = d_idx['longitude']
            filename = d_idx['filename']
            ii = np.nonzero((time >= start_time - step * 0.5)
                            & (time < end_time + step * 0.5))[0]
            for i in ii:
                t = time[i]
                i2 = np.argmin(np.abs(track['time'] - time[i]))
                lat0 = track['lat'][i2]
                lon0 = track['lon'][i2]
                j = np.argmin(np.abs(lat - lat0))
                k = np.argmin(np.abs(lon - lon0))
                j_ll = np.argmin(np.abs(lat_ll - lat0))
                k_ll = np.argmin(np.abs(lon_ll - lon0))
                d = ds.read(
                    filename,
                    VARS_AUX + [var],
                    sel={
                        'time': [i],
                        'latitude': j,
                        'longitude': k,
                    },
                    jd=True,
                )
                for a, b in TRANS.items():
                    if a in d.keys():
                        ds.rename(d, a, b)
                d['lat'] = np.array([d['lat']])
                d['lon'] = np.array([d['lon']])
                d['orog'] = np.array([orog_ll[j_ll, k_ll]])
                d['.']['lat']['.dims'] = ['time']
                d['.']['lon']['.dims'] = ['time']
                d['.']['orog'] = {'.dims': ['time']}
                if 'pfull' in ds.get_vars(d):
                    d['pfull'] = d['pfull'].reshape([1, len(d['pfull'])])
                    d['.']['pfull']['.dims'] = ['time', 'pfull']
                    d['pfull'] = d['pfull'][:, ::-1]
                    d[var2] = d[var2][:, ::-1]
                    ds.select(d, {'pfull': np.arange(27)})
                dd.append(d)
        d = ds.op.merge(dd, 'time')
        for var_aux in VARS_AUX:
            if TRANS[var_aux] in ds.get_vars(d_out) \
             and TRANS[var_aux] in ds.get_vars(d) \
             and not np.all(d_out[TRANS[var_aux]] == d[TRANS[var_aux]]):
                raise ValueError('%s: Field differs between input files' %
                                 TRANS[var_aux])
        d_out.update(d)
    d_out['pfull'] = d_out['pfull'] * 1e2
    if 'time' in d_out:
        d_out['time_bnds'] = misc.time_bnds(d_out['time'], step, start_time,
                                            end_time)
        d_out['time'] = np.mean(d_out['time_bnds'], axis=1)
    d_out['.'] = META
    return d_out