File: Dispersive.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 (329 lines) | stat: -rw-r--r-- 11,415 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package Demeter::UI::Athena::Dispersive;

use strict;
use warnings;

use Wx qw( :everything );
use base 'Wx::Panel';
use Wx::Event qw(EVT_BUTTON EVT_CHECKBOX EVT_FILEPICKER_CHANGED);

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

use Cwd;

use vars qw($label);
$label = "Calibrate dispersive XAS data";

my $tcsize = [120,-1];

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

  $this->{pixel} = q{};
  $this->{yaml} = File::Spec->catfile(Demeter->dot_folder, 'athena.dxas');

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

  if (not Demeter->co->default('athena', 'show_dispersive')) {
    $box->Add(Wx::StaticText->new($this, -1, "Dispersive data calibration is disabled at this time."),
	      0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5);
    $box->Add(1,1,1);
  } else {

    ################################################################################
    ## show calibration standard
    my $hbox = Wx::BoxSizer->new( wxHORIZONTAL);
    $hbox -> Add(Wx::StaticText->new($this, -1, 'Calibration standard: '), 0, wxALL, 3);
    $this->{group}   = Wx::StaticText->new($this, -1, q{});
    $hbox -> Add($this->{group}, 0, wxGROW|wxALL, 3);
    $box -> Add($hbox, 0, wxGROW|wxALL, 5);


    $hbox = Wx::BoxSizer->new( wxHORIZONTAL);
    $box -> Add($hbox, 0, wxGROW|wxALL, 5);
    my $leftbox = Wx::BoxSizer->new( wxVERTICAL);
    $hbox -> Add($leftbox, 0, wxGROW|wxALL, 5);

    $this->{beamline} = Wx::RadioBox->new($this, -1, 'Beamline', wxDefaultPosition, wxDefaultSize,
					  ['SLRI BL5', 'ESRF ID24',], 1, wxRA_SPECIFY_COLS);
    $leftbox->Add($this->{beamline}, 0, wxGROW|wxALL, 5);

    my $rightbox = Wx::BoxSizer->new( wxVERTICAL);
    $hbox -> Add($rightbox, 1, wxGROW|wxALL, 5);

    ################################################################################
    ## control for importing dispersive standard
    my $dispbox       = Wx::StaticBox->new($this, -1, 'Import dispersive standard', wxDefaultPosition, wxDefaultSize);
    my $dispboxsizer  = Wx::StaticBoxSizer->new( $dispbox, wxVERTICAL );
    ##$dispbox         -> SetFont( Wx::Font->new( Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)->GetPointSize, wxDEFAULT, wxNORMAL, wxNORMAL, 0, "" ) );

    $this->{import}  = Wx::FilePickerCtrl->new($this, -1, cwd, 'Select a file', "*",
					       wxDefaultPosition, wxDefaultSize,
					       wxFLP_OPEN|wxFLP_FILE_MUST_EXIST|wxFLP_USE_TEXTCTRL|wxFLP_CHANGE_DIR);
    $dispboxsizer -> Add($this->{import}, 1, wxGROW|wxALL, 5);
    $rightbox -> Add($dispboxsizer, 0, wxGROW|wxALL, 5);


    ################################################################################
    ## controls for calibration parameters
    my $gbs = Wx::GridBagSizer->new( 5, 5 );

    $gbs->Add(Wx::StaticText->new($this, -1, 'Offset'),    Wx::GBPosition->new(0,0));
    $gbs->Add(Wx::StaticText->new($this, -1, 'Linear'),    Wx::GBPosition->new(1,0));
    $gbs->Add(Wx::StaticText->new($this, -1, 'Quadratic'), Wx::GBPosition->new(2,0));

    $this->{offset}    = Wx::TextCtrl->new($this, -1, 0,  wxDefaultPosition, $tcsize, wxTE_PROCESS_ENTER);
    $this->{linear}    = Wx::TextCtrl->new($this, -1, 0,  wxDefaultPosition, $tcsize, wxTE_PROCESS_ENTER);
    $this->{quadratic} = Wx::TextCtrl->new($this, -1, 0,  wxDefaultPosition, $tcsize, wxTE_PROCESS_ENTER);
    $this->{constrain} = Wx::CheckBox->new($this, -1, "Constrain offset to linear term",);
    $this->{reset}     = Wx::Button  ->new($this, -1, "Reset parameters",);


    $gbs->Add($this->{offset},    Wx::GBPosition->new(0,1));
    $gbs->Add($this->{linear},    Wx::GBPosition->new(1,1));
    $gbs->Add($this->{quadratic}, Wx::GBPosition->new(2,1));
    #$gbs->Add($this->{constrain}, Wx::GBPosition->new(0,2));
    $gbs->Add($this->{reset},     Wx::GBPosition->new(1,2));

    $rightbox -> Add($gbs, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);


    ################################################################################
    ## actions
    $this->{refine} = Wx::Button->new($this, -1, 'Refine calibration parameters', wxDefaultPosition, $tcsize);
    $this->{replot} = Wx::Button->new($this, -1, 'Replot calibration data',       wxDefaultPosition, $tcsize);
    $this->{make}   = Wx::Button->new($this, -1, 'Make data group',               wxDefaultPosition, $tcsize);
    $box -> Add($this->{$_}, 0, wxGROW|wxALL, 5) foreach (qw(refine replot make));

    EVT_BUTTON  ($this, $this->{reset},     \&Reset);
    EVT_BUTTON  ($this, $this->{refine},    \&refine);
    EVT_BUTTON  ($this, $this->{replot},    \&replot);
    EVT_BUTTON  ($this, $this->{make},      \&make);
    EVT_CHECKBOX($this, $this->{constrain}, \&constrain);
    EVT_FILEPICKER_CHANGED($this, $this->{import}, \&set);
    map {$this->{$_}->Enable(0)} qw(reset refine replot make constrain);
    $this->{constrain}->Show(0);


    $box->Add(1,1,1);		# this spacer may not be needed, Journal.pm, for example
  };

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

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

## deprecated?
sub pull_values {
  my ($this, $data) = @_;
  1;
};

## this subroutine fills the controls when an item is selected from the Group list
sub push_values {
  my ($this, $data) = @_;
  return if not Demeter->co->default('athena', 'show_dispersive');
  $this->{group}->SetLabel($data->name);
  my $onoff = ($this->filecheck(1)) ? 1 : 0;
  map {$this->{$_}->Enable($onoff)} qw(reset refine replot make constrain);
  1;
};

## this subroutine sets the enabled/frozen state of the controls
sub mode {
  my ($this, $data, $enabled, $frozen) = @_;
  1;
};

## yes, there is some overlap between what push_values and mode do.
## This separation was useful in Main.pm.  Some of the other tools
## make mode a null op.

sub filecheck {
  my ($this, $quiet) = @_;
  $quiet ||= 0;
  my $file = $this->{import}->GetPath;
  if (($file =~ m{\A\s*\z}) or (-d $file)) {
    $::app->{main}->status("You have not yet specified a dispersive standard", 'error|nobuffer') if not $quiet;
    return 0;
  };
  if (not -e $file) {
    $::app->{main}->status("The specified dispersive standard does not exist", 'error|nobuffer') if not $quiet;
    return 0;
  };
  if (not -r $file) {
    $::app->{main}->status("The specified dispersive standard cannot be read", 'error|nobuffer') if not $quiet;
    return 0;
  };
  return $file;
};


sub set {
  my ($this, $event) = @_;

  my $file = $this->filecheck;
  return 0 if not $file;
  my $busy = Wx::BusyCursor->new();
  $::app->{main}->status("Processing calibration and dispersive standards and setting initial parameter guesses");
  my $nor2 = Demeter->co->default("dispersive", "bkg_nor2") || 1000;

  my @columns;
  if ($this->{beamline}->GetStringSelection =~ m{SLRI}) {
    @columns = (energy=>'$1', numerator=>'$2', denominator=>'$3', ln=>1);
  } elsif ($this->{beamline}->GetStringSelection =~ m{ESRF}) {
    @columns = (energy=>'$1', numerator=>'$2', denominator=>1, ln=>1);
  };

  $this->{pixel}      = Demeter::Data::Pixel->new(file=>$file, bkg_nor2=>$nor2, @columns);
  $this->{pixel}     -> standard($::app->current_data);
  $this->{pixel}     -> guess;
  $this->{offset}    -> SetValue(sprintf("%.6f", $this->{pixel}->offset));
  $this->{linear}    -> SetValue(sprintf("%.6f", $this->{pixel}->linear));
  $this->{quadratic} -> SetValue(sprintf("%.9f", $this->{pixel}->quadratic));
  map {$this->{$_}->Enable(1)} qw(reset refine replot make constrain);
  undef $busy;
  $this->replot(0);
};

sub Reset {
  my ($this, $event) = @_;
  $this->{pixel}->DEMOLISH;
  $this->set;
};

sub refine {
  my ($this, $event) = @_;
  my $file = $this->filecheck;
  return 0 if not $file;
  my $busy = Wx::BusyCursor->new();
  $::app->{main}->status("Refining dispersive calibration parameters");
  $this->{pixel}->file($file);
  $this->{pixel}->standard($::app->current_data);
  $this->{pixel}->offset($this->{offset} -> GetValue);
  $this->{pixel}->linear($this->{linear} -> GetValue);
  $this->{pixel}->quadratic($this->{quadratic} -> GetValue);
  $this->{pixel}->pixel;
  $this->{offset}    -> SetValue(sprintf("%.6f", $this->{pixel}->offset));
  $this->{linear}    -> SetValue(sprintf("%.6f", $this->{pixel}->linear));
  $this->{quadratic} -> SetValue(sprintf("%.9f", $this->{pixel}->quadratic));
  undef $busy;
  $this->replot(0);
};

sub replot {
  my ($this, $event) = @_;
  my $busy = Wx::BusyCursor->new();
  if ($event) {
    $this->{pixel}->offset($this->{offset} -> GetValue);
    $this->{pixel}->linear($this->{linear} -> GetValue);
    $this->{pixel}->quadratic($this->{quadratic} -> GetValue);
  };
  my $temp = $this->{pixel} -> apply;
  $this -> {pixel} -> standard -> po -> start_plot;
  $this -> {pixel} -> standard -> po -> set(e_mu=>1, e_norm=>1,
					    emin=>Demeter->co->default("dispersive", "emin"),
					    emax=>Demeter->co->default("dispersive", "emax"),
					    e_markers=>0, e_bkg=>0, e_pre=>0, e_post=>0, e_der=>0, e_sec=>0);
  $this -> {pixel} -> standard -> plot('E');
  $temp -> plot('E');
  $temp -> DEMOLISH;
  my $hash = {offset    => $this->{pixel}->offset,
	      linear    => $this->{pixel}->linear,
	      quadratic => $this->{pixel}->quadratic};
  my $string .= YAML::Tiny::Dump($hash);
  open(my $PERSIST, '>', $this->{yaml});
  print $PERSIST $string;
  close $PERSIST;

  $::app->{main}->status("Plotted calibration and dispersive standards");
  undef $busy;
};

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

  if ($event) {
    $this->{pixel}->offset($this->{offset} -> GetValue);
    $this->{pixel}->linear($this->{linear} -> GetValue);
    $this->{pixel}->quadratic($this->{quadratic} -> GetValue);
  };
  my $dxas = $this->{pixel} -> apply;

  my $index = $::app->current_index;
  if ($index == $::app->{main}->{list}->GetCount-1) {
    $::app->{main}->{list}->AddData($dxas->name, $dxas);
  } else {
    $::app->{main}->{list}->InsertData($dxas->name, $index+1, $dxas);
  };
  $::app->{main}->status("Did dispersion correction for " . $dxas->name." and made a new data group");
  $::app->modified(1);
  $::app->heap_check(0);
};

sub constrain {
  my ($this, $event) = @_;
  $::app->{main}->status("Nothing... (constrain)");
};


1;


=head1 NAME

Demeter::UI::Athena::Dispersive - A tool for calibrating dispersive XAFS data

=head1 VERSION

This documentation refers to Demeter version 0.9.26.

=head1 SYNOPSIS

This module provides a

=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