ingest
index
/Users/samuelveliveli/Downloads/marine-tracker/src/ingest.py

Study discovery and location-event ingestion from Movebank, with local caching.
 
Two data sources are supported, normalized to a common schema:
 
- **Live API** (``www.movebank.org/movebank/service/direct-read``): near-live
  GPS/Argos events. Every request needs HTTP Basic Auth -- there is no
  anonymous tier, confirmed by requesting the study list with no credentials
  (returns HTTP 401 with ``WWW-Authenticate: BASIC``). First access to a
  study's events also requires accepting that study's license terms: the API
  returns the terms as HTML, and the caller must echo back the MD5 hash of
  that HTML as ``license-md5`` to receive real data. Used only when
  ``MOVEBANK_USERNAME``/``MOVEBANK_PASSWORD`` are set (see auth.py).
- **Movebank Data Repository** (``datarepository.movebank.org``): a separate,
  fully public, DOI-archival system with no auth. Confirmed working via its
  DSpace REST API (search -> item -> bundles -> bitstreams -> CSV content).
  Used as the always-available fallback so the app works with zero setup,
  clearly labeled as historical (not live) data.
 
Both paths are real research studies with real citations, not synthetic data.
Cached pulls are stored as parquet under data/raw/, named with the study
identifier and pull date, since Movebank tag data changes at most a few
times a day.

 
Modules
       
argparse
hashlib
io
pandas
requests

 
Functions
       
default_study_citations()
Return [{name, taxon, citation, license}] for the currently active default studies.
 
Required reading before this data is shown anywhere in the app: Movebank's
terms require surfacing each study's citation, not just noting it in code.
Falls back to principal-investigator attribution when a study's citation
field is empty (seen in practice on the live API), since some form of
attribution must still be surfaced.
discover_live_studies()
Query the live Movebank API for all studies, filtered to marine mammal taxa.
 
Requires MOVEBANK_USERNAME/MOVEBANK_PASSWORD. Returns a DataFrame with
columns id, name, taxon_ids, license_type, i_have_download_access,
timestamp_last_deployed_location -- sorted by most recently active first.
Raises RuntimeError if no live credentials are configured.
fetch_live_events(study_id)
Download all location events for one live-API study, handling the license-md5 handshake.
 
Movebank returns the study's license terms as HTML on first access; this
function accepts by echoing back the MD5 hash of that HTML, then re-requests
to get the actual CSV event data. Returns a DataFrame in the common event schema.
fetch_repository_events(bitstream_url)
Download a movement-data CSV bitstream from the Movebank Data Repository. No auth required.
ingest(refresh=False)
Pull (or load from cache) the default studies and return {study_name: DataFrame}.
 
Uses the live API if MOVEBANK_USERNAME/MOVEBANK_PASSWORD are set,
otherwise falls back to the public Data Repository. Set refresh=True to
bypass today's cache and re-pull.

 
Data
        DATA_RAW_DIR = PosixPath('/Users/samuelveliveli/Downloads/marine-tracker/data/raw')
DEFAULT_LIVE_STUDIES = [{'name': 'Bowhead whale Cumberland Sound', 'study_id': 467034665, 'taxon': 'Balaena mysticetus'}, {'name': 'churchill harbour seals', 'study_id': 3792819345, 'taxon': 'Phoca vitulina'}, {'name': 'Trichechus manatus Sirenia NE Brazil', 'study_id': 1936932457, 'taxon': 'Trichechus manatus'}]
DEFAULT_REPOSITORY_STUDIES = [{'bitstream_url': 'https://datarepository.movebank.org/server/api/c...eams/aca1254b-7574-4677-a4d0-5fc23415859f/content', 'citation': 'Andrews-Goff V, Gales N, Childerhouse SJ, Laveri...1:e114729. https://doi.org/10.3897/BDJ.11.e114729', 'doi': '10.5441/001/1.294', 'license': 'CC0 1.0 Universal', 'name': "Movements of Australia's east coast humpback whales", 'taxon': 'Megaptera novaeangliae'}, {'bitstream_url': 'https://datarepository.movebank.org/server/api/c...eams/53183515-d3f3-4a9b-acb4-a90f8b02bcc6/content', 'citation': 'Baker L, Flemming JEM, Jonsen ID, Lidgard DC, Iv...nt Ecology 3(1):20. doi:10.1186/s40462-015-0047-4', 'doi': '10.5441/001/1.910p0c20', 'license': 'CC0 1.0 Universal', 'name': 'Grey seals (Halichoerus grypus) at Sable Island', 'taxon': 'Halichoerus grypus'}, {'bitstream_url': 'https://datarepository.movebank.org/server/api/c...eams/2c398911-0b86-466d-a6d5-8da2d9ca9c18/content', 'citation': 'Andrews-Goff V, Double M. 2022. Deployment detai...ian Antarctic Data Centre. doi:10.26179/jd0n-va59', 'doi': '10.5441/001/1.vr276ns3', 'license': 'CC0 1.0 Universal', 'name': 'Antarctic blue whales, east Antarctic sector of the Southern Ocean', 'taxon': 'Balaenoptera musculus'}]
EVENT_COLUMNS = ['timestamp', 'location_lat', 'location_long', 'individual_local_identifier', 'individual_taxon_canonical_name', 'study_name', 'source']
LIVE_API_BASE = 'https://www.movebank.org/movebank/service/direct-read'
MARINE_MAMMAL_GENERA = {'arctocephalus', 'balaena', 'balaenoptera', 'berardius', 'callorhinus', 'caperea', ...}
REPOSITORY_API_BASE = 'https://datarepository.movebank.org/server/api'