| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Lab::Moose::DataFile::Read; |
|
2
|
|
|
|
|
|
|
$Lab::Moose::DataFile::Read::VERSION = '3.900'; |
|
3
|
|
|
|
|
|
|
#ABSTRACT: Read a gnuplot-style 2D data file |
|
4
|
|
|
|
|
|
|
|
|
5
|
6
|
|
|
6
|
|
2590493
|
use v5.20; |
|
|
6
|
|
|
|
|
45
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
6
|
|
|
6
|
|
38
|
use warnings; |
|
|
6
|
|
|
|
|
14
|
|
|
|
6
|
|
|
|
|
188
|
|
|
8
|
6
|
|
|
6
|
|
37
|
use strict; |
|
|
6
|
|
|
|
|
16
|
|
|
|
6
|
|
|
|
|
164
|
|
|
9
|
6
|
|
|
6
|
|
33
|
use MooseX::Params::Validate 'validated_list'; |
|
|
6
|
|
|
|
|
27
|
|
|
|
6
|
|
|
|
|
67
|
|
|
10
|
6
|
|
|
6
|
|
1587
|
use Moose::Util::TypeConstraints 'enum'; |
|
|
6
|
|
|
|
|
16
|
|
|
|
6
|
|
|
|
|
57
|
|
|
11
|
6
|
|
|
6
|
|
2722
|
use List::Util qw//; |
|
|
6
|
|
|
|
|
34
|
|
|
|
6
|
|
|
|
|
139
|
|
|
12
|
6
|
|
|
6
|
|
47
|
use PDL; |
|
|
6
|
|
|
|
|
13
|
|
|
|
6
|
|
|
|
|
76
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
#use PDL::Core qw/pdl cat dog/; |
|
15
|
6
|
|
|
6
|
|
20885
|
use Fcntl 'SEEK_SET'; |
|
|
6
|
|
|
|
|
18
|
|
|
|
6
|
|
|
|
|
316
|
|
|
16
|
6
|
|
|
6
|
|
37
|
use Carp; |
|
|
6
|
|
|
|
|
24
|
|
|
|
6
|
|
|
|
|
330
|
|
|
17
|
6
|
|
|
6
|
|
39
|
use Exporter 'import'; |
|
|
6
|
|
|
|
|
13
|
|
|
|
6
|
|
|
|
|
183
|
|
|
18
|
6
|
|
|
6
|
|
1963
|
use Data::Dumper; |
|
|
6
|
|
|
|
|
19403
|
|
|
|
6
|
|
|
|
|
4013
|
|
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our @EXPORT = qw/read_gnuplot_format/; |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# produce 2D PDL for each block. Cat them into a 3d PDL |
|
24
|
|
|
|
|
|
|
sub get_blocks { |
|
25
|
2
|
|
|
2
|
0
|
18
|
my ( $fh, $num_columns ) = validated_list( |
|
26
|
|
|
|
|
|
|
\@_, |
|
27
|
|
|
|
|
|
|
fh => { isa => 'FileHandle', optional => 1 }, |
|
28
|
|
|
|
|
|
|
num_columns => { isa => 'Int' }, |
|
29
|
|
|
|
|
|
|
); |
|
30
|
|
|
|
|
|
|
|
|
31
|
2
|
|
|
|
|
946
|
my @blocks; |
|
32
|
|
|
|
|
|
|
my @rows; |
|
33
|
2
|
|
|
|
|
67
|
while ( my $line = <$fh> ) { |
|
34
|
15
|
100
|
|
|
|
50
|
if ( $line =~ /^#/ ) { |
|
35
|
2
|
|
|
|
|
8
|
next; |
|
36
|
|
|
|
|
|
|
} |
|
37
|
13
|
100
|
|
|
|
41
|
if ( $line =~ /^\s*$/ ) { |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# Finish block. Need check for number of rows if we have |
|
40
|
|
|
|
|
|
|
# multiple subsequent blank lines |
|
41
|
2
|
50
|
|
|
|
5
|
if ( @rows > 0 ) { |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Give \@rows, not @rows to get a 2D piddle if we |
|
44
|
|
|
|
|
|
|
# only have a single row. |
|
45
|
2
|
|
|
|
|
9
|
push @blocks, pdl( \@rows ); |
|
46
|
2
|
|
|
|
|
303
|
@rows = (); |
|
47
|
|
|
|
|
|
|
} |
|
48
|
2
|
|
|
|
|
9
|
next; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# awk splitting behaviour |
|
52
|
11
|
|
|
|
|
29
|
my @nums = split( ' ', $line ); |
|
53
|
11
|
50
|
|
|
|
22
|
if ( @nums != $num_columns ) { |
|
54
|
0
|
|
|
|
|
0
|
die "num cols not $num_columns"; |
|
55
|
|
|
|
|
|
|
} |
|
56
|
11
|
|
|
|
|
72
|
push @rows, [@nums]; |
|
57
|
|
|
|
|
|
|
} |
|
58
|
2
|
50
|
|
|
|
11
|
if ( @rows > 0 ) { |
|
59
|
2
|
|
|
|
|
8
|
push @blocks, pdl( \@rows ); |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# bring blocks to same number of rows: reshape and add NaNs. |
|
63
|
2
|
|
|
|
|
261
|
my $max_rows = List::Util::max( map { ( $_->dims )[1] } @blocks ); |
|
|
4
|
|
|
|
|
24
|
|
|
64
|
|
|
|
|
|
|
|
|
65
|
2
|
|
|
|
|
7
|
for my $block (@blocks) { |
|
66
|
4
|
|
|
|
|
102
|
my $rows = ( $block->dims() )[1]; |
|
67
|
4
|
100
|
|
|
|
12
|
if ( $rows < $max_rows ) { |
|
68
|
2
|
|
|
|
|
20
|
$block->reshape( $num_columns, $max_rows ); |
|
69
|
2
|
|
|
|
|
436
|
$block->slice(":,${rows}:-1") .= "NaN"; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
|
|
73
|
2
|
|
|
|
|
7
|
return PDL::cat(@blocks); |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub read_gnuplot_format { |
|
77
|
2
|
|
|
2
|
1
|
2431
|
my ( $type, $fh, $file, $num_columns ) = validated_list( |
|
78
|
|
|
|
|
|
|
\@_, |
|
79
|
|
|
|
|
|
|
type => { isa => enum( [qw/columns maps bare/] ) }, |
|
80
|
|
|
|
|
|
|
fh => { isa => 'FileHandle', optional => 1 }, |
|
81
|
|
|
|
|
|
|
file => { isa => 'Str', optional => 1 }, |
|
82
|
|
|
|
|
|
|
num_columns => { isa => 'Int' }, |
|
83
|
|
|
|
|
|
|
); |
|
84
|
|
|
|
|
|
|
|
|
85
|
2
|
50
|
66
|
|
|
5068
|
if ( !( $fh || $file ) ) { |
|
86
|
0
|
|
|
|
|
0
|
croak "read_2d_gnuplot_format needs either 'fh' or 'file' argument"; |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
2
|
100
|
|
|
|
14
|
if ( !$fh ) { |
|
90
|
1
|
50
|
|
|
|
50
|
open $fh, '<', $file |
|
91
|
|
|
|
|
|
|
or croak "cannot open file $file: $!"; |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# Rewind filehandle. |
|
95
|
2
|
50
|
|
|
|
71
|
seek $fh, 0, SEEK_SET |
|
96
|
|
|
|
|
|
|
or croak "cannot seek: $!"; |
|
97
|
|
|
|
|
|
|
|
|
98
|
2
|
|
|
|
|
11
|
my $blocks = get_blocks( fh => $fh, num_columns => $num_columns ); |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# $blocks is 3D PDL with following dims |
|
101
|
|
|
|
|
|
|
# 0st dim: column |
|
102
|
|
|
|
|
|
|
# 1st dim: row (in block) |
|
103
|
|
|
|
|
|
|
# 2nd dim: block |
|
104
|
|
|
|
|
|
|
|
|
105
|
2
|
50
|
|
|
|
327
|
if ( $type eq 'bare' ) { |
|
|
|
50
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
0
|
return $blocks; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
elsif ( $type eq 'columns' ) { |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# merge blocks |
|
111
|
2
|
|
|
|
|
9
|
my $result = $blocks->clump( 1, 2 ); |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# switch row/column dimensions |
|
114
|
2
|
|
|
|
|
151
|
$result = $result->xchg( 0, 1 ); |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# return one pdl for each column |
|
117
|
2
|
|
|
|
|
9
|
return dog($result); |
|
118
|
|
|
|
|
|
|
} |
|
119
|
|
|
|
|
|
|
elsif ( $type eq 'maps' ) { |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# 3D gnuplot data file (two x values, three y value): |
|
122
|
|
|
|
|
|
|
# x11 y11 z11 |
|
123
|
|
|
|
|
|
|
# x12 y12 z12 |
|
124
|
|
|
|
|
|
|
# x13 y13 z13 |
|
125
|
|
|
|
|
|
|
# |
|
126
|
|
|
|
|
|
|
# x21 y21 z21 |
|
127
|
|
|
|
|
|
|
# x22 y22 z22 |
|
128
|
|
|
|
|
|
|
# x23 y23 z23 |
|
129
|
|
|
|
|
|
|
# |
|
130
|
|
|
|
|
|
|
# x-cordinate changes with each block: x11, x12 and x13 will be equal |
|
131
|
|
|
|
|
|
|
# in most cases (exception: sweep of B-field or temperature where |
|
132
|
|
|
|
|
|
|
# they will be almost equal. |
|
133
|
|
|
|
|
|
|
# |
|
134
|
|
|
|
|
|
|
# Parse into three 2x3 piddles for x, y and z data |
|
135
|
|
|
|
|
|
|
# (first piddle dim (x) goes to the right): |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# x11 x21 |
|
138
|
|
|
|
|
|
|
# x12 x22 |
|
139
|
|
|
|
|
|
|
# x13 x23 |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# y11 y21 |
|
142
|
|
|
|
|
|
|
# y12 y22 |
|
143
|
|
|
|
|
|
|
# y13 y23 |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# and same for z |
|
146
|
0
|
|
|
|
|
|
my $result = $blocks->xchg( 0, 2 ); |
|
147
|
0
|
|
|
|
|
|
return dog($result); |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
} |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
1; |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
__END__ |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=pod |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=encoding UTF-8 |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 NAME |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Lab::Moose::DataFile::Read - Read a gnuplot-style 2D data file |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 VERSION |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
version 3.900 |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
use Lab::Moose::DataFile::Read; |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
# Read gnuplot ASCII datafile and return each column as a 1D PDL |
|
172
|
|
|
|
|
|
|
my @columns = read_gnuplot_format( |
|
173
|
|
|
|
|
|
|
type => 'columns', |
|
174
|
|
|
|
|
|
|
file => 'data.dat', |
|
175
|
|
|
|
|
|
|
num_columns => 2, |
|
176
|
|
|
|
|
|
|
); |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# Read block structured 3D gnuplot ASCII datafile and return |
|
179
|
|
|
|
|
|
|
# 2D PDL for each parameter (column) |
|
180
|
|
|
|
|
|
|
my @pixel_maps = read_gnuplot_format( |
|
181
|
|
|
|
|
|
|
type => 'maps', |
|
182
|
|
|
|
|
|
|
file => '3d_data.dat', |
|
183
|
|
|
|
|
|
|
num_columns => 3, |
|
184
|
|
|
|
|
|
|
); |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# Read 3D gnuplot ASCII datafile and return 3D PDL with dimensions |
|
187
|
|
|
|
|
|
|
# [column, line, block] |
|
188
|
|
|
|
|
|
|
my $pdl = read_gnuplot_format( |
|
189
|
|
|
|
|
|
|
type => 'bare', |
|
190
|
|
|
|
|
|
|
file => '3d_data.dat', |
|
191
|
|
|
|
|
|
|
num_columns => 3, |
|
192
|
|
|
|
|
|
|
); |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 Functions |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head2 read_gnuplot_format |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Exported by default. Allowed parameters: |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=over |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=item * type |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Either C<'columns'>, C<'maps'>, or C<'bare'>. |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=item * file |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=item * fh |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Provide an open file handle instead of a filename. |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=item * num_columns (mandatory) |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
Number of columns in the datafile. Used for a consistency check. |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=back |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
This software is copyright (c) 2023 by the Lab::Measurement team; in detail: |
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Copyright 2016 Simon Reinhardt |
|
223
|
|
|
|
|
|
|
2017 Andreas K. Huettel, Simon Reinhardt |
|
224
|
|
|
|
|
|
|
2018 Simon Reinhardt |
|
225
|
|
|
|
|
|
|
2020 Andreas K. Huettel |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
229
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=cut |