#!/bin/ksh

# **************************** LICENSE START ***********************************
#
# Copyright 2012 ECMWF and INPE. This software is distributed under the terms
# of the Apache License version 2.0. In applying this license, ECMWF does not
# waive the privileges and immunities granted to it by virtue of its status as
# an Intergovernmental Organization or submit itself to any jurisdiction.
#
# ***************************** LICENSE END ************************************


set -a
set -x

# ----------------------------------------------------------------
# Script to run OBSTAT in a properly set environment.  
#
# OBSTAT = flexible ODB and BUFR feedback observation statistics 
# and plot application. Can be run on your workstation or the
# linux_cluster
# -----------------------------------------------------------------

# Include the control variables set via the Metview interface
if [[ $# -ne 0 ]] ; then
   . $1 
fi


FIRSTCYCLE=${FIRTSCYCLE:-"2010081512"}
LASTCYCLE=${LASTCYCLE:-"2010081512"}

# a 4-letter expver (e.g. 0001 for oper, 0028 for e-suite, 
#  eixd for rd, ER40 for ERA-40)
EXPVER1=${EXPVER1:-"0001"}   

# rd, od, e4 or ei
CLASS1=${CLASS1:-"od"}       

# DA, DCDA or SCDA
STREAM1=${STREAM1:-"DCDA"}  

# Activate the three next lines to overlay plots for two experiments (optional)
#EXPVER2=0029
#CLASS2=od
#STREAM2=DA

# yes: generates generic statistics for all data present in the input database
LALLDATA=${LALLDATA:-"no"}

# yes: make obstat plot
OBSTATPLOT=${OBSTATPLOT:-"yes"}
      
# yes: make varbc time series plot 
VARBC_PARAMS=no  

# yes: produce condition number plot 
CONDITION_NUMBER=no 
   
# yes: produce one normalized high resolution grib for the whole period
GEOGRIB_AVE_PERIOD=${GEOGRIB_AVE_PERIOD:-"yes"} 

# yes: concatenate scat data for the whole period
SCAT_PERIOD=${SCAT_PERIOD:-"yes"}  

# yes: produce ascii for the production of data coverage maps
LCOVERAGE=no       

# yes: the fast mode obstat plot where the obstat statistics
# files are retrieved from ecfs, i.e. without need to retrieve
# and process feedback/ODB files. This restrict the statistics 
# to the prepIFS/ops specified regions/instruments (not valid for gridded statistics)

USE_ECFSLOG=${USE_ECFSLOG:-"no"}

# yes: produce departure, varBC or condition number webplots;
# no: only produce postscript files
WEBPLOTS=no        

# A3, A4 or EPS  (A4, A3: size of plot; 
# EPS: one plot per page, suitable for publications)
LAYOUT=A4

# Default for short comment string for obstat plots (OBSTATPLOT=yes)
COMMENT=${COMMENT:-""}
if [[ x$COMMENT != x ]] ; then
   if [[  x$EXPVER2 = x ]]  ; then
       COMMENT="/$STREAM1 "
   else
       COMMENT="/$STREAM1 (black)  v. $EXPVER2/$STREAM2"
   fi
fi

# Activate the line below to define your personal web folder name. Only relevant
#  when WEBPLOTS=yes.
#WEBNOTE='Operations 0001 '

# -------------------------------------------------------------------------
# Settings required for user specific obstat statistics recalculated from 
# BUFR feedback or ODB-files, i.e. if USE_ECFSLOG='no'.
# Also controls the observing system selection for time series plots
#---------------------------------------------------------------------------

# stat definition file
STATDEF=${STATDEF:-"/home/mo/obstat/OBSTAT/stat.ref"}      
#PARAMS="/home/mo/obstat/obstat/data/params.txt"
#FLAGS="/home/mo/obstat/obstat/data/flags.txt"

cluster=$( echo $HOST | cut -c1-3)
if [[ $cluster = bee || $ARCH = rs6000 ]] ; then
  # linux cluster or AIX server
  mkdir $SCRATCH/OBSTATG
  STOREDIR=${STOREDIR:-"$SCRATCH/OBSTAT_GRIB/"}           # directory for ODB and feedback files
  EXEDIR=${EXEDIR:-"$SCRATCH/OBSTAT_GRIB/tmp_"}        # work directory with text and ps files
else
  # workstation
  mkdir /tmp/OBSTAT_GRIB
  STOREDIR=${STOREDIR:-"$SCRATCH/"}           # directory for ODB and feedback files
  EXEDIR=${EXEDIR:-"$SCRATCH/tmp_"}
fi
GRIBDIR=${GRIBDIR:-"$SCRATCH/$EXPVER1"}              # GRIB storage directory

# ODB storage directory
ODBDIR=${ODBDIR:-"$SCRATCH/$EXPVER1"}              

# put yes to calculate stats from ODB or BUFR feedback files (valid for ascii stats)
STACALC=${STACALC:-"yes"}   

# put yes to accumulate stat-files cycle by cycle (valid for ascii stats)
STAACCUM=${STAACCUM:-"no"}    

#  Determine which ODB or BUFR feedback files to extract, possibly a 
#  subset of observation types, i.e. only conventional data

# format of obs files : BUFR, ODB, USER
OBSFORMAT=${OBSFORMAT:-"ODB"}    

#USERDATA="$SCRATCH/data"        #Uncomment if OBSFORMAT=USE
UPDSOFT=${UPDSOFT:-"true"}

#------------------------------
# Check obs format
#------------------------------

if [[ $OBSFORMAT = ODB ]] ; then

  # Define ODB file type, i.e. CCMA, ECMA or ODBCMP
  ODBFILETYPE=${ODBFILETYPE:-"ECMA"}

  # yes: retrieve odb data from ecfs
  # no:  assumes odb data to be available in $STOREDIR
  RETRIEVEODB=${RETRIEVEODB:-"yes"}   

  # List of ODB databases to process
  if [[ $ODBFILETYPE = CCMA || $ODBFILETYPE = MONDB ]] ; then
    # For CCMA data all data is in one ODB database, so the default is 
    # datalist="oneodb". 
    # For specialized tasks, i.e. field-of-view related statistics for TOVS data,
    # the user must use datalist="tovs" which allows obstat use use an sql that
    # extract TOVS specific items from the CCMA. Other specialized requests are 
    # ahown as comments here below.

    ##datalist="tovs"
    ##datalist="reo3"
    ##datalist="conv"
    datalist="oneodb"
  else
    # Specify list of ODB databases to process
    # Some examples are included as commnets below

    ##datalist="conv satob scatt ssmi msu ssu amsua amsub hirs geos reo3 airs iasi gpsro tmi mhs ssmis amsre"
    datalist="tmi"
  fi


elif [[ $OBSFORMAT = BUFR ]] ; then
  # Specify list of BUFR feedback files to process
  # Some examples are commented out below

  ##datalist="fbconv fbscat fbssmi fbrad1c fbgeos fbtovs fbreo3 fbairs"
  datalist="fbssmi"

elif [[ $OBSFORMAT = USER ]] ; then

  datalist="reo3"
  DATA_INTERFACE="/home/mo/obstat/obstat/data/data_interface"

else
  echo "OBSFORMAT $OBSFORMAT is not implemented at ECMWF"
  exit 1
fi


#---------------------------------------------------------------------------------
# More specialized control switches that usually are kept at the default values
# are specified below
#----------------------------------------------------------------------------------

# put yes (recommended) to avoid storing many ODB or BUFR feedback files
FBKCLEAN=${FBKCLEAN:-"yes"}      

# put yes to delete intermediate stat-files after merging
STACLEAN=${STACLEAN:-"no"}       

# put yes to generate a postscript plot
STAPLOT=${STAPLOT:-"yes"}      


#STATFILE=statbcbc.txt    #(optional) file to plot (STAPLOT=yes)
                          # if statfile is not specified it writes stat$EXPVER.txt
#STATFILE=$HOME/obstat/stat.ref

# OVERFILE is used to do specialized comparison of two experiments (optional)
# If both EXPVER1 and EXPVER2 are specified the user specified OVERFILE is not used.
# OVERFILE="$STOREDIR/tmp$EXPVER2/stat$EXPVER2.txt" 

#TSTEP=6   # Set timestep in hours between analyses. Typically 12 or 6.
           # For fast mode options TSTEP must be at least equal to the 
           # 3D-Var/4D-Var assimilation period.  This makes it possible do
           # statistics in steps of e.g. 24 hours.
# Select a NSEW box for obstat ODS file ASCII print out of observation statistics

#ODSNORTH=49. 
#ODSSOUTH=40.
#ODSEAST=145. 
#ODSWEST=138.  
#ODSFLAGFILTER=5

#--------------------------------------
# Further control of plot layout
#--------------------------------------

# put yes to replace rms by std.dev
STDEV=${STDEV:-"yes"} 
         
WHISKERS=yes         # put yes to get st.dev box and min/max whiskers on histogram plots
FIXEDAXES=yes        # put yes to get fixed preselected axis limits on obstat plots
PLOTBIAS=yes         # put yes to include bias correction curves on satellite data plots
PLOTAISRBIAS=no      # put yes to include AIRS bias correction curves on AIRS data plots
HUMMINLEV=70.        # limit TEMP humidity vertical scale to HUMMINLEV hPa




# Use these to raise the print level of diagnostic output and possibly stop it early
# verbosity level (0 to 5)
IVERB=0             

# if specified, number of bufr reads before stopping 
#IMAXREAD=1000     

# EMOS cycle used for BUFR tables and EMOS libraries
EMOS_CYCLE=000360    

# put yes to merge stat-files into one
STAMERGE=${STAMERGE:-"yes"}  

# put yes to gzip stat files to save space
GZIPSTAT=no    

# Default script directory, libraries and sql
GRIB_API_VERSION=1.9.2; export GRIB_API_VERSION

DATADIR=${DATADIR:-"/home/mo/obstat/OBSTAT/data"}

LIBS="/home/mo/obstat/OBSTAT/lib/current/${ARCH}"

GRIB_API_INCLUDE="-I/usr/local/lib/metaps/lib/grib_api/jasper/include -I/usr/local/lib/metaps/lib/grib_api/${GRIB_API_VERSION}/include"

GRIB_API_LIB="-L/usr/local/lib/metaps/lib/grib_api/${GRIB_API_VERSION}/lib -lgrib_api_f90 -lgrib_api -L/usr/local/lib/metaps/lib/grib_api/jasper/lib -ljasper"

OBSTAT_LIB="$LIBS/libobstat.a"


#To speed up ploting it is possible to avoid relinking obstat executable(s)and
# instead use the default(s) specified below.
BINDIR="/home/mo/obstat/OBSTAT/bin/current/${ARCH}"
if [[ $ARCH = linux ]] ; then
  OBSTATPLOTEXE="$BINDIR/obstat_plot.x_linux"
  TIMESERIESPLOTEXE="$BINDIR/obstat_timeseries.x_linux"
  GEOPLOTEXE="$BINDIR/sat_geo_plot_linux"
  SCATTERPLOTEXE="$BINDIR/sat_scat_plot_linux"
  NORMALIZEGRIBEXE="$BINDIR/obstat_normalize_grib"
  ADDGRIBEXE="$BINDIR/obstat_add_grib"
  CONSCATEXE="$BINDIR/obstat_normalize_scat"
fi

#----------------------------------------
# Perform obstat in fast or slow mode
#----------------------------------------
$DATADIR/obstat_interface

