#! /bin/bash # invoke gnuplot to compare hydrology output from WEPS generated runs. # invocation: allhydro [Col_num-1 Col_num-2 ... Colnum-# file-1] [Col_num-1 Col_num-2 ... Colnum-# file-2] ... [Col_num-1 Col_num-2 ... Colnum-# file-#] # set up column and title correspondence mincol=4 maxcol=30 coltitle[4]=`echo "4) Potential Evapotranspiration (mm)"` coltitle[5]=`echo "5) Potential Surface Evaporation (mm)"` coltitle[6]=`echo "6) Potential Plant Transpiration (mm)"` coltitle[7]=`echo "7) Actual Surface Evaporation (mm)"` coltitle[8]=`echo "8) Actual Plant Transpiration (mm)"` coltitle[9]=`echo "9) Deep Percolation (mm)"` coltitle[10]=`echo "10) Irrigation Depth (mm)"` coltitle[11]=`echo "11) Rainfall Depth (mm)"` coltitle[12]=`echo "12) Total Water Depth (mm)"` coltitle[13]=`echo "13) Runoff Depth (mm)"` coltitle[14]=`echo "14) Infiltration Depth (mm)"` coltitle[15]=`echo "15) Prev Day Soil Water Content (mm)"` coltitle[16]=`echo "16) Soil Water Content (mm)"` coltitle[17]=`echo "17) Snow Depth (mm)"` coltitle[18]=`echo "18) Snow Water Content (mm)"` coltitle[19]=`echo "19) Accuracy Check (mm)"` coltitle[20]=`echo "20) soil surface dryness ratio"` coltitle[21]=`echo "21) Daily average air temperature (C)"` coltitle[22]=`echo "22) Vapor Transmissivity (mm/sqrt(day))"` coltitle[23]=`echo "23) Evaporation Limit (mm)"` coltitle[24]=`echo "24) Standing Biomass Evaporation Reduction Factor"` coltitle[25]=`echo "25) Flat Biomass Evaporation Reduction Factor"` coltitle[26]=`echo "26) Total Biomass Evaporation Reduction Factor"` coltitle[27]=`echo "27) 1st Crop rooting depth (meters)"` coltitle[28]=`echo "28) 1st Root Zone Available Water Content (mm)"` coltitle[29]=`echo "29) 1st Root Zone Available Water Capacity (mm)"` coltitle[30]=`echo "30) 1st Plant Water Stress Factor"` coltitle[31]=`echo "31) 2nd Crop rooting depth (meters)"` coltitle[32]=`echo "32) 2nd Root Zone Available Water Content (mm)"` coltitle[33]=`echo "33) 2nd Root Zone Available Water Capacity (mm)"` coltitle[34]=`echo "34) 2nd Plant Water Stress Factor"` # short column titles shortcoltitle[4]=`echo "PET"` shortcoltitle[5]=`echo "P Surf E"` shortcoltitle[6]=`echo "P Trans"` shortcoltitle[7]=`echo "A Surf E"` shortcoltitle[8]=`echo "A Trans"` shortcoltitle[9]=`echo "Perc"` shortcoltitle[10]=`echo "Irr"` shortcoltitle[11]=`echo "Rain"` shortcoltitle[12]=`echo "Tot W"` shortcoltitle[13]=`echo "Runoff"` shortcoltitle[14]=`echo "Infilt"` shortcoltitle[15]=`echo "Prev SWC"` shortcoltitle[16]=`echo "Now SWC"` shortcoltitle[17]=`echo "Snow D"` shortcoltitle[18]=`echo "Snow W"` shortcoltitle[19]=`echo "Error"` shortcoltitle[20]=`echo "Dry Rat"` shortcoltitle[21]=`echo "Air T"` shortcoltitle[22]=`echo "V Trans"` shortcoltitle[23]=`echo "E lim"` shortcoltitle[24]=`echo "St E Red"` shortcoltitle[25]=`echo "Fl E Red"` shortcoltitle[26]=`echo "Tot E Red"` shortcoltitle[27]=`echo "1-Root D"` shortcoltitle[28]=`echo "1-RZ AWC"` shortcoltitle[29]=`echo "1-RZ Cap"` shortcoltitle[30]=`echo "1-Stress"` shortcoltitle[31]=`echo "2-Root D"` shortcoltitle[32]=`echo "2-RZ AWC"` shortcoltitle[33]=`echo "2-RZ Cap"` shortcoltitle[34]=`echo "2-Stress"` # set up column groupings tgroups=6 colgroupnum[4]=1 colgroupnum[5]=1 colgroupnum[6]=1 colgroupnum[7]=1 colgroupnum[8]=1 colgroupnum[9]=1 colgroupnum[10]=1 colgroupnum[11]=1 colgroupnum[12]=1 colgroupnum[13]=1 colgroupnum[14]=1 colgroupnum[15]=1 colgroupnum[16]=1 colgroupnum[17]=1 colgroupnum[18]=1 colgroupnum[19]=1 colgroupnum[20]=4 colgroupnum[21]=5 colgroupnum[22]=6 colgroupnum[23]=1 colgroupnum[24]=3 colgroupnum[25]=3 colgroupnum[26]=3 colgroupnum[27]=2 colgroupnum[28]=1 colgroupnum[29]=1 colgroupnum[30]=3 colgroupnum[31]=2 colgroupnum[32]=1 colgroupnum[33]=1 colgroupnum[34]=3 # group y axis labels grouplabel[1]=`echo "Depth of Water (mm)"` grouplabel[2]=`echo "Root Depth (meters)"` grouplabel[3]=`echo "Factor (0-1)"` grouplabel[4]=`echo "Dryness Ratio"` grouplabel[5]=`echo "Temperature (C)"` grouplabel[6]=`echo "Vapor Transmissivity (mm/sqrt(day))"` # grab and count command arguments let ncmdarg=0 for ncmd in $* do let ncmdarg=ncmdarg+1 cmdarg[ncmdarg]="$ncmd" # echo "Command argument $ncmdarg = ${cmdarg[ncmdarg]}" done # check command argument count, zero means none if [[ ncmdarg -eq 0 ]] then echo 'invocation: allhydro [Col_num-1 Col_num-2 ... Colnum-# file-1] [Col_num-1 Col_num-2 ... Colnum-# file-2] ... [Col_num-1 Col_num-2 ... Colnum-# file-#]' echo 'Valid Column Numbers are:' for num in $( seq $mincol $maxcol ) do echo ${coltitle[num]} done exit fi # parse command arguments checking for numbers and when not a number checking for a valid file name # initialize total number of columns parsed tcols=0 # initialize total number of valid file names parsed (must assume one when column numbers preceed file names) tfiles=1 # initialize column count for first file name tcolfile[tfiles]=0 for ncmd in $( seq 1 $ncmdarg ) do # test if it is an integer number echo "${cmdarg[ncmd]}" | grep [^0-9] > /dev/null 2>&1 if [ "$?" -eq "0" ] then # not a column number, check for file name if [ -e "${cmdarg[ncmd]}" ] then # valid file name, check for preceeding columns if [[ tcolfile[tfiles] -gt 0 ]] then # columns are listed in front of this name # back fill column file name pairings for ncol in $( seq 1 ${tcolfile[tfiles]} ) do # write file name let index=(tcols-tcolfile[tfiles])+ncol filename[index]="${cmdarg[ncmd]}" done # add file name to unique file names list ufilename[tfiles]="${cmdarg[ncmd]}" # Increment file number counter let tfiles+=1 fi # find number of index blocks in this file linecount=`wc -l < ${ufilename[tfiles-1]}` lineswithnumbers=`grep -c '\.' < ${ufilename[tfiles-1]}` let fileindex=(linecount-lineswithnumbers-1)/2 #echo "fileindex = ${fileindex}" # check to use minimum of all files numindex=`echo "$fileindex $numindex" | stats min` #echo "numindex now equal to $numindex" else # not a valid file name, show error message and exit echo \'"${cmdarg[ncmd]}"\' is not valid file name. exit fi else # it is an integer number, check for valid column number if [[ ${cmdarg[ncmd]} -lt $mincol ]] then echo \'"${cmdarg[ncmd]}"\' is a Bad Column Number echo 'invocation: allhydro [Col_num-1 Col_num-2 ... Colnum-# file-1] [Col_num-1 Col_num-2 ... Colnum-# file-2] ... [Col_num-1 Col_num-2 ... Colnum-# file-#]' echo 'Valid Column Numbers are:' for num in $( seq $mincol $maxcol ) do echo ${coltitle[num]} done exit elif [[ ${cmdarg[ncmd]} -gt $maxcol ]] then echo \'"${cmdarg[ncmd]}"\' is a Bad Column Number echo 'invocation: allhydro [Col_num-1 Col_num-2 ... Colnum-# file-1] [Col_num-1 Col_num-2 ... Colnum-# file-2] ... [Col_num-1 Col_num-2 ... Colnum-# file-#]' echo 'Valid Column Numbers are:' for num in $( seq $mincol $maxcol ) do echo ${coltitle[num]} done exit fi # valid column number, add into colum number array and increment indexes let tcols+=1 let tcolfile[tfiles]+=1 colnum[tcols]="${cmdarg[ncmd]}" # set column group number colgroup[tcols]=${colgroupnum[${colnum[tcols]}]} # set column file number colfilenum[tcols]=${tfiles} fi done # screen for terminating valid file name if [ ! -e "${filename[tcols]}" ] then echo "No terminating file name." echo 'invocation: allhydro [Col_num-1 Col_num-2 ... Colnum-# file-1] [Col_num-1 Col_num-2 ... Colnum-# file-2] ... [Col_num-1 Col_num-2 ... Colnum-# file-#]' fi # set tfiles to the correct count let tfiles-=1 # echo the resulting arrays of column numbers and file names # echo "The input command line results in the following columns and file names:" # for num in $( seq 1 ${tcols} ) # do # echo "${colnum[num]} ${colgroup[num]} ${filename[num]}" # done # exit # set up graph to column group correspondence # sort group list (result is zero reference array) colgroupsort=(`echo ${colgroup[@]} | transpose | sort | transpose`) # total number of column groups to be plotted tgroups=1 # list of groups represented groupnum[1]=${colgroupsort[0]} let tindex=tcols-1 for num in $( seq 1 ${tindex} ) do let index=num-1 if [[ ${colgroupsort[num]} -ne ${colgroupsort[index]} ]] then let tgroups+=1 groupnum[tgroups]=${colgroupsort[num]} fi done # show groups # echo "unique group numbers" # echo ${groupnum[@]} # exit # find maximum column value for each column and assign that to the maximum for the group for num in $( seq 1 $tcols ) do echo -n " Find maximum value: column ${colnum[num]} in ${filename[num]} : " # find selected column maximum value for this file filemaxval=`dm "if x${colnum[num]}>0 then x${colnum[num]} else SKIP" < ${filename[num]} | stats max` echo "${filemaxval}" # check for maximum for this column group for all files groupmaxval[${colgroup[num]}]=`echo "$filemaxval ${groupmaxval[${colgroup[num]}]}" | stats max` echo "groupmaxval(${colgroup[num]}) now equal to ${groupmaxval[${colgroup[num]}]}" done # rather than loop within gnuplot, create specific plot lines # for each year so these parameters can be added to the # title of the graph. # associate graph title name with column number specified titlebase=${coltitle[${colnum[1]}]} # create text for graph title, using file names graphtitle="Files: " for num in $( seq 1 ${tfiles} ) do graphtitle=`echo "${graphtitle} ${num}) ${ufilename[num]} "` done # Set up plot sizes for multiple plots (ie. groups) if [[ $tgroups -gt 1 ]] then # plot sizes for each group psizebase=$( echo "scale=4; 0.9/${tgroups}" | bc -l ) for num in $( seq 1 $tgroups ) do # create coordinates # y size for plot (x is 1.0) if [[ $num -eq 1 ]] then # increase the size of the first plot psize[num]=$( echo "scale=4; ${psizebase}+0.05" | bc -l ) # place plots from the top down in the order specified porigin[num]=$( echo "scale=4; 0.05+($tgroups-$num)*${psizebase}" | bc -l ) elif [[ $num -eq $tgroups ]] then # increase the size of the last plot psize[num]=$( echo "scale=4; ${psizebase}+0.05" | bc -l ) porigin[num]=0 else psize[num]=${psizebase} porigin[num]=$( echo "scale=4; 0.05+($tgroups-$num)*${psizebase}" | bc -l ) fi done fi # write single instance lines top plot file #--- start pdf --- echo "set terminal pdf" > temp.plt echo "set output 'allhydro.pdf'" >> temp.plt echo "set xrange [0:366]" >> temp.plt view_pause=0 #--- end pdf --- #--- start emf --- # create temporary directory for files if needed #tempdir="htemp" #mkdir -p ${tempdir} ## clear any files #rm ${tempdir}/*.emf #echo "set terminal emf monochrome dashed" > temp.plt #view_pause=0 #echo "set line style 1 lt 1 lw 1" >> temp.plt #echo "set line style 2 lt 2 lw 1" >> temp.plt #echo "set line style 3 lt 3 lw 1" >> temp.plt #echo "set line style 4 lt 4 lw 1" >> temp.plt #echo "set line style 5 lt 5 lw 1" >> temp.plt #echo "set line style 6 lt 1 lw 3" >> temp.plt #echo "set line style 7 lt 2 lw 3" >> temp.plt #echo "set line style 8 lt 3 lw 3" >> temp.plt #echo "set line style 9 lt 4 lw 3" >> temp.plt #echo "set line style 10 lt 5 lw 3" >> temp.plt #echo "set line style 11 lt 1 lw 5" >> temp.plt #--- end emf --- #--- start screen --- #view_pause=-1 #echo "" > temp.plt #--- end screen --- # set x axis range echo "set xrange [0:366]" >> temp.plt #echo "set xrange [90:150]" >> temp.plt # set legend location echo "set key left top" >> temp.plt for year in $(seq $numindex) do echo "Simulation Year = $year" # # --- emf output files --- # echo "set output '${tempdir}/${year}.emf'" >> temp.plt # create graph title line echo "set title 'Simulation Yr ${year}, ${graphtitle}'" >> temp.plt # set up single or multiple plot windows if [[ $tgroups -gt 1 ]] then echo "multiple plots" echo "set size 1.0, 1.0" >> temp.plt echo "set origin 0.0, 0.0" >> temp.plt echo "set multiplot" >> temp.plt fi # create plot commands for each group for idx in $( seq 1 $tgroups ) do # only print title once, above first graph if [[ $idx -gt 1 ]] then echo "set title ''" >> temp.plt fi # remove x axis from all but last graph if [[ $idx -lt $tgroups ]] then # remove x axis labels echo "set format x ''" >> temp.plt echo "set xlabel ''" >> temp.plt else # remove x axis labels echo "set format x '% g'" >> temp.plt echo "set xlabel 'Day of Year'" >> temp.plt fi # individual sizes for multiplots if [[ $tgroups -gt 1 ]] then # set plot size echo "set size 1.0, ${psize[idx]}" >> temp.plt echo "set origin 0.0, ${porigin[idx]}" >> temp.plt fi # set axis scales, labels echo "set yrange [0:${groupmaxval[${groupnum[idx]}]}]" >> temp.plt echo "set ylabel '${grouplabel[${groupnum[idx]}]}'" >> temp.plt # create plot lines for each column and file in this group index=0 for num in $(seq 1 $tcols) do if [[ colgroup[num] -eq groupnum[idx] ]] then let index+=1 let yearm=year-1 plotline[index]="'${filename[num]}' index ${yearm} using 2:${colnum[num]} with lines title '${colfilenum[num]}) ${shortcoltitle[${colnum[num]}]}', \\" fi done # add plot command to first plotline plotline[1]=`echo "${plotline[1]}" | sed -e 's/^.*/plot &/'` # strip last plotline of 3 extra characters plotline[index]=`echo "${plotline[index]}" | sed -e 's/...$//'` # write out plot lines to file for num in $(seq 1 $index) do echo ${plotline[num]} >> temp.plt done done # check multiple plots if [[ $tgroups -gt 1 ]] then # this command necessary to display all plots on some terminals echo "unset multiplot" >> temp.plt fi echo "pause ${view_pause}" >> temp.plt done gnuplot temp.plt #rm temp.plt exit