Search code examples
awkgrepgnuplotxmgrace

How to grep some labels from a xmgrace file and then use them in gnuplot script as tick labels and arrow positions


From my computationa tool I am getting two plots one in xmgrace and other in some dat file format. The top few lines in xmgrace format (say it is a file grace.agr) is having tick lable and their positions which I want to use in my gnuplot script. My grace.dat lookes like (total ticklabels may be more here I am mentioning only few)

 @ page size 595, 842
 @ view 0.120000, 0.150000, 0.900000, 1.280000
 @ default linewidth 2.0
 @ xaxis  label char size 1.5
 @ xaxis  ticklabel char size 1.25
 @ yaxis  label char size 1.5
 @ yaxis  ticklabel char size 1.25
 @ xaxis  tick major grid on
 @ xaxis  tick spec type both
 @ xaxis  tick spec           8
@ xaxis  tick major   0, 0.00000
 @ xaxis  ticklabel            0 ,"\xG"
@ xaxis  tick major   1, 0.67643
 @ xaxis  ticklabel            1 ,"M           "
@ xaxis  tick major   2, 1.06696
 @ xaxis  ticklabel            2 ,"K           "
@ xaxis  tick major   3, 1.84803
 @ xaxis  ticklabel            3 ,"\xG"
@ xaxis  tick major   4, 1.98549
 @ xaxis  ticklabel            4 ,"A           "
@ xaxis  tick major   5, 2.66192
 @ xaxis  ticklabel            5 ,"L           "
@ xaxis  tick major   6, 3.05245
 @ xaxis  ticklabel            6 ,"H           "
@ xaxis  tick major   7, 3.83352
 @ xaxis  ticklabel            7 ,"A           "
@ with g0

With the help of grep, awk and some other tricks as mentioned in code section I managed to append below data in a file (say the file name is SETTICKS.dat)

cat SETTICKS.dat gives me this:
set xtics ( "\xG" 0.00000, "M" 0.67643, "K" 1.06696, "\xG" 1.84803, "A" 1.98549, "L" 2.66192, "H" 3.05245, "A" 3.83352, )

grep 'bandindex:   1' -B10000 grace.agr | grep xaxis | grep 'type both' -A 1000 | grep 'xaxis  tick spec           ' -A1000 | grep '0, 0.00000' -A1000 | awk '{print $5}' | grep -v '^[0-9]' | awk '{p
rint substr($1,2); }'| awk -F\|  '{ print substr($1,1,4)}' > xlable-1.txt
# "
grep 'bandindex:   1' -B10000 grace.agr | grep xaxis | grep 'type both' -A 1000 | grep 'xaxis  tick spec           ' -A1000 | grep '0, 0.00000' -A1000 | awk '{print $5}' | grep -v '^[0-9]' | awk '{p
rint substr($1,2); }'| awk -F\|  '{ print substr($1,1,1)}' >  xlable-2.txt

#"X"
paste  xlable-* | awk '$1 =$1$2 {print}' | awk '{print$1}' > xlable-3.txt

#Tick_position
grep 'bandindex:   1' -B10000 grace.agr | grep xaxis | grep 'type both' -A 1000 | grep 'xaxis  tick spec           ' -A1000 | grep '0, 0.00000' -A1000 | awk '{print $6}' | awk '!/"/' | awk 'NF > 0'  
> xlable-4.txt

#comma
grep 'bandindex:   1' -B10000 grace.agr | grep xaxis | grep 'type both' -A 1000 | grep 'xaxis  tick spec           ' -A1000 | grep '0, 0.00000' -A1000 | awk '{print $5}' | grep -v '^[0-9]' | awk -F\
|  '{ print substr($1,1,1)}' > xlable-5.txt

paste xlable-4.txt xlable-5.txt | awk '$1 =$1$2 {print}' | awk '{print$1}' > xlable-6.txt
#cat xlable-6.txt

paste xlable-3.txt xlable-6.txt | awk '{print $1, $2}' | awk 'BEGIN { ORS = " " } { print }' >  xlable-7.txt

echo  "set xtics (" > x-tick-1.txt
echo  ")" > x-tick-2.txt


paste x-tick-1.txt xlable-7.txt x-tick-2.txt > SETTICKS.dat

I want to use ticks and their positions from above mentioned grace.agr file at two places in the gnuscript as below

(1)
set xtics ("\xG" 0.00000, "M" 0.67643, "K" 1.06696, "\xG" 1.84803 ,"A" 1.98549, "L" 2.66192, "H" 3.05245 ,"A" 3.83352,)  and then 

(2) 

set arrow from 0.00000,Y11 to 0.00000,Y12 nohead
set arrow from 0.67643,Y11 to 0.67643,Y12 nohead
set arrow from 1.06696,Y11 to 1.06696,Y12 nohead
set arrow from 1.84803,Y11 to 1.84803,Y12 nohead
set arrow from 1.98549,Y11 to 1.98549,Y12 nohead
set arrow from 2.66192,Y11 to 2.66192,Y12 nohead
set arrow from 3.05245,Y11 to 3.05245,Y12 nohead
set arrow from 3.83352,Y11 to 3.83352,Y12 nohead

where Y11 and Y12 are -10 and 10 respectively.


Solution

  • I propose you a solution just using awk.

    For the first part:

    awk 'BEGIN {mystring="";FS=", *"}/tick major +[0-9]+/{myfield1=$2;getline;gsub(" ","");mystring=mystring$2" "myfield1", ";}END{print "set xtics ("mystring") and then ";}' grace.dat
    

    you'll get this output:

    set xtics ("\xG" 0.00000, "M" 0.67643, "K" 1.06696, "\xG" 1.84803, "A" 1.98549, "L" 2.66192, "H" 3.05245, "A" 3.83352, ) and then
    

    For second part:

    awk 'BEGIN {FS=", *"}/tick major +[0-9]+/{myfield1=$2;getline;gsub(" ","");print "set arrow from "myfield1",Y11 to "myfield1",Y12 nohead"}' grace.dat
    

    you'll get this output:

    set arrow from 0.00000,Y11 to 0.00000,Y12 nohead
    set arrow from 0.67643,Y11 to 0.67643,Y12 nohead
    set arrow from 1.06696,Y11 to 1.06696,Y12 nohead
    set arrow from 1.84803,Y11 to 1.84803,Y12 nohead
    set arrow from 1.98549,Y11 to 1.98549,Y12 nohead
    set arrow from 2.66192,Y11 to 2.66192,Y12 nohead
    set arrow from 3.05245,Y11 to 3.05245,Y12 nohead
    set arrow from 3.83352,Y11 to 3.83352,Y12 nohead