#!/bin/ksh

#------------------------------------------------------------------
# smoke_regional.sh
# Creation of regional graphics from the HYSPLIT Smoke Forecast
#------------------------------------------------------------------
# Last Revised: 29 Mar 2007 (RRD) - parallel version        
#------------------------------------------------------------------

set -x

# . /meso/save/wx20mg/bluesky/scripts/exsmoke_envir.sh

echo "Starting regional graphics: ${DATA}"

# concsfc - output file for ground-level concentrations
# concpbl - output file for column integrated concentrations

  DIRLIST="1 2 3 4 5 6 7 8 9 10 11 12" 
  concsfc="bin_sfc.1hr"
  concpbl="bin_pbl.1hr"

#------------------------------------------------------------------
# graphic subgrid domains
#------------------------------------------------------------------

   grid[1]="37.0:-116.0";  regn[1]="calif";   radius[1]="1200"
   grid[2]="45.0:-92.0" ;  regn[2]="ncent";   radius[2]="1200"
   grid[3]="43.0:-74.0" ;  regn[3]="neast";   radius[3]="1400"
   grid[4]="45.0:-116.0";  regn[4]="nwest";   radius[4]="1400"
   grid[5]="33.0:-94.0" ;  regn[5]="scent";   radius[5]="1200"
   grid[6]="32.0:-84.0" ;  regn[6]="seast";   radius[6]="1600"
   grid[7]="35.0:-107.0";  regn[7]="swest";   radius[7]="1200"
   grid[8]="22.0:-102.0";  regn[8]="mexico";  radius[8]="1400"
   grid[9]="15.0:-90.0" ;  regn[9]="centam";  radius[9]="1400"
  grid[10]="65.0:-150.0"; regn[10]="alaska"; radius[10]="1400"
  grid[11]="52.0:-112.0"; regn[11]="westca"; radius[11]="2200"
  grid[12]="58.0:-80.0 "; regn[12]="eastca"; radius[12]="2400"
  grid[13]="37.0:-109.0"; regn[13]="test";   radius[13]="1200"

#------------------------------------------------------------------
# REGIONAL MAPS ...
# Graphics are created as Postscript files representing 3h average 
# air concentrations.  In the current configuration one file per
# 3-h is created which can then be put together for the animation.  
#------------------------------------------------------------------

  for dd in ${DIRLIST}; do
    name=${regn[$dd]}

#   create working directories for results
    if [ ! -d ${DATA}/${name} ];then mkdir ${DATA}/${name}; fi
    mkdir ${COMOUT}/${name}
    cd ${DATA}/${name}
    if [ ! -d arc ];then mkdir arc; fi
    if [ ! -d for ];then mkdir for; fi
    mkdir ${COMOUT}/${name}/arc
    mkdir ${COMOUT}/${name}/for

#   convert binary concentration file to postscript
    cp ${FIXsmoke}/smoke_labels.cfg LABELS.CFG
    if [ -f concplot0001.ps ];then rm concplot00??.ps; fi  
#
    export pgm=hysplit_concplot
    ${EXECsmoke}/hysplit_concplot -i${DATA}/${concsfc} -f1 -a2 -c4 -z90 \
       -h${grid[$dd]} -g0:${radius[$dd]} -x1.0E+09  -n-3 \
       -j${FIXhysplt}/hysplit_arlmap -v100.0+20.0+5.0+1.0       >> $pgmout 2>errfile

    echo "Finished Regional Postscript graphics: `date`"  
    rm -f LABELS.CFG

#   clean output directories of any old gif images
    if [ -f arc/smoke01.gif ];then rm arc/smoke??.gif; fi
    if [ -f for/smoke01.gif ];then rm for/smoke??.gif; fi
    if [ -f arc/icon.gif ];then rm arc/icon.gif; fi
    if [ -f for/icon.gif ];then rm for/icon.gif; fi

#   Use Imagemagick to convert from Postscript to GIF for web page  
    if [ -f concplot0001.ps ];then   

       let frame=1
       fnum=`printf %2.2d $frame`

       while  [ -f concplot00${fnum}.ps ]; do
           
           if [ $frame -le 8 ];then
#             archive frames
              fout=$fnum
              dout="arc"
           else
#             forecast frames
              let temp=$frame-8
              fout=`printf %2.2d $temp`
              dout="for"
           fi

#          create GIF images from Postscript file
           convert -crop 0x0 -density 96 concplot00${fnum}.ps ${dout}/smoke${fout}.gif
           cp ${dout}/smoke${fout}.gif ${COMOUT}/${name}/${dout}/.
           export err=$? ; err_chk

#          create small image for initial time period 
           if [ "$fout" = "01" ];then
              convert -crop 0x0 -density 36 concplot00${fnum}.ps ${dout}/icon.gif
              cp ${dout}/icon.gif ${COMOUT}/${name}/${dout}/.
           fi

#          convert text generate file to shapefile
           if [ -f GIS_00100_ps_${fnum}.txt ];then 
           echo "`wc -c GIS_00100_ps_${fnum}.txt`" | read flen tmp
           if [ $flen -gt 4 ];then

#             ${EXECUTILsmoke}/ascii2shp ${dout}/shape${fout} polygons \
#                   <GIS_00100_ps_${fnum}.txt 

              export pgm=ascii2shp

              ${EXECUTILsmoke}/ascii2shp -d ${dout}/shape${fnum} polygons \
                    <GIS_00100_ps_${fnum}.txt >> $pgmout 2>errfile

              rm -f GIS_00100_ps_${fnum}.txt 
              rm -f gistmp_ps.txt 
           fi
           fi
           let frame=$frame+1
           fnum=`printf %2.2d $frame`
       done
       echo "Finished GIF file conversion: `date`"   
    fi

#------------------------------------------------------------------
# Create tar archives and animation
#------------------------------------------------------------------

    cd ${DATA}/${name}
    if [ -f concplot0001.ps ];then 
       tar -cf files_ps.tar concplot00??.ps   
       #rm -f concplot00??.ps 

       for dir in arc for; do
           cd ${DATA}/${name}/${dir}

#          test for shape files to archive
           ls shape??.??? >filelist.txt
           echo "`wc -c filelist.txt`" | read flen tmp
                                                                                  
           if [ $flen -gt 0 ];then
#             archive and cleanup

              tar -cf files_gis.tar -L filelist.txt   
              rm -f shape??.???
              rm -f filelist.txt
           fi

#          create animation loop 
           if [ -f loop.gif ];then rm loop.gif; fi
           if [ -f smoke01.gif ];then 
              convert -loop 100 -delay 50 smoke??.gif loop.gif 
              cp loop.gif ${COMOUT}/${name}/${dir}/.
              tar -cf files_gif.tar smoke??.gif    
              cp files_gif.tar ${COMOUT}/${name}/${dir}/. 
           fi
       done
       echo "Finished animation for directory: ${name}"  
    else
       echo "No GIF graphics for ${name}"       
    fi

  done
  echo "Regional graphics completed: `date`"   

exit
