#!/bin/bash
#
# run the triangulation interpolation for wind station
# input consists of a latitude, longitude pair

# compile and copy command lines
# ~/weps/windgen/src/interpolate> lf95 -ap -dbl -in -nfix interpolate.f95 trarcs.for mirroring.f95 stripack.for polysizing.f95 readpoly.f95 vrout.for
# ~/weps/windgen/src/interpolate> lf95 -ap -dbl -in -nfix interp_wdb.f95 stripack.for
# ~/weps/windgen/src/interpolate> cp interpolate.exe interp_wdb.exe ~/weps/weps.install/bin/Windows/

# command line for running from weps.install
# bin/Windows/interpolate.exe -v -d -f db/windgen/wind_gen_his_upper_US_NRCS.idx -p db/gis/WINDGEN_20100115/WINDGEN_20100115.pol -lat 49.0 -lon -97.19 -n

# you can also create a voronoi diagram with no polygon specified (such as cligen)
# bin/Windows/interpolate.exe -c -n -v -f db/cligen/upd_US_cligen_stations.idx -lat 49.0 -lon -97.19
# but the cligen index file needs to be translated into a windgen format.

# this and other settings make this for running in the weps.install directory
exedir="./bin/Windows"
windindex="./db/windgen/wind_gen_his_upper_US_NRCS.idx"
polfile="./db/gis/WINDGEN_20102907/WINDGEN_20102907.pol"
winddata="./db/windgen/wind_gen_his_upper_US.wdb"

if [ ! -d "${exedir}" ]
then
    echo "${exedir} does not exist. Check run location"
    exit
fi

if [ ! -f "${windindex}" ]
then
    echo "${windindex} does not exist. Check run location"
    exit
fi

if [ ! -f "${polfile}" ]
then
    echo "${polfile} does not exist. Check run location"
    exit
fi

if [ ! -f "${winddata}" ]
then
    echo "${winddata} does not exist. Check run location"
    exit
fi

# Triangulate wind points and locate test points in triangulation
# return station numbers and interpolation coefficients
# -d running in debug mode, creating plot files
${exedir}/interpolate -d -f ${windindex} -p ${polfile} -lat ${1} -lon ${2} -o weights.txt | grep -v "#" > testinterp

# extract station numbers to be used with wind_wdb4
cat testinterp | dm 'if x1 > 0 then x1 else SKIP' > col1
cat testinterp | dm 'if x3 > 0 then x3 else SKIP' > col3
cat testinterp | dm 'if x5 > 0 then x5 else SKIP' > col5
cat col1 col3 col5 | sort | uniq > stations

# extract stations into individual wdb files
while read stanum
do
    #echo "stanum: ${stanum}"
    echo ${exedir}/wind_wdb4 -f ${winddata} -o ${stanum} -s "${stanum}"
    ${exedir}/wind_wdb4 -f ${winddata} -o ${stanum} -s "${stanum}"
done < stations

# create interpolated wdb file from triangular interpolation
while read record
do
    # interpolate
    ${exedir}/interp_wdb temp.wdb ${record}
done < testinterp

# run windgen with interpolate file
${exedir}/wind_gen4 -f temp.wdb -s 999999 -o wind_gen.win

while read stanum
do
    rm ${stanum}
done < stations

rm testinterp col1 col3 col5 stations
rm temp.wdb
rm weights.txt

# paths to files that need to be added to weps.install
# ${exedir}/interpolate.exe
# ${exedir}/wind_wdb4.exe
# ${exedir}/interp_wdb.exe
# ./db/wind_gen/windgen_east.pol

# these files are already there
# ${exedir}/wind_gen4
# ./db/wind_gen/wind_gen_his_upper_US.wdb
# ./db/wind_gen/wind_gen_his_upper_US_\(NRCS\).idx