Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/elmerice/IceSheet/Greenland/DATA/scripts/MAR_Bamber5km_to_EPSG3413.sh
5348 views
1
#!/bin/bash
2
Ice_DENSITY=910.0 # kg/m^3
3
4
#####################################
5
#####################################
6
function usage {
7
echo "This code will reproject MARv3.5 forcing"
8
echo "from the Bamber 5km grid to EPSG 3413 (Standard Nort Pole stereopolar projection for ISMIP6)"
9
echo "convert SMBCORR variable from mm (water eq.) to m (ice eq.) using an ice density of $Ice_DENSITY kg/m^3"
10
echo
11
echo "Files can be downloaded from: ftp://ftp.climato.be/fettweis/"
12
echo "use yearly files interpolated on the 5km grid"
13
echo "usage : "
14
echo " sh MAR_Bamber5km_to_EPSG3413.sh <FILE_TO_PROCESS>"
15
echo
16
exit 1
17
}
18
19
#####################################
20
#####################################
21
main() {
22
23
24
FILE_IN=${1}
25
26
# check file dexist
27
if [ ! -f "$FILE_IN" ]; then
28
echo "File $FILE does not exist. --ABORT-- "
29
exit 1
30
fi
31
32
# number of tile steps
33
nTime=$(ncdmnsz TIME $FILE_IN)
34
# test that this is not empty
35
if [ -z "$nTime" ]; then
36
echo "There is no TIME dimension?. --ABORT-- "
37
exit 1
38
fi
39
40
echo "There is $nTime timesteps in input file $FILE_IN"
41
42
filename=$(basename "$FILE_IN")
43
filename="${filename%.*}"
44
FILE_OUT=$filename"_EPSG3413.nc"
45
echo "projecting all timesteps in $FILE_OUT"
46
47
if [ -f "$FILE_OUT" ]; then
48
echo "output file already exist. --ABORT--"
49
exit 1
50
fi
51
52
53
#### Output Grid definition
54
## Grid resolution
55
res=5000
56
## Grid cell centers
57
xmin=-720000
58
xmax=960000
59
ymin=-3450000
60
ymax=-570000
61
#######################
62
# get corner definition rq. this will perform only integer operation.
63
xlc=$(($xmin-$res/2))
64
xrc=$(($xmax+$res/2))
65
ylc=$(($ymin-$res/2))
66
yuc=$(($ymax+$res/2))
67
#echo 'grid corners :' $xlc $xrc $ylc $yuc
68
###############
69
70
for (( c=1; c<=nTime; c++ ))
71
do
72
# remove temporary files
73
rm -rf tmp[1-4].nc
74
echo "Reproject Time $c"
75
76
## extract current_time
77
ncks -O -F -v SMBCORR -d TIME,$c,$c,1 $FILE_IN tmp1.nc
78
79
#Fix the coordinates to the Bamber 5km grid definition
80
ncap2 -O -v -s "smb=SMBCORR" -s "X=1.0*(X-5)-800.0" -s "Y=1.0*(Y-5)-3400.0" tmp1.nc tmp2.nc
81
82
### reprojection using gdalwrap
83
gdalwarp -overwrite -of netcdf \
84
-s_srs "+proj=stere +lat_0=90 +lat_ts=71 +lon_0=-39 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=km +no_defs" \
85
-t_srs EPSG:3413 -tr $res $res -te $xlc $ylc $xrc $yuc \
86
NETCDF:"tmp2.nc":smb tmp3.nc
87
88
# make a time record dimension
89
ncecat -O -u TIME -v Band1 tmp3.nc tmp4.nc
90
91
# Append current time step to output File
92
ncrcat -h --rec_apn tmp4.nc $FILE_OUT
93
94
done
95
# remove temporary files
96
rm -rf tmp[1-4].nc
97
98
## copy time variable dimension
99
ncks -h -A -v TIME $FILE_IN $FILE_OUT
100
101
## fix variable names and attributes
102
ncrename -h -v Band1,SMBCORR $FILE_OUT
103
ncap2 -h -A -s "smb_ice=SMBCORR/$Ice_DENSITY" $FILE_OUT
104
ncatted -h -a long_name,smb_ice,d,, -a long_name,SMBCORR,o,c,"Surface Mass Balance (with corrections)" -a units,smb_ice,o,c,"mIE/yr (Ice density=$Ice_DENSITY kg m^-3)" -a comment,global,a,c,"This is File $FILE_IN re-projected to EPSG:3413\n" $FILE_OUT
105
106
}
107
108
109
# ncdmnsz $dmn_nm $fl_nm : What is dimension size?
110
function ncdmnsz { ncks --trd -m -M ${2} | grep -E -i ": ${1}, size =" | cut -f 7 -d ' ' | uniq ; }
111
##
112
113
if [ $# -eq 0 ]
114
then
115
echo "No arguments supplied -- ABORT --"
116
echo
117
usage
118
fi
119
120
main "$@"
121
122