Exemplo n.º 1
0
def parse_time(time):
    if len(time) != 2:
        raise ValueError('Invalid time: %s' % time)
    start = aq.from_iso(time[0])
    end = aq.from_iso(time[1])
    if start is None or end is None:
        raise ValueError('Invalid time: %s' % time)
    return [start, end]
Exemplo n.º 2
0
def read_time_periods(filename):
    tp = []
    with open(filename, 'r') as f:
        for line in f.readlines():
            if line.strip() == '':
                continue
            s1, s2 = line.split()
            start = aq.from_iso(s1)
            end = aq.from_iso(s2)
            tp.append([start, end])
    return tp
Exemplo n.º 3
0
def pts(d):
    time_trans = str.maketrans({'/': '-', ' ': 'T'})
    time = np.array(
        [aq.from_iso(x.translate(time_trans)) for x in d['date_time']],
        np.float64)
    pts = {
        'time': time,
        'ta': d['tair'],
        'p': d['press'],
        'hur': d['hum'],
        'lat': d['lat'],
        'lon': d['long'],
        'z': d['alt'],
        'ps': d['ps'],
        'tas': d['tas'],
        'hurs': d['hurs'],
        'uas': d['uas'],
        'vas': d['vas'],
        'station_lon': d['station_lon'],
        'station_lat': d['station_lat'],
        'station_z': d['station_z'],
    }
    pts['.'] = HEADER_PTS
    pts['.']['.'] = d['.'].get('.', {})
    return pts
Exemplo n.º 4
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
Exemplo n.º 5
0
def run(type_, input_, output,
	point=None,
	time=None,
	track=None,
	track_override_year=None,
	track_lon_180=False,
	**kwargs
):
	"""
alcf model - extract model data at a point or along a track

Usage:

    alcf model <type> point: { <lon> <lat> } time: { <start> <end> } <input>
    	<output> [options]
    alcf model <type> track: <track> <input> <output>

Arguments:

- `type`: input data type (see Types below)
- `input`: input directory
- `output`: output directory
- `lon`: point longitude
- `lat`: point latitutde
- `start`: start time (see Time format below)
- `end`: end time (see Time format below)
- `track`: track NetCDF file (see Track below)
- `options`: see Options below

Options:

- `track_override_year: <year>`: Override year in track.
    Use if comparing observations with a model statistically. Default: `none`.
- `--track_lon_180`: expect track longitude between -180 and 180 degrees

Types:

- `amps`: Antarctic Mesoscale Prediction System (AMPS)
- `era5`: ERA5
- `jra55`: JRA-55
- `merra2`: Modern-Era Retrospective Analysis for Research and Applications,
	Version 2 (MERRA-2)
- `nzcsm`: New Zealand Convection Scale Model (NZCSM)
- `nzesm`: New Zealand Earth System Model (NZESM) (experimental)
- `um`: UK Met Office Unified Model (UM)

Time format:

"YYYY-MM-DD[THH:MM[:SS]]", where YYYY is year, MM is month, DD is day,
HH is hour, MM is minute, SS is second. Example: 2000-01-01T00:00:00.

Track:

Track file is a NetCDF file containing 1D variables `lon`, `lat`, and `time`.
`time` is time in format conforming with the NetCDF standard,
`lon` is longitude between 0 and 360 degrees and `lat` is latitude between
-90 and 90 degrees.
	"""
	time1 = None
	track1 = None
	if track is not None:
		track1 = ds.read(track)
		if track_override_year is not None:
			date = aq.to_date(track1['time'])
			date[1][:] = track_override_year
			track1['time'] = aq.from_date(date)
		if track_lon_180:
			track1['lon'] = np.where(
				track1['lon'] > 0,
				track1['lon'],
				360. + track1['lon']
			)
		time1 = track1['time'][0], track1['time'][-1]
	elif point is not None and time is not None:
		pass
	else:
		raise ValueError('Point and time or track is required')

	if time is not None:
			time1 = [None, None]
			for i in 0, 1:
				time1[i] = aq.from_iso(time[i])
				if time1[i] is None:
					raise ValueError('Invalid time format: %s' % time[i])

	# if os.path.isdir(output):
	t1, t2 = time1[0], time1[1]
	for t in np.arange(np.floor(t1 - 0.5), np.ceil(t2 - 0.5)) + 0.5:
		output_filename = os.path.join(output, '%s.nc' % \
			aq.to_iso(t).replace(':', ''))
		d = model(type_, input_, point, time=[t, t + 1.], track=track1)
		if d is not None:
			ds.write(output_filename, d)
			print('-> %s' % output_filename)
Exemplo n.º 6
0
def run(type_, input_, output,
	point=None,
	time=None,
	track=None,
	track_override_year=None,
	track_lon_180=False,
	debug=False,
	**kwargs
):
	'''
alcf-model -- Extract model data at a point or along a track.
==========

Synopsis
--------

    alcf model <type> point: { <lon> <lat> } time: { <start> <end> } <input> <output> [options]

    alcf model <type> track: <track> <input> <output>

Arguments
---------

- `type`: Input data type (see Types below).
- `input`: Input directory.
- `output`: Output directory.
- `lon`: Point longitude.
- `lat`: Point latitutde.
- `start`: Start time (see Time format below).
- `end`: End time (see Time format below).
- `track`: Track NetCDF file (see Files below).
- `options`: See Options below.

Options
-------

- `--track_lon_180`: Expect track longitude between -180 and 180 degrees.
- `track_override_year: <year>`: Override year in track. Use if comparing observations with a model statistically. Default: `none`.

Types
-----

- `amps`: Antarctic Mesoscale Prediction System (AMPS).
- `era5`: ERA5.
- `jra55`: JRA-55.
- `merra2`: Modern-Era Retrospective Analysis for Research and Applications, Version 2 (MERRA-2).
- `nzcsm`: New Zealand Convection Scale Model (NZCSM).
- `nzesm`: New Zealand Earth System Model (NZESM). [Experimental]
- `um`: UK Met Office Unified Model (UM).

Time format
-----------

`YYYY-MM-DD[THH:MM[:SS]]`, where `YYYY` is year, `MM` is month, `DD` is day, `HH` is hour, `MM` is minute, `SS` is second. Example: `2000-01-01T00:00:00`.

Files
-----

The track file is a NetCDF file containing 1D variables `lon`, `lat`, and `time`. `time` is time in format conforming with the NetCDF standard, `lon` is longitude between 0 and 360 degrees and `lat` is latitude between -90 and 90 degrees.

Examples
--------

Extract MERRA-2 model data in `M2I3NVASM.5.12.4` at 45 S, 170 E between 1 and 2 January 2020 and store the output in the directory `alcf_merra2_model`.

    alcf model merra2 point: { -45.0 170.0 } time: { 2020-01-01 2020-01-02 } M2I3NVASM.5.12.4 alcf_merra2_model
	'''
	time1 = None
	track1 = None
	if track is not None:
		track1 = ds.read(track)
		if track_override_year is not None:
			date = aq.to_date(track1['time'])
			date[1][:] = track_override_year
			track1['time'] = aq.from_date(date)
		if track_lon_180:
			track1['lon'] = np.where(
				track1['lon'] > 0,
				track1['lon'],
				360. + track1['lon']
			)
		time1 = track1['time'][0], track1['time'][-1]
	elif point is not None and time is not None:
		pass
	else:
		raise ValueError('Point and time or track is required')

	if time is not None:
			time1 = [None, None]
			for i in 0, 1:
				time1[i] = aq.from_iso(time[i])
				if time1[i] is None:
					raise ValueError('Invalid time format: %s' % time[i])

	# if os.path.isdir(output):
	t1, t2 = time1[0], time1[1]
	for t in np.arange(np.floor(t1 - 0.5), np.ceil(t2 - 0.5)) + 0.5:
		output_filename = os.path.join(output, '%s.nc' % \
			aq.to_iso(t).replace(':', ''))
		d = model(type_, input_, point, time=[t, t + 1.],
			track=track1, debug=debug)
		if d is not None:
			ds.write(output_filename, d)
			print('-> %s' % output_filename)