diff --git a/uxarray/core/api.py b/uxarray/core/api.py index f877c6eb2..fbc009803 100644 --- a/uxarray/core/api.py +++ b/uxarray/core/api.py @@ -2,10 +2,11 @@ import os from pathlib import Path -from typing import TYPE_CHECKING, Any, Mapping, Sequence, TypeAlias +from typing import Any, Mapping, Sequence, TypeAlias from warnings import warn import numpy as np +import xarray as xr from uxarray.core.dataset import UxDataset from uxarray.core.utils import ( @@ -22,9 +23,6 @@ _stack_cell_dims, ) -if TYPE_CHECKING: - from xarray import Dataset - __all__ = [ "open_grid", "open_multigrid", @@ -35,7 +33,7 @@ def open_grid( - grid_filename_or_obj: str | os.PathLike[Any] | dict | Dataset, + grid_filename_or_obj: str | os.PathLike[Any] | dict | xr.Dataset, chunks=None, use_dual: bool | None = False, **kwargs: dict[str, Any], @@ -85,8 +83,6 @@ def open_grid( >>> uxgrid = ux.open_grid("grid_filename.nc", chunks=-1) """ - import xarray as xr - # Handle chunk-related kwargs first. data_chunks = kwargs.pop("data_chunks", None) if data_chunks is not None: @@ -144,9 +140,9 @@ def open_grid( def open_multigrid( - grid_filename_or_obj: str | Path | "Dataset", + grid_filename_or_obj: str | Path | xr.Dataset, gridnames: list[str] | None = None, - mask_filename: str | Path | "Dataset" | None = None, + mask_filename: str | Path | xr.Dataset | None = None, mask_active_value: MaskActiveValue = 1, **kwargs: dict[str, Any], ) -> dict[str, Grid]: @@ -175,8 +171,6 @@ def open_multigrid( dict[str, Grid] Dictionary mapping grid names to ``Grid`` objects. """ - import xarray as xr - grid_ds_opened = False if isinstance(grid_filename_or_obj, xr.Dataset): grid_ds = grid_filename_or_obj @@ -314,7 +308,7 @@ def _active_mask_values_for_grid(grid_name: str) -> np.ndarray: def list_grid_names( - grid_filename_or_obj: str | Path | "Dataset", **kwargs: dict[str, Any] + grid_filename_or_obj: str | Path | xr.Dataset, **kwargs: dict[str, Any] ) -> list[str]: """List all grid names available within a grid file. @@ -331,8 +325,6 @@ def list_grid_names( ``['grid']`` for single-grid files or the detected grid names for multi-grid files. """ - import xarray as xr - grid_ds_opened = False if isinstance(grid_filename_or_obj, xr.Dataset): grid_ds = grid_filename_or_obj @@ -352,8 +344,8 @@ def list_grid_names( def open_dataset( - grid_filename_or_obj: str | os.PathLike[Any] | dict | Dataset, - filename_or_obj: str | os.PathLike[Any] | Dataset | None = None, + grid_filename_or_obj: str | os.PathLike[Any] | dict | xr.Dataset, + filename_or_obj: str | os.PathLike[Any] | xr.Dataset | None = None, chunks=None, chunk_grid: bool = True, use_dual: bool | None = False, @@ -426,8 +418,6 @@ def open_dataset( >>> ux_ds = ux.open_dataset("combined_file.nc") """ - import xarray as xr - if grid_kwargs is None: grid_kwargs = {} @@ -481,7 +471,7 @@ def open_dataset( def open_mfdataset( - grid_filename_or_obj: str | os.PathLike[Any] | dict | Dataset, + grid_filename_or_obj: str | os.PathLike[Any] | dict | xr.Dataset, paths: str | os.PathLike, chunks=None, chunk_grid: bool = True, @@ -546,8 +536,6 @@ def open_mfdataset( >>> ux_ds = ux.open_mfdataset("grid_filename.g", "grid_filename_vortex_*.nc") """ - import xarray as xr - if grid_kwargs is None: grid_kwargs = {} @@ -588,8 +576,6 @@ def _get_grid( def concat(objs, *args, **kwargs): # Ensure there is at least one object to concat. - import xarray as xr - if not objs: raise ValueError("No objects provided for concatenation.") diff --git a/uxarray/grid/coordinates.py b/uxarray/grid/coordinates.py index 1c32e67a0..b1775e786 100644 --- a/uxarray/grid/coordinates.py +++ b/uxarray/grid/coordinates.py @@ -1,4 +1,5 @@ import math +import warnings import numpy as np import xarray as xr @@ -750,8 +751,6 @@ def _set_desired_longitude_range(uxgrid): """ if _is_projected_grid(uxgrid): if not getattr(uxgrid, "_projected_warning_issued", False): - import warnings - warnings.warn( "Projected (non-spherical) coordinates detected on this grid " "(standard_name='projection_x_coordinate' or length units). " diff --git a/uxarray/grid/grid.py b/uxarray/grid/grid.py index 5f61b4c50..2c753f85a 100644 --- a/uxarray/grid/grid.py +++ b/uxarray/grid/grid.py @@ -1,8 +1,8 @@ import copy import os +import warnings from html import escape from typing import Optional, Sequence -from warnings import warn import cartopy.crs as ccrs import numpy as np @@ -169,7 +169,7 @@ def __init__( # grid spec not provided, check if grid_ds is a minimum representable UGRID dataset if source_grid_spec is None: - warn( + warnings.warn( "Attempting to construct a Grid without passing in source_grid_spec. Direct use of Grid constructor" "is only advised if grid_ds is following the internal unstructured grid definition, including" "variable and dimension names. Using ux.open_grid() or ux.from_dataset() is suggested.", @@ -817,7 +817,7 @@ def descriptors(self) -> set: @property def parsed_attrs(self) -> dict: """Dictionary of parsed attributes from the source grid.""" - warn( + warnings.warn( "Grid.parsed_attrs will be deprecated in a future release. Please use Grid.attrs instead.", DeprecationWarning, ) @@ -2011,8 +2011,6 @@ def compute_face_areas( ``face_areas`` property instead, which ensures mathematical correctness by using theoretical equal areas. """ - import warnings - warnings.warn( "compute_face_areas() is deprecated. Use the face_areas property instead for better performance and caching.", DeprecationWarning, @@ -2264,7 +2262,7 @@ def to_geodataframe( ) if exclude_antimeridian is not None: - warn( + warnings.warn( DeprecationWarning( "The parameter ``exclude_antimeridian`` will be deprecated in a future release. Please " "use ``periodic_elements='exclude'`` or ``periodic_elements='split'`` instead." diff --git a/uxarray/io/_esmf.py b/uxarray/io/_esmf.py index d0f846ecf..0b8f2131f 100644 --- a/uxarray/io/_esmf.py +++ b/uxarray/io/_esmf.py @@ -1,3 +1,6 @@ +from datetime import datetime + +import numpy as np import xarray as xr from uxarray.constants import INT_DTYPE, INT_FILL_VALUE @@ -119,10 +122,6 @@ def _encode_esmf(ds: xr.Dataset) -> xr.Dataset: xr.Dataset An xarray.Dataset formatted according to ESMF Unstructured Grid conventions. """ - from datetime import datetime - - import numpy as np - out_ds = xr.Dataset() # Node Coordinates (nodeCoords)