# Grids Every horizontal grid in seagrid is a {class}`seagrid.grids.Grid`. It holds the cell-centre coordinates of a data source, computes cell sizes and areas, and gives the [pyresample](https://pyresample.readthedocs.io) geometries used for resampling. ## Building grids Use a factory rather than building coordinates by hand: ```python from seagrid import grids grids.nasa_l3("4km") # NASA OBPG L3 mapped grid (MODIS, VIIRS, ...) grids.cbiomes_halfdeg() # CBIOMES 0.5° climatology grid grids.regular_latlon(0.25, lat_range=(-80, 90)) grids.from_dataset(ds) # from a Dataset's lat/lon (1-D or 2-D) grids.from_area_def(area_def) # from any pyresample AreaDefinition ``` A grid is **rectilinear** when it is defined by 1-D `lat`/`lon` vectors and **curvilinear** when it is defined by 2-D arrays, as for model grids. A rectilinear grid with uniform spacing is **regular** and has a {attr}`~seagrid.grids.Grid.resolution`. ## Grids in the settings A source describes its grid with a `grid` key in its settings section. The key takes one of three forms: ```toml [my_source] grid = "nasa_l3_4km" # a named grid # a regular lat/lon grid grid = { dlat = 0.05, lat_range = [-90, 90], lon_range = [-180, 180] } # any pyresample area definition (arguments of pyresample.create_area_def) grid = { projection = "EPSG:3413", shape = [448, 304], area_extent = [-3850000, -5350000, 3750000, 5850000] } ``` {func}`seagrid.grids.from_settings` turns any of these into a `Grid`. ## Metrics ```python g = grids.nasa_l3("9km") g.dx, g.dy # zonal width and meridional height of each cell, metres g.area # cell area, m² g.to_dataset() # lat/lon coordinates plus dx, dy, area as an xarray Dataset ``` Metrics are exact on a sphere for rectilinear grids: the cell areas of a global grid sum to $4 \pi R^2$. For curvilinear grids they are computed from great-circle distances between neighbouring cells. ## Row order and pyresample seagrid's sources return latitude **ascending** (south to north), and a grid's rows follow its data. pyresample area definitions always count rows from the **north**. A grid therefore gives two pyresample views: | Attribute | Type | Row order | Use | |---|---|---|---| | {attr}`~seagrid.grids.Grid.geometry` | `GridDefinition` | same as the data | Always safe; holds full 2-D coordinates in memory | | {attr}`~seagrid.grids.Grid.area_def` | `AreaDefinition` | north to south | Exact and cheap; regular or projected grids only | Convert data between the two orders with {meth}`~seagrid.grids.Grid.to_area_order` and {meth}`~seagrid.grids.Grid.from_area_order`: ```python import pyresample as pr out = pr.kd_tree.resample_nearest( src.area_def, src.to_area_order(data), dst.area_def, radius_of_influence=5e4 ) result = dst.from_area_order(out) # back in seagrid's row order ``` ## Subsetting and wrapping data ```python sub = g.subset(lat_bounds=(30, 60), lon_bounds=(-40, 0)) j, i = g.bbox_slices(lat_bounds=(30, 60), lon_bounds=(-40, 0)) # for data arrays da = g.data_array(arr, name="chl", dims=("month",), month=range(1, 13)) ```