#! /bin/bash # invoke gnuplot to plot a barrier contained in a barriers.out file # specify barrier by number 1-n # invocation: allbarriers [Barr_num Doy-1 Doy-2 ... Doy-# file-1] [Barr_num Doy-1 Doy-2 ... Doy-# file-2] ... [Barr_num Doy-1 Doy-2 ... Doy-# file-#] # column 1-3 daysim, doy, yr # 14 columns per barrier # for each barrier # list of columns # name # beg_flg # ! 0 - number of days temperature is above base trigger temperature since previous known state date # ! 1 - number of days temperature is below base trigger temperature since previous known state date # ! 2 - accumulation of Growing degree days (GDD) since previous known state date # ! 3 - accumulation of Cooling degree days (CDD) since previous known state date # ! 4 - accumulation of rainfall depth (above minimum depth) since previous known state date # ! 5 - accumulation of days with rainfall below minimum depth (rainfall above minimum depth # ! resets accumulation) since previous known state date # ! 6 - accumulation of humidity levels above base humidity since previous known state date # ! 7 - accumulation of humidity levels below base humidity since previous known state date # beg_accum ! total accumulation of beg_flg specified quantity since given day of year # beg_thresh ! accumulation threshold value for each of the methods above # end_flg ! multilevel flag defining the method for determining completion of leaf emergence/drop # ! 0 - days to complete transition are specified # ! 1 - Growing Degree Days (GDD) to full transition are specified # ! 2 - Cooling Degree Days (CDD) to full transition are specified # end_accum ! total accumlation of end_flg specified quantity since beg_threshold exceeded # end_thresh ! Accumulation threshold value where full transition occurs # np ! number of points in barrier_params and polyline point array # for each point # delta_x ! distance along barrier # amzbr ! Height in meters. # amxbrw ! Width in meters. # ampbr ! Porosity as fraction of the outline of each barrier, silhouette area # set up columns titles for barriers.out file mincol=2 maxcol=4 coltitle[2]=`echo "2) Height (m)"` coltitle[3]=`echo "3) Width (m)"` coltitle[4]=`echo "4) Porosity"` # short column titles shortcoltitle[2]=`echo "Ht"` shortcoltitle[3]=`echo "Wd"` shortcoltitle[4]=`echo "Por"` # set up column groupings tgroups=3 colgroupnum[2]=1 colgroupnum[3]=2 colgroupnum[4]=3 # group y axis labels grouplabel[1]=`echo "Height (m)"` grouplabel[2]=`echo "Width (m)"` grouplabel[3]=`echo "Fraction (0-1)"` # create column number, file name pairs # screen column numbers for valid values # determine number of index blocks in each series # 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: allbarriers [Barr_num Doy-1 Doy-2 ... Doy-# file-1] [Barr_num Doy-1 Doy-2 ... Doy-# file-2] ... [Barr_num Doy-1 Doy-2 ... Doy-# file-#]' exit fi # parse command arguments checking for numbers and when not a number checking for a valid file name # initialize total number of valid file names parsed (must assume one when barrier numbers preceed file names) tfiles=1 # number of files / barriers (1 barrier per file name) numbefile=0 # 0 - count of numbers that preceed file name (first number is barrier number, remaining numbers are day of year values) tdoys=0 # count of number of day of year values declare -A bardoy # Associative array (multiple dimensions) indexes are actually sring values so must use ${} for index assignment and reference 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 number, check for file name if [ -e "${cmdarg[ncmd]}" ] then # valid file name, check for preceeding barriers if [[ numbefile -gt 0 ]] then # a barrier number is listed in front of this name # add file name to file names list filename[tfiles]="${cmdarg[ncmd]}" # Increment file number counter let tfiles+=1 fi # find number of index blocks in this file linecount=`wc -l < ${filename[tfiles-1]}` lineswithnumbers=`grep -c '\.' < ${filename[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" # reset number counts numbefile=0 tdoys=0 else # not a valid file name, show error message and exit echo \'"${cmdarg[ncmd]}"\' is not valid file name. exit fi else # increment count of numbers before file name let numbefile+=1 if [ ${numbefile} -eq 1 ] then # add into barrier number array barnum[tfiles]="${cmdarg[ncmd]}" else # increment day of year count let tdoys+=1 # assign count to array day of year count numbardoy[tfiles]="${tdoys}" # add into day of year array bardoy[${tfiles},${tdoys}]="${cmdarg[ncmd]}" #echo "Add: ${cmdarg[ncmd]} to bardoy[${tfiles},${tdoys}]" #$echo "Added: ${bardoy[${tfiles},${tdoys}]}" fi fi done # set tfiles to the correct count let tfiles-=1 # screen for terminating valid file name if [ ! -e "${filename[tfiles]}" ] then echo "No terminating file name." echo 'invocation: allbarriers [Barr_num Doy-1 Doy-2 ... Doy-# file-1] [Barr_num Doy-1 Doy-2 ... Doy-# file-2] ... [Barr_num Doy-1 Doy-2 ... Doy-# file-#]' exit fi # find number of barriers in each file # save posbarrier and pospoints for each barrier in each file declare -A posbarrier # Associative array (multiple dimensions) indexes are actually sring values so must use ${} for index assignment and reference declare -A pospoints # Associative array (multiple dimensions) indexes are actually sring values so must use ${} for index assignment and reference for numfile in $( seq 1 ${tfiles} ) do tbarriers[numfile]=0 # extract first data line in file line=$(cat ${filename[numfile]} | linex 4 ) # find number of data fields in line tfields=$( echo ${line} | dm N ) # set position in line of first barrier posbarr=4 let remfields=tfields-posbarr+1 while [ ${remfields} -ge 16 ] do # single barrier must have at least 16 data fields, or more depending on number of points in barrier # Column 11 has noumber of point specfied along barrier let pospnts=${posbarr}+7 nbarpoints=$( echo ${line} | dm x${pospnts} ) let nbarfields=8+nbarpoints*4 if [ ${remfields} -ge ${nbarfields} ] then let tbarriers[numfile]+=1 # column position of barrier name posbarrier[${tbarriers[numfile]},${numfile}]=${posbarr} #echo "NUMFILE BARR POSBARR STORVAL: ${tbarriers[numfile]} ${numfile} ${posbarr} ${posbarrier[${tbarriers[numfile]},${numfile}]}" # column position of number barrier points pospoints[${tbarriers[numfile]},${numfile}]=${pospnts} fi let posbarr=posbarr+nbarfields let remfields=remfields-nbarfields done done tbars=${tfiles} # check for valid barrier numbers in inputs for nbar in $( seq 1 ${tbars} ) do if [ ${barnum[nbar]} -gt ${tbarriers[nbar]} ] then echo "Barrier number ${barnum[nbar]} greater than ${tbarriers[nbar]} barriers in ${filename[nbar]}" echo 'invocation: allbarriers [Barr_num Doy-1 Doy-2 ... Doy-# file-1] [Barr_num Doy-1 Doy-2 ... Doy-# file-2] ... [Barr_num Doy-1 Doy-2 ... Doy-# file-#]' exit else # add barrier beginning column in file (barrier name column) posbarnam[nbar]=${posbarrier[${barnum[nbar]},${nbar}]} # add points beginning column in file (barrier number of points column posnpnts[nbar]=${pospoints[${barnum[nbar]},${nbar}]} fi done # check for valid day of year values for each barrier for nbar in $( seq 1 ${tbars} ) do for numdoy in $( seq 1 ${numbardoy[nbar]} ) do if [ ${bardoy[${nbar},${numdoy}]} -lt 1 ] || [ ${bardoy[${nbar},${numdoy}]} -gt 365 ] then echo "Day of year value out of range: ${bardoy[${nbar},${numdoy}]}" exit fi done done # create file for plotting points along the barrier for each day of year specified. for nbar in $( seq 1 ${tbars} ) do # extract first data line in file line=$(cat ${filename[nbar]} | linex 4 ) barname=$( echo ${line} | dm s${posbarnam[nbar]} ) nbarpnts=$( echo ${line} | dm x${posnpnts[nbar]} ) # for each point in the barrier, create data line # cat file and extract each day of year line # construct string to extract all day of year lines for numdoy in $( seq 1 ${numbardoy[nbar]} ) do if [ ${numdoy} -eq 1 ] then extstr=$( echo "\$2==\"${bardoy[${nbar},${numdoy}]}\"" ) else extstr=$( echo "${extstr} || \$2==\"${bardoy[${nbar},${numdoy}]}\"" ) fi done > tempbar cntdoy=0 while read line do # abut columns for each day of year in a year let cntdoy+=1 for npnt in $(seq 1 ${nbarpnts} ) do let "posx=(${posnpnts[nbar]}+1)+4*(${npnt}-1)" let posh=${posx}+1 let posw=${posx}+2 let posp=${posx}+3 if [ ${cntdoy} -eq 1 ] then lineout[npnt]=$( echo "${line}" | dm x${posx} x${posh} x${posw} x${posp} ) else tmpline=$( echo "${line}" | dm x${posh} x${posw} x${posp} ) lineout[npnt]=$( echo "${lineout[npnt]} ${tmpline}" ) fi done # all days for this year added, print out and reset if [ ${cntdoy} -eq ${numbardoy[nbar]} ] then for npnt in $(seq 1 ${nbarpnts} ) do echo "${lineout[npnt]}" >> tempbar done tdoy="${cntdoy}" cntdoy=0 echo "" >> tempbar echo "" >> tempbar fi done <<< "$( cat ${filename[nbar]} | awk "${extstr}" )" let "tcols=${tdoy}*3" cntdoy=1 for ncol in $( seq 1 ${tcols} ) do let colnum[ncol]=${ncol}+1 let "refcolnum[ncol]=(${ncol}-1)%3+2" # set column group number colgroup[ncol]=${colgroupnum[${refcolnum[ncol]}]} coldoy[ncol]=${bardoy[${nbar},${cntdoy}]} #echo "COL: ${ncol} ${colnum[ncol]} ${refcolnum[ncol]} ${colgroup[ncol]} ${coldoy[ncol]}" if [ ${colgroup[ncol]} -eq 3 ] then let cntdoy+=1 fi done # set up graph to column group correspondence # sort group list (result is zero reference array) colgroupsort=(`echo ${colgroup[@]} | transpose | sort | transpose`) #echo "COLGRP: ${colgroup[@]}"# 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 tempbar: " # find selected column maximum value for this file filemaxval=$( dm "if x${colnum[num]}>0 then x${colnum[num]} else SKIP" < tempbar | stats max ) echo "${filemaxval}" # special case for displaying friction velocity and threshold # do not scale according to the soil wetness threshold increase if [ ${colnum[num]} -ne 47 ] && [ ${colnum[num]} -ne 49 ] || [ ${tcols} -eq 1 ] then # check for maximum for this column group for all files if [ ${tcols} -ne 1 ] && [ ${colnum[num]} -ge 45 ] && [ ${colnum[num]} -le 49 ] then # expand maximum a little since not scaled to all maximum filemaxval=$( echo "scale=4; ${filemaxval}*1.2" | bc -l ) fi groupmaxval[${colgroup[num]}]=`echo "$filemaxval ${groupmaxval[${colgroup[num]}]}" | stats max` echo "groupmaxval(${colgroup[num]}) now equal to ${groupmaxval[${colgroup[num]}]}" fi 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. # create text for graph title, using file names graphtitle="File: ${filename[nbar]} Barrier: ${barname}" # 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 # rather than loop within gnuplot, create specific plot lines # for each year so these parameters can be added to the # title of the graph. # write single instance lines top plot file #--- start pdf --- echo "set terminal pdf font 'Helvetica,4'" > temp.plt echo "set output 'barrier_profile_${nbar}.pdf'" >> temp.plt view_pause=0 #--- end pdf --- #--- start postscript --- #echo "set terminal postscript 'Helvetica,6'" > temp.plt #echo "set output 'allplot.eps'" >> temp.plt #view_pause=0 #--- end postscript --- ##--- 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 legend location echo "set key left top" >> temp.plt echo "set xrange [0:366]" >> temp.plt #echo "set xrange [0:100]" >> temp.plt echo "unset border" >> temp.plt for year in $(seq $numindex) do echo "Simulation Year = $year" # # --- emf output files --- # echo "set output '${tempdir}/${year}.emf'" >> temp.plt # zero out special group counter sumgroupcnt=0 # 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 yrange [0:2]" >> 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]="'tempbar' index ${yearm} using 1:${colnum[num]} with lines title 'DOY: ${coldoy[num]}) ${shortcoltitle[${refcolnum[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 rm tempbar done exit