Debian Codegraph @

artemis_parts/plot.pl

raw
1 # -*- cperl -*-
2 ## This file is part of Artemis, copyright (c) 2002-2006 Bruce Ravel
3 ##
4
5 ###===================================================================
6 ### plotting subsystem
7 ###===================================================================
8
9 sub plot {
10 my $data = $paths{$current}->data;
11 #unless ($list -> info('exists', $paths{$data}->{id}.".0")) {
12 Echo("You're last plot was an Energy plot."), return "" if (lc($_[0]) eq 'e');
13 Echo("You have not opened a data file or a feff calculation yet."), return ""
14 unless ($n_data or $n_feff);
15 #};
16 ## need a foreach loop for multiple data sets
17 my ($data_requested, $data_there) = (0, 1);
18 foreach my $d ($list->info('selection')) {
19 next unless (ref($paths{$d}) =~ /Ifeffit/);
20 next unless ($paths{$d}->type =~ /(bkg|data|diff|fit|res)/);
21 ++$data_requested;
22 if ($paths{$d}->type eq 'data') {
23 $data_there &&= ((-e $paths{$d}->get('file')) or ($paths{$d}->get('file') eq ""));
24 } else {
25 my $this = $paths{$d}->data;
26 $data_there &&= ((-e $paths{$this}->get('file')) or ($paths{$this}->get('file') eq ""));
27 };
28 };
29 ## if ($data_requested and $n_data and (not $data_there)) {
30 ## Echo("You have not yet loaded data or your data file does not exist. Try \"Change data file\" in the Data menu.");
31 ## return "";
32 ## };
33 ## this is flawed (do a foreach loop)
34 my $feff_requested = grep(/(feff\d+)\.\d+/, $list->info('selection'));
35 my $feffinp_exist = 0;
36 foreach ($list->info('selection')) {
37 next unless (ref($paths{$_}) =~ /Ifeffit/);
38 next unless ($paths{$_}->type eq 'path');
39 $feffinp_exist ||= (-e $paths{$_}->get("feff.inp"));
40 };
41 if ($feff_requested and $n_feff and not $feffinp_exist) {
42 Echo("Your feff calculation does not exist. Perhaps you chould change the path.");
43 return "";
44 };
45 if (not $feff_requested and not $data_requested) {
46 #Echo("You have not selected any form of data or any paths for plotting.");
47 #return "";
48 my $first = &first_data;
49 $list->selectionClear;
50 $list->selectionSet($first);
51 $list->selectionSet($list->info('next', $first));
52 };
53 my $space = lc($_[0]);
54 Echo("Plotting in $space space ... ") unless $_[1];
55 my $after_fit = $_[2];
56 $top -> Busy();
57 my $command = '';
58
59 my $param_err .= &verify_data_parameters;
60 if ($param_err) {
61 $param_err = "There were errors among the data parameters:\n\n" . $param_err;
62 post_message($param_err, "Error Messages");
63 Error("plot aborted due to errors in data parameters");
64 $top->Unbusy;
65 return;
66 };
67
68 ## need variables for paths
69 if ($feff_requested) {
70 my $error = "";
71 $error .= &verify_parens;
72 if ($error) {
73 post_message($error, "Error Messages");
74 Error("plot aborted due to errors in math expressions");
75 $top->Unbusy;
76 return;
77 };
78 ##$command .= &erase_all_variables;
79 ##&read_gsd(1); # update gsd object
80 if ($parameters_changed) {
81 map { $command .= $_ -> write_gsd } (@gds);
82 $parameters_changed = 0;
83 };
84 };
85
86 ## before plotting, adjust the selection so that if the head of a
87 ## fit branch is requested for plotting, it is deselected and the
88 ## latest fit is selected instead
89 foreach my $p ($list->info('selection')) {
90 next unless ($paths{$p}->type eq 'fit');
91 next if $paths{$p}->get('parent');
92 next unless $paths{$p}->get('thisfit'); # this keeps it from
93 # plotzing when writing a
94 # script without actually
95 # running
96 $list->selectionClear($p);
97 $list->selectionSet($paths{$p}->get('thisfit'));
98 };
99 $top -> update;
100
101 SWITCH: {
102 ($space eq 'k') and do {
103 $command .= $paths{$data} -> plot_k($list, \%plot_features, \@extra, $stash_dir);
104 $last_plot = 'k';
105 last SWITCH;
106 };
107 ($space eq 'r') and do {
108 $command .= $paths{$data} -> plot_R($list, \%plot_features, \@extra, $stash_dir);
109 $last_plot = 'r';
110 last SWITCH;
111 };
112 ($space eq 'q') and do {
113 $command .= $paths{$data} -> plot_q($list, \%plot_features, \@extra, $stash_dir);
114 $last_plot = 'q';
115 last SWITCH;
116 };
117 $command .= $paths{$data} -> plot_R($list, \%plot_features, \@extra, $stash_dir);
118 $last_plot = 'r';
119 };
120 return $command if $_[1];
121
122 #$notebook -> raise('ifeffit');
123 $paths{gsd} -> dispose($command, $dmode);
124 $paths{gsd} -> dispose($extra[6], $dmode) if $extra[5]; # indicators
125 $extra[6] = "";
126 #$config{general}{plot_tab} and $plotsel -> raise($space);
127
128 $top->Unbusy;
129 $command and Echo(@done);
130
131 };
132
133
134 sub replot {
135 my $mode = $_[0]; # replot, print, or device_type
136 Echo("You have not yet plotted anything."), return 0 unless ($last_plot);
137 my ($title, $suf, $dev);
138 SWITCH: {
139 ($mode eq 'replot') and do {
140 $paths{gsd}->dispose($Ifeffit::Path::last_plot_command, 1);
141 Echo("Unzoomed.");
142 return;
143 };
144 ($mode =~ /(gif|png)/) and do {
145 ($title, $suf, $dev) = ('Artemis: '.uc($1).' file name', $1, $mode );
146 last SWITCH;
147 };
148 ($mode =~ /ps/) and do {
149 ($title, $suf, $dev) = ('Artemis: Postscript file name', 'ps', $mode);
150 last SWITCH;
151 };
152 ($mode eq 'print') and do {
153 ($title, $suf, $dev) = ('', 'ps', $config{general}{ps_device});
154 last SWITCH;
155 };
156 ($title, $suf, $dev) = ('Athena: Image file name', 'img', $mode);
157 };
158 my $path = $current_data_dir || Cwd::cwd;
159 if ($mode eq 'print') {
160 if ($is_windows) {
161 Echo("Printing under Windows is not yet supported");
162 } else {
163 ## this is alarmingly crufty!
164 my $tmp = '...artemis.tmp';
165 $paths{gsd}->dispose("plot(device=\"$dev\", file=\"$tmp\")\n", 7);
166 local $| = 1;
167 my $to = $config{general}{print_spooler};
168 Echo("Sending image to printer via the \"$to\" command");
169 open OUT, "| $to" or
170 die "could not open pipe to $to\n";
171 open IN, $tmp or die "could not open temp file for printing";
172 while (<IN>) { print OUT; };
173 close IN; close OUT; unlink $tmp;
174 };
175 return;
176 } else {
177 ##local $Tk::FBox::a;
178 ##local $Tk::FBox::b;
179 my $types = [["$suf image files", $suf],
180 ['All Files', '*'],];
181 my $file = $top -> getSaveFile(-defaultextension=>$suf,
182 -filetypes=>$types,
183 #(not $is_windows) ?
184 # (-sortcmd=>sub{$Tk::FBox::a cmp $Tk::FBox::b}) : () ,
185 -initialdir=>$path,
186 -initialfile=>"artemis.$suf",
187 -title => $title);
188 if ($file) {
189 ## make sure I can write to $file
190 open F, ">".$file or do { Error("You cannot write to \"$file\"."); return };
191 close F;
192 my ($name, $pth, $suffix) = fileparse($file);
193 $current_data_dir = $pth;
194 Echo("saving image to $file");
195 $paths{gsd}->dispose("plot(device=\"$dev\", file=\"$file\")\n", 7);
196 };
197 };
198 };
199
200
201 sub zoom {
202 Echo('No data'), return unless $current;
203 Echo("You have not yet plotted anything."),
204 return unless $last_plot;
205 Echo('Click corners to zoom');
206 $paths{gsd}->dispose('zoom', 1);
207 Echo('Zooming done!');
208 };
209
210 sub cursor {
211 Echo('No data'), return unless $current;
212 Echo("You have not yet plotted anything."),
213 return unless $last_plot;
214 Echo('Click on a point');
215 $paths{gsd}->dispose('cursor(crosshair=true)', 1);
216 Echo(sprintf("You selected x=%f y=%f",
217 Ifeffit::get_scalar("cursor_x"),
218 Ifeffit::get_scalar("cursor_y")));
219 };
220
221
222
223 sub keyboard_plot {
224 my $who = $top->focusCurrent;
225 $multikey = "";
226 Echo("Plot Current group: specify plot space (k r q)");
227 $echo -> focus();
228 $echo -> grab;
229 $echo -> waitVariable(\$multikey);
230 $echo -> grabRelease;
231 $who -> focus;
232 Echo("$multikey is not a plot space!"), return unless (lc($multikey) =~ /^[kqr]$/);
233 SWITCH: {
234 &plot('k', 0), last SWITCH if (lc($multikey) eq 'k');
235 &plot('r', 0), last SWITCH if (lc($multikey) eq 'r');
236 &plot('q', 0), last SWITCH if (lc($multikey) eq 'q');
237 };
238 };
239
240
241 sub select_all {
242 foreach my $p (keys %paths) {
243 next unless (ref($paths{$p}) =~ /Ifeffit/);
244 my $pp = $p;
245 ## ($pp = $1 . "_" . ("fit", "res", "bkg")[$2]) if $p =~ /(data\d)\.(\d)/;
246 next if ($paths{$pp}->type eq 'feff');
247 next if ($paths{$pp}->type eq 'gsd');
248 next unless ($list->infoExists($paths{$p}->get('id')));
249 next if (($paths{$pp}->type eq 'path') and not ($paths{$pp}->get('include')));
250 $list -> selectionSet($paths{$p}->get('id'));
251 };
252 };
253
254
255 sub deselect_all {
256 $list -> selectionClear;
257 $list -> selectionSet($current);
258 $list -> anchorSet($current);
259 };
260
261 ## END OF THE PLOTTING SUBSYSTEM
262