=====================================================================
archiver-get: Fetch PV data from an EPICS Archiver Appliance
=====================================================================

.. This text is RST (ReStructured Text),
   see also http://docutils.sourceforge.net/rst.html

Introduction
------------

The `EPICS Archiver Appliance <https://slacmshankar.github.io/epicsarchiver_docs/>`_
archives Process Variable (PV) data in EPICS control systems and provides HTTP/JSON
endpoints for data retrieval.

``archiver-get`` provides a convenient command-line interface to:

- Download archived PV data in ``camonitor`` (default), aligned table, CSV, or raw JSON format.
- Check if one or more PVs exist in the archiver (``check``).
- Retrieve and format PV metadata without downloading archive samples (``meta``).
- Find PVs matching one or more regular expressions (``find``).
- Generate a user configuration file ``$HOME/.archiver-get.json`` (``make-config``).

Configuration
-------------

Unless ``--base-url`` is provided on the command line, ``archiver-get`` reads
its archiver URLs (``FACILITY_URLS`` and ``DEFAULT_BASE_URL``) from
``$HOME/.archiver-get.json``.

You can generate this configuration file automatically using the ``make-config``
command:

- Create a configuration with a custom base URL::

    archiver-get make-config http://archiver.example.org:17665

- Create a configuration with predefined HZB facility URLs::

    archiver-get make-config HZB

Once configured, you can select a specific facility from
``$HOME/.archiver-get.json`` using ``-f`` / ``--facility`` (case-insensitive),
or override the URL directly with ``-b`` / ``--base-url``.

Commands
--------

- ``get``: Fetch archiver data for the specified PV(s).
- ``check``: Check if all provided PVs exist in the archiver and exit (non-zero exit code if any PV is missing).
- ``meta``: Fetch and print metadata for all provided PVs without downloading archive data (can be combined with ``--json``, ``--table``, ``--csv``, ``--map``, ``--map-key``).
- ``find``: Find PVs matching the given regular expression(s) and exit (can be combined with ``--json``; non-zero exit code if no PVs match).
- ``make-config``: Create ``$HOME/.archiver-get.json`` taking either ``HZB`` or a base URL.

Time Range Rules and Defaults
-----------------------------

- If none of ``--from``, ``--to``, or ``--range`` is given, fetches the last 1 hour from now.
- If only ``--from`` is given, fetches 1 day starting at ``--from``.
- If only ``--to`` is given, fetches 1 day ending at ``--to``.
- ``--range`` accepts either ``FROM,TO`` timestamps or a duration ``<NUMBER><UNIT>``
  with units: ``s`` (seconds), ``m`` (minutes), ``h`` (hours), ``d`` (days), ``w`` (weeks),
  ``M`` (months, where ``1M`` = 30 days). Examples: ``45s``, ``10m``, ``2h``, ``3d``, ``2w``, ``1M``.
- Duration ``--range`` can be combined with either ``--from`` (anchors start) or
  ``--to`` (anchors end), but not both.

Output Formats and Table Options
--------------------------------

- **Default (camonitor format):** Prints each sample chronologically with PV name,
  timestamp, value, and alarm status/severity.
- ``--json``: Return data in raw JSON format instead of the default camonitor format.
- ``--table``: Format output as a human-readable aligned table with a column for each PV.
- ``--csv``: Format output as semicolon-separated CSV with a column for each PV.
- ``--map MAPPING``: In ``--table`` or ``--csv`` mode, specify custom abbreviations
  for PV column headers, alarm statuses/severities, or ``disconnected`` in the form
  ``PV1->COL1,PV2->COL2,TIMEOUT->TMO,disconnected->DISC``. Can be specified multiple times.
- ``--map-key``: In ``--table`` mode, abbreviate PV column headers and status/alarm flags
  and print a legend below.
- ``--forward-fill``: In ``--table`` or ``--csv`` mode, fill missing cells with the
  most recent known value of each PV.
- ``--include-initial``: In ``camonitor``, ``table``, or ``csv`` mode, include the initial
  Archiver sample prior to ``--from``.

Command-Line Help Output
------------------------

::

  usage: archiver_get.py [-h] [--version] [-o OUTPUT] [-b BASE_URL]
                         [-f FACILITY] [-s FROM_DATE] [-e TO_DATE]
                         [-r DATE_RANGE] [--json | --table | --csv]
                         [--map MAPPING] [--map-key] [--forward-fill]
                         [--include-initial] [-v] [-n]
                         COMMAND PV [PV ...]

  Download PV data from an EPICS Archiver Appliance in camonitor (default), table, CSV, or JSON format. Without --base-url, archiver URLs are read from $HOME/.archiver-get.json.

  positional arguments:
    COMMAND               Action to perform: 'get' (fetch PV archive data),
                          'check' (check if PVs exist), 'meta' (fetch PV
                          metadata), or 'find' (find PVs matching regex), or
                          'make-config' (create $HOME/.archiver-get.json with
                          <URL> or 'HZB')
    PV                    One or more EPICS Process Variable names (or regular
                          expressions when 'find' is used, or a single argument
                          '<URL>|HZB' when 'make-config' is used)

  options:
    -h, --help            show this help message and exit
    --version             show program's version number and exit
    -o, --output OUTPUT   Specify output FILE (default: print to standard
                          output)
    -b, --base-url BASE_URL
                          Specify archiver base URL directly (if not given, URLs
                          are read from $HOME/.archiver-get.json)
    -f, --facility FACILITY
                          Specify facility from $HOME/.archiver-get.json to set
                          the archiver base URL (case is ignored, e.g. upper or
                          lower case). Cannot be combined with --base-url.
    -s, --from FROM_DATE  Start date in ISO 8601 format (e.g.
                          2019-01-31T08:00:00+02). If given alone without --to
                          or --range, defaults to a 1-day range starting at
                          --from.
    -e, --to TO_DATE      End date in ISO 8601 format (e.g.
                          2019-01-31T08:01:00+02). If given alone without --from
                          or --range, defaults to a 1-day range ending at --to.
    -r, --range DATE_RANGE
                          Specify date range either as 'FROM,TO' or as a
                          duration '<NUMBER><UNIT>' (s=seconds, m=minutes,
                          h=hours, d=days, w=weeks, M=months [30 days]; e.g.
                          10m, 2h, 1M). Anchors to 'now' unless --from or --to
                          is specified.
    --json                Return data in raw JSON format instead of the default
                          camonitor format
    --table               Format output as a human-readable aligned table with a
                          column for each PV
    --csv                 Format output as semicolon-separated CSV with a column
                          for each PV
    --map MAPPING         In --table or --csv mode, specify custom abbreviations
                          for PV column headers, alarm statuses/severities, or
                          'disconnected' in the form
                          'PV1->COL1,PV2->COL2,TIMEOUT->TMO,disconnected->DISC'.
                          Can be specified multiple times.
    --map-key             In --table mode, abbreviate PV column headers and
                          status/alarm flags and print a legend below
    --forward-fill        In --table or --csv mode, fill missing cells with the
                          most recent known value of each PV
    --include-initial     In camonitor, table, or csv mode, include the initial
                          Archiver sample prior to --from
    -v, --verbose         Show what the script does (printed to stderr)
    -n, --dry-run, --dry_run
                          Just show what the script would do

  Commands:
    get          Fetch archiver data for the specified PV(s)
    check        Check if all provided PVs exist in the archiver and exit
                 (non-zero exit code if any PV is missing)
    meta         Fetch and print metadata for all provided PVs without downloading
                 archive data (can be combined with --json, --table, --csv, --map, --map-key)
    find         Find PVs matching the given regular expression(s) and exit
                 (can be combined with --json; non-zero exit code if no PVs match)
    make-config  Create $HOME/.archiver-get.json taking either 'HZB' or a base URL

  Configuration:
    Without --base-url, the program reads its URLs (FACILITY_URLS and
    DEFAULT_BASE_URL) from $HOME/.archiver-get.json.
    Use 'make-config HZB' or 'make-config <URL>' to create this file.

  Time range defaults and rules:
    - If none of --from, --to, or --range is given, fetches the last 1 hour from now.
    - If only --from is given, fetches 1 day starting at --from.
    - If only --to is given, fetches 1 day ending at --to.
    - --range accepts either 'FROM,TO' timestamps or a duration '<NUMBER><UNIT>'
      with units: s (seconds), m (minutes), h (hours), d (days), w (weeks),
      M (months, where 1M = 30 days). Examples: 45s, 10m, 2h, 3d, 2w, 1M.
    - Duration --range can be combined with either --from (anchors start) or
      --to (anchors end), but not both.
