File: Rebin.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 (286 lines) | stat: -rw-r--r-- 9,646 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package Demeter::UI::Athena::Rebin;

use strict;
use warnings;

use Wx qw( :everything );
use base 'Wx::Panel';
use Wx::Event qw(EVT_BUTTON EVT_CHOICE EVT_TEXT_ENTER);
use Wx::Perl::TextValidator;

use Demeter::UI::Wx::SpecialCharacters qw(:all);

use Scalar::Util qw(looks_like_number);

use vars qw($label);
$label = "Rebin data";

my $tcsize = [60,-1];

sub new {
  my ($class, $parent, $app) = @_;
  my $this = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize, wxMAXIMIZE_BOX );

  my $box = Wx::BoxSizer->new( wxVERTICAL);
  $this->{sizer}  = $box;
  $this->{rebinned} = q{};;

  my $gbs = Wx::GridBagSizer->new( 3, 3 );

  my $label = Wx::StaticText->new($this, -1, 'Rebinning');
  $gbs->Add($label, Wx::GBPosition->new(0,0));

  $label = Wx::StaticText->new($this, -1, 'Edge energy');
  $gbs->Add($label, Wx::GBPosition->new(1,0));

  $label = Wx::StaticText->new($this, -1, 'Edge region');
  $gbs->Add($label, Wx::GBPosition->new(2,0));

  $label = Wx::StaticText->new($this, -1, 'Pre-edge grid');
  $gbs->Add($label, Wx::GBPosition->new(3,0));

  $label = Wx::StaticText->new($this, -1, 'XANES grid');
  $gbs->Add($label, Wx::GBPosition->new(4,0));

  $label = Wx::StaticText->new($this, -1, 'EXAFS grid');
  $gbs->Add($label, Wx::GBPosition->new(5,0));



  $label = Wx::StaticText->new($this, -1, 'to');
  $gbs->Add($label, Wx::GBPosition->new(2,3));

  $label = Wx::StaticText->new($this, -1, 'eV');
  $gbs->Add($label, Wx::GBPosition->new(2,5));

  $label = Wx::StaticText->new($this, -1, 'eV');
  $gbs->Add($label, Wx::GBPosition->new(3,3), Wx::GBSpan->new(1,2));

  $label = Wx::StaticText->new($this, -1, 'eV');
  $gbs->Add($label, Wx::GBPosition->new(4,3), Wx::GBSpan->new(1,2));

  $label = Wx::StaticText->new($this, -1, "$ARING$MACRON$ONE");
  $gbs->Add($label, Wx::GBPosition->new(5,3), Wx::GBSpan->new(1,2));

  $this->{abs}   = Wx::StaticText->new($this, -1, q{}, wxDefaultPosition, [60,-1]);
  $this->{edge}   = Wx::StaticText->new($this, -1, q{}, wxDefaultPosition, [60,-1]);
  foreach my $w (qw(emin emax pre xanes exafs)) {
    my $value = Demeter->co->default('rebin', $w) || Demeter->co->demeter('rebin', $w);
    $this->{$w}  = Wx::TextCtrl->new($this, -1, $value, wxDefaultPosition, [120,-1], wxTE_PROCESS_ENTER);
    my $re = ($w eq 'emin') ? qr([-0-9.]) : qr([0-9.]);
    $this->{$w} -> SetValidator( Wx::Perl::TextValidator->new( $re ) );
    EVT_TEXT_ENTER($this, $this->{$w}, sub{$this->plot($app->current_data)});
  };
  $gbs->Add($this->{abs},   Wx::GBPosition->new(0,2), Wx::GBSpan->new(1,3));
  $gbs->Add($this->{edge},  Wx::GBPosition->new(1,2));
  $gbs->Add($this->{emin},  Wx::GBPosition->new(2,2));
  $gbs->Add($this->{emax},  Wx::GBPosition->new(2,4));
  $gbs->Add($this->{pre},   Wx::GBPosition->new(3,2));
  $gbs->Add($this->{xanes}, Wx::GBPosition->new(4,2));
  $gbs->Add($this->{exafs}, Wx::GBPosition->new(5,2));

  $box->Add($gbs, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 2);

  $this->{replot} = Wx::Button->new($this, -1, 'Plot data and rebinned data',       wxDefaultPosition, $tcsize);
  $this->{plotk}  = Wx::Button->new($this, -1, 'Plot data and rebinned data in k',  wxDefaultPosition, $tcsize);
  $this->{make}   = Wx::Button->new($this, -1, 'Make rebinned data group',          wxDefaultPosition, $tcsize);
  $this->{marked} = Wx::Button->new($this, -1, 'Rebin marked data and make groups', wxDefaultPosition, $tcsize);
  $box -> Add($this->{$_}, 0, wxGROW|wxALL, 2) foreach (qw(replot plotk make marked));

  EVT_BUTTON($this, $this->{replot}, sub{$this->plot($app->current_data, 'E')});
  EVT_BUTTON($this, $this->{plotk},  sub{$this->plot($app->current_data, 'k')});
  EVT_BUTTON($this, $this->{make},   sub{$this->make($app)});
  EVT_BUTTON($this, $this->{marked}, sub{$this->marked($app)});

  my $textbox        = Wx::StaticBox->new($this, -1, 'Feedback', wxDefaultPosition, wxDefaultSize);
  my $textboxsizer   = Wx::StaticBoxSizer->new( $textbox, wxVERTICAL );
  $box              -> Add($textboxsizer, 1, wxBOTTOM|wxGROW, 5);
  $this->{feedback}  = Wx::TextCtrl->new($this, -1, q{}, wxDefaultPosition, wxDefaultSize,
					 wxTE_MULTILINE|wxTE_READONLY|wxTE_RICH2);
  $this->{feedback} -> SetFont( Wx::Font->new( 9, wxTELETYPE, wxNORMAL, wxNORMAL, 0, "" ) );
  $textboxsizer     -> Add($this->{feedback}, 1, wxGROW|wxALL, 5);

  #$box->Add(1,1,1);

  $this->{document} = Wx::Button->new($this, -1, 'Document section: rebinning');
  $box -> Add($this->{document}, 0, wxGROW|wxALL, 2);
  EVT_BUTTON($this, $this->{document}, sub{  $app->document("process.rebin")});

  $this->SetSizerAndFit($box);
  return $this;
};

sub pull_values {
  my ($this, $data) = @_;
  1;
};
sub push_values {
  my ($this, $data) = @_;
  $this->Enable(1);
  $this->{abs}  -> SetLabel($data->name);
  $this->{edge} -> SetLabel($data->bkg_e0);
  foreach my $w (qw(emin emax pre xanes exafs)) {
    my $key = 'rebin_'.$w;
    my $value = $this->{$w}->GetValue;
    $data->co->set_default('rebin', $w, $value);
  };
  delete $this->{rebinned};
  if ($data->rebinned) {
    $this->Enable(0);
  } elsif ($data->datatype eq 'chi') {
    $this->Enable(0);
  } else {
    #$this->{rebinned} = $data->rebin;
    $this->plot($data, 'E') if not $::app->{plotting};
  };
};
sub mode {
  my ($this, $data, $enabled, $frozen) = @_;
  1;
};

sub plot {
  my ($this, $data, $space) = @_;
  $this->{feedback}->Clear;
  my %hash;
  foreach my $w (qw(emin emax pre xanes exafs)) {
    my $key = 'rebin_'.$w;
    my $value = $this->{$w}->GetValue;
    if (not looks_like_number($value)) {
      $::app->{main}->status("Not rebinning -- your value for $w is not a number!", 'error|nobuffer');
      return;
    };
    $hash{$w} = $value;
    #$data->co->set_default('rebin', $w, $value);
  };
  $this->{rebinned} = $data->rebin(\%hash);
  $::app->{main}->{PlotE}->pull_single_values;
  $data->po->set(e_mu=>1, e_markers=>1, e_bkg=>0, e_pre=>0, e_post=>0, e_norm=>0, e_der=>0, e_sec=>0, e_i0=>0, e_signal=>0);
  $data->po->start_plot;
  $_->plot($space) foreach ($data, $this->{rebinned});
  my @datax = $data->get_array('energy');
  my @binx  = $this->{rebinned}->get_array('energy');
  my $text  = sprintf("Rebinned data from %d points to %d points with kernel width %d", $#datax+1, $#binx+1, Demeter->co->default('rebin', 'width'));
  $this->{feedback}->SetValue($text);
  $::app->{main}->status("Plotted ".$data->name." with its rebinned data");
  $::app->heap_check(0);
};

sub make {
  my ($this, $app) = @_;

  my %hash;
  foreach my $w (qw(emin emax pre xanes exafs)) {
    my $key = 'rebin_'.$w;
    my $value = $this->{$w}->GetValue;
    if (not looks_like_number($value)) {
      $app->{main}->status("Not rebinning -- your value for $w is not a number!", 'error|nobuffer');
      return;
    };
    $hash{$w} = $value;
    $app->current_data->co->set_default('rebin', $w, $value);
  };
  $this->{rebinned} = $app->current_data->rebin(\%hash);
  $this->{rebinned}->_update('all');

  my $index = $app->current_index;
  if ($index == $app->{main}->{list}->GetCount-1) {
    $app->{main}->{list}->AddData($this->{rebinned}->name, $this->{rebinned});
  } else {
    $app->{main}->{list}->InsertData($this->{rebinned}->name, $index+1, $this->{rebinned});
  };
  $app->{main}->status("Rebinned " . $app->current_data->name." and made a new data group");
  $app->modified(1);
  $app->heap_check(0);
};
sub marked {
  my ($this, $app) = @_;
  my $busy = Wx::BusyCursor->new();

  my %hash;
  foreach my $w (qw(emin emax pre xanes exafs)) {
    my $key = 'rebin_'.$w;
    my $value = $this->{$w}->GetValue;
    if (not looks_like_number($value)) {
      $app->{main}->status("Not rebinning marked groups -- your value for $w is not a number!", 'error|nobuffer');
      return;
    };
    $hash{$w} = $value;
    $app->current_data->co->set_default('rebin', $w, $value);
  };

  my $count = 0;
  my $index = $app->current_index;
  foreach my $j (reverse (0 .. $app->{main}->{list}->GetCount-1)) {
    if ($app->{main}->{list}->IsChecked($j)) {
      $this->{rebinned} = $app->{main}->{list}->GetIndexedData($j)->rebin(\%hash);
      if ($index == $app->{main}->{list}->GetCount-1) {
	$app->{main}->{list}->AddData($this->{rebinned}->name, $this->{rebinned});
      } else {
	$app->{main}->{list}->InsertData($this->{rebinned}->name, $j+1, $this->{rebinned});
      };
      ++$count;
    };
  };
  $this->{rebinned} = $app->current_data->rebin;
  undef $busy;
  return if not $count;
  $app->modified(1);
  $app->{main}->status("Made rebinned groups from $count marked groups.");
};
1;


=head1 NAME

Demeter::UI::Athena::Rebin - A rebinning tool for continuous scan data for Athena

=head1 VERSION

This documentation refers to Demeter version 0.9.26.

=head1 SYNOPSIS

This module provides a tool for rebinning continuous scan data onto a
standard EXAFS energy grid.

=head1 CONFIGURATION


=head1 DEPENDENCIES

Demeter's dependencies are in the F<Build.PL> file.

=head1 BUGS AND LIMITATIONS

=over 4

=item *

This 'n' that

=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