#! /bin/bash # invoke gnuplot to plot a barrier contained in a barriers.out file # specify barrier by number 1-n # invocation: allbarriers file-1 file-2 ... file-# # column 1-3 daysim, doy, yr # 14 columns per barrier # for each barrier # list of columns # nn - sinday # nn - doy # nn - year # 1 - name # 2 - np ! number of points in barrier_params and polyline point array # for each point # 3 - delta_x ! distance along barrier # 4 - amzbr ! Height in meters. # 5 - amxbrw ! Width in meters. # 6 - ampbr ! Porosity as fraction of the outline of each barrier, silhouette area # tran_days ! count of days since transition began # seas_flg # ! 0 - interpolation between all points (no more data on this line) # ! 1 - transition at each time point (no more data on this line) # ! 2 - Seasonal transitions (data below follows) # trig_logic # ! 0 - OR logic, phase transition begins when any threshold is reached # ! 1 - AND logic, phase transition begins when all thresholds are reached # nthresh ! number of threshold methods # 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 # ! 8 - day length beyond beg_thresh value # 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 # set up columns titles for barriers.out file mincol=2 maxcol=6 coltitle[2]=`echo "2) Height (m)"` coltitle[3]=`echo "3) Width (m)"` coltitle[4]=`echo "4) Porosity"` # these also, if they exist coltitle[5]=`echo "5) Begin Trigger Accumulation"` coltitle[6]=`echo "6) End Trigger Accumulation"` # short column titles shortcoltitle[2]=`echo "Ht"` shortcoltitle[3]=`echo "Wd"` shortcoltitle[4]=`echo "Por"` # these also, if they exist shortcoltitle[5]=`echo "BTr"` shortcoltitle[6]=`echo "ETr"` # set up column groupings tgroups=3 colgroupnum[2]=1 colgroupnum[3]=2 colgroupnum[4]=3 # these also, if they exist colgroupnum[5]=4 colgroupnum[6]=4 # group y axis labels grouplabel[1]=`echo "Height (m)"` grouplabel[2]=`echo "Width (m)"` grouplabel[3]=`echo "Fraction (0-1)"` # this also, if they exist grouplabel[4]=`echo "Accumulation"` # 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 file-1 file-2 ... 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 for ncmd in $( seq 1 $ncmdarg ) do # check for file name if [ -e "${cmdarg[ncmd]}" ] then # add file name to unique file names list ufilename[tfiles]="${cmdarg[ncmd]}" # Increment file number counter let tfiles+=1 # 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 done # set tfiles to the correct count let tfiles-=1 # 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 ${ufilename[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 10 ] do # single barrier must have at least 10 data fields, or more depending on number of points in barrier # number of points specified along the barrier let pospnts=${posbarr}+1 tpoints[numfile]=$( echo ${line} | dm x${pospnts} ) # find field position for season type (transition method) flag let posseason=posbarr+2+${tpoints[numfile]}*4 # find season type (transition method) flag seasontype=$( echo ${line} | dm x${posseason} ) # find how many fields are past seasontype let remfields=remfields-3-${tpoints[numfile]}*4 if [ ${seasontype} -eq 2 ] then # seasontype is 2 # find number of transition types let posnthresh=posseason+2 nthresh=$( echo ${line} | dm x${posnthresh} ) let remfields=remfields-2-nthresh*6 # a zero answer here indicates first line is good else let remfields=remfields-2 fi # check for wrong number of fields on line if [ ${remfields} -ne 0 ] then echo " ${remfields} Incorrect number of values on first barrier line. Wrong file version, corrupt file??" exit fi done done # set columns to be plotted for each barrier file tcol=0 for tfile in $( seq 1 ${tfiles} ) do # extract first data line in file line=$(cat ${ufilename[tfile]} | linex 4 ) barname=$( echo ${line} | dm s${posbarr} ) # insert barrier height, width, porosity columns into colnum array for ncol in $(seq 2 4 ) do let tcols+=1 let colnum[tcols]=${pospnts}+${ncol} refcolnum[tcols]="${ncol}" # set column group number colgroup[tcols]=${colgroupnum[${refcolnum[tcols]}]} # set column file number colfilenum[tcols]=${tfile} colbarname[tcols]="${barname}" #echo "TCOLS COLNUM COLGRP COLFNUM BARFILE: ${tcols} ${colnum[tcols]} ${colgroup[tcols]} ${colfilenum[tcols]} ${ufilename[tfile]}" done # let posaccflg=${posbarnam[nbar]}+1 # begaccflg=$( echo ${line} | dm x${posaccflg} ) # if [ ${begaccflg} -ge 0 ] # then # let tcols+=1 # let colnum[tcols]=${posaccflg}+1 # refcolnum[tcols]="5" # # set column group number # colgroup[tcols]=${colgroupnum[${refcolnum[tcols]}]} # # set column file number # colfilenum[tcols]=${tfile} # colbarname[tcols]="${barname}" # fi # let posaccflg=${posbarnam[nbar]}+4 # endaccflg=$( echo ${line} | dm x${posaccflg} ) # if [ ${endaccflg} -ge 0 ] # then # let tcols+=1 # let colnum[tcols]=${posaccflg}+1 # refcolnum[tcols]="6" # # set column group number # colgroup[tcols]=${colgroupnum[${refcolnum[tcols]}]} # # set column file number # colfilenum[tcols]=${tfile} # colbarname[tcols]="${barname}" # 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 ${ufilename[colfilenum[num]]} : " filemaxval=`dm "if x${colnum[num]}>0 then x${colnum[num]} else SKIP" < ${ufilename[colfilenum[num]]} | 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="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 # 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 'allbarriers.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 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]="'${ufilename[colfilenum[num]]}' index ${yearm} using 2:${colnum[num]} with lines title '${colfilenum[num]}) ${shortcoltitle[${refcolnum[num]}]} ${colbarname[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