#!/bin/ksh

#------------------------------------------------------------------
# smoke_national.sh
# National Graphics for the HYSPLIT Wildfire Smoke Forecast         
#------------------------------------------------------------------
# Last Revised: 29 Mar 2007 (RRD) - parallel execution
#------------------------------------------------------------------

set -x

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

echo "Starting national graphics: ${DATA}"

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

  concsfc="bin_sfc.1hr"
  concpbl="bin_pbl.1hr"

#------------------------------------------------------------------
# NATIONAL MAP ... 
#------------------------------------------------------------------


# national shapefile map for analysis/forecast archive 
  cp ${FIXsmoke}/smoke_labels.cfg LABELS.CFG
  if [ -f concplot.ps ];then rm concplot.ps; fi  
  export pgm=hysplit_concplot

  ${EXECsmoke}/hysplit_concplot -i${DATA}/${concpbl} -f1 -a2 -c4  \
     -l32 -x1.0E+09 -m3 -h45.0:-115.0 -g0:4000             \
     -j${FIXhysplt}/hysplit_arlmap -v100.0+20.0+5.0+1.0      >> $pgmout 2>errfile

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

# national results stay in upper-level directory
  if [ -f concplot0025.ps ];then   
#    crop white space - this is not required, but looks better in browser
#    create national map icon at initialization time
     convert -crop 0x0 -density 84 concplot0025.ps icon84.gif 

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

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

# ARL revised shapefile conversion that supports real numbers in dbf file
# Contains additional command line options for integer or real numbers
            export pgm=ascii2shp

            ${EXECUTILsmoke}/ascii2shp -d shape${fnum} polygons \
                  <GIS_05000_ps_${fnum}.txt 

#           create GIF images from Postscript file
            convert -crop 0x0 -density 96 concplot00${fnum}.ps smoke${fnum}.gif 

            rm -f GIS_05000_ps_${fnum}.txt 
            rm -f gistmp_ps.txt 
         fi
         fi

         let frame=$frame+1
         fnum=`printf %2.2d $frame`
     done
     rm -f concplot00??.ps
     cd $DATA/bluesky/working
     tar -cf $DATA/files_fires.tar NOAA????_????????.OUT
     cd $DATA
     tar -cf files_gen.tar GIS_?????_ps_??.???   
     rm -f GIS_05000_ps_??.???

     tar -cf files_gis.tar shape??.???        
     rm -f shape??.???

     tar -cf files_gif.tar smoke??.gif   
     rm -f smoke??.gif

     echo "Finished National Shapefiles: `date`"  
  fi

exit
