Skip to content

Instantly share code, notes, and snippets.

@MPoinelli
Last active April 9, 2024 17:34
Show Gist options
  • Save MPoinelli/ef5b6c32444465f58c7caef068a41aaa to your computer and use it in GitHub Desktop.
Save MPoinelli/ef5b6c32444465f58c7caef068a41aaa to your computer and use it in GitHub Desktop.
Convert MITgcm iteration number to a date
#!/bin/bash
# Help message function
print_help() {
echo "Convert MITgcm iteration number to a date."
echo "written by: Poinelli, Mattia"
echo "Jet Propulsion Laboratory"
echo "April, 2024"
echo
echo "Based on ts2dte.m in mitgcm/utils/matlab"
echo
echo "Usage:"
echo " ts2dte <ts> <deltat> <startyr> <startmo> <startdy> <form>"
echo
echo "Input:"
echo "ts time step number"
echo "deltat time step length in s (default 1200)"
echo "startyr start year (default 1992)"
echo "startmo start month (default 1)"
echo "startdy start day (default 1)"
echo "form format of date"
echo
echo "Example:"
echo "ts2dte 1000 10 2010 2 4"
echo "returns: Thu Feb 4 02:46:40 PST 2010"
exit 0
}
# Parse command line arguments
if [[ $# -lt 1 ]]; then
print_help
fi
# Default values
ts="$1"
deltat="${2:-1200}"
startyr="${3:-1992}"
startmo="${4:-1}"
startdy="${5:-1}"
form="$6"
# Calculate the number of seconds to add to the start date
seconds=$((ts * deltat))
# Format the start date
start_date="$startyr-$startmo-$startdy"
# Calculate the final date by adding the seconds to the start date
final_date=$(date -d "$start_date + $seconds seconds")
# Format the final date according to the provided format string
if [ -z "$form" ]; then
echo "$final_date"
else
echo "$final_date" | date "+$form"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment