## Overview
This pipeline processes California Underwater Glider Network (CUGN) data -- objectively-mapped Spray glider sections off the California coast -- and derives biogeochemical parameters (total alkalinity, DIC, pH, nitrate) using empirical algorithms, in the same style as CORE_ARGO.m / BGC_ARGO.m. Output is written in GliderVIZ/ODV-compatible text format.

## Lines Covered
- **Along** -- alongshore composite section
- **Line 56, 66, 80, 90** -- cross-shore CalCOFI lines

`CUGN_line_93.nc` is present in the raw archive but is **not** processed by this pipeline: it's an older line (2005-2006) that was never instrumented for oxygen, and every derived parameter here (ESPER, CANYONB) requires oxygen as a predictor.

## Data Sources

### Primary Data
**CUGN gridded/binned NetCDF files** (`CUGN_along.nc`, `CUGN_line_56.nc`, `CUGN_line_66.nc`, `CUGN_line_80.nc`, `CUGN_line_90.nc`)
- **Manual download (current method):** https://spraydata.ucsd.edu/projects/cugn/ -- download the per-line `.nc` files and place them in `CUGN/data/`. This is a manual step; the files are not versioned/dated releases like the SOCCOM snapshot used by BGC_ARGO.m, so re-downloading periodically is how you pick up new profiles.
- **ERDDAP alternative (can be polled/scripted):** the same data is served from Scripps IDG's ERDDAP instance at https://spraydata.ucsd.edu/erddap/, as one dataset per line:
  - `binnedCUGNalong`, `binnedCUGN56`, `binnedCUGN66`, `binnedCUGN80`, `binnedCUGN90` (and `binnedCUGN93`, unused here)
  - Each dataset can be queried via `tabledap` (e.g. `https://spraydata.ucsd.edu/erddap/tabledap/binnedCUGN80.nc`) or browsed at `https://spraydata.ucsd.edu/erddap/tabledap/binnedCUGN80.html`, which lets ERDDAP be polled/automated instead of manually re-downloading from the project page. Not currently wired into this pipeline -- `Extract_CUGN_to_mat.m` still expects the files to already be sitting in `CUGN/data/`.

### Required NetCDF Variables
- `temperature` [profiles x 50], `salinity` [profiles x 50], `doxy` [profiles x 50] -- 50 fixed depth levels (objectively mapped)
- `depth` [50] -- shared depth grid
- `lat`, `lon`, `time` [profiles] -- `time` is seconds since 1970-01-01 (Unix epoch)

## Data Processing Steps

Run `Run_CUGN_pipeline.m`, or the three stages individually in order:

1. **Extract_CUGN_to_mat.m** -- reads each line's `.nc` file, extracts T/S/O2/depth/position/time, computes pressure (`sw_pres`) and potential density (GSW/TEOS-10: `gsw_SA_from_SP`, `gsw_CT_from_t`, `gsw_rho`), converts Unix time to MATLAB serial date number, and saves `<line>.mat` back into `CUGN/data/`.
2. **Derive_empirical_parameters.m** -- loads each `.mat` file and estimates TA, DIC, pH, and nitrate three ways:
   - **CANYONB** (`ta_canb`, `dic_canb`, `pH_canb`, `no3_canb`) -- pH is corrected to match spec pH with `pHcanb = pH + (pH*0.0404 - 0.3168)` (the original script called a `carter_corr` function for this; that function isn't present anywhere on this machine's MATLAB path, so it's inlined here -- it's the same formula used inline in BGC_ARGO.m for the same purpose)
   - **ESPER_LIR** and **ESPER_NN** (`*_esplir`, `*_espnn`), Equation 7 (S, T, O2), plus their average (`*_espmix`) -- computed for reference but not used downstream
   - **3-way average** of ESPER-LIR, ESPER-NN, and CANYONB (`ta_avg`, `dic_avg`, `pH_avg`, `no3_avg`) -- **these are the values that actually make it into the final output file**
   - Adds Argo-style QC flags (1 = good/present, 9 = missing) for pressure, temperature, salinity, density, depth, oxygen, and the four `*_avg` parameters
   - Saves `<line>_withEmp.mat` into `CUGN/data/`
3. **write_CUGN_to_gliderviz_txt.m** -- loads each `_withEmp.mat`, calls `convert_s2gliderviztxt_CUGN_ArgoFlags.m` to write a GliderVIZ/ODV-format `.txt` file into `CUGN/for_distribution/`

## Output Files

### Final Data Products (`CUGN/for_distribution/`)
One tab-delimited `*_argoQC.txt` file per line (`AlongQC_argoQC.txt`, `Line 56_argoQC.txt`, etc. -- named from the `lines` label in `write_CUGN_to_gliderviz_txt.m`), in the same ODV format FloatViz/SAGE use for Argo floats.

**Standard columns:** Cruise, Station, Type, mon/day/yr, hh:mm, Lon [°E], Lat [°N], LOCATION_QV:ARGO

**Data columns (each followed by its own `_QV:ARGO` flag column):**
| Column | Description |
|---|---|
| Pressure[dbar] | Pressure |
| Temperature[°C] | Temperature |
| Salinity[pss] | Practical salinity |
| Sigma_theta[kg/m^3] | Potential density anomaly |
| Depth[m] | Depth (negative down, per GliderVIZ convention) |
| Oxygen[µmol/kg] | Dissolved oxygen |
| Nitrate_EST[µmol/kg] | Nitrate, 3-way average estimate |
| pHinsitu_EST[Total] | pH (in situ, total scale), 3-way average estimate |
| TALK_EST[µmol/kg] | Total alkalinity, 3-way average estimate |
| DIC_EST[µmol/kg] | DIC, 3-way average estimate |

### Quality Flags
0 = Uninspected, 1 = Good, 2 = Probably good, 3 = Probably bad, 4 = Bad, 9 = Missing or not inspected

### Intermediate Files (`CUGN/data/`)
- `<line>.mat` -- extracted physical variables (Extract_CUGN_to_mat.m output)
- `<line>_withEmp.mat` -- adds the CANYONB/ESPER estimates (Derive_empirical_parameters.m output)

## Notes
- Uncertainty (per the text file header): TA ±15 µmol/kg, DIC ±20 µmol/kg, pH ±0.05, nitrate ±3 µmol/kg -- same figures used for BGC_ARGO.m's CANYONB/ESPER estimates.
- This pipeline has no update/append mode like CORE_ARGO.m or BGC_ARGO.m -- every run reprocesses all 5 lines from whatever `.nc` files are currently in `CUGN/data/` and overwrites the output text files.
- Missing value indicator in the output text files: `-1e10`.

## References
- Rudnick, D. L. (2016). Ocean research enabled by underwater gliders. Annual Review of Marine Science, 8, 519-541. doi:10.1146/annurev-marine-122414-033913
- Rudnick, D. L., Davis, R. E., & Sherman, J. T. (2016). Spray Underwater Glider Operations. Journal of Atmospheric and Oceanic Technology, 33(6), 1113-1122. doi:10.1175/JTECH-D-15-0252.1
- ESPER_LIR / ESPER_NN: Carter et al., 2021, https://doi.org/10.1002/lom3.10461 -- https://github.com/BRCScienceProducts/ESPER
- CANYON-B: https://github.com/HCBScienceProducts/CANYON-B

## Contact
For questions about data processing, refer to Ben Werb bwerb@mbari.org or Yui Takeshita Yui@mbari.org
