| 1 |
## -*- cperl -*- |
| 2 |
## |
| 3 |
## This file is part of Athena, copyright (c) 2001-2009 Bruce Ravel |
| 4 |
## |
| 5 |
## This section of the code contains subroutines associated with |
| 6 |
## writing and reading macros |
| 7 |
|
| 8 |
|
| 9 |
sub setup_macro { |
| 10 |
my $instructions = <<EOH |
| 11 |
To begin recording a macro, click the button below. All subsequent |
| 12 |
calls to ifeffit will be recorded. When you have performed all the |
| 13 |
ifeffit operations you wish to record, press the \"Done\" button and |
| 14 |
you will be prompted for a filename in which to save the macro. |
| 15 |
|
| 16 |
Data processing chores which involve altering the data, e.g. |
| 17 |
deglitching and truncating, will not record properly as macros. |
| 18 |
EOH |
| 19 |
; |
| 20 |
|
| 21 |
$notes{macro} -> tagConfigure('inst', -wrap=>'word'); |
| 22 |
$notes{macro} -> insert('end', $instructions, "text"); |
| 23 |
|
| 24 |
my $doneline = $notecard{macro} -> Frame(qw/-relief flat -borderwidth 2/) |
| 25 |
-> pack(qw/-fill x -side bottom/); |
| 26 |
my $startline = $notecard{macro} -> Frame(qw/-relief groove -borderwidth 2/) |
| 27 |
-> pack(qw/-fill x -side bottom/); |
| 28 |
|
| 29 |
my $rectext = $startline -> Label(-text=>"Recording macro...", -justify=>'center'); |
| 30 |
my $start; |
| 31 |
$start = $startline -> Button(-text=>'Start recording', @button_list, |
| 32 |
-command=>sub{$start->packForget(); |
| 33 |
$rectext-> pack(-expand=>1, -fill=>'x', -pady=>4); |
| 34 |
($dmode & 8) or ($dmode += 8);}) |
| 35 |
-> pack(-expand=>1, -fill=>'x'); |
| 36 |
|
| 37 |
my $done = $doneline |
| 38 |
-> Button(-text=>'Done', @button_list, |
| 39 |
-command=>sub{$rectext -> packForget(); |
| 40 |
$start -> pack(-expand=>1, -fill=>'x'); |
| 41 |
($dmode & 8) and ($dmode -= 8); |
| 42 |
&save_macro;}) |
| 43 |
-> pack(-expand=>1, -fill=>'x'); |
| 44 |
}; |
| 45 |
|
| 46 |
sub save_macro { |
| 47 |
return unless @macro_buffer; |
| 48 |
local $Tk::FBox::a; |
| 49 |
local $Tk::FBox::b; |
| 50 |
my $path = $current_data_dir || Cwd::cwd; |
| 51 |
my $types = [['Ifeffit macro files', '.ifm'], |
| 52 |
['All Files', '*'],]; |
| 53 |
my $file = $top -> getSaveFile(-defaultextension=>'ifm', |
| 54 |
-filetypes=>$types, |
| 55 |
#(not $is_windows) ? |
| 56 |
# (-sortcmd=>sub{$Tk::FBox::a cmp $Tk::FBox::b}) : () , |
| 57 |
-initialdir=>$path, |
| 58 |
-initialfile=>'athena.ifm', |
| 59 |
-title => "Athena: Save Ifeffit macro"); |
| 60 |
if ($file) { |
| 61 |
my ($name, $pth, $suffix) = fileparse($file); |
| 62 |
$current_data_dir = $pth; |
| 63 |
#&push_mru($file, 0); |
| 64 |
open MAC, '>'.$file or do { |
| 65 |
Error("You cannot write macro to \"$file\"."); return |
| 66 |
}; |
| 67 |
print MAC "## Ifeffit macro file recorded using Athena $VERSION\n\n"; |
| 68 |
my $macros = write_macros(); |
| 69 |
print MAC $macros; |
| 70 |
map {print MAC $_} @macro_buffer; |
| 71 |
close MAC; |
| 72 |
@macro_buffer = (); |
| 73 |
}; |
| 74 |
}; |
| 75 |
|
| 76 |
sub load_macro { |
| 77 |
local $Tk::FBox::a; |
| 78 |
local $Tk::FBox::b; |
| 79 |
my $path = $current_data_dir || Cwd::cwd; |
| 80 |
my $types = [['Ifeffit macro files', '.ifm'], |
| 81 |
['All Files', '*'],]; |
| 82 |
my $file = $top -> getOpenFile(-filetypes=>$types, |
| 83 |
#(not $is_windows) ? |
| 84 |
# (-sortcmd=>sub{$Tk::FBox::a cmp $Tk::FBox::b}) : () , |
| 85 |
-initialdir=>$path, |
| 86 |
-title => "Athena: Load macro"); |
| 87 |
if ($file) { |
| 88 |
my ($name, $pth, $suffix) = fileparse($file); |
| 89 |
$current_data_dir = $pth; |
| 90 |
#&push_mru($file, 0); |
| 91 |
open MAC, $file or die "Could not open $file for reading macro\n"; |
| 92 |
while (<MAC>) { |
| 93 |
next if (/^\s*\#/); |
| 94 |
next if (/^\s*$/); |
| 95 |
if ($_ =~ /^\s*read_data/) { |
| 96 |
my ($file, $group); |
| 97 |
($_ =~ /file\s*=\s*([^ \t,]*)/) and ($file = $1); |
| 98 |
($_ =~ /group\s*=\s*([^ \t,]*)/) and ($group = $1); |
| 99 |
fill_skinny($list, $file, $group); |
| 100 |
} else { |
| 101 |
$groups{$current}->dispose($_, $dmode); |
| 102 |
}; |
| 103 |
}; |
| 104 |
close MAC; |
| 105 |
}; |
| 106 |
}; |
| 107 |
|
| 108 |
sub write_macros { |
| 109 |
my $string = " |
| 110 |
macro startup |
| 111 |
\"Athena startup message, used to set character size and font\" |
| 112 |
set(startup.x = range(0.1,1,0.1), startup.y = zeros(10)) |
| 113 |
newplot(startup.x, startup.y, nogrid, ymin=0, ymax=1, color=black, charsize=$config{plot}{charsize}, charfont=$config{plot}{charfont}) |
| 114 |
plot_text(0.4, 0.5, text=\"Welcome to Athena\") |
| 115 |
erase \@group startup |
| 116 |
end macro |
| 117 |
|
| 118 |
## macro for drawing markers (for e0 and the like) |
| 119 |
macro pmarker d.x d.y x style color yoffset |
| 120 |
\"plot a marker at X given D.X and D.Y and YOFFSET with STYLE, and COLOR\" |
| 121 |
set(p___y = interp(\$1, \$2, \$3) + \$6) |
| 122 |
plot_marker(\$3, p___y, \$4, color=\$5) |
| 123 |
end macro |
| 124 |
|
| 125 |
## making a step function |
| 126 |
macro step x.array shift x a.step |
| 127 |
\"Return A.STEP function centered at X with X.ARRAY as the x-axis and a SHIFT energy shift\" |
| 128 |
set(t___oss.x = \$1 + \$2, |
| 129 |
n___step = nofx(t___oss.x, \$3) - 1, |
| 130 |
n___points = npts(\$1) - n___step, |
| 131 |
t___oss.zeros = zeros(n___step), |
| 132 |
t___oss.ones = ones(n___points), |
| 133 |
\$4 = join(t___oss.zeros, t___oss.ones) ) |
| 134 |
end macro |
| 135 |
|
| 136 |
|
| 137 |
## log-ratio/phase difference macros: |
| 138 |
|
| 139 |
macro do_lograt stan unknown qmin qmax npi |
| 140 |
\"Do log-ratio/phase-difference analysis between STAN and UNKNOWN between QMIN and QMAX\" |
| 141 |
|
| 142 |
## do log-ratio fit |
| 143 |
guess(c___0 = 1, c___2 = 0, c___4 = 0) |
| 144 |
def(c___.ratio = ln(\$2.chiq_mag/\$1.chiq_mag), |
| 145 |
c___.even = c___0 - 2*c___2*\$1.q^2 + (2/3)*c___4*\$1.q^4, |
| 146 |
c___.resev = c___.ratio - c___.even) |
| 147 |
minimize(c___.resev, x=\$1.q, xmin=\$3, xmax=\$4) |
| 148 |
set(c___0 = c___0, c___2 = c___2, c___4 = c___4) |
| 149 |
|
| 150 |
## do phase difference fit |
| 151 |
guess(c___1 = 0, c___3 = 0) |
| 152 |
def(c___.diff = \$2.chiq_pha - \$1.chiq_pha, |
| 153 |
c___.odd = 2*c___1*\$1.q - (4/3) * c___3*\$1.q^3 + \$5*2*pi, |
| 154 |
c___.resod = c___.diff - c___.odd) |
| 155 |
minimize(c___.resod, x=\$1.q, xmin=\$3, xmax=\$4) |
| 156 |
set(c___1 = c___1, c___3 = c___3) |
| 157 |
end macro |
| 158 |
|
| 159 |
macro plot_lograt stan stan_lab unknown_lab qmax |
| 160 |
\"Plot log ratio and fit for STAN out to QMIN\" |
| 161 |
newplot((\$1.q)**2, c___.ratio, xmax=\$4**2, title=\"log-ratio between \$3 and \$2\", |
| 162 |
xlabel=\"k\\u2\\d (\\A\\u-2\\d)\", ylabel=\"log-ratio\", key=data) |
| 163 |
plot((\$1.q)**2, c___.even, key=fit) |
| 164 |
end macro |
| 165 |
|
| 166 |
macro plot_phdiff stan stan_lab unknown_lab qmax |
| 167 |
\"Plot phase difference and fit for STAN out to QMIN\" |
| 168 |
newplot(\$1.q, c___.diff, xmax=\$4, title=\"phase-difference between \$3 and \$2\", |
| 169 |
xlabel=\"k (\\A\\u-1\\d)\", ylabel=difference, key=data) |
| 170 |
plot(\$1.q, c___.odd, key=fit) |
| 171 |
end macro |
| 172 |
|
| 173 |
macro clean_lograt |
| 174 |
erase c___0 c___1 c___2 c___3 c___4 |
| 175 |
erase \@group c___ |
| 176 |
end macro |
| 177 |
|
| 178 |
macro fix_chik |
| 179 |
\"repair chi(k) data group that is not on a uniform k grid\" |
| 180 |
set(fix___a.k = range(0, ceil(\$1.k), 0.05)) |
| 181 |
set(fix___floor = floor(\$1.k) - 0.05) |
| 182 |
set(fix___a.kk = range(0, fix___floor, 0.05)) |
| 183 |
set(fix___n = npts(fix___a.kk)) |
| 184 |
set(fix___a.cc = zeros(fix___n)) |
| 185 |
set(fix___a.x = join(fix___a.kk, \$1.k)) |
| 186 |
set(fix___a.y = join(fix___a.cc, \$1.chi)) |
| 187 |
set(fix___a.chi = rebin(fix___a.x, fix___a.y, fix___a.k)) |
| 188 |
set(\$1.k = fix___a.k) |
| 189 |
set(\$1.chi = fix___a.chi) |
| 190 |
erase \@group fix___a fix___floor fix___n |
| 191 |
end macro |
| 192 |
|
| 193 |
## end of Athena's macros |
| 194 |
## |
| 195 |
## |
| 196 |
"; |
| 197 |
return $string; |
| 198 |
}; |
| 199 |
|
| 200 |
## xmin=floor(\$1),xmax=ceil(\$1), |
| 201 |
## ymin=floor(\$2),ymax=ceil(\$2) ) |
| 202 |
|
| 203 |
## END OF MACROS SUBSECTION |
| 204 |
########################################################################################## |