diff --git a/ush/CMakeLists.txt b/ush/CMakeLists.txt index 9e24306c..36690afa 100644 --- a/ush/CMakeLists.txt +++ b/ush/CMakeLists.txt @@ -1,5 +1,4 @@ list(APPEND ush_files - finddate.sh make_ntc_bull.pl make_NTC_file.pl make_tif.sh diff --git a/ush/finddate.sh b/ush/finddate.sh deleted file mode 100755 index b3d20b5b..00000000 --- a/ush/finddate.sh +++ /dev/null @@ -1,158 +0,0 @@ -#!/bin/bash -# finddate.sh -# author: Luke Lin phone: 457-5047 24 June 1998 -# author: Daniel Wesloh 30 January 2024 -# abstract: This script looks in ether forward or backward in time to -# generate either a variable containing sequential date/time stamps -# for a period up to a month or just the date/time stamp occurring -# at the end of such a period. -# Time stamp is in the form yyyyddmm. The script should be good for many -# years. Leap years are accounted for. Years go 1998, 1999, 2000, 2001, -# 2002, 2003, .... -# etc. -# -# usage: examples assume today's date is 19990929. -# To generate a sequence looking 10 days forward then execute: -# list=`sh /nwprod/util/scripts/finddate.sh 19990929 s+10` -# To generate just the date/time 10 days from now then execute: -# list=`sh /nwprod/util/scripts/finddate.sh 19990929 d+10` -# To generate a sequence looking 10 days backward then execute: -# list=`sh /nwprod/util/scripts/finddate.sh 19990929 s-10` -# To generate just the date/time 10 days ago then execute: -# list=`sh /nwprod/util/scripts/finddate.sh 19990929 d-10` -# list will contain 10 time stamps starting with 19990929. Time stamps -# are separated by blanks. -set +x - -# Takes four-digit year as argument -# Returns 0/true if argument is leap year -# Returns 1/false if argument is not leap year -function isleap() { - local -i year="10#$1" - local -i isleap=$((${year} % 4 == 0)) - if [ $((${year} % 100)) -eq 0 ] - then - isleap=$((${year} % 400 == 0)) - fi - test "${isleap}" -eq 1 -} - -# Takes four-digit year and two-digit month as argument -# Prints days in that month to stdout -function days_per_month() { - local -i year="10#$1" - local -i month="10#$2" - case "${month}" in - 1|3|5|7|8|10|12) - echo 31 - ;; - 4|6|9|11) - echo 30 - ;; - 2) - if isleap "${year}" - then - echo 29 - else - echo 28 - fi - ;; - *) - exit 1 - esac -} - -# Takes four-digit year, month, day and days ahead as arguments -# Prints date the given number of days after the given date in YYYYMMDD format to stdout -function n_days_ahead() { - local -i year="10#$1" - local -i month="10#$2" - local -i ndays="10#$4" - local -i day=$((10#$3 + ${ndays})) - - local -i month_days="$(days_per_month "${year}" "${month}")" - while [ "${day}" -gt "${month_days}" ]; - do - month=$((${month} + 1)) - day=$((${day} - ${month_days})) - - if [ "${month}" -gt 12 ]; - then - year=$((${year} + 1)) - month=$((${month} - 12)) - fi - - month_days="$(days_per_month "${year}" "${month}")" - done - - while [ "${day}" -lt "1" ]; - do - month=$((${month} - 1)) - - if [ "${month}" -lt "1" ]; - then - year=$((${year} - 1)) - month=$((${month} + 12)) - fi - - day=$(( ${day} + $(days_per_month "${year}" "${month}") )) - done - - printf '%04d%02d%02d' "${year}" "${month}" "${day}" -} - -function sequence_n_days_ahead() { - local -i year="10#${1}" - local -i month="10#${2}" - local -i day="10#${3}" - local -i ndays="10#${4}" - - if [ "${ndays}" -ge 0 ]; - then - local -i month_days="$(days_per_month "${year}" "${month}")" - for (( days_so_far=0 ; ${days_so_far} < ${ndays} ; days_so_far=${days_so_far} + 1 )); - do - local -i date_so_far="$(n_days_ahead "${year}" "${month}" "${day}" 1)" - printf '%08d ' "${date_so_far}" - - year="10#${date_so_far:0:4}" - month="10#${date_so_far:4:2}" - day="10#${date_so_far:6:2}" - done - else - for (( days_so_far=0 ; ${days_so_far} > ${ndays} ; days_so_far=${days_so_far} - 1)); - do - local -i date_so_far="$(n_days_ahead "${year}" "${month}" "${day}" -1)" - printf '%08d ' "${date_so_far}" - - year="10#${date_so_far:0:4}" - month="10#${date_so_far:4:2}" - day="10#${date_so_far:6:2}" - done - fi -} - -# Copy of finddate.sh -# Prints date or date sequence a given number of days from given date -# Given date is YYYYMMDD format -function finddate() { - local -i year_month_day="${1}" - local -i year="10#${year_month_day:0:4}" - local -i month="10#${year_month_day:4:2}" - local -i day="10#${year_month_day:6:2}" - - local second_arg="${2}" - local day_or_sequence="${second_arg:0:1}" - local -i ndays="${second_arg:1}" - - if [ "${day_or_sequence}" = "d" ]; - then - n_days_ahead "${year}" "${month}" "${day}" "${ndays}" - else - sequence_n_days_ahead "${year}" "${month}" "${day}" "${ndays}" - fi - echo -} - -# The old finddate.sh didn't provide functions, so call the main function here -finddate "$1" "$2" diff --git a/ush/test_finddate.sh b/ush/test_finddate.sh deleted file mode 100644 index dbd30dfa..00000000 --- a/ush/test_finddate.sh +++ /dev/null @@ -1,257 +0,0 @@ -#!/bin/bash - -echo TAP version 14 - -declare -i test_num=1 -declare -i suite_status=0 -function tap_start() { - declare -i num_tests="$1" - echo "1..${num_tests}" - test_num=1 - suite_status=0 -} -function tap_end() { - echo "Result: ${suite_status} failures" - exit ${suite_status} -} - -function tap_log_test() { - local -i status="$1" - local message="$2" - - if [ "${status}" -ne 0 ] - then - echo -n 'not ' - fi - - echo "ok ${test_num} - ${message}" - test_num=$((${test_num} + 1)) - suite_status=$((${suite_status} + ${status})) -} - -declare -i subtest_num=1 -declare -i subtest_suite_result=0 -function tap_log_subtest() { - local -i status="$1" - local message="$2" - echo -n ' ' - - if [ "${status}" -ne 0 ] - then - echo -n 'not ' - subtest_suite_result=$((${subtest_suite_result} + ${status})) - fi - - echo "ok ${subtest_num} - ${message}" - subtest_num=$((${subtest_num} + 1)) -} - -function tap_start_subtest() { - local name="${1}" - local -i ntests="${2}" - echo "# Subtest: ${name}" - echo " 1..${ntests}" - subtest_num=1 - subtest_suite_result=0 -} - -function tap_end_subtest() { - local name="${1}" - local -i status="${subtest_suite_result}" - - if [ "${status}" -ne 0 ]; - then - echo -n 'not ' - fi - - echo "ok ${test_num} - ${name}" - test_num=$((${test_num} + 1)) - suite_status=$((${suite_status} + ${status})) -} - -############################################################ - -echo 1..6 - -# Comment out the output from loading the test functions -echo -n '# ' -. test_find_date.sh 19990929 d+10 - -tap_start_subtest isleap 22 -for year in $(seq 1995 2005) $(seq 2095 2105); -do - if echo 1996 2000 2004 2096 2104 | grep --quiet -F -e ${year} -; - then - expected=0 - else - expected=1 - fi - - if isleap "${year}"; - then - actual=0 - else - actual=1 - fi - - if [ "${expected}" -eq "${actual}" ]; - then - result=0 - else - result=1 - fi - - tap_log_subtest "${result}" "Year ${year}" -done -tap_end_subtest isleap - -tap_start_subtest days_per_month 24 - -for year_month_days in 20210131 20210228 20210331 20210430 20210531 \ - 20210630 20210731 20210831 20210930 20211031 20211130 20211231 \ - 19950228 19960229 19990228 20000229 20010228 20040229 21000228; -do - declare -i year="${year_month_days:0:4}" - declare -i month="10#${year_month_days:4:2}" - declare -i ndays="10#${year_month_days:6:2}" - declare -i month_days="10#$(days_per_month "${year}" "${month}")" - if [ "${month_days}" -eq "${ndays}" ]; - then - result=0 - else - result=1 - fi - tap_log_subtest "${result}" "${year}-${month} expected ${ndays} got ${month_days}" -done - -tap_end_subtest days_per_month - -tap_start_subtest n_days_ahead 6 -start_date=19990929 -for days_year in 03662000 -3651998 07312001 -7301997 36532009 73052019; -do - declare -i ndays="10#${days_year:0:4}" - declare -i expected="10#${days_year:4}0929" - actual="$(n_days_ahead "${start_date:0:4}" "${start_date:4:2}" "${start_date:6:2}" "${ndays}")" - - if [ "${actual}" -eq "${expected}" ]; - then - result=0 - else - result=1 - fi - tap_log_subtest "${result}" \ - "${ndays} days from ${start_date}: exptected ${expected} got ${actual}" -done -tap_end_subtest n_days_ahead - -tap_start_subtest sequence_n_days_ahead 24 -declare -i start_date=19990929 -declare -i start_year="10#${start_date:0:4}" -declare -i start_month="10#${start_date:4:2}" -declare -i start_day="10#${start_date:6:2}" -for sign in '' '-'; -do - for ndays in $(seq 1 2 10) 400; - do - actual="$(sequence_n_days_ahead "${start_year}" "${start_month}" "${start_day}" "${sign}${ndays}")" - nwords="$(echo "${actual}" | wc -w)" - if [ "${nwords}" -eq "${ndays}" ]; - then - result=0 - else - result=1 - fi - tap_log_subtest "${result}" "Sequence length: ${sign}${ndays}" - - actual_last_date="$(echo "${actual}" | awk '{ print $NF; }')" - expected_last_date="$(n_days_ahead "${start_year}" "${start_month}" "${start_day}" "${sign}${ndays}")" - if [ "${actual_last_date}" = "${expected_last_date}" ]; - then - result=0 - else - result=1 - fi - tap_log_subtest "${result}" \ - "Last date in sequence: expected ${expected_last_date} actual ${actual_last_date}" - done -done -tap_end_subtest sequence_n_days_ahead - -tap_start_subtest "finddate matches old output" 4 -start_date=19990929 - -declare +i actual="$(finddate "${start_date}" s+10)" -declare +i expected="19990930 19991001 19991002 19991003 19991004 19991005 19991006 19991007 19991008 19991009 " -if [ "${actual}" = "${expected}" ]; -then - result=0 -else - result=1 -fi -tap_log_subtest "${result}" "s+10 matches old output" - -actual="$(finddate "${start_date}" d+10)" -expected="19991009" -if [ "${actual}" = "${expected}" ]; -then - result=0 -else - result=1 -fi -tap_log_subtest "${result}" "d+10 matches old output" - -actual="$(finddate "${start_date}" s-10)" -expected="19990928 19990927 19990926 19990925 19990924 19990923 19990922 19990921 19990920 19990919 " -if [ "${actual}" = "${expected}" ]; -then - result=0 -else - result=1 -fi -tap_log_subtest "${result}" "s-10 matches old output" - -actual="$(finddate "${start_date}" d-10)" -expected="19990919" -if [ "${actual}" = "${expected}" ]; -then - result=0 -else - result=1 -fi -tap_log_subtest "${result}" "d-10 matches old output" -tap_end_subtest "finddate matches old output" - -tap_start_subtest "finddate extreme examples" 3 -declare -i actual="10#$(finddate "${start_date}" d+366)" -declare -i expected="20000929" -if [ "${actual}" -eq "${expected}" ]; -then - result=0 -else - result=1 -fi -tap_log_subtest "${result}" "One-year ahead match: expected ${expected} got ${actual}" - -actual="10#$(finddate "${start_date}" d+3653)" -expected="20090929" -if [ "${actual}" -eq "${expected}" ]; -then - result=0 -else - result=1 -fi -tap_log_subtest "${result}" "Ten-year ahead match: expected ${expected} got ${actual}" - -actual="10#$(finddate "${start_date}" d+7305)" -expected="20190929" -if [ "${actual}" -eq "${expected}" ]; -then - result=0 -else - result=1 -fi -tap_log_subtest "${result}" "Twenty-year ahead match: expected ${expected} got ${actual}" -tap_end_subtest "finddate extreme examples" - -tap_end