#! /bin/bash
# Last edited on 2014-02-05 01:55:51 by stolfilocal

# Plots the volumes inside and outside China.
# 
# Arguments: {TITLE} {FNAME} {OPREF}
# 
# Where 
#   
#   {TITLE}   is the plot's title.
#   {FNAME}   is a data file with daily volumes. 
#   {OPREF}   is the prefix for the output plots. 
#   

title="$1"; shift # Plot title.
fname="$1"; shift 
opref="$1"; shift 

show="SHOW"

tmp="/tmp/$$"

color=( '#0022ff' '#ff0000' '#008800' '#8800dd' '#dd4400' '#0066ff' )

export GDFONTPATH=.:..

# Scatterplot China x NonChina
gnuplot <<EOF
set term png size 1200,1200 font "courbd,24"
set output "${tmp}-full.png"
set title "${title}"

set xlabel "In China" 
set xrange [-0.5:180.5]

set ylabel "Outside China" 
set yrange [-0.5:180.5]

set xzeroaxis lt 1 lw 2.5 lc rgb '#eecc99'
set yzeroaxis lt 1 lw 2.5 lc rgb '#eecc99'

set ytics 20.0
set mytics 4
set grid ytics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mytics

set xtics 20.0
set mxtics 4
set grid xtics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mxtics

set key right bottom

plot \
  (0.37*x) with lines title 'NC = 37% C' lw 4.0 lc rgb '#ff8855', \
  "${fname}" using 7:5 notitle with linespoints pt 7 ps 2.0 lt 1 lw 1.0 lc rgb '#0033ff'
    
quit
EOF

plotfile="${opref}_cnc.png"
convert ${tmp}-full.png -resize '50%' ${plotfile}

if [[ "/${show}" == "/SHOW" ]]; then
  display ${plotfile}
fi

# Time plot day x (China,NonChina)
gnuplot <<EOF
set term png size 1800,800 font "courbd,16"
set output "${tmp}-full.png"
set title "${title}"

set yrange [-0.5:180.5]
set xrange [:]


set xzeroaxis lt 1 lw 2.5 lc rgb '#eecc99'
set yzeroaxis lt 1 lw 2.5 lc rgb '#eecc99'

set xtics 1.0
set grid xtics lt 1 lw 3 lc rgb '#ffddaa'

set ytics 20.0
set mytics 4
set grid ytics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mytics

set key right top

pct(i,j) = 100.0*(column(i)/(column(i)+column(j)+0.0))

plot \
  "${fname}" using 0:5:xtic(3)  title 'Outside China' with linespoints pt 7 ps 1.5 lt 1 lw 1.0 lc rgb '#0033ff', \
  ""         using 0:7          title 'In China'      with linespoints pt 7 ps 1.5 lt 1 lw 1.0 lc rgb '#ff3300', \
  ""         using 0:(pct(7,5)) title '% C/(C+NC)'    with linespoints pt 7 ps 1.5 lt 1 lw 1.0 lc rgb '#338800'
    
quit
EOF

plotfile="${opref}_tim.png"
convert ${tmp}-full.png -resize '50%' ${plotfile}

if [[ "/${show}" == "/SHOW" ]]; then
  display ${plotfile}
fi


rm -fv ${tmp}{-*,}.*
    
        
