From 2b460e6d3b438aa42ceb5cec68c0794d49942425 Mon Sep 17 00:00:00 2001 From: DavidHuber-NOAA Date: Thu, 23 Apr 2026 12:46:33 +0000 Subject: [PATCH 1/3] Add the capability to source setpdy directly --- ush/setpdy.sh | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/ush/setpdy.sh b/ush/setpdy.sh index 1a378bd..10cf559 100755 --- a/ush/setpdy.sh +++ b/ush/setpdy.sh @@ -8,6 +8,7 @@ # PDYp2, ..., PDYp7. # History: Jul 27, 1998 - Implement new script # sep 17, 2015 - Add ability to specify number of dates +# Apr 23, 2026 - Add ability to source setpdy.sh directly # Usage: # setpdy.sh # Generate PDY script with PDYm7 to PDYp7 variables @@ -65,18 +66,45 @@ export err=$?; err_chk DATElne_plus=$(finddate.sh $PDY s+${dates_after_PDY}) export err=$?; err_chk +# Detect if setpdy is being sourced. If sourcing, we will add PDY variables to the environment. Otherwise, write a PDY file to be sourced in the parent script. +(return 0 2>/dev/null) && sourced=1 || sourced=0 + +# Write the PDY file so it can be sourced in the parent script. # Cut date-line to form PDY variables -if [ -f PDY ]; then rm PDY; fi +if [ -f PDY ]; then + rm PDY +fi for d in $(seq $dates_before_PDY -1 1); do # cut the date starting at position ($d-1)*9 from date-line - echo "export PDYm${d}=${DATElne_minus:((($d-1)*9)):8}" >>PDY + lhs=PDYm${d} + rhs=${DATElne_minus:((($d-1)*9)):8} + if [ $sourced -eq 1 ]; then + declare -x ${lhs}=${rhs} + else + echo "export ${lhs}=${rhs}" >>PDY + fi done -echo "export PDY=$PDY" >>PDY + +lhs=PDY +rhs=$PDY +if [ $sourced -eq 1 ]; then + declare -x ${lhs}=${rhs} +else + echo "export ${lhs}=${rhs}" >>PDY +fi + for d in $(seq $dates_after_PDY); do # cut the date starting at position ($d-1)*9 from date-line - echo "export PDYp${d}=${DATElne_plus:((($d-1)*9)):8}" >>PDY + lhs=PDYp${d} + rhs=${DATElne_plus:((($d-1)*9)):8} + if [ $sourced -eq 1 ]; then + declare -x ${lhs}=${rhs} + else + echo "export ${lhs}=${rhs}" >>PDY + fi done -chmod u+x PDY - -echo "Source PDY script to export PDYm${dates_before_PDY}, ..., PDY, ..., PDYp${dates_after_PDY} variables." +if [ $sourced -eq 0 ]; then + chmod u+x PDY + echo "Source PDY script to export PDYm${dates_before_PDY}, ..., PDY, ..., PDYp${dates_after_PDY} variables." +fi From e495c66fba4b0bbc713f0b95a77f8f0705bfe818 Mon Sep 17 00:00:00 2001 From: DavidHuber-NOAA Date: Thu, 23 Apr 2026 13:40:22 +0000 Subject: [PATCH 2/3] Use export instead of declare --- ush/setpdy.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ush/setpdy.sh b/ush/setpdy.sh index 10cf559..b705084 100755 --- a/ush/setpdy.sh +++ b/ush/setpdy.sh @@ -66,7 +66,7 @@ export err=$?; err_chk DATElne_plus=$(finddate.sh $PDY s+${dates_after_PDY}) export err=$?; err_chk -# Detect if setpdy is being sourced. If sourcing, we will add PDY variables to the environment. Otherwise, write a PDY file to be sourced in the parent script. +# Detect if setpdy is being sourced. If sourced, export PDY variables directly. Otherwise, write a PDY file to be sourced. (return 0 2>/dev/null) && sourced=1 || sourced=0 # Write the PDY file so it can be sourced in the parent script. @@ -74,12 +74,13 @@ export err=$?; err_chk if [ -f PDY ]; then rm PDY fi + for d in $(seq $dates_before_PDY -1 1); do # cut the date starting at position ($d-1)*9 from date-line lhs=PDYm${d} rhs=${DATElne_minus:((($d-1)*9)):8} if [ $sourced -eq 1 ]; then - declare -x ${lhs}=${rhs} + export ${lhs}=${rhs} else echo "export ${lhs}=${rhs}" >>PDY fi @@ -88,7 +89,7 @@ done lhs=PDY rhs=$PDY if [ $sourced -eq 1 ]; then - declare -x ${lhs}=${rhs} + export ${lhs}=${rhs} else echo "export ${lhs}=${rhs}" >>PDY fi @@ -98,7 +99,7 @@ for d in $(seq $dates_after_PDY); do lhs=PDYp${d} rhs=${DATElne_plus:((($d-1)*9)):8} if [ $sourced -eq 1 ]; then - declare -x ${lhs}=${rhs} + export ${lhs}=${rhs} else echo "export ${lhs}=${rhs}" >>PDY fi From 6aaabeca68145a866bc09477c03b7d5121ca9cf7 Mon Sep 17 00:00:00 2001 From: DavidHuber-NOAA Date: Thu, 23 Apr 2026 14:58:01 +0000 Subject: [PATCH 3/3] Cleanup setpdy.sh --- ush/setpdy.sh | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ush/setpdy.sh b/ush/setpdy.sh index b705084..1575faa 100755 --- a/ush/setpdy.sh +++ b/ush/setpdy.sh @@ -69,11 +69,8 @@ export err=$?; err_chk # Detect if setpdy is being sourced. If sourced, export PDY variables directly. Otherwise, write a PDY file to be sourced. (return 0 2>/dev/null) && sourced=1 || sourced=0 -# Write the PDY file so it can be sourced in the parent script. # Cut date-line to form PDY variables -if [ -f PDY ]; then - rm PDY -fi +if [ -f PDY ]; then rm PDY; fi for d in $(seq $dates_before_PDY -1 1); do # cut the date starting at position ($d-1)*9 from date-line