#! /bin/bash # invoke gnuplot to compare crop output from WEPS generated runs. # invocation: allshoot [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=5 maxcol=19 calcol=19 coltitle[5]=`echo "5) Shoot Heat Unit Index (end of shoot grow) (C)"` coltitle[6]=`echo "6) Storage root mass sum (total in all layers) (kg/m^2)"` coltitle[7]=`echo "7) Fibrous root mass sum (total in all layers) (kg/m^2)"` coltitle[8]=`echo "8) Mass required from root mass for one shoot (mg/shoot)"` coltitle[9]=`echo "9) Total shoot mass at end of shoot growth period (mg/shoot)"` coltitle[10]=`echo "10) Total root mass at end of shoot growth period (mg/shoot)"` coltitle[11]=`echo "11) Mass increment added to roots for the present day (mg/shoot)"` coltitle[12]=`echo "12) Mass increment added to shoot for the present day (mg/shoot)"` coltitle[13]=`echo "13) Mass increment removed from storage roots for the present day (mg/shoot)"` coltitle[14]=`echo "14) Total stem mass at end of shoot growth period (mg/shoot)"` coltitle[15]=`echo "15) Total stem area at end of shoot growth period (m^2/shoot)"` coltitle[16]=`echo "16) Total shoot length at end of shoot growth period (m)"` coltitle[17]=`echo "17) Length of actively growing shoot from root biomass (m)"` coltitle[18]=`echo "18) Crop shoot mass grown from root storage (kg/m^2)"` coltitle[19]=`echo "19) Number of crop stems per unit area (#/m^2)"` # short column titles shortcoltitle[5]=`echo "5) HUI"` shortcoltitle[6]=`echo "6) St Root"` shortcoltitle[7]=`echo "7) F Root"` shortcoltitle[8]=`echo "8) Mass req"` shortcoltitle[9]=`echo "9) T Shoot"` shortcoltitle[10]=`echo "10) T Root"` shortcoltitle[11]=`echo "11) R minc"` shortcoltitle[12]=`echo "12) Sh minc"` shortcoltitle[13]=`echo "13) St minc"` shortcoltitle[14]=`echo "14) T Stem"` shortcoltitle[15]=`echo "15) T Stem Area"` shortcoltitle[16]=`echo "16) T Shoot Len"` shortcoltitle[17]=`echo "17) Now Len"` shortcoltitle[18]=`echo "18) Crop shoot mass"` shortcoltitle[19]=`echo "19) # stems"` # set up column groupings tgroups=6 colgroupnum[5]=1 colgroupnum[6]=2 colgroupnum[7]=2 colgroupnum[8]=3 colgroupnum[9]=3 colgroupnum[10]=3 colgroupnum[11]=3 colgroupnum[12]=3 colgroupnum[13]=3 colgroupnum[14]=3 colgroupnum[15]=4 colgroupnum[16]=5 colgroupnum[17]=5 colgroupnum[18]=2 colgroupnum[19]=6 # group y axis labels grouplabel[1]=`echo "C"` grouplabel[2]=`echo "Mass (kg/m^2)"` grouplabel[3]=`echo "mg/shoot"` grouplabel[4]=`echo "m^2/shoot"` grouplabel[5]=`echo "meters"` grouplabel[6]=`echo "#/m^2)"` # 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: allshoot [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: allshoot [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: allshoot [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: allshoot [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 the number of unique crops in each file numcrops[1]=`cut -c 315-400 < ${ufilename[1]} | uniq | grep '[:print:]' | sort -u | wc -l` #echo "${ufilename[1]} has ${numcrops[1]} unique crops" for num in $( seq 2 ${tfiles} ) do numcrops[num]=`cut -c 315-400 < ${ufilename[num]} | uniq | grep '[:print:]' | sort -u | wc -l` #echo "${ufilename[num]} has ${numcrops[num]} unique crop(s)" if [[ numcrops[num] -ne numcrops[1] ]] then # for multiple file plots, the number of unique crops must be equal echo "Different number of unique crops for two files" echo "Check and try again" fi done # Find maximum values for each unique crop year # all files have the same number of crop years for idx in $( seq 1 ${numcrops[1]} ) do for num in $( seq 1 ${tfiles} ) do # extract unique crop names in order cropname=`cut -c 315-400 < ${ufilename[num]} | uniq | grep '[:print:]' | linex ${idx} | sed 's/ $//'` echo "Crop name: ${cropname} :end" # Find maximum column value for x axis "day after planting" column 4 for just the lines containing the crop name cropmaxdap[idx]=`grep "${cropname}" < ${ufilename[num]} | dm "if x4>0 then x4 else SKIP" | stats max` echo "cropmaxdap: ${cropmaxdap[idx]}" # set maximum across all files afcropmaxdap[idx]=`echo "${cropmaxdap[idx]} ${afcropmaxdap[idx]}" | stats max` echo "afcropmaxdap: ${afcropmaxdap[idx]}" # # find maximum for each stack for just the lines containing the crop name # for idy in $(seq 1 $tcols) # do # if [[ colfilenum[idy] -eq ${num} ]] # then # # add column number to dm string # if [[ colfileser[idy] -eq 1 ]] # then # summedcol="x${colnum[idy]}" # else # summedcol="${summedcol}+x${colnum[idy]}" # fi # fi # done # stackmaxval[idx]=`grep "${cropname}" < ${ufilename[num]} | dm "if x1>0 then ${summedcol} else SKIP" | stats max` # echo "Stack ${summedcol} maximum is ${stackmaxval[idx]}" # # set maximum across all files # afstackmaxval[idx]=`echo "${stackmaxval[idx]} ${afstackmaxval[idx]}" | stats max` # echo "afstackmaxval: ${afstackmaxval[idx]}" done done ## find maximum column value for x axis "day after planting" column 4 for all input files #for num in $( seq 1 ${tfiles} ) #do # # Check for maximum days after planting for one file # filemaxdap=`dm "if x4>0 then x4 else SKIP" < ${filename[num]} | stats max` # # Check for maximum days after planting for all file # allmaxdap=`echo "$filemaxdap $allmaxdap" | stats max` # echo "maximum day after planting now equal to $allmaxdap" #done # 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 'allshoot.pdf'" >> 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 legend location echo "set key left top" >> temp.plt ## set so that all graphs use same total days #echo "set xrange [0:${allmaxdap}]" >> temp.plt for year in $(seq $numindex) do echo "crop year = $year" # # --- emf output files --- # echo "set output '${tempdir}/${year}.emf'" >> temp.plt # set the crop that is growing cropnumyr=$( echo "(${year} - 1) % ${numcrops[1]}" | bc ) let cropnumyr++ # echo "${cropnumyr}" # set so that graphs use same total days and maximum values for that crop echo "set xrange [0:${afcropmaxdap[cropnumyr]}]" >> temp.plt # create graph title line echo "set title 'Crop 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 'Days After Planting'" >> 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 4:${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 # old stuff # grab column number and shift command arguments colnum=$1 shift if [[ ${colnum} = "" ]] then echo 'invocation: allshoot [Col num] [file-1 file-2 ... file-#]' echo 'Valid Column Numbers are:' for num in $( seq $mincol $calcol ) do echo ${coltitle[num]} done exit elif [[ ${colnum} -lt $mincol ]] then echo 'invocation: allshoot [Col num] [file-1 file-2 ... file-#]' echo 'Valid Column Numbers are:' for num in $( seq $mincol $calcol ) do echo ${coltitle[num]} done exit elif [[ ${colnum} -gt $calcol ]] then echo 'invocation: allshoot [Col num] [file-1 file-2 ... file-#]' echo 'Valid Column Numbers are:' for num in $( seq $mincol $calcol ) do echo ${coltitle[num]} done exit elif [[ ${colnum} -le $maxcol ]] then # special case calculated from columns let nfile=0 for nf in $* do # create filename array let nfile=nfile+1 filename[nfile]="$nf" # find number of index blocks linecount=`wc -l < ${nf}` lineswithnumbers=`grep -c '\.' < ${nf}` let fileindex=(linecount-lineswithnumbers-2)/2 #echo "fileindex = ${fileindex}" # check to use minimum of all files numindex=`echo "$fileindex $numindex" | stats min` #echo "numindex now equal to $numindex" echo " ${nf} checking column maximum value" # find selected column maximum value for this file filemaxval=`dm "if x${colnum}>0 then x${colnum} else SKIP" < ${nf} | stats max` #echo "filemaxval now equal to $filemaxval" # check for maximum of all files allmaxval=`echo "$filemaxval $allmaxval" | stats max` echo "allmaxval now equal to $allmaxval" done # write single instance lines top plot file #--- start pdf --- view_pause=0 echo "set terminal pdf" > temp.plt echo "set output 'allshoot.pdf'" >> temp.plt #--- end pdf --- #--- start screen --- #view_pause=-1 #echo "# sends to screen defaults" > temp.plt #--- end screen --- echo "set yrange [0:${allmaxval}]" >> temp.plt # echo "set yrange [0:0.2]" >> temp.plt for ind in $(seq $numindex) do echo "index = $ind" # create graph title line echo "set title 'Crop Year ${ind}, ${coltitle[colnum]}'" >> temp.plt #create plot lines for each file for num in $(seq $nfile) do let indm=ind-1 plotline[num]="'${filename[num]}' index ${indm} using 4:$colnum with lines title '${filename[num]}', \\" done # add plot command to first plotline plotline[1]=`echo "${plotline[1]}" | sed -e 's/^.*/plot &/'` # strip last plotline of 3 extra characters plotline[nfile]=`echo "${plotline[nfile]}" | sed -e 's/...$//'` # write out plot lines to file for num in $(seq $nfile) do echo ${plotline[num]} >> temp.plt done echo "pause ${view_pause}" >> temp.plt done gnuplot temp.plt exit fi # determine number of index blocks in each series by looping through # command arguments 1 to # let nfile=0 for nf in $* do # create filename array let nfile=nfile+1 filename[nfile]="$nf" # find number of index blocks linecount=`wc -l < ${nf}` lineswithnumbers=`grep -c '\.' < ${nf}` let fileindex=(linecount-lineswithnumbers-2)/2 #echo "fileindex = ${fileindex}" # check to use minimum of all files numindex=`echo "$fileindex $numindex" | stats min` #echo "numindex now equal to $numindex" echo " ${nf} checking column maximum value" # find selected column maximum value for this file filemaxval=`dm "if x${colnum}>0 then x${colnum} else SKIP" < ${nf} | stats max` #echo "filemaxval now equal to $filemaxval" # check for maximum of all files allmaxval=`echo "$filemaxval $allmaxval" | stats max` echo "allmaxval now equal to $allmaxval" echo " ${nf} checking number of days maximum value" # find selected column maximum value for this file filemaxdays=`dm "if x4 then x4 else SKIP" < ${nf} | stats max` #echo "filemaxdays now equal to $filemaxdays" # check for maximum of all files allmaxdays=`echo "$filemaxdays $allmaxdays" | stats max` echo "allmaxdays now equal to $allmaxdays" 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}]} # write single instance lines top plot file echo "set xrange [0:${allmaxdays}]" > temp.plt echo "set yrange [0:${allmaxval}]" >> temp.plt for ind in $(seq $numindex) do echo "index = $ind" # create graph title line echo "set title '${titlebase}, Crop Year ${ind}'" >> temp.plt #create plot lines for each file for num in $(seq $nfile) do let indm=ind-1 plotline[num]="'${filename[num]}' index ${indm} using 4:$colnum with lines title '${filename[num]}', \\" done # add plot command to first plotline plotline[1]=`echo "${plotline[1]}" | sed -e 's/^.*/plot &/'` # strip last plotline of 3 extra characters plotline[nfile]=`echo "${plotline[nfile]}" | sed -e 's/...$//'` # write out plot lines to file for num in $(seq $nfile) do echo ${plotline[num]} >> temp.plt done echo "pause -1" >> temp.plt done gnuplot temp.plt #rm temp.plt