geoips.xarray_utils package#

Submodules#

geoips.xarray_utils.data module#

Utilities for manipulating xarray Datasets and DataArrays.

geoips.xarray_utils.data.combine_preproc_xarrays_with_alg_xarray(dict_of_xarrays, alg_xarray, rgb_var=None)[source]#

Combine preprocessed xarrays with an xarray output from an algorithm.

The dimensions of the preprocessed xarrays must match the algorithm xarray.

Parameters:
  • dict_of_xarrays (dict) – Dictionary of xarray objects (e.g. output from geoips_netcdf reader).

  • alg_xarray (xarray dataset) – xarray dataset returned from algorithm.

  • rgb_var (bool) – Specify the product name produced by an algorithm that outputs RGB values. If so, there will not be any NaNs to replace and a composite will not be made. This function will first set all 0s to NaN during the merge process, then replace any remaining NaNs back to 0.

Returns:

Combined xarray dataset of input xarrays datasets

Return type:

xarray.Dataset

geoips.xarray_utils.data.get_lat_lon_points(checklat, checklon, diff, sect_xarray, varname, drop=False)[source]#

Pull values from xarray Datasets in specified geographic location.

Return points a given distance around a specified lat/lon location, from xarray Datasets.

Parameters:
  • checklat (float) – latitude of interest

  • checklon (float) – longitude of interest

  • diff (float) – check +- diff of latitude and longitude

  • sect_xarray (Dataset) – xarray dataset containing ‘latitude’ ‘longitude’ and varname variables

  • varname (str) – variable name of data array to use for returning data values

Returns:

  • min value in range

  • max value in range

  • and number of points in range

Return type:

float, float, int

geoips.xarray_utils.data.get_lat_lon_points_numpy(checklat, checklon, diff, lat_array, lon_array, data_array)[source]#

Pull values from numpy arrays in specified geographic location.

Return points a given distance around a specified lat/lon location, from numpy arrays.

Parameters:
  • checklat (float) – latitude of interest

  • checklon (float) – longitude of interest

  • diff (float) – check +- diff of latitude and longitude

  • lat_array (ndarray) – numpy ndarray of latitude locations - same shape as lon_array and data_array

  • lon_array (ndarray) – numpy ndarray of longitude locations - same shape as lat_array and data_array

  • data_array (ndarray) – numpy ndarray data values - same shape as lat_array and lon_array

Returns:

  • min value in range

  • max value in range

  • and number of points in range

Return type:

float, float, int

geoips.xarray_utils.data.get_minmax_latlon_from_area_def(area_def, lon_pad, lat_pad)[source]#

Retrieve the Min/Max Lat/Lon from the provided area_def.

Given a PyResample AreaDefinition Object, retrieve the minimum and maximum latitude and longitude values that should be used to sector the xarray efficiently.

If the provided area def encapsulates either of the poles, set the min/max latitude value to -90/90 degrees accordingly, and match the other latitude value to what was provided in the area_def bounds.

For longitudes, if the sector doesn’t contain a pole, wrap them to ensure they exist in the 0-360 range, otherwise set to -180, 180.

Parameters:
  • area_def (PyResample AreaDefinition()) –

    • The area_definition that defines the bounds of the data we will be sectoring.

  • lon_pad (float) –

    • The amount (in degrees) of padding to be added to the longitude bounds.

  • lat_pad (float) –

    • The amount (in degrees) of padding to be added to the latitude bounds.

Returns:

min_lon, min_lat, max_lon, max_lat

  • These degree (float) values will be returned in the order listed above.

Return type:

float

geoips.xarray_utils.data.get_sectored_xarrays(xobjs, area_def, varlist, get_bg_xarrays=False, check_center=True, drop=False)[source]#

Get all xarray objects sectored to area_def.

Return primary dataset, as well as VIS/IR overlay datasets.

geoips.xarray_utils.data.get_vis_ir_bg(sect_xarray)[source]#

Find matching vis/ir background for data in sect_xarray.

geoips.xarray_utils.data.point_in_area_def(area_def, lon, lat)[source]#

Determine whether or not the provided point (in degrees) is within area_def.

Parameters:
  • area_def (PyResample AreaDefinition()) –

    • The area_definition that defines the bounds of the data we will be sectoring.

  • lon (float) –

    • The longitude value in degrees.

  • lat (float) –

    • The latitude value in degrees.

Returns:

  • Whether or not the point is contained in the area definition.

Return type:

bool

geoips.xarray_utils.data.sector_xarray_dataset(full_xarray, area_def, varnames, lon_pad=3, lat_pad=0, verbose=False, hours_before_sector_time=18, hours_after_sector_time=6, drop=False, window_start_time=None, window_end_time=None)[source]#

Use the xarray to appropriately sector out data by lat/lon and time.

Parameters:
  • full_xarray (xarray.Dataset) – The full input xarray Dataset

  • area_def (pyresample.AreaDefinition) – The requested region to sector spatially from the full_xarray. If None, no spatial sectoring required.

  • varnames (list of str) – List of variable names within full_xarray to include in the final xarray Dataset

  • lon_pad (float, default=3) – Amount of padding to include around the longitude dimension

  • lat_pad (float, default=0) – Amount of padding to include around the latitude dimension

  • verbose (bool, default=False) – If True, include log output, otherwise return silently.

  • hours_before_sector_time (float, default=18) – For dynamic sectors, number of hours before sector start time to include when sectoring temporally.

  • hours_after_sector_time (float, default=6) – For dynamic sectors, number of hours after sector end time to include when sectoring temporally.

  • drop (bool, default=False) – If True, drop data outside range, temporally and spatially. If False, mask outside range.

  • window_start_time (datetime.datetime, default=None) – If specified, sector temporally between window_start_time and window_end_time. hours_before_sector_time and hours_after_sector_time are ignored if window start/end time are set!

  • window_end_time (datetime.datetime, default=None) – If specified, sector temporally between window_start_time and window_end_time. hours_before_sector_time and hours_after_sector_time are ignored if window start/end time are set!

Returns:

sectored dataset containing requested “varnames” sectored spatially and temporally as requested.

Return type:

xarray.Dataset

geoips.xarray_utils.data.sector_xarray_spatial(full_xarray, area_def, varnames, lon_pad=3, lat_pad=0, verbose=False, drop=False)[source]#

Sector an xarray object spatially. If full_xarray is None, return None.

Parameters:
  • full_xarray (xarray.Dataset) – xarray object to sector spatially

  • area_def (pyresample.AreaDefinition) – The requested region to sector spatially from the full_xarray. If None, no spatial sectoring required.

  • varnames (list of str) – list of variable names that should be sectored based on ‘time’

  • drop (bool) – Specify whether to remove points with no coverage (rather than masking)

Returns:

  • if full_xarray is None, return None,

  • else return resulting xarray Dataset.

Return type:

xarray.Dataset

geoips.xarray_utils.data.sector_xarray_temporal(full_xarray, mindt, maxdt, varnames, verbose=False, drop=False)[source]#

Sector an xarray object temporally. If full_xarray is None, return None.

Parameters:
  • full_xarray (xarray.Dataset) – xarray object to sector temporally

  • mindt (datetime.datetime) – minimum datetime of desired data

  • maxdt (datetime.datetime) – maximum datetime of desired data

  • varnames (list of str) – list of variable names that should be sectored based on ‘time’, mindt, maxdt

Returns:

  • if full_xarray is None, return None

  • return full original xarray object if ‘time’ is not included in varnames list

  • else, return sectored xarray object with only the desired times, specified by mindt and maxdt

Return type:

xarray Dataset, or None

geoips.xarray_utils.data.sector_xarrays(xobjs, area_def, varlist, verbose=False, hours_before_sector_time=18, hours_after_sector_time=6, check_center=True, drop=False, lon_pad=3, lat_pad=0, window_start_time=None, window_end_time=None)[source]#

Return dict of sectored xarray objects.

Parameters:
  • xobjs (dict of xarray.Dataset) – The full input xarray Datasets, with dataset names as keys to the dictionary.

  • area_def (pyresample.AreaDefinition) – The requested region to sector spatially from the full_xarray. If None, no spatial sectoring required.

  • varlist (list of str) – List of variable names within xarray Datasets to include in the final sectored xarray Datasets

  • verbose (bool, default=False) – If True, include log output, otherwise return silently.

  • hours_before_sector_time (float, default=18) – For dynamic sectors, number of hours before sector start time to include when sectoring temporally.

  • hours_after_sector_time (float, default=6) – For dynamic sectors, number of hours after sector end time to include when sectoring temporally.

  • check_center (bool, default=True) – If True and sector_type is “tc”, check that there is coverage in the center of the image prior to sectoring. Skip dataset altogether if no center coverage.

  • drop (bool, default=False) – If True, drop data outside range, temporally and spatially. If False, mask outside range.

  • lon_pad (float, default=3) – Amount of padding to include around the longitude dimension

  • lat_pad (float, default=0) – Amount of padding to include around the latitude dimension

  • window_start_time (datetime.datetime, default=None) – If specified, sector temporally between window_start_time and window_end_time. hours_before_sector_time and hours_after_sector_time are ignored if window start/end time are set!

  • window_end_time (datetime.datetime, default=None) – If specified, sector temporally between window_start_time and window_end_time. hours_before_sector_time and hours_after_sector_time are ignored if window start/end time are set!

Returns:

Dictionary of sectored datasets containing requested “varnames”, sectored spatially and temporally as requested.

Return type:

dict of xarray.Dataset

geoips.xarray_utils.time module#

Utils to handle time stamp information within xarray objects.

geoips.xarray_utils.time.fix_datetime(inxr)[source]#

Masks the input xarray that has improper datetimes (negatives).

Parameters:

inxr (xarray.Dataset) – Input xarray with datetimes in raw float64 value

Returns:

Xarray with times reformatted as datetimes

Return type:

xarray.Dataset

Notes

Depreciated versions of xarray cast this as a functional NaT value

geoips.xarray_utils.time.get_datetime_from_datetime64(dt64)[source]#

Get a python datetime object from a numpy datetime64 object.

Parameters:

dt64 (numpy.datetime64') – numpy.datetime64 object

Returns:

Python datetime object

Return type:

datetime.datetime

Notes

Backwards compatible with numpy versions

geoips.xarray_utils.time.get_max_from_xarray_time(xarray_obj, varname)[source]#

Get the maximum time as a datetime object from xarray object.

Parameters:
  • xarray_obj (xarray.Dataset or xarray.DataArray) – xarray object from which to extract the maximum time

  • varname (str) – Timestamp variable name from which to extract the maximum time

Returns:

Python datetime.datetime object representing maximum time of the Dataset or DataArray

Return type:

datetime.datetime

geoips.xarray_utils.time.get_min_from_xarray_time(xarray_obj, varname)[source]#

Get the minimum time as a datetime object from xarray object.

Parameters:
  • xarray_obj (xarray.Dataset or xarray.DataArray) – xarray object from which to extract the minimum time

  • varname (str) – Timestamp variable name from which to extract the minimum time

Returns:

Python datetime.datetime object representing minimum time of the Dataset or DataArray

Return type:

datetime.datetime

geoips.xarray_utils.time.get_posix_from_datetime(dt)[source]#

Return the POSIX timestamp in seconds.

Parameters:

dt (datetime.datetime) – datetime object to convert to posix timestamp

Returns:

representing seconds since 1 January 1970 at 00Z (epoch seconds)

Return type:

long

geoips.xarray_utils.time.xarray_to_time(inarr, mask)[source]#

Convert a input array (inarr) with a bool mask (mask) to datetime.

Notes

Uses a nan mask on the array to convert real values.

geoips.xarray_utils.xr_to_dtree module#

Takes in a dictionary of xarrays and converts to xarray datatree.

geoips.xarray_utils.xr_to_dtree.xarray_to_datatree(xarray_dict)[source]#

Convert a flat (non nested) dictionary of xarrays to DataTree format.

Module contents#

Geoips xarray utils init file.