#!/bin/sh
#
# p3dropproc.sh -- Process dropsondes from .sonde to .interp stages.
# Note that mission and flight command line arguments should not have
# ``m'' preprended or ``i'' appended.
#
# Convention on dropsonde quality:
# quality = wind_quality + 2*thermo_quality + 4*low_launch_flag
# wind_quality = 0 if good, 1 if bad
# thermo_quality = 0 if good, 1 if bad
#
if test $# != 3
then
    echo "Usage: p3dropproc.sh mission flight dropnum"
else
    mission=$1
    flight=$2
    dropnum=$3
    infile=drop${dropnum}.sonde
    outfile=drop${dropnum}.interp
    tmpfile=${outfile}.tmp
    insitufile=../insitu/${flight}i.insitu
    
    rafdropinit noaa $insitufile < $infile | \
    cdffill pres | cdfinterp pres 1050 -5 131 | \
    cdfmath 'lon0 lon = lat0 lat =' | \
    cdfmath "$mission mission =" | cdfmath "$dropnum dropnum =" | \
    cdfextr tdry dp rh u_wind v_wind dz thetae thetaes satmr \
	mr entropy entropys year month day hour min sec lon lat \
	mission dropnum | \
    cdfextr -s pres | \
    cdfmath 'year 2000 + year =' | \
    cdfjday year month day jday 2001 1 0 | \
    cdfmath 'sec 60 / min + 60 / hour + 24 / jday + jday =' > $tmpfile

    cdfmath 'pres vpres =' < $tmpfile | \
    cdfplot "0,0,7.5,10,a/150,350,x/1050,400,y/9,14,t/entropy (J\/kg\/K),lx/pres (mb),ly/entropy,vpres,1,p/entropys,vpres,5,p/m$mission\, P-3 drop $dropnum,170,450,l/7.5,0,15,10,a/-15,15,x/1050,400,y/7,14,t/U\, V (m\/s),lx/pres (mb),ly/u_wind,vpres,1,p/v_wind,vpres,5,p/2,u_wind,vpres,u_wind,vpres,0,0,3,s/4,v_wind,vpres,v_wind,vpres,0,0,3,s" && $PG

    read -p "quality = " quality

    cdfmerge ${outfile}.tmp '' | \
    cdfmath "$quality quality =" > $outfile
    rm ${outfile}.tmp
fi
