#!/bin/ksh
# Kornshell Script to extract E2OBS Initial conditions and ancillary data 
# The files 
# surfinit.nc and surfclim.nc provide global fields on 0.5x0.5 regular latxlon
# Provider ECMWF 
# Script setup---------------------------------------------
#set -x
forcing_version=met_forcing_v0
#data_label=daily_E2OBS           #obtain daily-mean fields
data_label=E2OBS                  #obtain tri-hourly fields
#Domain latmin latmax lonmin lonmax
#echo 23 52 -127 -65>latlon_domain #domain corresponding to CONUS
#echo 40 52 -7 13 >latlon_domain   #domain corresponding to AROME-France
#echo 51.5 51.5 -1 -1 >latlon_domain   #domain corresponding to Reading-UK
#echo 49 60 -12 5    >latlon_domain   #domain corresponding to UK
echo 25.25 75.25 -11.25 62.25 >latlon_domain  # domain EuroMed 14948 (148x101) points

startdate=20120101
enddate=20121231
nstep=8 # Number of forcing timestep in a day (8=tri-hourly)
# End setup---------------------------------------------
dateloop=$startdate
date_label=$startdate"_to_"$enddate
yyyymmend=`echo $enddate | cut -c1-6`
latindex=`awk '{print int((90+$1)*2+1)","int((90+$2)*2+1)}' latlon_domain`
lonindex=`awk '{print int((180+$3)*2+1)","int((180+$4)*2+1)}' latlon_domain`
nlat=`awk '{print int((90+$2)*2+1)-int((90+$1)*2+1)+1}' latlon_domain`
nlon=`awk '{print int((180+$4)*2+1)-int((180+$3)*2+1)+1}' latlon_domain`
echo "Latitude Longitude ranges (start,end)" = $latindex ";" $lonindex
echo "Domain (nlat x nlon) =" $nlat "x" $nlon
rm -rf  surfinit_${data_label}.nc  surfclim_${data_label}.nc

ncks -A -F -d lat,$latindex -d lon,$lonindex surfinit.nc surfinit_${data_label}.nc
ncks -A -F -d lat,$latindex -d lon,$lonindex surfclim.nc surfclim_${data_label}.nc
