blib/lib/Perlsac/rwsac.pm untested

filter: Statementstmt 0.0% 0 / 258 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 292 CC 5 SCAR 20.8
CC
5
Cov
0%
rsac
12.0
wsac
12.0
init
2.0
CRAP 8.0
1 package Perlsac::rwsac ;
2  
3 use strict ;
4 use warnings ;
5  
6 our $VERSION = 0.06 ;
7  
8 our @map ;
9 $map[0] = "delta" ;
10 $map[1] = "depmin" ;
11 $map[2] = "depmax" ;
12 $map[3] = "unused" ;
13 $map[4] = "odelta" ;
14 $map[5] = "b" ;
15 $map[6] = "e" ;
16 $map[7] = "o" ;
17 $map[8] = "a" ;
18 $map[9] = "internal" ;
19 $map[10] = "t0" ;
20 $map[11] = "t1" ;
21 $map[12] = "t2" ;
22 $map[13] = "t3" ;
23 $map[14] = "t4" ;
24 $map[15] = "t5" ;
25 $map[16] = "t6" ;
26 $map[17] = "t7" ;
27 $map[18] = "t8" ;
28 $map[19] = "t9" ;
29 $map[20] = "f" ;
30 $map[21] = "resp0" ;
31 $map[22] = "resp1" ;
32 $map[23] = "resp2" ;
33 $map[24] = "resp3" ;
34 $map[25] = "resp4" ;
35 $map[26] = "resp5" ;
36 $map[27] = "resp6" ;
37 $map[28] = "resp7" ;
38 $map[29] = "resp8" ;
39 $map[30] = "resp9" ;
40 $map[31] = "stla" ;
41 $map[32] = "stlo" ;
42 $map[33] = "stel" ;
43 $map[34] = "stdp" ;
44 $map[35] = "evla" ;
45 $map[36] = "evlo" ;
46 $map[37] = "evel" ;
47 $map[38] = "evdp" ;
48 $map[39] = "mag" ;
49 $map[40] = "user0" ;
50 $map[41] = "user1" ;
51 $map[42] = "user2" ;
52 $map[43] = "user3" ;
53 $map[44] = "user4" ;
54 $map[45] = "user5" ;
55 $map[46] = "user6" ;
56 $map[47] = "user7" ;
57 $map[48] = "user8" ;
58 $map[49] = "user9" ;
59 $map[50] = "dist" ;
60 $map[51] = "az" ;
61 $map[52] = "baz" ;
62 $map[53] = "gcarc" ;
63 $map[54] = "sb" ;
64 $map[55] = "sdelta" ;
65 $map[56] = "depmen" ;
66 $map[57] = "cmpaz" ;
67 $map[58] = "cmpinc" ;
68 $map[59] = "xminimum" ;
69 $map[60] = "xmaximum" ;
70 $map[61] = "yminimum" ;
71 $map[62] = "ymaximum" ;
72 $map[63] = "adjtm" ;
73 $map[64] = "unused" ;
74 $map[65] = "unused" ;
75 $map[66] = "unused" ;
76 $map[67] = "unused" ;
77 $map[68] = "unused" ;
78 $map[69] = "unused" ;
79 $map[70] = "nzyear" ;
80 $map[71] = "nzjday" ;
81 $map[72] = "nzhour" ;
82 $map[73] = "nzmin" ;
83 $map[74] = "nzsec" ;
84 $map[75] = "nzmsec" ;
85 $map[76] = "nvhdr" ;
86 $map[77] = "norid" ;
87 $map[78] = "nevid" ;
88 $map[79] = "npts" ;
89 $map[80] = "nsnpts" ;
90 $map[81] = "nwfid" ;
91 $map[82] = "nxsize" ;
92 $map[83] = "nysize" ;
93 $map[84] = "unused" ;
94 $map[85] = "iftype" ;
95 $map[86] = "idep" ;
96 $map[87] = "iztype" ;
97 $map[88] = "unused" ;
98 $map[89] = "iinst" ;
99 $map[90] = "istreg" ;
100 $map[91] = "ievreg" ;
101 $map[92] = "ievtyp" ;
102 $map[93] = "iqual" ;
103 $map[94] = "isynth" ;
104 $map[95] = "imagtyp" ;
105 $map[96] = "imagsrc" ;
106 $map[97] = "ibody" ;
107 $map[98] = "unused" ;
108 $map[99] = "unused" ;
109 $map[100] = "unused" ;
110 $map[101] = "unused" ;
111 $map[102] = "unused" ;
112 $map[103] = "unused" ;
113 $map[104] = "unused" ;
114 $map[105] = "leven" ;
115 $map[106] = "lpspol" ;
116 $map[107] = "lovrok" ;
117 $map[108] = "lcalda" ;
118 $map[109] = "unused" ;
119 $map[110] = "kstnm" ;
120 $map[111] = "kevnm" ;
121 $map[112] = "khole" ;
122 $map[113] = "ko" ;
123 $map[114] = "ka" ;
124 $map[115] = "kt0" ;
125 $map[116] = "kt1" ;
126 $map[117] = "kt2" ;
127 $map[118] = "kt3" ;
128 $map[119] = "kt4" ;
129 $map[120] = "kt5" ;
130 $map[121] = "kt6" ;
131 $map[122] = "kt7" ;
132 $map[123] = "kt8" ;
133 $map[124] = "kt9" ;
134 $map[125] = "kf" ;
135 $map[126] = "kuser0" ;
136 $map[127] = "kuser1" ;
137 $map[128] = "kuser2" ;
138 $map[129] = "kcmpnm" ;
139 $map[130] = "knetwk" ;
140 $map[131] = "kdatrd" ;
141 $map[132] = "kinst" ;
142  
143 sub init {
144 my ($b,$npts,$delta)=@_ ;
145 my %h ;
146 my $n = -1 ;
147 my $hname ;
148 for (1..70){
149 $n++ ;
150 $hname = $map[$n] ;
151 #print $f pack("f",$h{$hname}) ;
152 $h{$hname} = -12345.0 ;
153 }
154 for (1..40){
155 $n++ ;
156 $hname = $map[$n] ;
157 #print $f pack("i",$h{$hname}) ;
158 $h{$hname} = -12345 ;
159 }
160 for (1..1){
161 $n++ ;
162 $hname = $map[$n] ;
163 #print $f pack("a8",$h{$hname}) ;
164 $h{$hname} = "-12345 " ;
165 }
166 for (1..1){
167 $n++ ;
168 $hname = $map[$n] ;
169 #print $f pack("a16",$h{$hname}) ;
170 $h{$hname} = "-12345 " ;
171 }
172 for (1..21){
173 $n++ ;
174 $hname = $map[$n] ;
175 #print $f pack("a8",$h{$hname}) ;
176 $h{$hname} = "-12345 " ;
177 }
178 #required
179 $h{npts} = $npts ;
180 $h{nvhdr} = 6 ; #does not work when it is 7. Not sure about the reason.
181 $h{b} = $b ;
182 $h{e} = $b+$delta*$npts ;
183 $h{iftype} = 1 ;
184 $h{leven} = 1 ;
185 $h{delta} = $delta ;
186 for(my $i=0; $i<$npts; $i++){
187 $h{d}[$i] = 0.0 ;
188 }
189 $h{depmax} = 0.0 ;
190 $h{depmin} = 0.0 ;
191 return %h ;
192 }
193  
194 sub rsac {
195 my ($fname)=@_ ;
196 my $b ;
197 open(my $f,"<$fname") or die "cannot open file: $fname\n" ;
198 binmode($f) ;
199 read($f,$b,158*4) ;
200 my @h ;
201 #@h = unpack("f70i40a8a16(a8)21",$b) ;
202 @h = unpack("f70i40Z8Z16(Z8)21",$b) ;
203 my %h ;
204 for (my $n=0; $n<=132; $n++){
205 $h{$map[$n]} = $h[$n] ;
206 }
207 read($f,$b,$h{npts}*4) ;
208 my @d1 = unpack("f$h{npts}",$b) ;
209 my @d2 ;
210 my @d3 ;
211 if (2 <= $h{iftype} and $h{iftype} <= 4){
212 #1 ITIME {Time series file}
213 #2 IRLIM {Spectral file---real and imaginary}
214 #3 IAMPH {Spectral file---amplitude and phase}
215 #4 IXY {General x versus y data}
216 #5 IXYZ {General XYZ (3-D) file}
217 read($f,$b,$h{npts}*4) ;
218 @d2 = unpack("f$h{npts}",$b) ;
219 }
220 if ($h{iftype} == 5){
221 read($f,$b,$h{npts}*4) ;
222 @d3 = unpack("f$h{npts}",$b) ;
223 }
224 close($f) ;
225 $h{d} = [@d1] ;
226 $h{d2} = [@d2] ;
227 $h{d3} = [@d3] ;
228 #my @t ;
229 #for (my $n=0; $n<=$h{npts}; $n++){
230 # $t[$n] = $h{b}+$n*$h{delta} ;
231 #}
232 #$h{t} = [@t] ;
233 $h{t} = &calt($h{b},$h{npts},$h{delta}) ;
234 return %h ;
235 }
236  
237 sub calt {
238 my ($b,$npts,$delta)=@_ ;
239 my @t ;
240 for (my $n=0; $n<$npts; $n++){
241 $t[$n] = $b+$n*$delta ;
242 }
243 return @t ;
244 }
245  
246 sub wsac {
247 my ($fname,%h)=@_ ;
248 my $b ;
249 open(my $f,">$fname") or die "cannot open file: $fname\n" ;
250 binmode($f) ;
251 my $n = -1 ;
252 my $hname ;
253 for (1..70){
254 $n++ ;
255 $hname = $map[$n] ;
256 print $f pack("f",$h{$hname}) ;
257 }
258 for (1..40){
259 $n++ ;
260 $hname = $map[$n] ;
261 print $f pack("i",$h{$hname}) ;
262 }
263 for (1..1){
264 $n++ ;
265 $hname = $map[$n] ;
266 print $f pack("a8",$h{$hname}) ;
267 }
268 for (1..1){
269 $n++ ;
270 $hname = $map[$n] ;
271 print $f pack("a16",$h{$hname}) ;
272 }
273 for (1..21){
274 $n++ ;
275 $hname = $map[$n] ;
276 print $f pack("a8",$h{$hname}) ;
277 }
278 for (1..$h{npts}){
279 print $f pack("f",$h{d}[$_-1]) ;
280 }
281 if (2 <= $h{iftype} and $h{iftype} <= 4){
282 for (1..$h{npts}){
283 print $f pack("f",$h{d2}[$_-1]) ;
284 }
285 }
286 if ($h{iftype} == 5){
287 for (1..$h{npts}){
288 print $f pack("f",$h{d3}[$_-1]) ;
289 }
290 }
291 close($f) ;
292 return 1 ;
293 }
294  
295  
296 =head1 NAME
297  
298 Perlsac::rwsac - a module to read and write SAC file.
299  
300 =head1 DESCRIPTION
301  
302 This is the module for reading and writing the sac file, defined
303 at 'http://ds.iris.edu/files/sac-manual/manual/file_format.html'
304  
305 =head1 AUTHOR
306  
307 Hobin Lim
308  
309 =head1 LICENSE
310  
311 MIT
312  
313 =head1 INSTALLATION
314  
315 Using C<cpan>:
316  
317 cpan install Perlsac::rwsac
318  
319 Manual install:
320  
321 perl Makefile.PL
322 make
323 make install
324  
325 =head1 TUTORIALS
326  
327 1. Printing out time and data.
328  
329 #!/usr/bin/env perl
330
331 use strict ;
332 use warnings ;
333 use Perlsac::rwsac ;
334
335 my %h = Perlsac::rwsac::rsac("example.sac") ;
336  
337 my $b = $h{b} ;
338 my $npts = $h{npts} ;
339 my $delta = $h{delta} ;
340  
341 my @t = Perlsac::rwsac::calt($b,$npts,$delta) ;
342
343 for (my $n=0; $n<$h{npts}; $n++){
344 print "$t[$n] $h{d}[$n]\n" ;
345 }
346  
347 2. Dividing data by 'depmax' in headers and writing a new sac file.
348  
349 #!/usr/bin/env perl
350
351 use strict ;
352 use warnings ;
353 use Perlsac::rwsac ;
354
355 my %h = Perlsac::rwsac::rsac("example.sac") ;
356
357 for (my $n=0; $n<$h{npts}; $n++){
358 $h{d}[$n] /= $h{depmax} ;
359 }
360
361 &Perlsac::rwsac::wsac("example.sac.div",%h) ;
362  
363 3. Making a synthetic triangle-shaped waveform.
364  
365 #!/usr/bin/env perl
366
367 use strict ;
368 use warnings ;
369 use Perlsac::rwsac ;
370  
371 my $b = 0.0 ;
372 my $npts = 20 ;
373 my $delta = 0.1 ;
374  
375 my %h = Perlsac::rwsac::init($b, $npts, $delta) ; #b, npts, delta
376 #$h{d} are zero-padded.
377  
378 my @ys = (
379 0,0,0,0,0,
380 1,2,3,4,5,
381 4,3,2,1,0,
382 0,0,0,0,0) ;
383  
384 $h{d} = [@ys] ;
385
386 &Perlsac::rwsac::wsac('triangle.sac',%h) ;
387  
388 =head1 Limitations
389 1. Type of files (IFTYPE and LEVEN) are not be well addressed yet.
390  
391  
392  
393 =cut
394  
395 1 ;