WRF Module

pywrfkit.wrf.add_coords(var: DataArray, rename: bool = False) DataArray[source]

Add coordinate information to an xarray DataArray by averaging longitude and latitude over specific dimensions and assigning these as coordinates. Optionally, rename the coordinates.

Parameters:
  • var (xr.DataArray) – The input DataArray that contains ‘XLONG’ and ‘XLAT’ coordinates.

  • rename (bool, optional) – If True, rename ‘west_east’ to ‘longitudes’ and ‘south_north’ to ‘latitudes’. Default is False.

Returns:

The DataArray with updated coordinates. If rename is True, coordinates are also renamed.

Return type:

xr.DataArray

Example

>>> import xarray as xr
>>> import numpy as np
>>>
>>> # Create a sample DataArray with dummy dimensions and coordinates
>>> data = np.random.rand(10, 10)
>>> lon = np.linspace(-180, 180, 10)
>>> lat = np.linspace(-90, 90, 10)
>>> ds = xr.DataArray(data, coords=[('south_north', lat), ('west_east', lon)],
...                   name='temperature', attrs={'XLONG': lon, 'XLAT': lat})
>>>
>>> # Add coordinates and rename
>>> ds = add_coords(ds, rename=True)
>>> print(ds)
pywrfkit.wrf.renamelatlon(var: DataArray) DataArray[source]

Rename coordinates in an xarray DataArray from ‘west_east’ to ‘longitudes’ and from ‘south_north’ to ‘latitudes’.

Parameters:

var (xr.DataArray) – The input DataArray with ‘west_east’ and ‘south_north’ coordinates.

Returns:

The DataArray with renamed coordinates.

Return type:

xr.DataArray

Example

>>> import xarray as xr
>>> import numpy as np
>>>
>>> # Create a sample DataArray
>>> data = np.random.rand(10, 10)
>>> lon = np.linspace(-180, 180, 10)
>>> lat = np.linspace(-90, 90, 10)
>>> ds = xr.DataArray(data, coords=[('latitudes', lat), ('longitudes', lon)],
...                   name='temperature')
>>>
>>> # Rename coordinates
>>> ds = renamelatlon(ds)
>>> print(ds)