Skip to content

Commit 68e7f2c

Browse files
PaulWesselseisman
andauthored
Optionally create both pixel and gridline versions during downsamling (#39)
* Create both pixel and gridline version during downsamling While the master file cannot be changed from one registration to another without severe loss of the highest frequencies, for the downsampling we can create both pixel and gridline versions of whatever the master file was. This is helpful when you need to coregistered other data sets, especially images which usually are pixel registered - in contrast, lots of geophysical data are gridline registered. * Update scripts/srv_downsampler.sh Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Dongdong Tian <[email protected]>
1 parent 265738d commit 68e7f2c

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

recipes/earth_age.recipe

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#
1414
# Destination: Specify output node registration, file prefix, and netCDF format
1515
# DST_MODE=Cartesian
16-
# DST_NODES=g
16+
# DST_NODES=g,p
1717
# DST_PREFIX=earth_age
1818
# DST_FORMAT=ns+sa
1919
# DST_TILE_TAG=ER

recipes/earth_relief.recipe

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#
1414
# Destination: Specify output node registration, file prefix, and netCDF format
1515
# DST_MODE=Cartesian
16-
# DST_NODES=g
16+
# DST_NODES=g,p
1717
# DST_PREFIX=earth_relief
1818
# DST_FORMAT=ns
1919
# DST_TILE_TAG=ER

scripts/srv_downsampler.sh

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# usage: srv_downsampler.sh recipe.
55
# where
6-
# recipe: The name of the recipe file (e.g., earth_relief.recipe)
6+
# recipe: The name of the recipe file (e.g., earth_relief)
77
#
88
# These recipe files contain meta data such as where to get the highest-resolution
99
# master file from which to derive the lower-resolution versions, information about
@@ -34,7 +34,7 @@ fi
3434
cd ${TOPDIR}/staging
3535

3636
# 2. Get recipe full file path
37-
RECIPE=$TOPDIR/recipes/$1
37+
RECIPE=$TOPDIR/recipes/$1.recipe
3838
if [ ! -f $RECIPE ]; then
3939
echo "error: srv_downsampler.sh: Recipe $RECIPE not found"
4040
exit -1
@@ -56,6 +56,7 @@ source /tmp/par.sh
5656
# 4. Get the file name of the source file
5757
SRC_BASENAME=`basename ${SRC_FILE}`
5858
SRC_ORIG=${SRC_BASENAME}
59+
5960
# 5. Determine if this source is an URL and if we need to download it first
6061
is_url=`echo ${SRC_FILE} | grep -c :`
6162
if [ $is_url ]; then # Data source is an URL
@@ -66,8 +67,17 @@ if [ $is_url ]; then # Data source is an URL
6667
SRC_FILE=${SRC_BASENAME}
6768
fi
6869

69-
# 6. Extract the requested resolutions
70+
# 6. Extract the requested resolutions and registrations
71+
7072
grep -v '^#' $RECIPE > /tmp/res.lis
73+
DST_NODES=$(echo $DST_NODES | tr ',' ' ')
74+
REG=$(gmt grdinfo ${SRC_FILE} -Cn -o10)
75+
if [ $REG -eq 0 ]; then
76+
SRC_REG=g
77+
else
78+
SRC_REG=p
79+
fi
80+
7181
# 7. Replace underscores with spaces in the title and remark
7282
TITLE=`echo ${SRC_TITLE} | tr '_' ' '`
7383
REMARK=`echo ${SRC_REMARK} | tr '_' ' '`
@@ -100,25 +110,28 @@ while read RES UNIT CHUNK MASTER; do
100110
if [ ! ${RES} = "01" ]; then # Use plural unit
101111
UNIT_NAME="${UNIT_NAME}s"
102112
fi
103-
DST_FILE=${DST_PREFIX}_${RES}${UNIT}.grd
104-
grdtitle="${TITLE} at ${RES} arc ${UNIT_NAME}"
105-
# Note: The ${SRC_ORIG/+/\\+} below is to escape any plus-symbols in the file name with a backslash so grdedit -D will work
106-
if [ -f ${DST_FILE} ]; then # Do nothing
107-
echo "${DST_FILE} exist - skipping"
108-
elif [ "X${MASTER}" = "Xmaster" ]; then # Just make a copy of the master
109-
echo "Convert ${SRC_FILE} to ${DST_FILE}=${DST_FORMAT}"
110-
gmt grdconvert ${SRC_FILE} ${DST_FILE}=${DST_FORMAT} --IO_NC4_DEFLATION_LEVEL=9
111-
remark="Reformatted from master file ${SRC_ORIG/+/\\+} [${REMARK}]"
112-
gmt grdedit ${DST_FILE} -D+t"${grdtitle}"+r"${remark}"+z"${SRC_NAME} (${SRC_UNIT})"
113-
114-
else # Must downsample to a lower resolution via spherical Gaussian filtering
115-
# Get suitable Gaussian full-width filter rounded to nearest 0.1 km after adding 50 meters for noise
116-
echo "Down-filter ${SRC_FILE} to ${DST_FILE}=${DST_FORMAT}"
117-
FILTER_WIDTH=`gmt math -Q ${SRC_RADIUS} 2 MUL PI MUL 360 DIV $INC MUL 0.05 ADD 10 MUL RINT 10 DIV =`
118-
gmt grdfilter ${SRC_FILE} -Fg${FILTER_WIDTH} -D${FMODE} -I${RES}${UNIT} -r${DST_NODES} -G${DST_FILE}=${DST_FORMAT} --IO_NC4_DEFLATION_LEVEL=9 --IO_NC4_CHUNK_SIZE=${CHUNK} --PROJ_ELLIPSOID=Sphere
119-
remark="Obtained by Gaussian ${DST_MODE} filtering (${FILTER_WIDTH} km fullwidth) from ${SRC_FILE/+/\\+} [${REMARK}]"
120-
gmt grdedit ${DST_FILE} -D+t"${grdtitle}"+r"${remark}"+z"${SRC_NAME} (${SRC_UNIT})"
121-
fi
113+
for REG in ${DST_NODES}; do # Probably doing both pixel and gridline registered output, except for master */
114+
DST_FILE=${DST_PREFIX}_${RES}${UNIT}_${REG}.grd
115+
grdtitle="${TITLE} at ${RES} arc ${UNIT_NAME}"
116+
# Note: The ${SRC_ORIG/+/\\+} below is to escape any plus-symbols in the file name with a backslash so grdedit -D will work
117+
if [ -f ${DST_FILE} ]; then # Do nothing
118+
echo "${DST_FILE} exist - skipping"
119+
elif [ "X${MASTER}" = "Xmaster" ]; then # Just make a copy of the master to a new output file
120+
if [ ${REG} = ${SRC_REG} ]; then # Only do the matching node registration for master
121+
echo "Convert ${SRC_FILE} to ${DST_FILE}=${DST_FORMAT}"
122+
gmt grdconvert ${SRC_FILE} ${DST_FILE}=${DST_FORMAT} --IO_NC4_DEFLATION_LEVEL=9
123+
remark="Reformatted from master file ${SRC_ORIG/+/\\+} [${REMARK}]"
124+
gmt grdedit ${DST_FILE} -D+t"${grdtitle}"+r"${remark}"+z"${SRC_NAME} (${SRC_UNIT})"
125+
fi
126+
else # Must down-sample to a lower resolution via spherical Gaussian filtering
127+
# Get suitable Gaussian full-width filter rounded to nearest 0.1 km after adding 50 meters for noise
128+
echo "Down-filter ${SRC_FILE} to ${DST_FILE}=${DST_FORMAT}"
129+
FILTER_WIDTH=`gmt math -Q ${SRC_RADIUS} 2 MUL PI MUL 360 DIV $INC MUL 0.05 ADD 10 MUL RINT 10 DIV =`
130+
gmt grdfilter ${SRC_FILE} -Fg${FILTER_WIDTH} -D${FMODE} -I${RES}${UNIT} -r${REG} -G${DST_FILE}=${DST_FORMAT} --IO_NC4_DEFLATION_LEVEL=9 --IO_NC4_CHUNK_SIZE=${CHUNK} --PROJ_ELLIPSOID=Sphere
131+
remark="Obtained by Gaussian ${DST_MODE} filtering (${FILTER_WIDTH} km fullwidth) from ${SRC_FILE/+/\\+} [${REMARK}]"
132+
gmt grdedit ${DST_FILE} -D+t"${grdtitle}"+r"${remark}"+z"${SRC_NAME} (${SRC_UNIT})"
133+
fi
134+
done
122135
done < /tmp/res.lis
123136
# 10. Clean up /tmp
124137
rm -f /tmp/res.lis /tmp/par.sh

0 commit comments

Comments
 (0)