Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Typing #311

Closed
wants to merge 17 commits into from
Closed
2 changes: 1 addition & 1 deletion .prettierrc.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
tabWidth = 2
tabWidth = 4
semi = false
singleQuote = true
14 changes: 10 additions & 4 deletions xesmf/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import numpy.lib.recfunctions as nprec

huard marked this conversation as resolved.
Show resolved Hide resolved

def warn_f_contiguous(a):
def warn_f_contiguous(a: np.ndarray) -> None:
"""
Give a warning if input array if not Fortran-ordered.

Expand All @@ -41,7 +41,7 @@ def warn_f_contiguous(a):
warnings.warn('Input array is not F_CONTIGUOUS. ' 'Will affect performance.')


def warn_lat_range(lat):
def warn_lat_range(lat: np.ndarray) -> None:
"""
Give a warning if latitude is outside of [-90, 90]

Expand All @@ -58,7 +58,13 @@ def warn_lat_range(lat):

class Grid(ESMF.Grid):
@classmethod
def from_xarray(cls, lon, lat, periodic=False, mask=None):
def from_xarray(
cls,
lon: np.ndarray[float, int],
lat: np.ndarray[float, int],
periodic: bool = False,
mask=None,
huard marked this conversation as resolved.
Show resolved Hide resolved
):
"""
Create an ESMF.Grid object, for constructing ESMF.Field and ESMF.Regrid.

Expand Down Expand Up @@ -158,7 +164,7 @@ def from_xarray(cls, lon, lat):
Parameters
----------
lon, lat : 1D numpy array
Longitute/Latitude of cell centers.
Longitute/Latitude of cell centers.

Returns
-------
Expand Down
31 changes: 17 additions & 14 deletions xesmf/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,45 @@
"""

import numpy as np
import xarray


def wave_smooth(lon, lat):
r"""
def wave_smooth( # type: ignore
lon: np.ndarray[float] | xarray.DataArray, # type: ignore
lat: np.ndarray[float] | xarray.DataArray, # type: ignore
) -> np.ndarray[float] | xarray.DataArray: # type: ignore
"""
Spherical harmonic with low frequency.

Parameters
----------
lon, lat : 2D numpy array or xarray DataArray
Longitute/Latitude of cell centers
Longitute/Latitude of cell centers

Returns
-------
f : 2D numpy array or xarray DataArray depending on input
2D wave field
f : 2D numpy array or xarray DataArray depending on input2D wave field

Notes
-------
Equation from [1]_ [2]_:

.. math:: Y_2^2 = 2 + \cos^2(\\theta) \cos(2 \phi)
.. math:: Y_2^2 = 2 + cos^2(lat) * cos(2 * lon)

References
----------
.. [1] Jones, P. W. (1999). First-and second-order conservative remapping
schemes for grids in spherical coordinates. Monthly Weather Review,
127(9), 2204-2210.
schemes for grids in spherical coordinates. Monthly Weather Review,
127(9), 2204-2210.

.. [2] Ullrich, P. A., Lauritzen, P. H., & Jablonowski, C. (2009).
Geometrically exact conservative remapping (GECoRe): regular
latitudelongitude and cubed-sphere grids. Monthly Weather Review,
137(6), 1721-1741.
Geometrically exact conservative remapping (GECoRe): regular
latitude-longitude and cubed-sphere grids. Monthly Weather Review,
137(6), 1721-1741.
"""
# degree to radius, make a copy
lat = lat / 180.0 * np.pi
lon = lon / 180.0 * np.pi
lat *= np.pi / 180.0 # type: ignore
lon *= np.pi / 180.0 # type: ignore

f = 2 + np.cos(lat) ** 2 * np.cos(2 * lon)
f = 2 + pow(np.cos(lat), 2) * np.cos(2 * lon) # type: ignore
return f
Loading