#!/bin/sh
###############################################################################
##                                                                           ##
##                  r  e  g  r  i  d  l  p  j                                ##
##                                                                           ##
##    sh script to regrid input files to specified countries                 ##
##                                                                           ##
##    Usage: regridlpj [-dir path] filesuffix countrycode ...                ##
##                                                                           ##
##    written by Werner von Bloh, PIK Potsdam                                ##
##                                                                           ##
##    Last change: 03.03.2013                                                ##
##                                                                           ##
###############################################################################

if [ $# -lt 2 ]
then
  echo >&2 Error: Missing arguments 
  echo >&2 Usage: $0 [-dir path] suffix countrycode ...
  exit 1
fi
if [ $1 = "-dir" ]
then
  shift 1
  dir=$1
  shift 1
else
  dir=$PWD
fi

if [ $# -lt 2 ]
then
  echo >&2 Error: Missing arguments 
  echo >&2 Usage: $0 [-dir path] suffix countrycode ...
  exit 1
fi
name=$1
shift 1
if getcountry $LPJINPATH/input_VERSION2/cow_mg_2006_full.bin  $LPJINPATH/input_VERSION2/grid.bin $dir/grid_$name.clm $* ;
then
  regridsoil -same  $LPJINPATH/input_VERSION2/grid.bin $dir/grid_$name.clm  $LPJINPATH/input_VERSION2/soil_new_67420.bin $dir/soil_$name.bin
  regridsoil -same  /input_VERSION2/grid.bin $dir/grid_$name.clm  $LPJINPATH/input_VERSION2/glwd_lakes_and_rivers.bin $dir/lakes_$name.bin
  regridclm -same -size4 $LPJINPATH/input_VERSION2/grid.bin $dir/grid_$name.clm  $LPJINPATH/input_VERSION2/mlightning.clm $dir/lightning_$name.clm
  regridclm -same $LPJINPATH/input_VERSION2/grid.bin $dir/grid_$name.clm  $LPJINPATH/input_VERSION2/human_ignition.clm $dir/human_ignition_$name.clm
  regridclm -same $LPJINPATH/input_VERSION2/grid.bin $dir/grid_$name.clm  $LPJINPATH/input_VERSION2/popdens_HYDE_1901_2010_bi.clm $dir/popdens_$name.clm
  regridclm -same $LPJINPATH/input_VERSION2/grid.bin $dir/grid_$name.clm  $LPJINPATH/input_VERSION2/cft1700_2005_irrigation_systems_64bands.bin $dir/landuse_$name.clm
  regridclm -same $LPJINPATH/input_VERSION2/grid.bin $dir/grid_$name.clm  $LPJINPATH/input_VERSION2/cru_ts_3_10.1901.2009.dtr.clm $dir/tamp_$name.clm
  regridclm -same $LPJINPATH/input_VERSION2/grid.bin $dir/grid_$name.clm  $LPJINPATH/input_VERSION2/mwindspeed_1860-2100_67420.clm $dir/windspeed_$name.clm
  regridclm -same $LPJINPATH/input_VERSION2/grid.bin $dir/grid_$name.clm  $LPJINPATH/input_VERSION2/cru_ts_3_10.1901.2009.tmp.clm $dir/tmp_$name.clm
  regridclm -same $LPJINPATH/input_VERSION2/grid.bin $dir/grid_$name.clm  $LPJINPATH/input_VERSION2/gpcc_cru09_prec_monthly_1901_2009.clm $dir/prec_$name.clm
  regridclm -same $LPJINPATH/input_VERSION2/grid.bin $dir/grid_$name.clm  $LPJINPATH/input_VERSION2/cru_ts_3_10.1901.2009.cld.clm $dir/cld_$name.clm
  regridclm -same $LPJINPATH/input_VERSION2/grid.bin $dir/grid_$name.clm  $LPJINPATH/input_VERSION2/lwnet_erainterim_1901-2011.clm $dir/lwnet_$name.clm
  regridclm -same $LPJINPATH/input_VERSION2/grid.bin $dir/grid_$name.clm  $LPJINPATH/input_VERSION2/swdown_erainterim_1901-2011.clm $dir/swdown_$name.clm
  regridclm -same $LPJINPATH/input_VERSION2/grid.bin $dir/grid_$name.clm  $LPJINPATH/input_VERSION2/gpcc_cru09_wet_monthly_1901_2009.clm $dir/wet_$name.clm
  regridclm -same $LPJINPATH/input_VERSION2/grid.bin $dir/grid_$name.clm  $LPJINPATH/input_VERSION2/cow_mg_2006_full.bin $dir/country_$name.clm

  cat << EOF >input_$name.conf
/*********************************************************************/
/*                                                                   */
/*                 i  n  p  u  t  .  c  o  n  f                      */
/*                                                                   */
/* Configuration file for input dataset for LPJ C Version 4.0.001    */
/*                                                                   */
/* Created: $(date +"%d.%m.%Y")                                               */
/*                                                                   */
/*********************************************************************/

#include "include/conf.h" /* include constant definitions */
#undef LPJ

RAW $dir/soil_$name.bin
CLM $dir/grid_$name.clm 
#ifdef WITH_LANDUSE
CLM $dir/country_$name.clm
CLM $dir/landuse_$name.clm
#endif
CLM $dir/tmp_$name.clm
CLM $dir/prec_$name.clm
#ifdef USE_RADIATION
CLM $dir/lwnet_$name.clm
CLM $dir/swdown_$name.clm
#else
CLM $dir/cld_$name.clm
#endif
#ifdef WITH_SPITFIRE
CLM $dir/windspeed_$name.clm
CLM $dir/tamp_$name.clm
CLM $dir/lightning_$name.clm
CLM $dir/human_ignition_$name.clm
#endif
#if defined(WITH_SPITFIRE) || defined(WITH_FIREWOOD)
#if defined(FROM_RESTART)
CLM $dir/popdens_$name.clm 
#endif
#endif
TXT input_VERSION2/co2_1841-2010.dat

#ifdef ISRANDOM

CLM $dir/wet_$name.clm

#endif
EOF
else
  exit 1
fi
