Documentation

Install a library and predict a tide. Then read how the data is made.

There is a library for Python, JavaScript, Ruby, Swift, C and Kotlin. Each reads the same release.

Already have an app that reads tide files? Migrating to OpenTideConstants: your reader stays, one setting changes, four or five steps.

Quickstart

Install the Python, JavaScript or Ruby library from PyPI, npm or RubyGems. The package is opentideconstants, version 0.1.0, in each registry.

pip install opentideconstants
npm install opentideconstants
gem install opentideconstants

The Python, JavaScript and Ruby examples below predict a day of tide heights at San Francisco from release 20261130. On first use the library downloads what it needs from data.opentideconstants.org and caches it.

Swift, C and Kotlin build from the source repository. Run their commands from the root of a clone:

git clone https://github.com/opentideconstants/sdk.git
cd sdk

The Swift, C and Kotlin examples predict a synthetic station included in the clone. The prediction runs offline using the files under conformance/fixtures/predict/.

Python Python 3.10 or later. On Windows, use .venv\Scripts\python.exe where the commands show .venv/bin/python.

python3 -m venv .venv
.venv/bin/python -m pip install opentideconstants

Save as example.py.

from datetime import datetime, timedelta
from opentideconstants import OpenTideConstants

start = datetime.fromisoformat("2026-12-01T00:00:00-08:00")
with OpenTideConstants(release="20261130") as otc:
    station = otc.station_by_alias("noaa", "9414290")
    tides = otc.heights(station, start=start, stop=start + timedelta(days=1))
    print(f"{len(tides.items)} heights; first: {tides.items[0].height_m:.3f} m")
.venv/bin/python example.py

JavaScript Node 22.12 or later. The JavaScript and TypeScript SDK has no runtime dependencies.

npm install opentideconstants

Save as example.mjs.

import { OpenTideConstants } from "opentideconstants";

const start = new Date("2026-12-01T00:00:00-08:00");
const otc = await OpenTideConstants.open({ release: "20261130" });
try {
    const station = otc.stationByAlias("noaa", "9414290");
    const tides = await otc.heights(station, {
        start, stop: new Date(start.getTime() + 86_400_000)
    });
    console.log(`${tides.items.length} heights; first: ${tides.items[0].height_m.toFixed(3)} m`);
} finally {
    otc.close();
}
node example.mjs

Ruby Ruby 3.3 or later.

gem install opentideconstants

Save as example.rb.

require "opentideconstants"

start = Time.new(2026, 12, 1, 0, 0, 0, "-08:00")
OpenTideConstants.open(release: "20261130") do |otc|
    station = otc.station_by_alias(:noaa, "9414290")
    tides = otc.heights(station, start: start, stop: start + 86_400)
    puts format("%d heights; first: %.3f m", tides.items.length, tides.items.first.height_m)
end
ruby example.rb

Swift Swift 6.3 tools and zlib. This example runs on macOS 12 or later. The SDK also supports iOS 15 or later.

mkdir -p example-swift/Sources

Save as example-swift/Package.swift.

// swift-tools-version: 6.3
import PackageDescription

let package = Package(
    name: "TideExample",
    platforms: [.macOS(.v12)],
    dependencies: [.package(name: "OpenTideConstants", path: "../swift")],
    targets: [.executableTarget(
        name: "TideExample",
        dependencies: [.product(name: "OpenTideConstants", package: "OpenTideConstants")],
        path: "Sources"
    )]
)

Save as example-swift/Sources/main.swift.

import Foundation
import OpenTideConstants

let file = "conformance/fixtures/predict/OTC_20991231.7.jsonl"
let start = Date(timeIntervalSince1970: 1_267_401_600)
let otc = try await OpenTideConstants.open(file: file)
defer { otc.close() }
let station = try otc.requireStation(stationId: "OTC-P-0001")
let tides = try otc.heights(station: station, start: start, stop: start.addingTimeInterval(86_400))
print(String(format: "%d heights; first: %.3f m", tides.items.count, tides.items[0].heightM))
swift run --package-path example-swift TideExample

C A C compiler, CMake 3.15 or later and pkg-config. Keep the uncompressed JSON Lines file and its metadata file together.

cmake -S c -B c/build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_LIBDIR=lib
cmake --build c/build
cmake --install c/build --prefix "$PWD/.otc-install"

Save as example.c.

#include <stdio.h>
#include <opentideconstants.h>

int main(void)
{
    const char *file = "conformance/fixtures/predict/OTC_20991231.7.jsonl";
    otc_release *release = NULL;
    otc_station_t *station = NULL;
    otc_predict_opts opts;
    otc_height_t tides[144];
    size_t count = 0;
    int status = 1;

    if (otc_open_file(file, NULL, &release) != OTC_OK)
        return 1;
    if (otc_require_station(release, "OTC-P-0001", &station) == OTC_OK) {
        otc_predict_opts_init(&opts);
        if (otc_heights(release, station, &opts, 1267401600, 1267488000,
                        tides, 144, &count) == OTC_OK && count > 0) {
            printf("%zu heights; first: %.3f m\n", count, tides[0].height_m);
            status = 0;
        }
        otc_station_free(station);
    }
    otc_close(release);
    return status;
}
cc example.c $(PKG_CONFIG_PATH="$PWD/.otc-install/lib/pkgconfig" pkg-config --cflags --libs --static opentideconstants) -o example
./example

Kotlin Use JDK 17 to build this example. The SDK also supports JVM 11 or later and Android API 26 or later.

./kotlin/gradlew -p kotlin -PotcJvmOnly=true publishAllPublicationsToTestRepository
mkdir -p example-kotlin/src/main/kotlin

Save as example-kotlin/settings.gradle.kts.

pluginManagement {
    repositories {
        mavenCentral()
        gradlePluginPortal()
    }
}

Save as example-kotlin/build.gradle.kts.

plugins {
    kotlin("jvm") version "2.4.21"
    application
}

repositories {
    maven { url = uri("../kotlin/build/test-repository") }
    mavenCentral()
}

dependencies {
    implementation("org.opentideconstants:opentideconstants:0.1.0")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.11.0")
}

application { mainClass.set("MainKt") }
tasks.named<JavaExec>("run") { workingDir = projectDir.parentFile }

Save as example-kotlin/src/main/kotlin/Main.kt.

import java.time.Instant
import java.util.Locale
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.opentideconstants.OpenTideConstants

fun main() = runBlocking(Dispatchers.IO) {
    val file = "conformance/fixtures/predict/OTC_20991231.7.jsonl"
    val start = Instant.ofEpochSecond(1_267_401_600)
    OpenTideConstants.open(file = file).use { otc ->
        val station = otc.release.requireStation("OTC-P-0001")
        val tides = otc.release.heights(station, start, start.plusSeconds(86_400))
        println(String.format(Locale.ROOT, "%d heights; first: %.3f m", tides.items.size, tides.items.first().heightM))
    }
}
./kotlin/gradlew -p example-kotlin run

Each example prints the number of heights and the first height in metres above the station's mean level. The Python, JavaScript and Ruby examples predict San Francisco (Golden Gate), NOAA 9414290, from release 20261130:

144 heights; first: -0.756 m

The Swift, C and Kotlin examples predict the synthetic station:

144 heights; first: -0.987 m

In production

  • Find a station. By agency id with station_by_alias, by name with search, or nearest to a position with nearest(lat=37.8, lon=-122.4). JavaScript, Swift and Kotlin spell these in camelCase.
  • Pin a release. Pass release="20261130" when you open the library.
  • Run offline. Download the whole release once (40,524,269 B) with download("20261130", to="data/", formats=["jsonl"]), keep the files it writes together, and open data/OTC_20261130.jsonl.gz with file=. Without that step, a station you have not used before needs the network.
  • Choose units and datum. Heights take units="feet" and a datum the station lists. Currents take units="knots". C returns metres and metres per second.
  • Choose a prediction. 30 stations carry two: the default, named observed-best, and official, which reproduces the agency's own tables. Pass profile="official" to get the second. At the other stations both give the recommended set. In C, select a set by its id.
  • Use only what you may sell. Each set has its own licence. For commercial use, open the library in strict mode with commercial_only=True (commercialOnly in JavaScript, Swift and Kotlin), which keeps only sets that meet the strict commercial criterion. If you check by hand, read both commercial_use and restriction in the set’s licence, not the licence id: a licence with commercial_use true can still carry a restriction, as the TICON-4 “as published” sets do.
  • Show the credit. attribution returns the text to display for the stations you show.
  • No hosted API. OTC is data files and libraries. The JavaScript package can serve a release over HTTP on your own machine.

Predictions are not observations and are not for navigation. See Limits.

Reference

Stations
Find a station and see where its constants came from.
Data format
The fields of a release and its JSON Schema.
Sources
Where the constants come from, and how sources rank at a station.
Method
How OTC fits constants from gauge records, and the checks before a fit.
Conventions
The phase reference, nodal corrections and constituent names each set declares.
Accuracy
How predictions compare with official predictions, and how that is measured.
Not included
What OTC and its libraries leave out, and how to ask for it.
Tide Mechanics
How harmonic constants become a tide curve, with figures you can move.