| 1 |
|
| 2 |
## -*- cperl -*- |
| 3 |
## |
| 4 |
## This file is part of Athena, copyright (c) 2001-2006 Bruce Ravel |
| 5 |
## |
| 6 |
## This section of the code contains subroutines associated with |
| 7 |
## rebinning data. |
| 8 |
|
| 9 |
sub rebin { |
| 10 |
|
| 11 |
## generally, we do not change modes unless there is data. |
| 12 |
## exceptions include things like the prefernces and key bindings, |
| 13 |
## which are data-independent |
| 14 |
Echo("No data!"), return unless $current; |
| 15 |
Echo("No data!"), return if ($current eq "Default Parameters"); |
| 16 |
|
| 17 |
## this is a way of testing the current list of data groups for some |
| 18 |
## necessary property. for the demo, this will just be the list of |
| 19 |
## groups |
| 20 |
my @keys = (); |
| 21 |
foreach my $k (&sorted_group_list) { |
| 22 |
($groups{$k}->{is_xmu}) and push @keys, $k; |
| 23 |
}; |
| 24 |
|
| 25 |
## you must define a hash which will contain the parameters needed |
| 26 |
## to perform the task. the hash_pointer global variable will point |
| 27 |
## to this hash for use in set_properties. you might draw these |
| 28 |
## values from configuration parameters, as in the commented out |
| 29 |
## example |
| 30 |
my %rebin_params; |
| 31 |
$rebin_params{abs} = $groups{$current}->{bkg_z}; |
| 32 |
$rebin_params{edge} = $groups{$current}->{bkg_e0}; |
| 33 |
foreach (qw(emin emax pre exafs xanes)) { |
| 34 |
$rebin_params{$_} = $config{rebin}{$_}; |
| 35 |
}; |
| 36 |
|
| 37 |
## you probably do not want the standard and the unknown to be the |
| 38 |
## same group |
| 39 |
# set_properties(1, $keys[1], 0) if ($current eq $keys[0]); |
| 40 |
|
| 41 |
## you may wish to provide a better guess for which should be the |
| 42 |
## standard and which the unknown. you may also want to adjust the |
| 43 |
## view of the groups list to show the unknown -- the following |
| 44 |
## works... |
| 45 |
my $here = ($list->bbox($groups{$current}->{text}))[1] - 5 || 0; |
| 46 |
($here < 0) and ($here = 0); |
| 47 |
my $full = ($list->bbox(@skinny_list))[3] + 5; |
| 48 |
$list -> yview('moveto', $here/$full); |
| 49 |
|
| 50 |
## these two global variables must be set before this view is |
| 51 |
## displayed. these are used at the level of set_properties to |
| 52 |
## perform chores appropriate to this dialog when changing the |
| 53 |
## current group |
| 54 |
$fat_showing = 'rebin'; |
| 55 |
$hash_pointer = \%rebin_params; |
| 56 |
|
| 57 |
## disable many menus. this makes the chore of managing the views |
| 58 |
## much easier. the idea is that the main view is "home base". if |
| 59 |
## you want to do a different analysis chore, you must first return |
| 60 |
## to the main view |
| 61 |
map {$_ -> configure(-state=>'disabled')} |
| 62 |
($data_menu, $merge_menu, $anal_menu, $settings_menu); |
| 63 |
|
| 64 |
## this removes the currently displayed view without destroying its |
| 65 |
## contents |
| 66 |
$fat -> packForget; |
| 67 |
|
| 68 |
## define the parent Frame for this analysis chore and pack it in |
| 69 |
## the correct location |
| 70 |
my $rebin = $container->Frame(@fatgeom, -relief=>'sunken', -borderwidth=>3) |
| 71 |
-> pack(-fill=>'both', -expand=>1); |
| 72 |
#$foobar -> packPropagate(0); |
| 73 |
## global variable identifying which Frame is showing |
| 74 |
$which_showing = $rebin; |
| 75 |
|
| 76 |
## the standard label along the top identifying this analysis chore |
| 77 |
$rebin -> Label(-text=>"Data Rebinning", |
| 78 |
-font=>$config{fonts}{large}, |
| 79 |
-foreground=>$config{colors}{activehighlightcolor}) |
| 80 |
-> pack(-side=>'top', -fill=>'x', -anchor=>'w'); |
| 81 |
|
| 82 |
## a good solution to organizing widgets is to stack frames, so |
| 83 |
## let's make a frame for the standard and the other. note that the |
| 84 |
## "labels" are actually flat buttons which display hints in the |
| 85 |
## echo area |
| 86 |
my $frame = $rebin -> Frame(-borderwidth=>2, -relief=>'flat') |
| 87 |
-> pack(-side=>'top', -fill=>'x', -pady=>4); |
| 88 |
|
| 89 |
$frame -> Label(-text=>"Group: ", |
| 90 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 91 |
) |
| 92 |
-> grid(-row=>0, -column=>0, -sticky=>'e'); |
| 93 |
$widget{rb_group} = $frame -> Label(-text=>$groups{$current}->{label}, |
| 94 |
-foreground=>$config{colors}{button}) |
| 95 |
-> grid(-row=>0, -column=>1, -columnspan=>2, -sticky=>'w'); |
| 96 |
|
| 97 |
$frame -> Label(-text=>'Edge energy:', |
| 98 |
-foreground=>$config{colors}{activehighlightcolor},) |
| 99 |
-> grid(-row=>1, -column=>0, -sticky=>'e'); |
| 100 |
$frame -> Label(-textvariable=>\$rebin_params{edge}) |
| 101 |
-> grid(-row=>1, -column=>1, -columnspan=>2, -sticky=>'w', -padx=>2); |
| 102 |
|
| 103 |
$frame -> Label(-text=>'Edge region from:', |
| 104 |
-foreground=>$config{colors}{activehighlightcolor}) |
| 105 |
-> grid(-row=>2, -column=>0, -sticky=>'e'); |
| 106 |
$frame -> Entry(-width=>5, -textvariable=>\$rebin_params{emin}, |
| 107 |
-validate=>'key', |
| 108 |
-validatecommand=>[\&set_variable, 'rebin']) |
| 109 |
-> grid(-row=>2, -column=>1, -sticky=>'w', -padx=>2); |
| 110 |
$frame -> Label(-text=>' to ', |
| 111 |
-foreground=>$config{colors}{activehighlightcolor}) |
| 112 |
-> grid(-row=>2, -column=>2); |
| 113 |
$frame -> Entry(-width=>5, -textvariable=>\$rebin_params{emax}, |
| 114 |
-validate=>'key', |
| 115 |
-validatecommand=>[\&set_variable, 'rebin']) |
| 116 |
-> grid(-row=>2, -column=>3, -sticky=>'w', -padx=>2); |
| 117 |
$frame -> Label(-text=>'eV', |
| 118 |
-foreground=>$config{colors}{activehighlightcolor}) |
| 119 |
-> grid(-row=>2, -column=>4); |
| 120 |
|
| 121 |
$frame -> Label(-text=>'Pre edge grid:', |
| 122 |
-foreground=>$config{colors}{activehighlightcolor}) |
| 123 |
-> grid(-row=>3, -column=>0, -sticky=>'e'); |
| 124 |
$frame -> Entry(-width=>5, -textvariable=>\$rebin_params{pre}, |
| 125 |
-validate=>'key', |
| 126 |
-validatecommand=>[\&set_variable, 'rebin']) |
| 127 |
-> grid(-row=>3, -column=>1, -sticky=>'w', -padx=>2); |
| 128 |
$frame -> Label(-text=>'eV', |
| 129 |
-foreground=>$config{colors}{activehighlightcolor}) |
| 130 |
-> grid(-row=>3, -column=>2, -sticky=>'w',); |
| 131 |
|
| 132 |
$frame -> Label(-text=>'XANES grid:', |
| 133 |
-foreground=>$config{colors}{activehighlightcolor}) |
| 134 |
-> grid(-row=>4, -column=>0, -sticky=>'e'); |
| 135 |
$frame -> Entry(-width=>5, -textvariable=>\$rebin_params{xanes}, |
| 136 |
-validate=>'key', |
| 137 |
-validatecommand=>[\&set_variable, 'rebin']) |
| 138 |
-> grid(-row=>4, -column=>1, -sticky=>'w', -padx=>2); |
| 139 |
$frame -> Label(-text=>'eV', |
| 140 |
-foreground=>$config{colors}{activehighlightcolor}) |
| 141 |
-> grid(-row=>4, -column=>2, -sticky=>'w',); |
| 142 |
|
| 143 |
$frame -> Label(-text=>'EXAFS grid:', |
| 144 |
-foreground=>$config{colors}{activehighlightcolor}) |
| 145 |
-> grid(-row=>5, -column=>0, -sticky=>'e'); |
| 146 |
$frame -> Entry(-width=>5, -textvariable=>\$rebin_params{exafs}, |
| 147 |
-validate=>'key', |
| 148 |
-validatecommand=>[\&set_variable, 'rebin']) |
| 149 |
-> grid(-row=>5, -column=>1, -sticky=>'w', -padx=>2); |
| 150 |
$frame -> Label(-text=>'1/Ang', |
| 151 |
-foreground=>$config{colors}{activehighlightcolor}) |
| 152 |
-> grid(-row=>5, -column=>2, -sticky=>'w',); |
| 153 |
|
| 154 |
$frame -> Label(-text=>" ") |
| 155 |
-> grid(-row=>6, -column=>0, -columnspan=>5, -sticky=>'ew',); |
| 156 |
|
| 157 |
$widget{rb_plot} = |
| 158 |
$frame -> Button(-text=>'Plot data and rebinned data', @button_list, |
| 159 |
-width=>1, |
| 160 |
-state=>($groups{$current}->{is_xmu}) ? 'normal' : 'disabled', |
| 161 |
-command=>sub{rebin_do(\%rebin_params)}) |
| 162 |
-> grid(-row=>7, -column=>0, -columnspan=>5, -sticky=>'ew',); |
| 163 |
|
| 164 |
$widget{rb_save} = $frame -> Button(-text=>'Make rebinned data group', @button_list, |
| 165 |
-width=>1, |
| 166 |
-state=>'disabled', |
| 167 |
-command=>sub{rebin_group($current, \%rebin_params, $dmode)}) |
| 168 |
-> grid(-row=>8, -column=>0, -columnspan=>5, -sticky=>'ew',); |
| 169 |
$widget{rb_marked} = $frame -> Button(-text=>'Rebin marked data and make groups', @button_list, |
| 170 |
-width=>1, |
| 171 |
-command=>sub{ |
| 172 |
Echo("Rebinning marked groups ..."); |
| 173 |
$top -> Busy; |
| 174 |
my $restore = $current; |
| 175 |
foreach my $g (&sorted_group_list) { |
| 176 |
next unless $marked{$g}; |
| 177 |
set_properties(0, $g, 0); |
| 178 |
rebin_do(\%rebin_params); |
| 179 |
rebin_group($current, \%rebin_params, $dmode) |
| 180 |
}; |
| 181 |
set_properties(1, $restore, 0); |
| 182 |
$top -> Unbusy; |
| 183 |
Echo("Rebinning marked groups ... done!"); |
| 184 |
$top -> update; |
| 185 |
}) |
| 186 |
-> grid(-row=>9, -column=>0, -columnspan=>5, -sticky=>'ew',); |
| 187 |
|
| 188 |
|
| 189 |
## this is a spacer frame which pushes all the widgets to the top |
| 190 |
$rebin -> Frame(-background=>$config{colors}{darkbackground}) |
| 191 |
-> pack(-side=>'bottom', -expand=>1, -fill=>'both'); |
| 192 |
|
| 193 |
## at the bottom of the frame, there are full width buttons for |
| 194 |
## returning to the main view and for going to the appropriate |
| 195 |
## document section |
| 196 |
$rebin -> Button(-text=>'Return to the main window', @button_list, |
| 197 |
-background=>$config{colors}{background2}, |
| 198 |
-activebackground=>$config{colors}{activebackground2}, |
| 199 |
-command=>sub{$groups{$current}->dispose("erase \@group re___bin", $dmode); |
| 200 |
&reset_window($rebin, "rebinning", 0); |
| 201 |
}) |
| 202 |
-> pack(-side=>'bottom', -fill=>'x'); |
| 203 |
## help button |
| 204 |
$rebin -> Button(-text=>'Document section: rebinning data', @button_list, |
| 205 |
-command=>sub{pod_display("process::rebin.pod")}) |
| 206 |
-> pack(-side=>'bottom', -fill=>'x', -pady=>4); |
| 207 |
|
| 208 |
|
| 209 |
$top -> update; |
| 210 |
|
| 211 |
}; |
| 212 |
|
| 213 |
|
| 214 |
sub rebin_do { |
| 215 |
my $rparams = $_[0]; |
| 216 |
$$rparams{emin} ||= $config{rebin}{emin}; |
| 217 |
$$rparams{emax} ||= $config{rebin}{emax}; |
| 218 |
$$rparams{pre} ||= $config{rebin}{pre}; |
| 219 |
$$rparams{xanes} ||= $config{rebin}{xanes}; |
| 220 |
$$rparams{exafs} ||= $config{rebin}{exafs}; |
| 221 |
## these must be positive or bad stuff will happen |
| 222 |
$$rparams{pre} = abs($$rparams{pre}); |
| 223 |
$$rparams{xanes} = abs($$rparams{xanes}); |
| 224 |
$$rparams{exafs} = abs($$rparams{exafs}); |
| 225 |
## check if emin, emax out of order |
| 226 |
(($$rparams{emin}, $$rparams{emax}) = ($$rparams{emax}, $$rparams{emin})) if |
| 227 |
($$rparams{emin} > $$rparams{emax}); |
| 228 |
|
| 229 |
my $group = $groups{$current}->{group}; |
| 230 |
Echo("Rebinning data $groups{$group}->{label} ..."); |
| 231 |
my $e0shift = $groups{$group}->{bkg_eshift}; |
| 232 |
my @e = Ifeffit::get_array("$group.energy"); |
| 233 |
my ($efirst, $elast) = ($e[0]+$e0shift, $e[$#e]+$e0shift); |
| 234 |
my $e0 = $groups{$current}->{bkg_e0}; |
| 235 |
$groups{$group}->dispose("## Rebinning group $group:", $dmode); |
| 236 |
my @bingrid; |
| 237 |
my $ee = $efirst; |
| 238 |
while ($ee < $$rparams{emin}+$e0) { |
| 239 |
push @bingrid, $ee; |
| 240 |
$ee += $$rparams{pre}; |
| 241 |
}; |
| 242 |
$ee = $$rparams{emin}+$e0; |
| 243 |
while ($ee < $$rparams{emax}+$e0) { |
| 244 |
push @bingrid, $ee; |
| 245 |
$ee += $$rparams{xanes}; |
| 246 |
}; |
| 247 |
$ee = $$rparams{emax}+$e0; |
| 248 |
my $kk = $groups{$group}->e2k($$rparams{emax}); |
| 249 |
while ($ee < $elast) { |
| 250 |
push @bingrid, $ee; |
| 251 |
$kk += $$rparams{exafs}; |
| 252 |
$ee = $e0 + $groups{$group}->k2e($kk); |
| 253 |
}; |
| 254 |
push @bingrid, $elast; |
| 255 |
Ifeffit::put_array("re___bin.energy", \@bingrid); |
| 256 |
my $sets = "set($group.eee = $group.energy+$e0shift,\n"; |
| 257 |
$sets .= " re___bin.xmu = rebin($group.eee, $group.xmu, re___bin.energy)"; |
| 258 |
$sets .= ",\n re___bin.i0 = rebin($group.eee, $group.i0, re___bin.energy)" |
| 259 |
if ($groups{$group}->{i0}); |
| 260 |
$sets .= ")"; |
| 261 |
$groups{$group}->dispose($sets, $dmode); |
| 262 |
$groups{$group} -> plotE('em', $dmode, \%plot_features, \@indicator); |
| 263 |
my $color = $plot_features{c1}; |
| 264 |
$groups{$group} -> dispose("plot(re___bin.energy, re___bin.xmu, style=lines, color=\"$color\", key=rebinned)", $dmode); |
| 265 |
$last_plot='e'; |
| 266 |
$widget{rb_save} -> configure(-state=>'normal'); |
| 267 |
Echo("Rebinning data $groups{$group}->{label} ... done!"); |
| 268 |
}; |
| 269 |
|
| 270 |
sub rebin_group { |
| 271 |
my ($parent, $rparams, $mode) = @_; |
| 272 |
my ($group, $label) = ("Bin ".$groups{$parent}->{label}, ""); |
| 273 |
($group, $label) = group_name($group); |
| 274 |
my $e0shift = $groups{$parent}->{bkg_eshift}; |
| 275 |
$groups{$group} = Ifeffit::Group -> new(group=>$group, label=>$label); |
| 276 |
## copy the titles |
| 277 |
push @{$groups{$group}->{titles}}, |
| 278 |
"$groups{$parent}->{label} rebinned onto a grid with boundaries at $$rparams{emin} eV and $$rparams{emax} eV", |
| 279 |
"and steps sizes of $$rparams{pre} eV, $$rparams{xanes} eV, and $$rparams{exafs} 1/Ang"; |
| 280 |
$groups{$group} -> make(file=>"$groups{$parent}->{label} rebinned"); |
| 281 |
foreach (@{$groups{$parent}->{titles}}) { |
| 282 |
push @{$groups{$group}->{titles}}, $_; |
| 283 |
}; |
| 284 |
$groups{$group} -> put_titles; |
| 285 |
$groups{$group} -> set_to_another($groups{$parent}); |
| 286 |
$groups{$group} -> make(is_xmu => 1, is_chi => 0, is_rsp => 0, is_qsp => 0, is_bkg => 0, |
| 287 |
not_data => 0,); |
| 288 |
my $sets = "set($group.energy = re___bin.energy-$e0shift,\n"; |
| 289 |
$sets .= " $group.xmu = re___bin.xmu"; |
| 290 |
$sets .= ",\n $group.i0 = re___bin.i0" |
| 291 |
if ($groups{$group}->{i0}); |
| 292 |
$sets .= ")"; |
| 293 |
$groups{$group} -> dispose($sets, $dmode); |
| 294 |
|
| 295 |
## rebin arrays for detector groups |
| 296 |
$groups{$group} -> make(numerator => "$group.numer", |
| 297 |
denominator => "$group.denom"); |
| 298 |
my $cmd = "set(r___b.array = $groups{$parent}->{numerator},\n"; |
| 299 |
$cmd .= " $group.numer = rebin($parent.energy, r___b.array, $group.energy),\n"; |
| 300 |
$cmd .= " r___b.array = $groups{$parent}->{denominator},\n"; |
| 301 |
$cmd .= " $group.denom = rebin($parent.energy, r___b.array, $group.energy))\n"; |
| 302 |
$cmd .= "erase r___b.array\n"; |
| 303 |
$groups{$group} -> dispose($cmd,$dmode); |
| 304 |
|
| 305 |
++$line_count; |
| 306 |
fill_skinny($list, $group, 1, 0); |
| 307 |
my $memory_ok = $groups{$group} -> memory_check($top, \&Echo, \%groups, $max_heap, 0, 0); |
| 308 |
Echo ("WARNING: Ifeffit is out of memory!") if ($memory_ok == -1); |
| 309 |
}; |
| 310 |
|
| 311 |
## END OF REBINNING SUBSECTION |
| 312 |
########################################################################################## |