#! /bin/bash
# Last edited on 2009-12-12 00:07:41 by stolfi

# Processing, analyzing, and modeling the Wikipedia article count growth.

# Get the phase to start at:
if [[ $# -gt 0 ]]; then
  ini="$1"; shift
else
  ini=0
fi
if [[ $# -gt 0 ]]; then
  fin="$1"; shift
else
  fin=999
fi
  
step=0

step=$(( ${step} + 1 ))
if [[ ( ${ini} -le ${step} ) && ( ${fin} -ge ${step} ) ]]; then
echo "=============================================================" 1>&2 
echo "Step ${step}" 1>&2 
  do-reformat-size-raw.sh
fi

step=$(( ${step} + 1 ))
if [[ (${ini} -le ${step} ) && ( ${fin} -ge ${step} ) ]]; then
echo "=============================================================" 1>&2 
echo "Step ${step}" 1>&2 
  do-plot-size-glitches.sh
fi

setp=2
if [[ ( ${ini} -le ${step} ) && ( ${fin} -ge ${step} ) ]]; then
echo "=============================================================" 1>&2 
echo "Step ${step}" 1>&2 
  do-resample-size-data.sh
fi

step=$(( ${step} + 1 ))
if [[ ( ${ini} -le ${step} ) && ( ${fin} -ge ${step} ) ]]; then
echo "=============================================================" 1>&2 
echo "Step ${step}" 1>&2 
  do-reformat-edor-raw.sh
fi

step=$(( ${step} + 1 ))
if [[ ( ${ini} -le ${step} ) && ( ${fin} -ge ${step} ) ]]; then
echo "=============================================================" 1>&2 
echo "Step ${step}" 1>&2 
  do-resample-edor-data.sh
fi

step=$(( ${step} + 1 ))
if [[ ( ${ini} -le ${step} ) && ( ${fin} -ge ${step} ) ]]; then
echo "=============================================================" 1>&2 
echo "Step ${step} - PLOTTING THE MODELLED GROWTH" 1>&2 
  
# Only the irr-2009-11 dataset is worth processing:
datasets=( irr-2009-11 )
# datasets=( irr-2009-11 emo-2009-06 )


    for ff in ${datasets[@]} ; do
      for mod in 000 010 011 ; do
        echo "--------------------------------------------------------------" 1>&2 
        echo "dataset = ${ff} model = ${mod}" 1>&2 
        for sea in 0 1 ; do
          for pha in 0 1 2 ; do
            echo "pha = ${pha} seasonal = ${sea}" 1>&2
            cat rslt/wp-size-${ff}-int.txt \
              | predict-wp-size.gawk \
                  -f lib_date_time.gawk \
                  -f lib_functions.gawk \
                  -f model_params_${mod}.gawk \
                  -v phase=${pha} \
                  -v seasonal=${sea} \
              > rslt/wp-size-${ff}-prd-p${pha}-s${sea}-m${mod}.txt

            for logscale in 1 0 ; do
              plot-predicted-growth.sh \
                  ${pha} \
                  ${logscale} \
                  rslt/wp-size-${ff}-prd-p${pha}-s${sea}-m${mod}.txt \
                > rslt/wp-size-${ff}-prd-p${pha}-s${sea}-m${mod}-dz-y${logscale}.eps
              plot-predicted-growth-error.sh \
                  ${pha} \
                  ${logscale} \
                  rslt/wp-size-${ff}-prd-p${pha}-s${sea}-m${mod}.txt \
                > rslt/wp-size-${ff}-prd-p${pha}-s${sea}-m${mod}-de.eps
              plot-predicted-size.sh \
                  ${pha} \
                  ${logscale} \
                  rslt/wp-size-${ff}-prd-p${pha}-s${sea}-m${mod}.txt \
                > rslt/wp-size-${ff}-prd-p${pha}-s${sea}-m${mod}-sz-y${logscale}.eps
              plot-predicted-size-error.sh \
                  ${pha} \
                  ${logscale} \
                  rslt/wp-size-${ff}-prd-p${pha}-s${sea}-m${mod}.txt \
                > rslt/wp-size-${ff}-prd-p${pha}-s${sea}-m${mod}-se.eps
            done
          done
        done
      done
    done
fi

step=$(( ${step} + 1 ))
if [[ ( ${ini} -le ${step} ) && ( ${fin} -ge ${step} ) ]]; then
echo "=============================================================" 1>&2 
echo "Step ${step} - PLOTTING OMEGA AND SIGMA" 1>&2 
#

  plot-omega.sh > rslt/omega.eps
  plot-sigma.sh > rslt/sigma.eps
  
fi
