forked from ESCOMP/CTSM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkprocdata_map_all
executable file
·202 lines (178 loc) · 7.38 KB
/
mkprocdata_map_all
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/bash
# This script runs mkprocdata_map_wrap on all files matching a given
# pattern within a directory.
# Created by Bill Sacks, 5-26-11
# ----------------------------------------------------------------------
# LOCAL FUNCTIONS DEFINED HERE
# ----------------------------------------------------------------------
function Usage {
script_name=`basename $0`
echo "Usage: $script_name -p prefix -m map_file -t template_file [-d] [-e executable-path] [-h] [-i] [-l] [-o output_suffix] [-r diRectory] [-s suffix]"
echo ""
echo "This script runs mkprocdata_map_wrap on all files matching a"
echo "given pattern within a directory."
echo ""
echo "'prefix' gives the prefix of the files on which"
echo "mkprocdata_map_wrap should be run; 'prefix' should NOT contain"
echo "wildcard characters. The prefix is also used to translate"
echo "from input to output file names (see examples below)"
echo ""
echo "'map_file' gives the name (and full path if not in the current"
echo "directory) of the mapping file"
echo ""
echo "'template_file' gives the name (and full path if not in the"
echo "current directory) of the template file, from which we read"
echo "lats, lons and some other variables"
echo ""
echo "The following are optional arguments:"
echo ""
echo "[-d]: Do a test (Dry run): do all error-checking on"
echo " arguments and print commands that would be run, but"
echo " don't actually run commands"
echo ""
echo "[-e executable-path]: Gives the path of the mkprocdata_map executable."
echo " If not specified, the path is determined by the"
echo " default value in mkprocdata_map_wrap."
echo ""
echo "[-h]: Print this help message and exit"
echo ""
echo "[-i]: Ignore (skip) existing output files; if this option is"
echo " not specified, then the script dies with an error if"
echo " any of the desired output files already exist"
echo ""
echo "[-l]: Option passed to mkprocdata_map_wrap: rather than computing"
echo " landfrac and related variables by regridding the input file,"
echo " instead copy these variables directly from the template file."
echo ""
echo "[-o output_suffix]: suffix to append to the end of the prefix"
echo " on the output files"
echo " If not specified, '_2d' is used"
echo ""
echo "[-r diRectory]: Do the processing in the given directory."
echo " If not specified, processing is done in the"
echo " current working directory."
echo ""
echo "[-s suffix]: Run mkprocdata_map_wrap on all files matching the"
echo " pattern '\${prefix}\${suffix}'. The suffix can -"
echo " and often will - contain wildcards; but"
echo " remember to enclose 'suffix' in quotes to"
echo " prevent shell expansion."
echo " If not specified, run mkprocdata_map_wrap on all"
echo " files matching '\${prefix}*'"
echo ""
echo ""
echo "Example: $script_name -p Ib14_ne30np4_gx1v6 -m map_ne30np4_to_fv1.9x2.5_aave_da_091230.nc -t Ib19_1.9x2.5_gx1v6.clm2.h0.0001-01.nc"
echo "This will run mkprocdata_map_wrap on all files whose names begin"
echo "with 'Ib14_ne30np4_gx1v6' in the current directory, using the"
echo "mapping file named 'map_ne30np4_to_fv1.9x2.5_aave_da_091230.nc'"
echo "and the template file named 'Ib19_1.9x2.5_gx1v6.clm2.h0.0001-01.nc'"
echo "For an input file named:"
echo " Ib14_ne30np4_gx1v6.clm2.h0.0001-01-06-00000.nc"
echo "The output file will be named:"
echo " Ib14_ne30np4_gx1v6_2d.clm2.h0.0001-01-06-00000.nc"
echo ""
echo "Example: $script_name -o '_remap' -s '*.h0.0001*.nc' -p Ib14_ne30np4_gx1v6 -m map_ne30np4_to_fv1.9x2.5_aave_da_091230.nc -t Ib19_1.9x2.5_gx1v6.clm2.h0.0001-01.nc"
echo "This will run mkprocdata_map_wrap on all files whose names match"
echo "the pattern 'Ib14_ne30np4_gx1v6*.h0.0001*.nc', in the"
echo "current directory, using the mapping file named"
echo "'map_ne30np4_to_fv1.9x2.5_aave_da_091230.nc' and the"
echo "template file named Ib19_1.9x2.5_gx1v6.clm2.h0.0001-01.nc"
echo "For an input file named:"
echo " Ib14_ne30np4_gx1v6.clm2.h0.0001-01-06-00000.nc"
echo "The output file will be named:"
echo " Ib14_ne30np4_gx1v6_remap.clm2.h0.0001-01-06-00000.nc"
echo ""
}
# ----------------------------------------------------------------------
# BEGIN MAIN SCRIPT
# ----------------------------------------------------------------------
script_dir=`dirname $0`
source $script_dir/mkprocdata_map_functions.bash
# ----------------------------------------------------------------------
# Handle command-line arguments
# ----------------------------------------------------------------------
# define default values:
# required arguments:
prefix=""
map_file=""
template_file=""
# optional arguments:
directory="."
ignore_existing=0
output_suffix="_2d"
suffix="*"
dryrun=0
extra_args=""
while getopts de:hilm:o:p:r:s:t: opt; do
case $opt in
d) dryrun=1;;
e) extra_args="$extra_args -e $OPTARG";;
h) Usage; exit;;
i) ignore_existing=1;;
l) extra_args="$extra_args -l";;
m) map_file=$OPTARG;;
o) output_suffix=$OPTARG;;
p) prefix=$OPTARG;;
r) directory=$OPTARG;;
s) suffix=$OPTARG;;
t) template_file=$OPTARG;;
\?) Usage; exit 1
esac
done
# ----------------------------------------------------------------------
# Error checking on arguments
# ----------------------------------------------------------------------
if [ -z "$prefix" ]; then
echo "Must specify a prefix"
Usage
exit 1
fi
check_file_arg "$map_file" "map"
check_file_arg "$template_file" "template"
# Make sure directory is really a directory
if [ ! -d $directory ]; then
echo "ERROR: $directory is not a directory"
echo ""
Usage
exit 1
fi
# ----------------------------------------------------------------------
# Change to desired directory
# ----------------------------------------------------------------------
olddir=`pwd`
cd $directory
# ----------------------------------------------------------------------
# Get list of files matching the given pattern; make sure there really
# are some matching files
# ----------------------------------------------------------------------
files=`ls ${prefix}${suffix}`
if [ $? -ne 0 ]; then
echo "ERROR trying to find files matching: ${prefix}${suffix}"
echo ""
Usage
exit 1
fi
# ----------------------------------------------------------------------
# Loop through files matching the given pattern; run mkprocdata_map_wrap for each
# ----------------------------------------------------------------------
for infile in $files; do
outfile=${infile/$prefix/${prefix}${output_suffix}}
if [ -e $outfile ]; then
if [ $ignore_existing -eq 0 ]; then
echo ""
echo "ERROR: output file $outfile already exists"
exit 1
else
echo ""
echo "WARNING: output file $outfile already exists: skipping"
echo ""
fi
else # outfile does not exist
echo ""
do_cmd "${script_dir}/mkprocdata_map_wrap -i $infile -o $outfile -m $map_file -t $template_file $extra_args" $dryrun
fi
done
# ----------------------------------------------------------------------
# Clean up
# ----------------------------------------------------------------------
cd $olddir