#!/bin/sh
#
# c130insitu-final.sh -- process C130 data from netcdf file for EPIC2001
# This is the final version.  Take the heated Rosemount as standard.
#
if test $# != 2
then
    echo "Usage: c130insitu-final.sh mission flight"
else
    mission=$1
    flight=$2
    echo "c130-insitu-final $mission $flight"
    dir=`echo ${mission}*/c130/insitu`
    triggersmooth2=3
    B=-0.00162
    R0=-20

    cd $dir
    uniget ${flight}.nc | \
    cdfextr -rs sps1 vector32 vector64 vector41 vector31 base_time | \
    cdfextr year month day hour minute second pitch roll thdg ssrd akrd altx hgm232 latc lonc tashc uic vic wic psxc mrla1 dpla1 dpxc atx atwh oat theta thetav plwc6_rwo plwcc plwcc1 plwcf_lpc plwc2dc_rwi2 plwc2dp_lwo2 conc6_rwo concf_lpc dbar6_rwo dbarf_lpc xglwc irbc irtc rstb1 rstt vew vns gvew gvns ui vi | \
    cdfmath 'hour 3600 * minute 60 * second + + time =' | \
    cdfcat time 0 150000 50000 | \
    cdfthresh 'tashc > 90' 'atx > -15' | \
    cdfmath 'plwcf_lpc plwc6_rwo + 0.01 - rawtrigger =' | \
    cdffilter -l $triggersmooth2 trigger2 rawtrigger time | \
    cdfmath "roll $R0 - rstb1 atx - * $B * oat + coat1 =" | \
    rafsegsub atx coat1 trigger2 coat | \
    cdfentropy -q psxc coat mrla1 entropy satmr plwcf_lpc | \
    cdfentropy -d psxc coat coat satentropy satmr | \
    rafthetae -q psxc coat mrla1 thetae satmr plwcf_lpc | \
    rafthetae -d psxc coat coat thetaes satmr > ${flight}.insitu
fi
