---
pipeline:
  name: troll_calibration_group_and_convert
transform:
  image: us-central1-docker.pkg.dev/neon-shared-service/neonscience/neon-is-cal-grp-conv:v1.2.0
  cmd:
  - sh
  - "-c"
  - |-
    /bin/bash <<'EOF'
    # Use bash-scrict mode. See http://redsymbol.net/articles/unofficial-bash-strict-mode/
    set -euo pipefail
    IFS=$'\n\t'
    #
    # Refresh interim directories with each datum (otherwise they persist and cause probs)
    rm -r -f /tmp/pfs/filter_joined
    rm -rf $OUT_PATH
    mkdir -p /tmp/pfs/filter_joined
    mkdir -p $OUT_PATH # R modules must have pfs in the repo structure
    #
    # Get source type /SOURCE_TYPE/YYYY/MM/DD 
    source_type=$(echo $DATA_PATH | cut -f $SOURCE_TYPE_INDEX -d "/")
    echo $source_type
    #
    # Run filter-joiner for data (using environment variables below as input parameters)
    python3 -m filter_joiner.filter_joiner_main
    #
    # Run calibration conversion module
    if [ $source_type = "aquatroll200" ]; then 
      # Aquatroll
      Rscript ./flow.cal.conv.R \
        DirIn=/tmp/pfs/filter_joined \
        DirOut=/pfs/out \
        DirErr=/pfs/out/errored_datums \
        FileSchmData=$FILE_SCHEMA_DATA_AQUATROLL \
        FileSchmQf=$FILE_SCHEMA_FLAGS_AQUATROLL \
        DirSubCopy=flags \
        "TermFuncConv=pressure:def.cal.conv.poly.b|temperature:def.cal.conv.poly|conductivity:def.cal.conv.poly.split" \
        "TermQf=pressure|temperature|conductivity" \
        "TermFuncUcrt=pressure:def.ucrt.meas.cnst|temperature:def.ucrt.meas.cnst|conductivity:def.ucrt.meas.cnst"
    elif [ $source_type = "leveltroll500" ]; then 
      # Leveltroll
      Rscript ./flow.cal.conv.R \
        DirIn=/tmp/pfs/filter_joined \
        DirOut=/pfs/out \
        DirErr=/pfs/out/errored_datums \
        FileSchmData=$FILE_SCHEMA_DATA_LEVELTROLL \
        FileSchmQf=$FILE_SCHEMA_FLAGS_LEVELTROLL \
        DirSubCopy=flags \
        "TermFuncConv=pressure:def.cal.conv.poly.b|temperature:def.cal.conv.poly" \
        "TermQf=pressure|temperature" \
        "TermFuncUcrt=pressure:def.ucrt.meas.cnst|temperature:def.ucrt.meas.cnst"
    fi
    EOF
  env:
    # Environment variable to determine source type
    SOURCE_TYPE_INDEX: "4"
    # Environment variables for filter-joiner.
    CONFIG: |
      ---
      # Configuration for filter-joiner module that will bring together the data and calibrations
      # In Pachyderm root will be index 0, 'pfs' index 1, and the repo name index 2.
      # Metadata indices will typically begin at index 3.
      input_paths:
        - path:
            name: DATA_PATH
            # Filter for data directory
            glob_pattern: /pfs/DATA_PATH/*/*/*/*/*/**
            # Join on named location (already joined below by source type and day)
            join_indices: [7]
            outer_join: true
        - path:
            name: CALIBRATION_PATH
            # Filter for data directory
            glob_pattern: /pfs/CALIBRATION_PATH/*/*/*/*/*/**
            # Join on named location (already joined below by day)
            join_indices: [7]
    OUT_PATH: /tmp/pfs/filter_joined # Note that R modules use "pfs" in the path structure to determine datums
    LOG_LEVEL: INFO
    RELATIVE_PATH_INDEX: "3" # Must be consistent across inputs 
    LINK_TYPE: COPY # options are COPY or SYMLINK. MUST BE SIMLINK IF USING COMBINED MODULE.
    # Environment variables for calibration module
    PARALLELIZATION_INTERNAL: '3' # Option for calibration conversion module
input:
  cross:
  - pfs:
      name: FILE_SCHEMA_DATA_AQUATROLL
      repo: troll_avro_schemas
      glob: /aquatroll200/aquatroll200_calibrated.avsc
  - pfs:
      name: FILE_SCHEMA_FLAGS_AQUATROLL
      repo: troll_avro_schemas
      glob: /aquatroll200/flags_calibration_aquatroll200.avsc
  - pfs:
      name: FILE_SCHEMA_DATA_LEVELTROLL
      repo: troll_avro_schemas
      glob: /leveltroll500/leveltroll500_calibrated.avsc
  - pfs:
      name: FILE_SCHEMA_FLAGS_LEVELTROLL
      repo: troll_avro_schemas
      glob: /leveltroll500/flags_calibration_leveltroll500.avsc
  # Outer join all days
  - join:
    - pfs:
        name: CALIBRATION_PATH
        repo: troll_calibration_assignment
        glob: /(*)/(*)/(*)/(*)
        joinOn: $1/$2/$3/$4
        empty_files: false # Make sure to use false if LINK_TYPE=COPY. Can also be set to false for LINK_TYPE=SYMLINK.
    - pfs:
        name: DATA_PATH
        repo: troll_fill_log_files
        glob: /(*)/(*)/(*)/(*)
        joinOn: $1/$2/$3/$4
        outer_join: true
        empty_files: false # Make sure to use false if LINK_TYPE=COPY. Can also be set to false for LINK_TYPE=SYMLINK.
parallelism_spec:
  constant: 5
autoscaling: true
resource_requests:
  memory: 1.5G
  cpu: 3.3
resource_limits:
  memory: 3G
  cpu: 4.5
sidecar_resource_requests:
  memory: 3G
  cpu: 0.6
datum_set_spec:
  number: 1
scheduling_spec:
  node_selector:
    cloud.google.com/gke-ephemeral-storage-local-ssd: "true"
    nodepool.neonscience.org/pipeline: "yes"
    cloud.google.com/gke-spot: "true"
pod_spec: |-
  { "tolerations": [
    {
      "key": "nodepool.neonscience.org/pipeline",
      "operator": "Exists"
    },
    {
      "effect": "NoSchedule",
      "key": "cloud.google.com/gke-spot",
      "operator": "Exists"
    }  
  ] }
