-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathget_hurdat-storm.sh
executable file
·152 lines (120 loc) · 4.08 KB
/
get_hurdat-storm.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
###########################################################################
### Author: Panagiotis Velissariou <panagiotis.velissariou@noaa.gov>
###
### Version - 1.0 Mon Nov 16 2020
###########################################################################
###====================
# Make sure that the current working directory is in the PATH
[[ ! :$PATH: == *:".":* ]] && export PATH="${PATH}:."
# Get the directory where the script is located
if [[ $(uname -s) == Darwin ]]; then
# readonly scrDIR="$(cd "$(dirname "$(greadlink -f -n "${BASH_SOURCE[0]}" )" )" && pwd -P)"
readonly scrNAME="$( grealpath -s "${BASH_SOURCE[0]}" )"
readonly scrDIR="$(cd "$(dirname "${scrNAME}" )" && pwd -P)"
else
# readonly scrDIR="$(cd "$(dirname "$(readlink -f -n "${BASH_SOURCE[0]}" )" )" && pwd -P)"
readonly scrNAME="$( realpath -s "${BASH_SOURCE[0]}" )"
readonly scrDIR="$(cd "$(dirname "$(realpath -s "${BASH_SOURCE[0]}")" )" && pwd -P)"
fi
funcs="$( find . -type f -name "functions_build" | head -n 1 )"
if [ -f "${funcs}" ]; then
source "${funcs}"
else
echo " ### ERROR:: in ${scrNAME}"
echo " Cannot load the required file: ${funcs}"
echo " Exiting now ..."
echo
exit 1
fi
unset funcs
###====================
#########
# Call ParseArgs to get the user input.
ParseArgsTracs "${@}"
##########
# Set the variables for this script
MY_DATAFILE="hurdat2-1851-2019-052520.txt"
if [ -z "${MY_ROOTDIR:-}" ]; then
echo "The data directory is not specified."
echo "Run: \"$(basename ${scrNAME}) -h\" to see available options."
echo "Exiting ..."
exit 1
fi
if [ -z "${MY_BASIN:-}" ]; then
echo "The basin where the storm occured is not specified."
echo "Run: \"$(basename ${scrNAME}) -h\" to see available options."
echo "Exiting ..."
exit 1
fi
if [ -z "${MY_STORMNAME:-}" ]; then
echo "The name of the storm is not specified."
echo "Run: \"$(basename ${scrNAME}) -h\" to see available options."
echo "Exiting ..."
exit 1
fi
if [ -z "${MY_YEAR:-}" ]; then
echo "The year of the storm is not specified."
echo "Run: \"$(basename ${scrNAME}) -h\" to see available options."
echo "Exiting ..."
exit 1
fi
##########
############################################################
### START THE CALCULATIONS
############################################################
datDIR="${MY_ROOTDIR}"
datFILE="${datDIR}/${MY_DATAFILE}"
outFILE="`toLOWER ${MY_STORMNAME}`${MY_YEAR}-hurdat2.dat"
if [ ! -e "${datFILE}" ]; then
echo
echo "The data file \"${datFILE}\" does not exist."
echo "Adjust the variable MY_DATAFILE in the script"
echo "Exiting now ..."
echo
exit 1
fi
grep_cmd="grep -n -i \"^${MY_BASIN}.*${MY_YEAR}.*${MY_STORMNAME}\" ${datFILE}"
searchSTR="$( eval ${grep_cmd} 2>/dev/null )"
if [ -n "${searchSTR:+1}" ]; then
datIDS=( $( echo "${searchSTR}" | sed -e "s@[:,]@ @g" ) )
nDatIDS=${#datIDS[@]}
if [ ${nDatIDS:-0} -eq 4 ]; then
line1=${datIDS[0]}
lineN=$(( ${line1} + ${datIDS[3]} ))
datID=${datIDS[1]}
datST=${datIDS[2]}
sed_cmd="sed -n \"${line1},${lineN}p\" ${datFILE} > ${outFILE}"
eval "${sed_cmd} 2>&1"
err=$?
if [ ${err:-0} -eq 0 ]; then
echo "Created the data file: ${outFILE}"
else
echo
echo "ERROR :: Could not get a proper data block for ${MY_STORMNAME} due to \"sed\" errors."
echo "Command used: ${sed_cmd}"
echo
echo "===================="
cat ${outFILE}
echo "===================="
echo
[ -e "${outFILE}" ] && rm -f ${outFILE}
fi
else # err
echo "ERROR :: Could not find a proper storm block in the file: ${datFILE}"
echo "Used: BASIN = ${MY_BASIN}"
echo " YEAR = ${MY_YEAR}"
echo " STORM = ${MY_STORMNAME}"
echo
echo "searchSTR = ${searchSTR}"
[ -e "${outFILE}" ] && rm -f ${outFILE}
fi # nDatIDS
else
echo "ERROR :: Could not find a proper storm block in the file: ${datFILE}"
echo "Used: BASIN = ${MY_BASIN}"
echo " YEAR = ${MY_YEAR}"
echo " STORM = ${MY_STORMNAME}"
echo "Command used: ${grep_cmd}"
[ -e "${outFILE}" ] && rm -f ${outFILE}
fi # searchSTR
exit 0