File: HXMA.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 (140 lines) | stat: -rw-r--r-- 3,786 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
package Demeter::Plugins::HXMA;  # -*- cperl -*-

use Moose;
extends 'Demeter::Plugins::FileType';

has '+is_binary'   => (default => 0);
has '+description' => (default => "the HXMA and SXRMB beamlines at the CLS");
has '+version'     => (default => 0.1);
has 'beamline'     => (is => 'rw', isa => 'Str', default => q{});
has '+metadata_ini' => (default => File::Spec->catfile(File::Basename::dirname($INC{'Demeter.pm'}), 'Demeter', 'share', 'xdi', 'hxma.ini'));

sub is {
  my ($self) = @_;
  open D, $self->file or die "could not open " . $self->file . " as data (HXMA)\n";
  my $first = <D>;
  close D, return 1 if ($first =~ m{CLS Data Acquisition});
  close D;
  return 0;
};

sub fix {
  my ($self) = @_;

  my $file = $self->file;
  my $new = File::Spec->catfile($self->stash_folder, $self->filename);
  ($new = File::Spec->catfile($self->stash_folder, "toss")) if (length($new) > 127);
  open D, $file or $self->Croak("could not open $file as data (fix in HXMA)\n");
  open N, ">".$new or die "could not write to $new (fix in HXMA)\n";

  my $first = 1;
  my $identified_file_contents = 0;

  my $found_headers = 0;
  my ($energy, $lytle, $i0, $it, $ir) = (0,0,0,0,0);
  while (<D>) {
    if ((not $found_headers) and ($_ =~ m{\"Event-ID\"})) {
      $found_headers = 1;
      my @headers = split(/\s+/, $_);
      foreach my $i (1 .. $#headers) {
   	($energy = $i-1) if ($headers[$i] =~ m{Energy:sp});
   	($lytle  = $i-1) if ($headers[$i] =~ m{mcs03:fbk});
   	($i0     = $i-1) if ($headers[$i] =~ m{mcs04:fbk});
   	($it     = $i-1) if ($headers[$i] =~ m{mcs05:fbk});
   	($ir     = $i-1) if ($headers[$i] =~ m{mcs06:fbk});
      };
      $identified_file_contents = 1;
    };

    if ($_ !~ m{^\#}) {
      my @data = split(/,?\s+/, $_);
      if ($first) {

	printf N "# %s %s demystified:\n", $self->beamline, $file;
	print N "# ", "-" x 60, $/;
	if (($self->beamline eq 'HXMA') and $identified_file_contents) {
	  print N "# Energy        I0        It        Ir        Lytle$/";
	} else {
	  print N "# energy ", join(" ", (2..$#data)), $/;
	};
	$first = 0;
      };

      if (($self->beamline eq 'HXMA') and $identified_file_contents) {
	printf N "  %s  %s  %s  %s  %s$/",
	  $data[$energy], $data[$i0], $data[$it], $data[$ir], $data[$lytle];
      } else {
	print N join(" ", @data[1..$#data]), $/;
      };
    } elsif ($_ =~ m{BL1606-B}) {
      $self->beamline('SXRMB');
    } elsif ($_ =~ m{BL1606-I}) {
      $self->beamline('HXMA');
    };
  };

  close N;
  close D;
  $self->fixed($new);
  return $new;
};

sub suggest {
  my ($self, $which) = @_;
  $which ||= 'transmission';
  if ($which eq 'transmission') {
    return (energy      => '$1',
	    numerator   => '$2',
	    denominator => '$3',
	    ln          =>  1,);
  } else {
    return (energy      => '$1',
	    numerator   => '$5',
	    denominator => '$2',
	    ln          =>  0,);
  };
};



__PACKAGE__->meta->make_immutable;
1;
__END__

=head1 NAME

Demeter::Plugin::HXMA - Demystify files from the HXMA beamline at the CLS

=head1 VERSION

This documentation refers to Demeter version 0.9.26.

=head1 SYNOPSIS

This plugin strips the many columns not normally needed from a file
from the CLS HXMA beamline.  Most significantly, this strips the
leading 1 from every line of data, a feature which confuses Athena's
column selection dialog.  It also chooses the Energy:sp column as the
energy axis.

=head1 Methods

=over 4

=item C<is>

Recognize the HXMA file by the first line, which contains the phrase
"CLS Data Acquisition".

=item C<fix>

Strip out all columns except for energy, I0, I1, I2, and the Lytle
detector.  Also write sensible column labels to the output data file.

=back

=head1 AUTHOR

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