| 1 |
## -*- cperl -*- |
| 2 |
## |
| 3 |
## This file is part of Athena, copyright (c) 2001-2006 Bruce Ravel |
| 4 |
## |
| 5 |
## This section of the code contains subroutines associated with |
| 6 |
## converting pixel data to energy |
| 7 |
|
| 8 |
sub pixel { |
| 9 |
Echo("No data!"), return unless $current; |
| 10 |
Echo("No data!"), return if ($current eq "Default Parameters"); |
| 11 |
my %pixel_params = (offset => 0, |
| 12 |
linear => $config{pixel}{resolution}, |
| 13 |
quad => 0, |
| 14 |
constrain => 1,); |
| 15 |
|
| 16 |
my @save = ($plot_features{emin}, $plot_features{emax}); |
| 17 |
$plot_features{emin} = $config{pixel}{emin}; |
| 18 |
$plot_features{emax} = $config{pixel}{emax}; |
| 19 |
|
| 20 |
|
| 21 |
my @keys = (); |
| 22 |
my $count_pixel = 0; |
| 23 |
foreach my $k (&sorted_group_list) { |
| 24 |
($groups{$k}->{is_xmu}) and push @keys, $k; |
| 25 |
($pixel_params{standard} = $k) if (($k eq $current) and not $groups{$current}->{is_pixel}); |
| 26 |
++$count_pixel if ($groups{$k}->{is_pixel}); |
| 27 |
}; |
| 28 |
Echo("You need two or more xmu groups to do pixel to energy conversion"), |
| 29 |
return unless ($#keys >= 1); |
| 30 |
Echo("You need at least one pixel group to do pixel to energy conversion"), |
| 31 |
return unless ($count_pixel >= 1); |
| 32 |
|
| 33 |
$pixel_params{keys} = \@keys; |
| 34 |
$pixel_params{standard} ||= $keys[0]; |
| 35 |
my $standard_lab = "1: ".$groups{$keys[0]}->{label}; |
| 36 |
unless ($groups{$current}->{is_pixel}) { |
| 37 |
foreach my $k (&sorted_group_list) { |
| 38 |
if ($groups{$k}->{is_pixel}) { |
| 39 |
set_properties(1, $k, 0); |
| 40 |
last; |
| 41 |
}; |
| 42 |
}; |
| 43 |
# adjust the view |
| 44 |
my $here = ($list->bbox($groups{$current}->{text}))[1] - 5 || 0; |
| 45 |
($here < 0) and ($here = 0); |
| 46 |
my $full = ($list->bbox(@skinny_list))[3] + 5; |
| 47 |
$list -> yview('moveto', $here/$full); |
| 48 |
}; |
| 49 |
|
| 50 |
$fat_showing = 'pixel'; |
| 51 |
$hash_pointer = \%pixel_params; |
| 52 |
map {$_ -> configure(-state=>'disabled')} |
| 53 |
($data_menu, $merge_menu, $anal_menu, $settings_menu); |
| 54 |
$fat -> packForget; |
| 55 |
my $pixel = $container->Frame(@fatgeom, -relief=>'sunken', -borderwidth=>3) |
| 56 |
-> pack(-fill=>'both', -expand=>1); |
| 57 |
#$pixel -> packPropagate(0); |
| 58 |
$which_showing = $pixel; |
| 59 |
|
| 60 |
$pixel -> Label(-text=>"Dispersive XAS: convert pixels to energy", |
| 61 |
-font=>$config{fonts}{large}, |
| 62 |
-foreground=>$config{colors}{activehighlightcolor}) |
| 63 |
-> pack(-side=>'top', -fill=>'x', -anchor=>'w'); |
| 64 |
|
| 65 |
|
| 66 |
## select the alignment standard |
| 67 |
my $frame = $pixel -> Frame(-borderwidth=>2, -relief=>'flat') |
| 68 |
-> pack(-side=>'top', -fill=>'x', -ipady=>6); |
| 69 |
$widget{pixel_standard} = $frame -> Button(-text=>"Standard: ", @label_button, |
| 70 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 71 |
-activeforeground=>$config{colors}{activehighlightcolor}, |
| 72 |
-command=>[\&Echo, "The spectrum to which the pixel data will be aligned in energy."] |
| 73 |
) |
| 74 |
-> grid(-row=>0, -column=>0, -sticky=>'e', -ipady=>2); |
| 75 |
|
| 76 |
my $menu = $frame -> BrowseEntry(-variable => \$standard_lab, |
| 77 |
@browseentry_list, |
| 78 |
-browsecmd => sub { |
| 79 |
my $text = $_[1]; |
| 80 |
my $this = $1 if ($text =~ /^(\d+):/); |
| 81 |
Echo("Failed to match in browsecmd. Yikes! Complain to Bruce."), return unless $this; |
| 82 |
$this -= 1; |
| 83 |
$pixel_params{standard}=$pixel_params{keys}->[$this]; |
| 84 |
#$standard_lab="$groups{$s}->{label} ($s)"; |
| 85 |
&pixel_setup(\%pixel_params); |
| 86 |
}) |
| 87 |
-> grid(-row=>0, -column=>1, -columnspan=>2, -sticky=>'w'); |
| 88 |
##print join("\n",(($menu->children)[0]->children)[1]->children), $/; |
| 89 |
##Subwidget("yscrollbar")->configure(-background=>$config{colors}{background}); |
| 90 |
my $i = 1; |
| 91 |
foreach my $s (@keys) { |
| 92 |
$menu -> insert("end", "$i: $groups{$s}->{label}"); |
| 93 |
++$i; |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
# my $menu = $frame -> Optionmenu(-textvariable => \$standard_lab, |
| 99 |
# -borderwidth=>1, ) |
| 100 |
# -> grid(-row=>0, -column=>1, -sticky=>'w'); |
| 101 |
# foreach my $s (@keys) { |
| 102 |
# next if $groups{$s}->{is_pixel}; |
| 103 |
# $menu -> command(-label => $groups{$s}->{label}, |
| 104 |
# -command=>sub{$pixel_params{standard}=$s; |
| 105 |
# $standard_lab=$groups{$s}->{label}; |
| 106 |
# &pixel_setup(\%pixel_params); |
| 107 |
# }); |
| 108 |
# }; |
| 109 |
|
| 110 |
## the group for alignment is the current group in the group list |
| 111 |
$frame -> Button(-text=>"Other: ", |
| 112 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 113 |
-activeforeground=>$config{colors}{activehighlightcolor}, |
| 114 |
@label_button, |
| 115 |
-command=>[\&Echo, "The group currently selected for alignment to the standard."] |
| 116 |
) |
| 117 |
-> grid(-row=>1, -column=>0, -sticky=>'e', -ipady=>2); |
| 118 |
$widget{pixel_unknown} = $frame -> Label(-text=>$groups{$current}->{label}, |
| 119 |
-foreground=>$config{colors}{button}, |
| 120 |
-width=>20) |
| 121 |
-> grid(-row=>1, -column=>1, -columnspan=>2, -sticky=>'w', -pady=>2, -padx=>2); |
| 122 |
my $other_label; |
| 123 |
|
| 124 |
$widget{pixel_refine} = $frame -> |
| 125 |
Button(-text=>'Refine alignment parameters', @button_list, |
| 126 |
-command=>sub{ |
| 127 |
#pixel_refine(\%pixel_params, 1); |
| 128 |
pixel_refine(\%pixel_params, 2); |
| 129 |
#$widget{pixel_make} -> configure(-state=>'normal'); |
| 130 |
}) |
| 131 |
-> grid(-row=>2, -column=>0, -columnspan=>3, -sticky=>'ew', -padx=>4); |
| 132 |
|
| 133 |
|
| 134 |
$frame -> Button(-text=>"Offset: ", |
| 135 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 136 |
-activeforeground=>$config{colors}{activehighlightcolor}, |
| 137 |
@label_button, |
| 138 |
-command=>[\&Echo, "The constant term in the calibration refinement."]) |
| 139 |
-> grid(-row=>3, -column=>0, -sticky=>'e', -ipady=>2); |
| 140 |
$widget{pixel_offset} = $frame -> Entry(-width=>8, |
| 141 |
-textvariable=>\$pixel_params{offset}) |
| 142 |
-> grid(-row=>3, -column=>1, -sticky=>'w', -ipady=>2); |
| 143 |
$widget{pixel_constrain} = $frame -> |
| 144 |
Checkbutton(-text=>'constrain offset to linear term', |
| 145 |
-onvalue=>1, -offvalue=>0, |
| 146 |
-selectcolor=> $config{colors}{single}, |
| 147 |
-variable=>\$pixel_params{constrain}) |
| 148 |
-> grid(-row=>3, -column=>2, -sticky=>'w', -ipady=>2); |
| 149 |
|
| 150 |
$frame -> Button(-text=>"Linear: ", |
| 151 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 152 |
-activeforeground=>$config{colors}{activehighlightcolor}, |
| 153 |
@label_button, |
| 154 |
-command=>[\&Echo, "The linear term in energy in the calibration refinement."]) |
| 155 |
-> grid(-row=>4, -column=>0, -sticky=>'e', -ipady=>2); |
| 156 |
$widget{pixel_linear} = $frame -> Entry(-width=>8, |
| 157 |
-textvariable=>\$pixel_params{linear}) |
| 158 |
-> grid(-row=>4, -column=>1, -sticky=>'w', -ipady=>2); |
| 159 |
$widget{pixel_linear_button} = $frame -> Button(-text=>'Reset offset', |
| 160 |
@button_list, |
| 161 |
-command=> |
| 162 |
sub { |
| 163 |
my $stan = $groups{$pixel_params{standard}}->{group}; |
| 164 |
$pixel_params{offset} = $groups{$stan}->{bkg_e0} - $groups{$current}->{bkg_e0}*$pixel_params{linear}; |
| 165 |
} ) |
| 166 |
-> grid(-row=>4, -column=>2, -sticky=>'w', -ipady=>2, -padx=>4); |
| 167 |
|
| 168 |
|
| 169 |
$frame -> Button(-text=>"Quadratic: ", |
| 170 |
-foreground=>$config{colors}{activehighlightcolor}, |
| 171 |
-activeforeground=>$config{colors}{activehighlightcolor}, |
| 172 |
@label_button, |
| 173 |
-command=>[\&Echo, "The quadratic term in energy in the calibration refinement."]) |
| 174 |
-> grid(-row=>5, -column=>0, -sticky=>'e', -ipady=>2); |
| 175 |
$widget{pixel_quad} = $frame -> Entry(-width=>8, |
| 176 |
-textvariable=>\$pixel_params{quad}) |
| 177 |
-> grid(-row=>5, -column=>1, -sticky=>'w', -ipady=>2); |
| 178 |
|
| 179 |
|
| 180 |
$widget{pixel_replot} = $frame -> |
| 181 |
Button(-text=>'Replot standard and pixel data', @button_list, |
| 182 |
-command=>sub{pixel_setup(\%pixel_params)}, |
| 183 |
) |
| 184 |
-> grid(-row=>6, -column=>0, -columnspan=>3, -sticky=>'ew', -padx=>4); |
| 185 |
$widget{pixel_make} = $frame -> |
| 186 |
Button(-text=>'Make data group', @button_list, |
| 187 |
-command=>sub{&pixel_make_group(\%pixel_params)},) |
| 188 |
-> grid(-row=>7, -column=>0, -columnspan=>3, -sticky=>'ew', -padx=>4,); |
| 189 |
|
| 190 |
$widget{pixel_all} = $frame -> |
| 191 |
Button(-text=>'Convert all MARKED pixel groups', @button_list, |
| 192 |
-command=>sub{&pixel_make_all(\%pixel_params)},) |
| 193 |
-> grid(-row=>8, -column=>0, -columnspan=>3, -sticky=>'ew', -pady=>3, -padx=>4); |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
$pixel -> Button(-text=>'Return to the main window', @button_list, |
| 199 |
-background=>$config{colors}{background2}, |
| 200 |
-activebackground=>$config{colors}{activebackground2}, |
| 201 |
-command=>sub{foreach my $g (keys %groups) { # clean up def-ed arrays |
| 202 |
next unless $groups{$g}->{made_pixel}; |
| 203 |
$groups{$g}->make(made_pixel=>0); |
| 204 |
$groups{$g}->dispose("erase $g.ec $g.xc", $dmode); |
| 205 |
##$groups{$g}->dispose("erase $g.xc", $dmode); |
| 206 |
}; |
| 207 |
$groups{$current}->dispose("unguess", $dmode); |
| 208 |
&reset_window($pixel, "dispersive XAS calibration", \@save); |
| 209 |
}) |
| 210 |
-> pack(-side=>'bottom', -fill=>'x'); |
| 211 |
## help button |
| 212 |
$pixel -> Button(-text=>'Document section: converting pixel data to energy', @button_list, |
| 213 |
-command=>sub{pod_display("process::pixel.pod")},) |
| 214 |
-> pack(-side=>'bottom', -fill=>'x', -pady=>4); |
| 215 |
|
| 216 |
&pixel_setup(\%pixel_params); |
| 217 |
$plotsel -> raise('e'); |
| 218 |
$top -> update; |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
sub pixel_initial { |
| 223 |
my $rhash = $_[0]; |
| 224 |
my $stan = $groups{$$rhash{standard}}->{group}; |
| 225 |
$$rhash{linear} ||= 0.4; |
| 226 |
$$rhash{quad} ||= 0; |
| 227 |
$$rhash{offset} ||= $groups{$stan}->{bkg_e0} - $groups{$current}->{bkg_e0}*$$rhash{linear}; |
| 228 |
}; |
| 229 |
|
| 230 |
sub pixel_setup { |
| 231 |
my $rhash = $_[0]; |
| 232 |
my $stan = $groups{$$rhash{standard}}->{group}; |
| 233 |
my $group = $groups{$current}->{group}; |
| 234 |
$groups{$stan} ->dispatch_bkg if $groups{$stan} ->{update_bkg}; |
| 235 |
$groups{$group}->dispatch_bkg if $groups{$group}->{update_bkg}; |
| 236 |
&pixel_initial($rhash); |
| 237 |
my $command = "set(pixel___b=$$rhash{linear}, pixel___c=$$rhash{quad},\n"; |
| 238 |
$command .= " pixel___a = $$rhash{offset})\n"; |
| 239 |
$command .= "def($group.ec = pixel___a + pixel___b*$group.energy + abs(pixel___c)*$group.energy**2,\n"; |
| 240 |
$command .= " $group.xc = qinterp($group.ec, $group.flat, $stan.energy))\n"; |
| 241 |
$groups{$current} -> dispose($command, $dmode); |
| 242 |
$groups{$current} -> make(made_pixel=>1); |
| 243 |
&pixel_plot($rhash); |
| 244 |
}; |
| 245 |
|
| 246 |
sub pixel_refine { |
| 247 |
my $rhash = $_[0]; |
| 248 |
my $order = $_[1]; |
| 249 |
Echonow("Refining pixel calibration parameters ..."); |
| 250 |
my $stan = $groups{$$rhash{standard}}->{group}; |
| 251 |
my $group = $groups{$current}->{group}; |
| 252 |
$groups{$stan} ->dispatch_bkg if $groups{$stan} ->{update_bkg}; |
| 253 |
$groups{$group}->dispatch_bkg if $groups{$group}->{update_bkg}; |
| 254 |
my $e0 = $groups{$$rhash{standard}}->{bkg_e0}; |
| 255 |
my $ee = $groups{$$rhash{standard}}->{bkg_e0} + 20; |
| 256 |
my $ed = $groups{$current}->{bkg_e0}; |
| 257 |
my $command = "guess(pixel___b=$$rhash{linear}, pixel___c=$$rhash{quad})\n" if ($order == 2); |
| 258 |
if ($$rhash{constrain}) { |
| 259 |
$command .= "def(pixel___a=$e0-pixel___b*$ed)\n"; |
| 260 |
} else { |
| 261 |
$command .= "guess(pixel___a=$$rhash{offset},)\n"; |
| 262 |
}; |
| 263 |
$command .= "step $stan.energy $ee 0 p___ixel.step\n"; |
| 264 |
$command .= "set p___ixel.drop = -1*(p___ixel.step - 1)\n"; |
| 265 |
$command .= "def(p___ixel.first = $stan.flat*p___ixel.drop - $group.xc*p___ixel.drop,\n"; |
| 266 |
$command .= " p___ixel.second = p___ixel.step * sqrt(abs($stan.energy-$e0)) * (($stan.flat-1) - ($group.xc-1)),\n"; |
| 267 |
$command .= " p___ixel.diff = p___ixel.first + p___ixel.second)\n"; |
| 268 |
$command .= "set(pixel___xmin = floor($group.ec),\n"; |
| 269 |
$command .= " pixel___xmax = ceil($group.ec))\n"; |
| 270 |
$command .= "minimize(p___ixel.diff, x=$stan.energy, xmin=pixel___xmin, xmax=pixel___xmax)\n"; |
| 271 |
##$command .= "minimize($group.diff)\n"; |
| 272 |
$groups{$current} -> dispose($command, $dmode); |
| 273 |
$$rhash{offset} = sprintf("%.5f", Ifeffit::get_scalar("pixel___a")); |
| 274 |
$$rhash{linear} = sprintf("%.5f", Ifeffit::get_scalar("pixel___b")); |
| 275 |
$$rhash{quad} = sprintf("%.5g", Ifeffit::get_scalar("pixel___c")); |
| 276 |
Echonow("Refining pixel calibration parameters ... replotting ..."); |
| 277 |
&pixel_plot($rhash); |
| 278 |
Echo("Refining pixel calibration parameters ... replotting ... done!"); |
| 279 |
}; |
| 280 |
|
| 281 |
sub pixel_plot { |
| 282 |
my $rhash = $_[0]; |
| 283 |
my $stan = $groups{$$rhash{standard}}->{group}; |
| 284 |
my $group = $groups{$current}->{group}; |
| 285 |
$groups{$$rhash{standard}}->plotE('emn', $dmode, {emin=>$plot_features{emin}, |
| 286 |
emax=>$plot_features{emax}, |
| 287 |
}, \@indicator); |
| 288 |
my $fitcolor = $config{plot}{c1}; |
| 289 |
my $command = "plot($group.ec, $group.flat, style=lines, color=$fitcolor, key=$groups{$current}->{label})\n"; |
| 290 |
$groups{$current} -> dispose($command, $dmode); |
| 291 |
}; |
| 292 |
|
| 293 |
sub pixel_make_group { |
| 294 |
my $rhash = $_[0]; |
| 295 |
my $stan = $groups{$$rhash{standard}}->{group}; |
| 296 |
my $was = $groups{$current}->{group}; |
| 297 |
(my $group = $groups{$current}->{label}) =~ s/_pixel/_data/; |
| 298 |
my ($new, $label) = group_name($group); |
| 299 |
$groups{$new} = Ifeffit::Group -> new(group=>$new, label=>$label); |
| 300 |
$groups{$new} -> set_to_another($groups{$$rhash{standard}}); |
| 301 |
$groups{$new} -> make(is_xmu => 1, is_chi => 0, is_rsp => 0, |
| 302 |
is_qsp => 0, is_bkg => 0, is_pixel => 0, |
| 303 |
not_data => 0); |
| 304 |
$groups{$new} -> make(bkg_e0 => $groups{$$rhash{standard}}->{bkg_e0}, |
| 305 |
file => $groups{$current}->{label} . " converted to energy", |
| 306 |
); |
| 307 |
|
| 308 |
$groups{$new}->{titles} = []; |
| 309 |
push @{$groups{$new}->{titles}}, |
| 310 |
("Converted from pixel data $groups{$current}->{label}", |
| 311 |
"Calibrated to $groups{$$rhash{standard}}->{label}", |
| 312 |
"Offset: $$rhash{offset}", |
| 313 |
"Linear term: $$rhash{linear}", |
| 314 |
"Quadratic term: $$rhash{quad}"); |
| 315 |
$groups{$new} -> put_titles; |
| 316 |
my $sets = "set($new.energy = $was.ec + " . $groups{$$rhash{standard}}->{bkg_eshift} . ",\n"; |
| 317 |
$sets .= " $new.xmu = $was.xmu)"; |
| 318 |
$groups{$new} -> dispose($sets, $dmode); |
| 319 |
|
| 320 |
my ($pre1, $pre2, $nor1, $nor2, $spl1, $spl2, $kmin, $kmax) = |
| 321 |
set_range_params($new); |
| 322 |
$groups{$new} -> make( |
| 323 |
bkg_pre1 => $pre1, |
| 324 |
bkg_pre2 => $pre2, |
| 325 |
bkg_nor1 => $nor1, |
| 326 |
bkg_nor2 => $nor2, |
| 327 |
bkg_spl1 => $spl1, |
| 328 |
bkg_spl2 => $spl2, |
| 329 |
bkg_spl1e => $groups{$new}->k2e($spl1), |
| 330 |
bkg_spl2e => $groups{$new}->k2e($spl2), |
| 331 |
fft_kmin => $kmin, |
| 332 |
fft_kmax => $kmax, |
| 333 |
); |
| 334 |
$groups{$new} -> kmax_suggest(\%plot_features) if ($groups{$new}->{fft_kmax} == 999); |
| 335 |
|
| 336 |
$groups{$new} -> make(update_bkg => 1); |
| 337 |
++$line_count; |
| 338 |
fill_skinny($list, $new, 1); |
| 339 |
my $memory_ok = $groups{$new} |
| 340 |
-> memory_check($top, \&Echo, \%groups, $max_heap, 0, 0); |
| 341 |
Echo ("WARNING: Ifeffit is out of memory!") if ($memory_ok == -1); |
| 342 |
|
| 343 |
}; |
| 344 |
|
| 345 |
sub pixel_make_all { |
| 346 |
my $rhash = $_[0]; |
| 347 |
my $m = 0; |
| 348 |
map {$m += $_} values %marked; |
| 349 |
Error("Batch conversion aborted. There are no marked groups."), return 1 unless ($m); |
| 350 |
##Error("Merging aborted. There is just 1 marked group."), return 1 if ($m==1); |
| 351 |
Echonow("Batch converting marked groups from pixel to energy ..."); |
| 352 |
foreach my $g (&sorted_group_list) { |
| 353 |
next unless $marked{$g}; |
| 354 |
next unless $groups{$g}->{is_pixel}; |
| 355 |
set_properties(0, $g, 0); |
| 356 |
&pixel_make_group($rhash); |
| 357 |
}; |
| 358 |
Echo("Batch converting marked groups from pixel to energy ... done!"); |
| 359 |
}; |