kind: Workflow
metadata:
  annotations:
    argo-connector/keywords: ModGP,SDM,CWR
    argo-connector/license: https://spdx.org/licenses/MIT
    argo-connector/notify: 'true'
    argo-connector/submitterId1: https://orcid.org/0000-0002-0487-2151
    argo-connector/submitterName1: Lena Perzlmaier
    workflows.argoproj.io/description: Crop Wild Relatives distribution modeling workflow
      using ModGP. This workflow uses the DestinEarth HDA Stac API to retrieve the
      relevant ERA5 data.
    workflows.argoproj.io/pod-name-format: v2
    workflows.argoproj.io/title: Workflow for species distribution modeling with ModGP
spec:
  arguments:
    parameters:
    - description: Genus of species for distribution modeling
      name: species
      value: Lycopodium
    - description: Number of cores for SDM calculation
      name: numberOfCores
      value: '9'
    - description: URL of CWR source repository
      name: modgpSource
      value: https://github.com/Senckenberg-DCBiodivIT/uc-CWR.git
    - description: Revision of CWR source
      name: modgpRevision
      value: aada4fc
    - description: Start year for bioclimatic data retrieval
      name: startYearBioclim
      value: '1995'
    - description: End year for bioclimatic data retrieval
      name: endYearBioclim
      value: '2025'
  artifactGC:
    forceFinalizerRemoval: true
  entrypoint: workflow
  onExit: exit-handler
  podGC:
    strategy: OnWorkflowCompletion
  templates:
  - inputs: {}
    metadata: {}
    name: workflow
    outputs: {}
    steps:
    - - arguments:
          parameters:
          - name: startYearBioclim
            value: '{{ workflow.parameters.startYearBioclim }}'
          - name: endYearBioclim
            value: '{{ workflow.parameters.endYearBioclim }}'
        name: check-bioclim-in-s3
        template: check-bioclim-in-s3
      - arguments:
          parameters:
          - name: numberOfCores
            value: '{{ workflow.parameters.numberOfCores }}'
        name: create-credentials
        template: create-credentials
    - - arguments:
          parameters:
          - name: startYearBioclim
            value: '{{ workflow.parameters.startYearBioclim }}'
          - name: endYearBioclim
            value: '{{ workflow.parameters.endYearBioclim }}'
        name: retrieve-bioclim-data
        template: retrieve-bioclim-data
        when: '{{ steps.check-bioclim-in-s3.outputs.parameters.should-retrieve }}
          == true'
    - - arguments:
          artifacts:
          - from: '{{ steps.create-credentials.outputs.artifacts.credentials-file
              }}'
            name: credentials-file
          parameters:
          - name: species
            value: '{{ workflow.parameters.species }}'
          - name: startYearBioclim
            value: '{{ workflow.parameters.startYearBioclim }}'
          - name: endYearBioclim
            value: '{{ workflow.parameters.endYearBioclim }}'
          - name: source
            value: '{{ workflow.parameters.modgpSource }}'
          - name: codeRevision
            value: '{{ workflow.parameters.modgpRevision }}'
          - name: numberOfCores
            value: '{{ workflow.parameters.numberOfCores }}'
        name: modgp
        template: modgp
  - container:
      command:
      - uv
      - run
      - main.py
      - --start-year
      - '{{ inputs.parameters.startYearBioclim }}'
      - --end-year
      - '{{ inputs.parameters.endYearBioclim }}'
      - --keep-raw
      - 'False'
      env:
      - name: DEDL_USER
        valueFrom:
          secretKeyRef:
            key: user
            name: dedl-secret
      - name: DEDL_PWD
        valueFrom:
          secretKeyRef:
            key: pwd
            name: dedl-secret
      image: ghcr.io/lenipe/bioclim:sha-6dab483
      name: ''
      resources: {}
    inputs:
      artifacts:
      - archive:
          none: {}
        name: bioclim-raw-data
        optional: true
        path: /app/data
        s3:
          key: bioclim-raw-data
      parameters:
      - name: startYearBioclim
      - name: endYearBioclim
    metadata: {}
    name: retrieve-bioclim-data
    outputs:
      artifacts:
      - archive:
          none: {}
        name: bioclim-raw-data-updated
        path: /app/data
        s3:
          key: bioclim-raw-data
      - archive:
          none: {}
        artifactGC:
          strategy: OnWorkflowCompletion
        name: bioclim-processed-data
        path: /app/data/bioclim/BV_{{ inputs.parameters.startYearBioclim }}-{{ inputs.parameters.endYearBioclim
          }}.nc
        s3:
          key: cache_cwr_dedl/Data/Environment/BV_{{ inputs.parameters.startYearBioclim
            }}-{{ inputs.parameters.endYearBioclim }}.nc
  - inputs:
      artifacts:
      - archive:
          none: {}
        name: bioclim-environment-folder
        optional: true
        path: /tmp/bioclim
        s3:
          key: cache_cwr_dedl/Data/Environment
      parameters:
      - name: startYearBioclim
      - name: endYearBioclim
    metadata: {}
    name: check-bioclim-in-s3
    outputs:
      parameters:
      - name: should-retrieve
        valueFrom:
          path: /tmp/should-retrieve.txt
    script:
      command:
      - sh
      image: alpine:3.20
      name: ''
      resources: {}
      source: |
        target="/tmp/bioclim/BV_{{ inputs.parameters.startYearBioclim }}-{{ inputs.parameters.endYearBioclim }}.nc"

        if [ -s "$target" ]; then
          echo "Target exists and has content: $target"
          echo "false" > /tmp/should-retrieve.txt
        else
          echo "Target does not exist or is empty: $target"
          echo "true" > /tmp/should-retrieve.txt
        fi
  - inputs:
      parameters:
      - name: numberOfCores
    metadata: {}
    name: create-credentials
    outputs:
      artifacts:
      - artifactGC:
          strategy: OnWorkflowCompletion
        name: credentials-file
        path: SHARED-APICredentials.R
    script:
      command:
      - sh
      env:
      - name: GBIF_USER
        valueFrom:
          secretKeyRef:
            key: user
            name: gbif-api-secret
      - name: GBIF_EMAIL
        valueFrom:
          secretKeyRef:
            key: email
            name: gbif-api-secret
      - name: GBIF_PWD
        valueFrom:
          secretKeyRef:
            key: pwd
            name: gbif-api-secret
      - name: CDS_USER
        valueFrom:
          secretKeyRef:
            key: user
            name: cds-api-secret
      - name: CDS_KEY
        valueFrom:
          secretKeyRef:
            key: key
            name: cds-api-secret
      - name: DEDL_USER
        valueFrom:
          secretKeyRef:
            key: user
            name: dedl-secret
      - name: DEDL_PWD
        valueFrom:
          secretKeyRef:
            key: pwd
            name: dedl-secret
      image: alpine
      name: ''
      resources: {}
      source: |
        cat <<EOF > SHARED-APICredentials.R
          # CDS
          API_User <- "$CDS_USER"
          API_Key <- "$CDS_KEY"

          numberOfCores <- {{ inputs.parameters.numberOfCores }}

          options(gbif_user = "$GBIF_USER")
          options(gbif_email = "$GBIF_EMAIL")
          options(gbif_pwd = "$GBIF_PWD")

          # DEDL
          DEDL_User = "$DEDL_USER"
          DEDL_Pwd = "$DEDL_PWD"
  - container:
      args:
      - /src/ModGP_MASTER.R
      - '{{ inputs.parameters.species }}'
      - '{{ inputs.parameters.startYearBioclim }}'
      - '{{ inputs.parameters.endYearBioclim }}'
      - '200'
      - '200'
      - 'FALSE'
      - 'FALSE'
      - 'TRUE'
      - 'TRUE'
      command:
      - Rscript
      env:
      - name: CWR_ON_DESTINE
        value: '1'
      image: ghcr.io/biodt/cwr:0.6.0
      name: cwr-container
      resources: {}
      workingDir: /src
    inputs:
      artifacts:
      - git:
          repo: '{{ inputs.parameters.source }}'
          revision: '{{ inputs.parameters.codeRevision }}'
        name: src
        path: /src
      - name: credentials-file
        path: /src/R_scripts/SHARED-APICredentials.R
      - archive:
          none: {}
        name: data-cache
        optional: true
        path: /src/Data
        s3:
          key: cache_cwr_dedl/Data
      parameters:
      - name: species
      - name: startYearBioclim
      - name: endYearBioclim
      - name: numberOfCores
      - name: source
      - name: codeRevision
    metadata: {}
    name: modgp
    outputs:
      artifacts:
      - archive:
          none: {}
        name: data-cache-updated
        path: /src/Data
        s3:
          key: cache_cwr_dedl/Data
      - archive:
          none: {}
        name: exports
        path: /src/Exports/ModGP
  - inputs: {}
    metadata: {}
    name: exit-handler
    outputs: {}
    steps:
    - - arguments: {}
        inline:
          http:
            headers:
            - name: Authorization
              valueFrom:
                secretKeyRef:
                  key: token
                  name: argo-connector-auth-secret
            insecureSkipVerify: true
            successCondition: response.statusCode == 202
            timeoutSeconds: 10
            url: https://workflow.earth/connector/notify/{{ workflow.namespace }}/{{workflow.name
              }}
          inputs: {}
          metadata: {}
          outputs: {}
          retryStrategy:
            backoff:
              duration: 1m
              factor: '2'
              maxDuration: 30m
            limit: '10'
        name: notify-argo-connector
        when: '{{ workflow.status }} == Succeeded && {{ workflow.annotations.argo-connector/notify
          }} == true'
