Debian Codegraph @

athena_parts/convolve.pl

raw
1
2 ## -*- cperl -*-
3 ##
4 ## This file is part of Athena, copyright (c) 2001-2006 Bruce Ravel
5 ##
6 ## This file contains the data convolution dialog
7
8
9
10 sub convolve {
11
12 ## generally, we do not change modes unless there is data.
13 ## exceptions include things like the prefernces and key bindings,
14 ## which are data-independent
15 Echo("No data!"), return unless $current;
16 Echo("No data!"), return if ($current eq "Default Parameters");
17
18 ## this is a way of testing the current list of data groups for some
19 ## necessary property. for the demo, this will just be the list of
20 ## groups
21 my @keys = ();
22 foreach my $k (&sorted_group_list) {
23 ($groups{$k}->{is_xmu}) and push @keys, $k;
24 };
25 Echo("You need at least one xmu group to do convolution"), return unless (@keys);
26
27 ## you must define a hash which will contain the parameters needed
28 ## to perform the task. the hash_pointer global variable will point
29 ## to this hash for use in set_properties. you might draw these
30 ## values from configuration parameters
31 my %conv_params;
32 $conv_params{econv} = 0;
33 $conv_params{noise} = 0;
34 $conv_params{function} = "Lorentizan";
35 $conv_params{current} = $groups{$current}->{label};
36
37
38 ## The Athena standard for analysis chores that need a specialized
39 ## plotting range is to save the plotting range from the main view
40 ## and restore it when the main view is restored
41 # my @save = ($plot_features{emin}, $plot_features{emax});
42 # $plot_features{emin} = $config{foobar}{emin};
43 # $plot_features{emax} = $config{foobar}{emax};
44
45 ## these two global variables must be set before this view is displayed
46 $fat_showing = 'convolve';
47 $hash_pointer = \%conv_params;
48
49 ## disable many menus. this makes the chore of managing the views
50 ## much easier. the idea is that the main view is "home base". if
51 ## you want to do a different analysis chore, you must first return
52 ## to the main view
53 map {$_ -> configure(-state=>'disabled')}
54 ($data_menu, $merge_menu, $anal_menu, $settings_menu);
55
56 ## this removes the currently displayed view without destroying its
57 ## contents
58 $fat -> packForget;
59
60 ## define the parent Frame for this analysis chore and pack it in
61 ## the correct location
62 my $conv = $container->Frame(@fatgeom, -relief=>'sunken', -borderwidth=>3)
63 -> pack(-fill=>'both', -expand=>1);
64 #$conv -> packPropagate(0);
65 ## global variable identifying which Frame is showing
66 $which_showing = $conv;
67
68 ## the standard label along the top identifying this analysis chore
69 $conv -> Label(-text=>"Data convolution",
70 -font=>$config{fonts}{large},
71 -foreground=>$config{colors}{activehighlightcolor})
72 -> pack(-side=>'top', -fill=>'x', -anchor=>'w');
73
74 ## a good solution to organizing widgets is to stack frames, so
75 ## let's make a frame for the standard and the other. note that the
76 ## "labels" are actually flat buttons which display hints in the
77 ## echo area
78 my $frame = $conv -> Frame(-borderwidth=>2, -relief=>'flat')
79 -> pack(-side=>'top', -fill=>'x', -pady=>8);
80 $frame -> Label(-text=>"Group: ", -foreground => $config{colors}{button},)
81 -> grid(-row=>0, -column=>0, -sticky=>'e', -pady=>2, -ipadx=>6);
82 $frame -> Label(-textvariable=>\$conv_params{current},)
83 -> grid(-row=>0, -column=>1, -sticky=>'w', -pady=>2);
84 $frame -> Label(-text=>"Convolution function: ",
85 -foreground=>$config{colors}{activehighlightcolor},
86 )
87 -> grid(-row=>1, -column=>0, -sticky=>'e', -pady=>2);
88 $frame -> Optionmenu(-textvariable => \$conv_params{function},
89 -variable => \$conv_params{function},
90 -borderwidth=>1,
91 -options => ['Lorentzian', 'Gaussian'])
92 -> grid(-row=>1, -column=>1, -sticky=>'w');
93
94 $frame -> Label(-text=>"Convolution width: ",
95 -foreground=>$config{colors}{activehighlightcolor},
96 )
97 -> grid(-row=>2, -column=>0, -sticky=>'e', -pady=>2);
98 $widget{conv_econv} = $frame -> Entry(-width=>8,
99 -textvariable => \$conv_params{econv},
100 -validate=>'key',
101 -validatecommand=>[\&set_variable, 'conv_econv']
102 )
103 -> grid(-row=>2, -column=>1, -sticky=>'w', -pady=>2);
104
105 $frame -> Label(-text=>"Noise (fraction of edge step): ",
106 -foreground=>$config{colors}{activehighlightcolor},
107 )
108 -> grid(-row=>3, -column=>0, -sticky=>'e', -pady=>2);
109 $widget{conv_noise} = $frame -> Entry(-width=>8,
110 -textvariable => \$conv_params{noise},
111 -validate=>'key',
112 -validatecommand=>[\&set_variable, 'conv_noise']
113 )
114 -> grid(-row=>3, -column=>1, -sticky=>'w', -pady=>2);
115
116
117 # $frame = $conv -> Frame(-borderwidth=>2, -relief=>'flat')
118 # -> pack(-side=>'top', -fill=>'x', -pady=>4);
119 $frame -> Button(-text=>'Plot data and convolution', @button_list,
120 -width=>1,
121 -command=>sub{convolve_plot(\%conv_params)}
122 )
123 -> grid(-row=>4, -column=>0, -columnspan=>2, -sticky=>'ew', -pady=>6);
124
125 $widget{conv_group} = $frame -> Button(-text=>'Make data group',
126 -width=>1,
127 @button_list,
128 -state=>'disabled',
129 -command=>sub{convolve_group(\%conv_params)}
130 )
131 -> grid(-row=>5, -column=>0, -columnspan=>2, -sticky=>'ew', -pady=>2);
132
133 ## this is a spacer frame which pushes all the widgets to the top
134 $conv -> Frame(-background=>$config{colors}{darkbackground})
135 -> pack(-side=>'bottom', -expand=>1, -fill=>'both');
136
137
138 ## at the bottom of the frame, there are full width buttons for
139 ## returning to the main view and for going to the appropriate
140 ## document section
141 $conv -> Button(-text=>'Return to the main window', @button_list,
142 -background=>$config{colors}{background2},
143 -activebackground=>$config{colors}{activebackground2},
144 -command=>sub{&reset_window($conv, "convolution", 0);
145 })
146 -> pack(-side=>'bottom', -fill=>'x');
147 ## help button
148 $conv -> Button(-text=>'Document section: Convoluting data', @button_list,
149 -command=>sub{pod_display("process::conv.pod")})
150 -> pack(-side=>'bottom', -fill=>'x', -pady=>4);
151
152
153 ## and finally....
154 $groups{$current}->plotE('emn', $dmode, \%plot_features, \@indicator);
155 $top -> update;
156
157 };
158
159 sub convolve_plot {
160 my $rhash = $_[0];
161 Error("The convolution width must be positive."), return if ($$rhash{econv} < 0);
162 Error("The noise level is a fraction of the edge step) and must be non-negative."), return if ($$rhash{noise} < 0);
163 Echonow("Convoluting is time consuming (patience is a virtue!) ...");
164 $top->Busy;
165 my $group = $groups{$current}->{group};
166 my $suff = ($groups{$current}->{bkg_flatten}) ? 'flat' : 'norm';
167 my $function = ($$rhash{function} eq 'Lorentzian') ? 'lconvolve' : 'gconvolve';
168 my $color = $config{plot}{c1};
169 my $key = 'convolution';
170 my $eshift = $groups{$current}->{bkg_eshift};
171 my $step = $groups{$current}->{bkg_step};
172 my $yoff = $groups{$current}->{plot_yoffset};
173 my $command = "## make convoluted spectrum:\n";
174 if ($$rhash{noise}) {
175 $command .= "set(c___onv_nn = npts($group.energy),\n";
176 $command .= " c___onv_noise = $$rhash{noise})\n";
177 $command .= "random(output=$group.random, npts=c___onv_nn, dist=normal, sigma=c___onv_noise)\n";
178 if ($$rhash{econv} == 0) {
179 $command .= "set c___onv.y = $group.$suff+$group.random\n";
180 } else {
181 $command .= "set c___onv.y = $function($group.energy, $group.$suff, $$rhash{econv})+$group.random\n";
182 };
183 } else {
184 if ($$rhash{econv} == 0) {
185 $command .= "set c___onv.y = $group.$suff\n";
186 } else {
187 $command .= "set c___onv.y = $function($group.energy, $group.$suff, $$rhash{econv})\n";
188 };
189 };
190 $command .= "plot(\"$group.energy+$eshift\", \"c___onv.y+$yoff\", key=$key, style=lines, color=$color)\n",
191 $groups{$current}->plotE('emn', $dmode, \%plot_features, \@indicator);
192 $groups{$current}->dispose($command, $dmode);
193 $widget{conv_group}->configure(-state=>'normal');
194 Echo("Convoluting ... done!");
195 $top->Unbusy;
196 };
197
198
199
200 ## make a new data group out of the convolved function, make this an
201 ## xmu group so it can be treated like normal data
202 sub convolve_group {
203 my $rhash = $_[0];
204 my $group = $groups{$current}->{group};
205 my ($new, $label) = group_name("Conv ".$$rhash{econv}." ".$group);
206 $groups{$new} = Ifeffit::Group -> new(group=>$new,
207 label=>"Conv " . $$rhash{econv} . $groups{$current}->{label});
208 $groups{$new} -> set_to_another($groups{$group});
209 $groups{$new} -> make(is_xmu => 1, is_chi => 0, is_rsp => 0,
210 is_qsp => 0, is_bkg => 0, is_nor => 1,
211 not_data => 0);
212 $groups{$new} -> make(bkg_e0 => $groups{$group}->{bkg_e0});
213 $groups{$new} -> make(file => "$$rhash{function} conv. of $groups{$current}->{label} by $$rhash{econv} volts with $$rhash{noise} noise");
214 $groups{$new}->{titles} = [];
215 push @{$groups{$new}->{titles}},
216 "$$rhash{function} convolution of $groups{$current}->{label} by $$rhash{econv} volts",
217 "with noise of $$rhash{noise} (compared to normalized spectrum)";
218 $groups{$new} -> put_titles;
219 my $sets = "set($new.energy = $group.energy,";
220 $sets .= " $new.xmu = c___onv.y)";
221 $groups{$new} -> dispose($sets, $dmode);
222 ++$line_count;
223 fill_skinny($list, $new, 1, 1);
224 project_state(0);
225 my $memory_ok = $groups{$new}
226 -> memory_check($top, \&Echo, \%groups, $max_heap, 0, 0);
227 Echo("WARNING: Ifeffit is out of memory!"), return if ($memory_ok == -1);
228 Echo("Saved convolution of $groups{$current}->{label} as a new data group");
229 };
230
231
232 ## END OF CONVOLUTION SUBSECTION
233 ##########################################################################################