# Working with data sources A data source is one product, such as OSTIA SST or MODIS-Aqua chlorophyll. Every source is a {class}`seagrid.sources.DataSource` with the same methods, whatever the provider, so code written for one source works for all of them. The simplest way to use a source is its module: ```python from seagrid import modis modis.describe() ds = modis.open_dataset("2023-06-01", "par", timetype="monthly") ``` Each module (`seagrid.ostia`, `seagrid.modis`, `seagrid.oc_cci`, `seagrid.acspo`, ...) has every method below as a function and is also reachable as `seagrid.ostia` after `import seagrid`. The same object is available from the registry, which is handy when the source name is a variable: ```python import seagrid seagrid.list_sources() for name in ("ostia", "acspo"): print(seagrid.get_source(name).time_range()) ``` ## Choosing what to open Each method takes the same arguments: | Argument | Meaning | Default | |---|---|---| | `dtm` | any time inside the time step | the first available date | | `data_var` | variable (for NASA L3 products it chooses the file) | the source's default | | `timetype` | `"daily"`, `"8day"`, `"monthly"`, ... (see {doc}`timetypes`) | the source's default | | `resolution` | `"4km"`, `"9km"`, ... | the source's default | Variants of a product, such as near-real-time data, are selected when the source is created: `ostia.source(variant="nrt")` or `seagrid.get_source("ostia", variant="nrt")`. Keyword arguments override settings for that object only: `seagrid.get_source("ostia", datadir="/scratch/ostia")`. ## Methods | Method | Does | |---|---| | `open_dataset(dtm, ...)` | download if needed, open, apply seagrid conventions | | `open_dataarray(dtm, data_var)` | one variable | | `open_mfdataset(start, end, ...)` | a time series along `time` | | `download(dtm, ...)` / `download_range(start, end)` | fetch into the cache | | `check_remote(dtm, ...)` | is the time step on the server? (no download) | | `downloaded(start, end)` | which time steps are cached | | `filename`, `local_path`, `remote_url` | naming | | `time_range()`, `dtm_vec()` | available time steps | | `grid(resolution)` | the {class}`~seagrid.grids.Grid` of the files | | `describe()` | summary for listings | | `lats()`, `lons()`, `dx()`, `dy()`, `area()` | 2-D coordinates (°) and cell metrics (m, m²) of the grid | | `landmask()`, `distance_from_land()` | True on land; great-circle distance to land in km | | `longhurst_provinces()`, `longhurst_biomes()`, `longhurst_basins()` | Longhurst province, biome and basin of every cell (`version="2007"` or `"1995"`) | | `clear_cache(disk=False)` | forget the fields above (and with `disk=True` the stored ones) | ## Geographic fields Every source gives the coordinates, cell sizes and land of its grid, as methods that keep their result: ```python from seagrid import ostia, modis lats, lons = ostia.lats(), ostia.lons() # numpy, (3600, 7200) area = modis.area(resolution="9km") # m² land = ostia.landmask() # DataArray, True on land dist = ostia.distance_from_land() # DataArray, km (float32) ``` The first call computes the field and keeps it on the source object, so later calls return the same array at once (read-only; use `.copy()` to change it). `cache=False` computes it again and keeps nothing. `landmask` and `distance_from_land` take seconds to minutes on a global grid, so they are also stored as Zarr in `/geo/` (the `geodir` setting changes this) and read from there by later sessions. A stored field is used only if it was built on the same grid. Land comes from the source wherever it has its own: | Sources | Land is | |---|---| | `ostia`, `avhrr`, `mur25` | the land bit of the GHRSST L4 `mask` variable (lakes are not land) | | `smoc`, `cmems_globgc`, `glorys_carb`, `mercator12` | the cells where the model's surface field is missing (a model has no clouds) | | `glorys`, `ecco2`, `scrippsgog` | `kmt == 0` (`landmask(k)` for deeper levels) | | every other source | GEBCO: a cell is land if more than half of it is above sea level | The first two kinds open the first available time step, so they may download one file; GEBCO is about 7.5 GB, downloaded on first use. ## Longhurst provinces Longhurst's biogeographical provinces come in two versions: | Version | Provinces | Data | |---|---|---| | `"2007"` (default) | 57, numbered as in Longhurst (2007) | a 1/6° grid, `Longhurst_Regions_2007.nc`. No server offers it at present: copy it into the `longhurst` data directory. | | `"1995"` | 54 "initial static boundaries" (Longhurst 1995; 1998), other numbers | polygons, downloaded once from Marine Regions (or the SAERI copy). Using it warns. | Every source puts them on its own grid, with the same caching as `landmask`: ```python prov = ostia.longhurst_provinces() # 1-57 (0 on land), int16 biome = ostia.longhurst_biomes() # 1 Coastal, 2 Polar, 3 Trades, 4 Westerlies basin = ostia.longhurst_basins() # 1 Arctic, 2 Atlantic, 3 Indian, 4 Pacific, 5 Southern old = ostia.longhurst_provinces(version="1995") # warns seagrid.longhurst.province_table() # ID, Code, Province, Biome, Basin ``` Each cell takes the province at its centre. The provinces' coastline is coarse, so some ocean cells near the coast are in none; they take the nearest province, using the source's `landmask()`. `fill=False` leaves them 0 (and needs no land mask). `seagrid.longhurst.open_dataset()` gives `regions`, `biomes` and `basins` on the 1/6° grid, unfilled (`seagrid.longhurst.source(variant="1995")` for the polygons), and `seagrid.longhurst.resample(grid, version=...)` puts them on any grid. ## Conventions of the returned data - `lat`/`lon` coordinates with latitude ascending (south to north); - SST in °C, with `units` attributes set; - a `time` coordinate: the start of the time step; - `attrs["seagrid_history"]` lists every change seagrid made (renames, unit conversions, flips), and `attrs["seagrid_source"]` names the source. ## Means over longer periods Coarser time types than a source's native ones are computed: a source with daily files also offers `"8day"` and `"monthly"`. The mean skips missing values cell by cell, is computed one file at a time (so memory use is one time step), and is cached in `/derived/`. Files that are missing on the server (or fail to open) are skipped with a warning, and the mean is built from the rest. The number of files used and the dates skipped are in `ds.attrs["seagrid_n_files"]`, `ds.attrs["seagrid_missing"]` and the history. Because the mean is cached, rebuild it with `force=True` if the missing files appear later. A period with no files raises `FileNotFoundError`, and so does a period that reaches past the end of the source's time range (it is not complete yet). ```python sst = seagrid.get_source("ostia").open_dataset("2010-06-01", timetype="monthly") ``` ## Time ranges of rolling archives Settings can describe archives that move: `dtm_max = "today"`, relative dates such as `dtm_min = "-720D"`, and `latency = "70D"` for products that are published with a delay. `seagrid check SOURCE` and the full test level check the configured range against the server.