#!/bin/sh
#
# p3insituproc.sh -- Process P-3 insitu data for EPIC.
#
# Corrections:
# 1. A corrected groundspeed, incorporating the low pass filtered
#    gps groundspeed and the high pass filtered ins groundspeed is
#    substituted for the pure ins groundspeed in the wind calculation.
# 2. The air velocity relative to the aircraft is scaled by a true
#    airspeed correction factor.
# 3. Special correction for mission 2 (010905i) in which gps dropped
#    out for part of the flight.
# 4. The temperature is reduced to match the C-130 temperature by
#    the formula cta = ta - dt where
#    dt = dt1 + (dt2 - dt1)*(ps - p1)/(p2 - p1).  The values of
#    constants are given below.
#
if test $# != 2
then
    echo "Usage: p3insituproc.sh mission date"
else
    
    mission=$1
    date=$2
    echo "p3insituproc.sh $mission $date"
    infile=${date}I.std
    outfile=${date}i.insitu
    slen=250
    tasfactor=1.014
    dt1=0.3
    dt2=0.5
    p1=980
    p2=620

    cd ${mission}*/p3/insitu

    rertape < $infile | hrdair | cdfcat tptime 0 100000 40000 | \
    cdfmath "ps $p1 - $p2 $p1 - / $dt2 $dt1 - * $dt1 + dt =" | \
    cdfmath "ta dt - cta =" | \
    cdfentropy -d ps cta td entropy satmr | \
    cdfentropy -d ps cta cta entropys satmr | \
    rafthetae -d ps cta td thetae satmr | \
    rafthetae -d ps cta cta thetaes satmr | \
    cdfmath "ts $tasfactor * cts =" > barf

    if test "$date" = "010905"
    then
	echo "special fix for 010905i"
	p3m02fix.sh barf barf2
	mv barf2 barf
    fi

    cdfextr -r gugndl gvgndl gsxh gsyh < barf | \
    cdffilter -l $slen gugndl gugnd tptime | \
    cdffilter -l $slen gvgndl gvgnd tptime | \
    cdffilter -h $slen gsxh gsx tptime | \
    cdffilter -h $slen gsyh gsy tptime | \
    cdfmath "gugndl gsxh + cgsx =" | \
    cdfmath "gvgndl gsyh + cgsy =" | \
    cdfmath "wx gsx - $tasfactor * ax =" | \
    cdfmath "wy gsy - $tasfactor * ay =" | \
    cdfmath "ax cgsx + cwx =" | \
    cdfmath "ay cgsy + cwy =" > $outfile

    rm barf
    uniput -r ${date}i.nc < $outfile
fi
