Path: blob/devel/elmerice/IceSheet/Greenland/DATA/scripts/MAR_Bamber5km_to_EPSG3413.sh
5348 views
#!/bin/bash1Ice_DENSITY=910.0 # kg/m^323#####################################4#####################################5function usage {6echo "This code will reproject MARv3.5 forcing"7echo "from the Bamber 5km grid to EPSG 3413 (Standard Nort Pole stereopolar projection for ISMIP6)"8echo "convert SMBCORR variable from mm (water eq.) to m (ice eq.) using an ice density of $Ice_DENSITY kg/m^3"9echo10echo "Files can be downloaded from: ftp://ftp.climato.be/fettweis/"11echo "use yearly files interpolated on the 5km grid"12echo "usage : "13echo " sh MAR_Bamber5km_to_EPSG3413.sh <FILE_TO_PROCESS>"14echo15exit 116}1718#####################################19#####################################20main() {212223FILE_IN=${1}2425# check file dexist26if [ ! -f "$FILE_IN" ]; then27echo "File $FILE does not exist. --ABORT-- "28exit 129fi3031# number of tile steps32nTime=$(ncdmnsz TIME $FILE_IN)33# test that this is not empty34if [ -z "$nTime" ]; then35echo "There is no TIME dimension?. --ABORT-- "36exit 137fi3839echo "There is $nTime timesteps in input file $FILE_IN"4041filename=$(basename "$FILE_IN")42filename="${filename%.*}"43FILE_OUT=$filename"_EPSG3413.nc"44echo "projecting all timesteps in $FILE_OUT"4546if [ -f "$FILE_OUT" ]; then47echo "output file already exist. --ABORT--"48exit 149fi505152#### Output Grid definition53## Grid resolution54res=500055## Grid cell centers56xmin=-72000057xmax=96000058ymin=-345000059ymax=-57000060#######################61# get corner definition rq. this will perform only integer operation.62xlc=$(($xmin-$res/2))63xrc=$(($xmax+$res/2))64ylc=$(($ymin-$res/2))65yuc=$(($ymax+$res/2))66#echo 'grid corners :' $xlc $xrc $ylc $yuc67###############6869for (( c=1; c<=nTime; c++ ))70do71# remove temporary files72rm -rf tmp[1-4].nc73echo "Reproject Time $c"7475## extract current_time76ncks -O -F -v SMBCORR -d TIME,$c,$c,1 $FILE_IN tmp1.nc7778#Fix the coordinates to the Bamber 5km grid definition79ncap2 -O -v -s "smb=SMBCORR" -s "X=1.0*(X-5)-800.0" -s "Y=1.0*(Y-5)-3400.0" tmp1.nc tmp2.nc8081### reprojection using gdalwrap82gdalwarp -overwrite -of netcdf \83-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" \84-t_srs EPSG:3413 -tr $res $res -te $xlc $ylc $xrc $yuc \85NETCDF:"tmp2.nc":smb tmp3.nc8687# make a time record dimension88ncecat -O -u TIME -v Band1 tmp3.nc tmp4.nc8990# Append current time step to output File91ncrcat -h --rec_apn tmp4.nc $FILE_OUT9293done94# remove temporary files95rm -rf tmp[1-4].nc9697## copy time variable dimension98ncks -h -A -v TIME $FILE_IN $FILE_OUT99100## fix variable names and attributes101ncrename -h -v Band1,SMBCORR $FILE_OUT102ncap2 -h -A -s "smb_ice=SMBCORR/$Ice_DENSITY" $FILE_OUT103ncatted -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_OUT104105}106107108# ncdmnsz $dmn_nm $fl_nm : What is dimension size?109function ncdmnsz { ncks --trd -m -M ${2} | grep -E -i ": ${1}, size =" | cut -f 7 -d ' ' | uniq ; }110##111112if [ $# -eq 0 ]113then114echo "No arguments supplied -- ABORT --"115echo116usage117fi118119main "$@"120121122