def main(inargs):
    """Run the program"""

    # Read data
    dset_in = xray.open_dataset(inargs.infile)
    gio.check_xrayDataset(dset_in, inargs.metric)

    subset_dict = gio.get_subset_kwargs(inargs)
    darray = dset_in[inargs.metric].sel(**subset_dict)

    # Make selection
    metric_threshold = uconv.get_threshold(darray.values,
                                           inargs.metric_threshold)

    assert inargs.threshold_direction in ['greater', 'less']
    if inargs.threshold_direction == 'greater':
        indexes = darray >= metric_threshold
    elif inargs.threshold_direction == 'less':
        indexes = darray <= metric_threshold

    darray_selection = darray.loc[indexes]

    # Write outputs
    gio.write_dates(inargs.outfile, darray_selection['time'].values)
    metadata_dict = {inargs.infile: dset_in.attrs['history']}
    gio.write_metadata(inargs.outfile, file_info=metadata_dict)
Esempio n. 2
0
def read_data(infile, var, subset={}):
    """Read an input data file into a pandas dataframe."""

    dset = xray.open_dataset(infile)
    gio.check_xrayDataset(dset, var)

    subset['method'] = 'nearest'

    dataframe = dset[var].sel(**subset).to_pandas()
    metadata = dset.attrs['history']

    return dataframe, metadata
def main(inargs):
    """Run the program."""

    # Read the data
    dset_in = xray.open_dataset(inargs.infile)
    gio.check_xrayDataset(dset_in, inargs.var)

    subset_dict = gio.get_subset_kwargs(inargs)
    darray = dset_in[inargs.var].sel(**subset_dict)

    assert darray.dims == ('time', 'latitude', 'longitude'), \
    "Order of the data must be time, latitude, longitude"

    # Generate datetime list
    dt_list, dt_list_metadata = get_datetimes(darray, inargs.date_file)

    # Calculate the composites
    if not inargs.date_file:
        inargs.no_sig = True
    cmeans, cmean_atts, pvals, pval_atts = calc_composites(
        darray, dt_list, sig_test=not inargs.no_sig)

    # Write the output file
    d = {}
    d['latitude'] = darray['latitude']
    d['longitude'] = darray['longitude']

    for season in season_months.keys():
        d[inargs.var + '_' + season] = (['latitude',
                                         'longitude'], cmeans[season])
        if not inargs.no_sig:
            d['p_' + season] = (['latitude', 'longitude'], pvals[season])

    dset_out = xray.Dataset(d)

    for season in season_months.keys():
        dset_out[inargs.var + '_' + season].attrs = cmean_atts[season]
        if not inargs.no_sig:
            dset_out['p_' + season].attrs = pval_atts[season]

    output_metadata = {
        inargs.infile: dset_in.attrs['history'],
    }
    if inargs.date_file:
        output_metadata[inargs.date_file] = dt_list_metadata

    gio.set_global_atts(dset_out, dset_in.attrs, output_metadata)
    dset_out.to_netcdf(inargs.outfile, format='NETCDF3_CLASSIC')
def get_seasonal_index(nc_file, var, start, end):
    """Get seasonal aggregate of metric from netCDF file."""

    dset = xray.open_dataset(nc_file)
    gio.check_xrayDataset(dset, var)

    metric_df = dset[var].to_pandas()
    metric_metadata = dset.attrs['history']

    metric_aggregate_df = aggregate_data(metric_df,
                                         timescale='seasonal',
                                         method='mean')
    filtered_dates_metric_aggregate_df = time_filter(metric_aggregate_df,
                                                     start, end)

    return filtered_dates_metric_aggregate_df, metric_metadata
def main(inargs):
    """Run the program."""

    # Read the data
    dset_in_u = xray.open_dataset(inargs.infileu)
    gio.check_xrayDataset(dset_in_u, inargs.varu)

    dset_in_v = xray.open_dataset(inargs.infilev)
    gio.check_xrayDataset(dset_in_v, inargs.varv)

    subset_dict = gio.get_subset_kwargs(inargs)

    darray_u = dset_in_u[inargs.varu].sel(**subset_dict)
    darray_v = dset_in_v[inargs.varv].sel(**subset_dict)

    lat_axis = darray_u['latitude'].values
    lon_axis = darray_u['longitude'].values
    axis_order = axis_letters(darray_u.dims)

    # Calculate the desired quantity
    data_out = calc_quantity(darray_u.values, darray_v.values, inargs.quantity,
                             lat_axis, lon_axis, axis_order)

    # Write the output file
    d = {}
    for dim in darray_u.dims:
        d[dim] = darray_u[dim]

    for var in data_out.keys():
        d[var] = (darray_u.dims, data_out[var])

    dset_out = xray.Dataset(d)

    for var in data_out.keys():
        dset_out[var].attrs = var_atts[var]

    outfile_metadata = {
        inargs.infileu: dset_in_u.attrs['history'],
        inargs.infilev: dset_in_v.attrs['history']
    }
    gio.set_global_atts(dset_out, dset_in_u.attrs, outfile_metadata)
    dset_out.to_netcdf(inargs.outfile, format='NETCDF3_CLASSIC')
Esempio n. 6
0
def main(inargs):
    """Plot each timestep."""

    # Extract data
    dset_in = xray.open_dataset(inargs.infile)
    gio.check_xrayDataset(dset_in, inargs.variable)

    data_dict, lat_tag = extract_data(dset_in, inargs.variable,
                                      inargs.latitude, inargs.valid_lon,
                                      inargs.dates)

    # Create the plot
    if not inargs.legend_list:
        legend_list = []
    else:
        legend_list = inargs.legend_list

    wmin, wmax = inargs.wavenumbers
    plot_hilbert(
        data_dict,
        inargs.dates,
        wmin,
        wmax,
        inargs.nrows,
        inargs.ncols,
        lat_tag,
        outfile=inargs.ofile,
        ybounds=inargs.ybounds,
        figure_size=inargs.figure_size,
        highlights=inargs.highlights,
        env_list=inargs.envelope,
        legend_list=legend_list,
        periodogram=inargs.periodogram,
        no_title=inargs.no_title,
        axis_size=inargs.axis_label_size,
        legend_size=inargs.legend_label_size,
        title_size=inargs.title_size,
    )

    gio.write_metadata(inargs.ofile,
                       file_info={inargs.infile: dset_in.attrs['history']})
def read_data(inargs, runmean_window):
    """Read input data into an xray DataArray."""

    dset_in = xray.open_dataset(inargs.infile)
    gio.check_xrayDataset(dset_in, inargs.variable)

    darray = subset_data(dset_in, inargs)
    indep_var = darray['longitude'].values

    if inargs.valid_lon:
        start_lon, end_lon = inargs.valid_lon
        lon_vals = numpy.array(
            [start_lon, end_lon, darray['longitude'].values.min()])
        assert numpy.sum(lon_vals >= 0) == 3, "Longitudes must be 0 to 360"
        darray.loc[dict(longitude=slice(0, start_lon))] = 0
        darray.loc[dict(longitude=slice(end_lon, 360))] = 0

    darray = running_mean(darray, runmean_window)
    metadata_dict = {inargs.infile: dset_in.attrs['history']}

    return darray, indep_var, metadata_dict