File: SingleFile.pm

package info (click to toggle)
libdemeter-perl 0.9.27%2Bds6-9
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 74,028 kB
  • sloc: perl: 73,233; python: 2,196; makefile: 1,999; ansic: 1,368; lisp: 454; sh: 74
file content (267 lines) | stat: -rw-r--r-- 7,292 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package Demeter::Plot::SingleFile;

=for Copyright
 .
 Copyright (c) 2006-2019 Bruce Ravel (http://bruceravel.github.io/home).
 All rights reserved.
 .
 This file is free software; you can redistribute it and/or
 modify it under the same terms as Perl itself. See The Perl
 Artistic License.
 .
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

=cut

use autodie qw(open close);

use Moose;
extends 'Demeter::Plot';

has 'file'     => (is => 'rw', isa => 'Str',  default => q{});
has '+backend' => (default => q{singleplot});

has 'columns' => (
		    traits    => ['Array'],
		    is        => 'rw',
		    isa       => 'ArrayRef[Str]',
		    default   => sub { [] },
		    handles   => {
				  'add_columns'  => 'push',
				  'remove_columns'   => 'pop',
				  'clear_columns' => 'clear',
				 }
		   );
has 'labels' => (
		    traits    => ['Array'],
		    is        => 'rw',
		    isa       => 'ArrayRef[Str]',
		    default   => sub { [] },
		    handles   => {
				  'add_labels'  => 'push',
				  'remove_labels'   => 'pop',
				  'clear_labels' => 'clear',
				 }
		   );

after start_plot => sub {
  my ($self) = @_;
  $self->cleantemp;
  if ($self->mo->standard) {
    my $command = $self->template("plot", "start");
    $self->dispose($command, "plotting");
  };
  $self->lastplot(q{});
  return $self;
};

sub prep {
  my ($self, @rest) = @_;
  my %args = @rest;
  $args{standard} = $args{data} if not $args{standard};
  die "Missing filename in Demeter::Plot::SingleFile setup"               if (not $args{file});
#  die "Cannot write to filename in Demeter::Plot::SingleFile setup"       if (not -w $args{file});
  die "Missing standard in Demeter::Plot::SingleFile setup"               if (not $args{standard});
  die "Standard must be Demeter::Data in Demeter::Plot::SingleFile setup" if (ref($args{standard}) ne 'Demeter::Data');

  $args{space} = 'q' if (lc($args{space}) eq 'kq');
  $self->space($args{space}) if $args{space};
  $args{standard}->standard;
  $self->file($args{file});
  $self->start_plot;
  return $self;
};

override after_plot_hook => sub {
  my ($self, $data, $part) = @_;
  $part ||= q{};
  $self->add_columns($self->fetch_string('p___lot_string'));
  if ($part eq 'win') {
    $self->add_labels('window');
  } else {
    my $label = $data->name || $data->group;
    $label =~ s{[-,= \t]+}{_}g;
    $label .= "_fit"        if ($part eq 'fit');
    $label .= "_residual"   if ($part eq 'res');
    $label .= "_background" if ($part eq 'bkg');
    $label .= "_running"    if ($part eq 'run');
    $self->add_labels($label);
  };
  return $self;
};

override finish => sub {
  my ($self) = @_;
  my $command = $self->template("plot", "end");
  $self->dispose($command, "plotting");
  print "Wrote plot to \"".$self->file."\"\n" if ($self->mo->ui eq 'screen');
  return $self;
};

override end_plot => sub {
  my ($self) = @_;
  $self->cleantemp;
  return $self;
};


1;

=head1 NAME

Demeter::Plot::SingleFile - Sending a plot to a single file

=head1 VERSION

This documentation refers to Demeter version 0.9.26.

=head1 SYNOPSIS

This is used to send a plot to a single file for easy import into an
external plotting program.

  #!/usr/bin/perl
  use Demeter qw(:ui=screen);

  my $file = 'examples/data/fe.060';
  my $data = Demeter::Data->new(file=>$file,);
  $data -> set(fft_kmax    => 3,    fft_kmin  => 14,
	       bkg_spl2    => 18,
	       bkg_nor2    => 1800,
	       energy      => '$1',
	       numerator   => '$2',
	       denominator => '$3',
	       ln          => 1,
	      );

  ## set up the plot to output file
  $data->plot_with('singlefile');  # 1

  ## prep for the singlefile plot
  $data->po->prep(file=>"foo.dat", standard=>$data, space=>'k');

  ## prep is the same as doing the following
  #$data->standard;                 # 2
  #$data->po->space('k');           # 3
  #$data->po->file("foo.dat");      # 4
  #$data->po->start_plot;

  ## make a sequence of plots
  $data->po->set(kweight => 1, kmax => 17, space => 'k');
  $data->set(plot_multiplier => 5,   'y_offset'=>14, name=>'kw=1, scaled by 5');
  $data->plot;

  $data->po->kweight(2);
  $data->set(plot_multiplier => 1,   'y_offset'=>7,  name=>'kw=2, unscaled');
  $data->plot;

  $data->po->kweight(3);
  $data->set(plot_multiplier => 0.2, 'y_offset'=>0,  name=>'kw=3, scaled by 0.2');
  $data->plot;

  ## the finish method of the Demeter::Plot::SingleFile actually writes out the file
  $data->po->finish;               # 5

=head1 DESCRIPTION

This plotting backend does not work quite as transparently as the
other plotting backends.  In order to get the figure you actually want
written correctly to an output file, a bit more care is required than
for this than for other plotting backends.  The following steps
(marked by their numbers in the example above) must be taken when
using this plotting backend:

=over 4

=item 1.

You must use the C<plot_with> method to change to this plotting
backend.  You B<may not> use the C<:plotwith=> pragma at the top of
your script to generate a column data file -- you must explicitly
switch to this backend in the run-time part of your script.  If you
try to use the pragma, your script will fail with a confusing and
misleading error message.

Note that the C<start_plot> method gets called automatically when you
change plotting backends.

=item 2.

You B<must> set a data standard.  This is required to correctly set
the column containing the x-axis and is particularly important in
energy plots where some data might need to be interpolated onto the
correct grid.

=item 3.

It is a good idea to explicitly declare the plotting space before
calling C<start_plot>.  This is particularly true for kq plots, for
which you should set the space to C<q>.

=item 4.

You B<must> set the C<file> attribute to the name of the target output
file which will contain the data required to replicate the plot in
another program.  You should set this before starting to actually
generate the plot.

=item 2-4.

Alternately, you can use the C<prep> method of the SingleFile object
to do steps 2 through 4 in one swoop.

=item 5.

Calling the C<finish> method of the SingleFile backend is what
actually writes the output file to disk.

=back

In a GUI, of course, all these steps will be handled behind the
scenes.

=head1 BUGS AND LIMITATIONS

=over 4

=item *

markers and indicators

=item *

quadplot -- need to write out an 8 column file (e mu k chik r chir q chiq)

=item *

stddev and variance, filter, f1f2

=back

Please report problems to the Ifeffit Mailing List
(L<http://cars9.uchicago.edu/mailman/listinfo/ifeffit/>)

Patches are welcome.

=head1 AUTHOR

Bruce Ravel, L<http://bruceravel.github.io/home>

L<http://bruceravel.github.io/demeter/>


=head1 LICENCE AND COPYRIGHT

Copyright (c) 2006-2019 Bruce Ravel (L<http://bruceravel.github.io/home>). All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlgpl>.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

=cut