Metadata-Version: 2.4
Name: comfort-db
Version: 0.1.0
Summary: Helper library for working with the COMFORT oceanographic dataset (SQLite)
Author: Are Olsen, Boris Peter Koch, Sebastian Maneth
Author-email: Yvonne Jenniges <yvonne.jenniges@gmail.com>
License-Expression: MIT
Project-URL: Repository, https://github.com/y-jenniges/comfort-db
Project-URL: Issues, https://github.com/y-jenniges/comfort-db/issues
Project-URL: Documentation, https://comfort-db.readthedocs.io/en/latest/
Keywords: oceanography,data-exploration,data-analysis,COMFORT,SQLite
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.23
Requires-Dist: pandas>=2.0
Requires-Dist: scipy>=1.10
Requires-Dist: gsw>=3.6.16
Requires-Dist: python-dateutil>=2.8.2
Requires-Dist: python-dotenv>=1.0
Provides-Extra: plot
Requires-Dist: matplotlib>=3.8.1; extra == "plot"
Requires-Dist: seaborn>=0.13; extra == "plot"
Provides-Extra: geo
Requires-Dist: shapely>=2.0; extra == "geo"
Requires-Dist: geopandas>=0.14.4; extra == "geo"
Requires-Dist: cartopy>=0.22; extra == "geo"
Provides-Extra: grid
Requires-Dist: xarray>=2023.1; extra == "grid"
Requires-Dist: netcdf4>=1.6; extra == "grid"
Provides-Extra: scale
Requires-Dist: scikit-learn>=1.2; extra == "scale"
Provides-Extra: dash
Requires-Dist: dash>=2.14; extra == "dash"
Requires-Dist: dash-bootstrap-components>=1.5; extra == "dash"
Requires-Dist: plotly>=5.18; extra == "dash"
Provides-Extra: docs
Requires-Dist: sphinx>=7.0; extra == "docs"
Requires-Dist: sphinx-rtd-theme>=2.0; extra == "docs"
Provides-Extra: all
Requires-Dist: comfort-db[plot]; extra == "all"
Requires-Dist: comfort-db[geo]; extra == "all"
Requires-Dist: comfort-db[grid]; extra == "all"
Requires-Dist: comfort-db[scale]; extra == "all"
Requires-Dist: comfort-db[dash]; extra == "all"
Requires-Dist: comfort-db[docs]; extra == "all"
Provides-Extra: test
Requires-Dist: pytest>=7.4; extra == "test"
Requires-Dist: pytest-cov>=4.1; extra == "test"
Requires-Dist: openpyxl>=3.1; extra == "test"
Provides-Extra: dev
Requires-Dist: comfort-db[all]; extra == "dev"
Requires-Dist: comfort-db[test]; extra == "dev"
Dynamic: license-file

# comfort-db

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/y-jenniges/comfort-db/blob/main/LICENSE)
[![Python](https://img.shields.io/badge/python-3.10%2B-blue)](https://www.python.org/)
[![Documentation Status](https://img.shields.io/readthedocs/comfort-db)](https://comfort-db.readthedocs.io/en/latest/)

Python library for working with the [COMFORT](https://www.doi.org/10.11582/2022.00039)[1] oceanographic SQLite dataset 
with [code documentation](https://comfort-db.readthedocs.io/en/latest/). 

`comfort-db` wraps database access, preprocessing and basic analysis utilities into one library. 
This includes filtering for quality and space-time ranges, unit conversions, spatiotemporal gridding, profile and water-mass analysis, 
geospatial utilities, scaling, plotting and visualisation using a dashboard.

The data structure broadly follows oceanographic ship measurement practice: 
At a latitude, longitude and time, a station defines where measurements were taken.
At a station, profiles are measured, i.e. a parameter like temperature at different depths with a specific instrument.
There can be multiple profiles at the same station, though in the COMFORT dataset, it is mostly one profile per station. 

This library bases on and extends code developed in [2], including parts of the gridding, unit conversion, analysis and plotting 
routines.

[1] Korablev, A., Olsen, A., (2022) Geophysical Institute, University of Bergen, Bjerknes Centre for Climate Research. COMFORT Dataset [Data set]. NIRD RDA. https://doi.org/10.11582/2022.00039

[2] Jenniges, Y., Sonnewald, M., Maneth, S., Olsen, A., Koch, Boris P., (2025) Unveiling 3D ocean biogeochemical provinces in the North Atlantic: 
A systematic comparison and validation of clustering methods, Ecological Informatics, Volume 91, 103390, ISSN 1574-9541,
https://doi.org/10.1016/j.ecoinf.2025.103390.

---

## Installation

```bash
pip install comfort-db
```

Optional extras add more specialised functionality:

| Extra | Adds | Enables                                               |
|---|---|-------------------------------------------------------|
| `plot` | matplotlib, seaborn | `comfort.plot`                                        |
| `geo` | shapely, geopandas, cartopy | Water masses, Longhurst provinces, coastline distance (`distance_to_coast`) |
| `grid` | xarray, netcdf4 | Spatio-temporal gridding, bathymetry masking, biome classification (`load_biomes`), xarray output |
| `scale` | scikit-learn | `ParamScaler`                                         |
| `dash` | dash, plotly | Interactive Dash explorer app                         |
| `docs` | sphinx, sphinx-rtd-theme | Building the documentation locally                    |
| `all` | everything above |                                                       |

Install via e.g.

```bash
pip install "comfort-db[geo,plot]"
```

**From source (editable, for development):**

```bash
git clone https://github.com/y-jenniges/comfort-db.git
cd comfort-db
pip install -e ".[dev]"
```

To build the documentation locally:

```bash
sphinx-build documentation/source documentation/build/html
```

Requires Python 3.10 or newer.

---

## Quick start

```python
import comfort

# Overview of the database (no data loaded, takes some minutes)
comfort.info("/path/to/comfort.sqlite")

# Filter at SQL level, get back one DataFrame per parameter
# (output="xarray" returns an xarray Dataset and needs the `grid` extra)
data = comfort.load_comfort(
    "/path/to/comfort.sqlite",
    parameters=["NITRATE", "DIC"],
    quality_flags=comfort.QC_GOOD,  # quality filter
    lat_min=30, lat_max=70,  # latitude filter
    date_min="2000-01-01 00:00:00",  # date filter, format YYYY-MM-DD HH:MM:SS
    target_depths=[0, 50, 100, 200, 500, 1000],  # depth interpolation
    limit=500_000,  # first 500,000 rows per parameter (a warning is logged if rows are cut)
    output="dict",
)

# Table exploration
with comfort.connect("/path/to/comfort.sqlite") as conn:
    summary = comfort.describe_variables(conn, parameters=["NITRATE", "DIC"], quality_flags=comfort.QC_GOOD)
```

---

## Testing

```bash
pip install -e ".[dev]"
pytest

# Include the bathymetry test (needs a GEBCO NetCDF file)
export BATHYMETRY_PATH=/path/to/gebco_bathymetry.nc
pytest
```

---

## Citing

If you use `comfort-db` in published work, please cite the COMFORT dataset [1] and this library: 
```bibtex
@article{jenniges_inprep,
    author = {Yvonne Jenniges and Are Olsen and Boris Peter Koch and Sebastian Maneth},
    title = {{comfort-db}: A Python toolkit to access, preprocess and explore the oceanographic COMFORT dataset},
    journal = {Journal of Open Source Software},
    year = {in prep.},
}
```

## License

MIT
