|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Load required modules and environments |
| 4 | +source $MODULESHOME/init/sh |
| 5 | +module load miniforge |
| 6 | +conda activate /nbhome/role.medgrp/.conda/envs/uwtools || { echo "Error activating conda environment. Exiting."; exit 1; } |
| 7 | + |
| 8 | +set -eu |
| 9 | + |
| 10 | +# Helper functions |
| 11 | +print_usage() { |
| 12 | + echo "Usage: $0 START_DATE END_DATE [--ncrcat] [--adjust-timestamps]" |
| 13 | + echo " START_DATE and END_DATE must be in YYYY-MM-DD format." |
| 14 | + echo " --ncrcat: Enable ncrcat step (skips subset, fill, and submit_python steps)." |
| 15 | + echo " --adjust-timestamps: Adjust timestamps during ncrcat step." |
| 16 | +} |
| 17 | + |
| 18 | +validate_date_format() { |
| 19 | + if [[ ! "$1" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then |
| 20 | + echo "Error: Date $1 must be in YYYY-MM-DD format." |
| 21 | + exit 1 |
| 22 | + fi |
| 23 | +} |
| 24 | + |
| 25 | +log_message() { |
| 26 | + echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" |
| 27 | +} |
| 28 | + |
| 29 | +# Default options |
| 30 | +DO_NCRCAT=false |
| 31 | +ADJUST_TIMESTAMPS=false |
| 32 | +PYTHON_SCRIPT="../write_glorys_boundary_daily.py" |
| 33 | + |
| 34 | +# Parse arguments |
| 35 | +START_DATE="$1" |
| 36 | +END_DATE="$2" |
| 37 | +shift 2 |
| 38 | + |
| 39 | +while [[ $# -gt 0 ]]; do |
| 40 | + case "$1" in |
| 41 | + --ncrcat) |
| 42 | + DO_NCRCAT=true |
| 43 | + ;; |
| 44 | + --adjust-timestamps) |
| 45 | + ADJUST_TIMESTAMPS=true |
| 46 | + ;; |
| 47 | + *) |
| 48 | + echo "Unknown argument: $1" |
| 49 | + print_usage |
| 50 | + exit 1 |
| 51 | + ;; |
| 52 | + esac |
| 53 | + shift |
| 54 | +done |
| 55 | + |
| 56 | +validate_date_format "$START_DATE" |
| 57 | +validate_date_format "$END_DATE" |
| 58 | + |
| 59 | + |
| 60 | +start_date_epoch=$(date -d "$START_DATE" +%s) |
| 61 | +end_date_epoch=$(date -d "$END_DATE" +%s) |
| 62 | +if [[ $start_date_epoch -gt $end_date_epoch ]]; then |
| 63 | + log_message "Error: START_DATE ($START_DATE) must not be after END_DATE ($END_DATE). Exiting." |
| 64 | + exit 1 |
| 65 | +fi |
| 66 | + |
| 67 | +# Ensure --adjust-timestamps is only used with --ncrcat |
| 68 | +if $ADJUST_TIMESTAMPS && ! $DO_NCRCAT; then |
| 69 | + echo "Error: --adjust-timestamps can only be used with --ncrcat." |
| 70 | + exit 1 |
| 71 | +fi |
| 72 | + |
| 73 | +# Warn user when --ncrcat is enabled |
| 74 | +if $DO_NCRCAT; then |
| 75 | + log_message "WARNING: --ncrcat is enabled. The script will SKIP subset, fill, and submit_python steps." |
| 76 | + log_message "Ensure that all daily outputs already exist for the specified date range." |
| 77 | +fi |
| 78 | + |
| 79 | +# Prepare directories |
| 80 | +CURRENT_DATE=$(date +%Y-%m-%d-%H-%M) |
| 81 | +mkdir -p ./log/$CURRENT_DATE ./outputs scripts |
| 82 | + |
| 83 | +# Define user configurations |
| 84 | +log_message "Generating config.yaml..." |
| 85 | +cat <<EOF > config.yaml |
| 86 | +_WALLTIME: "1440" |
| 87 | +_NPROC: "1" |
| 88 | +_EMAIL_NOTIFACTION: "fail" |
| 89 | +_USER_EMAIL: "$USER@noaa.gov" |
| 90 | +_LOG_PATH: "./log/$CURRENT_DATE/%x.o%j" |
| 91 | +_UDA_GLORYS_DIR: "/uda/Global_Ocean_Physics_Reanalysis/global/daily" |
| 92 | +_UDA_GLORYS_FILENAME: "mercatorglorys12v1_gl12_mean" |
| 93 | +_REGIONAL_GLORYS_ARCHIVE: "/archive/$USER/datasets/glorys" |
| 94 | +_BASIN_NAME: "NWA12" |
| 95 | +_OUTPUT_PREFIX: "GLORYS" |
| 96 | +_VARS: "thetao so uo vo zos" |
| 97 | +_LON_MIN: "-100.0" |
| 98 | +_LON_MAX: "-30.0" |
| 99 | +_LAT_MIN: "5.0" |
| 100 | +_LAT_MAX: "60.0" |
| 101 | +_PYTHON_SCRIPT: "$PYTHON_SCRIPT" |
| 102 | +first_date: "$START_DATE" |
| 103 | +last_date: "$END_DATE" |
| 104 | +glorys_dir: "/archive/$USER/datasets/glorys/NWA12/filled" |
| 105 | +output_dir: "./outputs" |
| 106 | +hgrid: './ocean_hgrid.nc' |
| 107 | +ncrcat_names: |
| 108 | + - 'thetao' |
| 109 | + - 'so' |
| 110 | + - 'zos' |
| 111 | + - 'uv' |
| 112 | +segments: |
| 113 | + - id: 1 |
| 114 | + border: 'south' |
| 115 | + - id: 2 |
| 116 | + border: 'north' |
| 117 | + - id: 3 |
| 118 | + border: 'east' |
| 119 | +variables: |
| 120 | + - 'thetao' |
| 121 | + - 'so' |
| 122 | + - 'zos' |
| 123 | + - 'uv' |
| 124 | +EOF |
| 125 | + |
| 126 | + |
| 127 | +log_message "Preparing scripts directory..." |
| 128 | +[[ -d scripts ]] || mkdir scripts |
| 129 | +for template in subset_glorys fill_glorys submit_python_make_obc_day ncrcat_obc; do |
| 130 | + rm -f scripts/${template}.sh |
| 131 | + uw template render --input-file template/${template}_template.sh \ |
| 132 | + --values-file config.yaml \ |
| 133 | + --output-file scripts/${template}.sh || { log_message "Error rendering ${template}. Exiting."; exit 1; } |
| 134 | +done |
| 135 | + |
| 136 | +# Skip main steps if --ncrcat is enabled |
| 137 | +if ! $DO_NCRCAT; then |
| 138 | + |
| 139 | + # Submit jobs |
| 140 | + current_date_epoch=$start_date_epoch |
| 141 | + job_ids=() |
| 142 | + |
| 143 | + while [[ $current_date_epoch -le $end_date_epoch ]]; do |
| 144 | + current_date=$(date -d "@$current_date_epoch" +%Y-%m-%d) |
| 145 | + year=$(date -d "$current_date" +%Y) |
| 146 | + month=$(date -d "$current_date" +%m) |
| 147 | + day=$(date -d "$current_date" +%d) |
| 148 | + |
| 149 | + log_message "Submitting subset job for $current_date..." |
| 150 | + subset_job_id=$(sbatch --job-name="glorys_subset_${year}_${month}_${day}" \ |
| 151 | + scripts/subset_glorys.sh $year $month $day | awk '{print $4}') |
| 152 | + |
| 153 | + log_message "Submitting fill_nan job for $current_date..." |
| 154 | + fill_job_id=$(sbatch --dependency=afterok:$subset_job_id \ |
| 155 | + --job-name="glorys_fill_${year}_${month}_${day}" \ |
| 156 | + scripts/fill_glorys.sh $year $month $day | awk '{print $4}') |
| 157 | + |
| 158 | + log_message "Submitting Python job for $current_date..." |
| 159 | + python_job_id=$(sbatch --dependency=afterok:$fill_job_id \ |
| 160 | + --job-name="python_make_obc_day_${year}_${month}_${day}" \ |
| 161 | + scripts/submit_python_make_obc_day.sh $year $month $day | awk '{print $4}') |
| 162 | + |
| 163 | + job_ids+=($python_job_id) |
| 164 | + current_date_epoch=$((current_date_epoch + 86400)) |
| 165 | + done |
| 166 | +fi |
| 167 | + |
| 168 | +# Optional ncrcat step |
| 169 | +if $DO_NCRCAT; then |
| 170 | + log_message "Submitting ncrcat job..." |
| 171 | + dependency_str=$(IFS=,; echo "${job_ids[*]:-}") |
| 172 | + if $ADJUST_TIMESTAMPS; then |
| 173 | + sbatch --job-name="obc_ncrcat" scripts/ncrcat_obc.sh --config config.yaml --ncrcat_years --adjust_timestamps |
| 174 | + else |
| 175 | + sbatch --job-name="obc_ncrcat" scripts/ncrcat_obc.sh --config config.yaml --ncrcat_years |
| 176 | + fi |
| 177 | +fi |
| 178 | + |
| 179 | +log_message "Workflow completed." |
0 commit comments