View Javadoc

1   /*
2    The TauP Toolkit: Flexible Seismic Travel-Time and Raypath Utilities.
3    Copyright (C) 1998-2000 University of South Carolina
4   
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License
7    as published by the Free Software Foundation; either version 2
8    of the License, or (at your option) any later version.
9   
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14  
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  
19   The current version can be found at
20   <A HREF="www.seis.sc.edu">http://www.seis.sc.edu</A>
21  
22   Bug reports and comments should be directed to
23   H. Philip Crotwell, crotwell@seis.sc.edu or
24   Tom Owens, owens@seis.sc.edu
25  
26   */
27  
28  package edu.sc.seis.TauP;
29  
30  
31  import java.io.BufferedInputStream;
32  import java.io.BufferedOutputStream;
33  import java.io.DataInputStream;
34  import java.io.DataOutputStream;
35  import java.io.EOFException;
36  import java.io.File;
37  import java.io.FileInputStream;
38  import java.io.FileNotFoundException;
39  import java.io.FileOutputStream;
40  import java.io.IOException;
41  import java.io.InputStream;
42  import java.text.DecimalFormat;
43  
44  /*** Class that represents a sac file. All headers are have the same names
45   * as within the Sac program. Can read the whole file or just the header
46   * as well as write a file.
47   *
48   * @version 1.1 Wed Feb  2 20:40:49 GMT 2000
49   * @author H. Philip Crotwell
50   */
51  public class SacTimeSeries {
52      public float delta = FLOAT_UNDEF;
53      public float depmin = FLOAT_UNDEF;
54      public float depmax = FLOAT_UNDEF;
55      public float scale = FLOAT_UNDEF;
56      public float odelta = FLOAT_UNDEF;
57      public float b = FLOAT_UNDEF;
58      public float e = FLOAT_UNDEF;
59      public float o = FLOAT_UNDEF;
60      public float a = FLOAT_UNDEF;
61      public float fmt = FLOAT_UNDEF;
62      public float t0 = FLOAT_UNDEF;
63      public float t1 = FLOAT_UNDEF;
64      public float t2 = FLOAT_UNDEF;
65      public float t3 = FLOAT_UNDEF;
66      public float t4 = FLOAT_UNDEF;
67      public float t5 = FLOAT_UNDEF;
68      public float t6 = FLOAT_UNDEF;
69      public float t7 = FLOAT_UNDEF;
70      public float t8 = FLOAT_UNDEF;
71      public float t9 = FLOAT_UNDEF;
72      public float f = FLOAT_UNDEF;
73      public float resp0 = FLOAT_UNDEF;
74      public float resp1 = FLOAT_UNDEF;
75      public float resp2 = FLOAT_UNDEF;
76      public float resp3 = FLOAT_UNDEF;
77      public float resp4 = FLOAT_UNDEF;
78      public float resp5 = FLOAT_UNDEF;
79      public float resp6 = FLOAT_UNDEF;
80      public float resp7 = FLOAT_UNDEF;
81      public float resp8 = FLOAT_UNDEF;
82      public float resp9 = FLOAT_UNDEF;
83      public float stla = FLOAT_UNDEF;
84      public float stlo = FLOAT_UNDEF;
85      public float stel = FLOAT_UNDEF;
86      public float stdp = FLOAT_UNDEF;
87      public float evla = FLOAT_UNDEF;
88      public float evlo = FLOAT_UNDEF;
89      public float evel = FLOAT_UNDEF;
90      public float evdp = FLOAT_UNDEF;
91      public float mag = FLOAT_UNDEF;
92      public float user0 = FLOAT_UNDEF;
93      public float user1 = FLOAT_UNDEF;
94      public float user2 = FLOAT_UNDEF;
95      public float user3 = FLOAT_UNDEF;
96      public float user4 = FLOAT_UNDEF;
97      public float user5 = FLOAT_UNDEF;
98      public float user6 = FLOAT_UNDEF;
99      public float user7 = FLOAT_UNDEF;
100     public float user8 = FLOAT_UNDEF;
101     public float user9 = FLOAT_UNDEF;
102     public float dist = FLOAT_UNDEF;
103     public float az = FLOAT_UNDEF;
104     public float baz = FLOAT_UNDEF;
105     public float gcarc = FLOAT_UNDEF;
106     public float sb = FLOAT_UNDEF;
107     public float sdelta = FLOAT_UNDEF;
108     public float depmen = FLOAT_UNDEF;
109     public float cmpaz = FLOAT_UNDEF;
110     public float cmpinc = FLOAT_UNDEF;
111     public float xminimum = FLOAT_UNDEF;
112     public float xmaximum = FLOAT_UNDEF;
113     public float yminimum = FLOAT_UNDEF;
114     public float ymaximum = FLOAT_UNDEF;
115     public float unused6 = FLOAT_UNDEF;
116     public float unused7 = FLOAT_UNDEF;
117     public float unused8 = FLOAT_UNDEF;
118     public float unused9 = FLOAT_UNDEF;
119     public float unused10 = FLOAT_UNDEF;
120     public float unused11 = FLOAT_UNDEF;
121     public float unused12 = FLOAT_UNDEF;
122     public int nzyear = INT_UNDEF;
123     public int nzjday = INT_UNDEF;
124     public int nzhour = INT_UNDEF;
125     public int nzmin = INT_UNDEF;
126     public int nzsec = INT_UNDEF;
127     public int nzmsec = INT_UNDEF;
128     public int nvhdr = 6;
129     public int norid = INT_UNDEF;
130     public int nevid = INT_UNDEF;
131     public int npts = INT_UNDEF;
132     public int nsnpts = INT_UNDEF;
133     public int nwfid = INT_UNDEF;
134     public int nxsize = INT_UNDEF;
135     public int nysize = INT_UNDEF;
136     public int unused15 = INT_UNDEF;
137     public int iftype = INT_UNDEF;
138     public int idep = INT_UNDEF;
139     public int iztype = INT_UNDEF;
140     public int unused16 = INT_UNDEF;
141     public int iinst = INT_UNDEF;
142     public int istreg = INT_UNDEF;
143     public int ievreg = INT_UNDEF;
144     public int ievtyp = INT_UNDEF;
145     public int iqual = INT_UNDEF;
146     public int isynth = INT_UNDEF;
147     public int imagtyp = INT_UNDEF;
148     public int imagsrc = INT_UNDEF;
149     public int unused19 = INT_UNDEF;
150     public int unused20 = INT_UNDEF;
151     public int unused21 = INT_UNDEF;
152     public int unused22 = INT_UNDEF;
153     public int unused23 = INT_UNDEF;
154     public int unused24 = INT_UNDEF;
155     public int unused25 = INT_UNDEF;
156     public int unused26 = INT_UNDEF;
157     public int leven = INT_UNDEF;
158     public int lpspol = INT_UNDEF;
159     public int lovrok = INT_UNDEF;
160     public int lcalda = INT_UNDEF;
161     public int unused27 = INT_UNDEF;
162     public String kstnm = STRING8_UNDEF;
163     public String kevnm = STRING16_UNDEF;
164     public String khole = STRING8_UNDEF;
165     public String ko = STRING8_UNDEF;
166     public String ka = STRING8_UNDEF;
167     public String kt0 = STRING8_UNDEF;
168     public String kt1 = STRING8_UNDEF;
169     public String kt2 = STRING8_UNDEF;
170     public String kt3 = STRING8_UNDEF;
171     public String kt4 = STRING8_UNDEF;
172     public String kt5 = STRING8_UNDEF;
173     public String kt6 = STRING8_UNDEF;
174     public String kt7 = STRING8_UNDEF;
175     public String kt8 = STRING8_UNDEF;
176     public String kt9 = STRING8_UNDEF;
177     public String kf = STRING8_UNDEF;
178     public String kuser0 = STRING8_UNDEF;
179     public String kuser1 = STRING8_UNDEF;
180     public String kuser2 = STRING8_UNDEF;
181     public String kcmpnm = STRING8_UNDEF;
182     public String knetwk = STRING8_UNDEF;
183     public String kdatrd = STRING8_UNDEF;
184     public String kinst = STRING8_UNDEF;
185 
186     public float[] y;
187     public float[] x;
188     public float[] real;
189     public float[] imaginary;
190     public float[] amp;
191     public float[] phase;
192 
193     // undef values for sac
194     public static float FLOAT_UNDEF = -12345.0f;
195     public static int INT_UNDEF = -12345;
196     public static String STRING8_UNDEF = "-12345  ";
197     public static String STRING16_UNDEF = "-12345          ";
198 
199     /* TRUE and FLASE defined for convenience. */
200     public static final int TRUE   =  1;
201     public static final int FALSE  =  0;
202 
203     /* Constants used by sac. */
204     public static final int IREAL  =  0;
205     public static final int ITIME  =  1;
206     public static final int IRLIM  =  2;
207     public static final int IAMPH  =  3;
208     public static final int IXY    =  4;
209     public static final int IUNKN  =  5;
210     public static final int IDISP  =  6;
211     public static final int IVEL   =  7;
212     public static final int IACC   =  8;
213     public static final int IB     =  9;
214     public static final int IDAY   = 10;
215     public static final int IO     = 11;
216     public static final int IA     = 12;
217     public static final int IT0    = 13;
218     public static final int IT1    = 14;
219     public static final int IT2    = 15;
220     public static final int IT3    = 16;
221     public static final int IT4    = 17;
222     public static final int IT5    = 18;
223     public static final int IT6    = 19;
224     public static final int IT7    = 20;
225     public static final int IT8    = 21;
226     public static final int IT9    = 22;
227     public static final int IRADNV = 23;
228     public static final int ITANNV = 24;
229     public static final int IRADEV = 25;
230     public static final int ITANEV = 26;
231     public static final int INORTH = 27;
232     public static final int IEAST  = 28;
233     public static final int IHORZA = 29;
234     public static final int IDOWN  = 30;
235     public static final int IUP    = 31;
236     public static final int ILLLBB = 32;
237     public static final int IWWSN1 = 33;
238     public static final int IWWSN2 = 34;
239     public static final int IHGLP  = 35;
240     public static final int ISRO   = 36;
241     public static final int INUCL  = 37;
242     public static final int IPREN  = 38;
243     public static final int IPOSTN = 39;
244     public static final int IQUAKE = 40;
245     public static final int IPREQ  = 41;
246     public static final int IPOSTQ = 42;
247     public static final int ICHEM  = 43;
248     public static final int IOTHER = 44;
249     public static final int IGOOD  = 45;
250     public static final int IGLCH  = 46;
251     public static final int IDROP  = 47;
252     public static final int ILOWSN = 48;
253     public static final int IRLDTA = 49;
254     public static final int IVOLTS = 50;
255     public static final int INIV51 = 51;
256     public static final int INIV52 = 52;
257     public static final int INIV53 = 53;
258     public static final int INIV54 = 54;
259     public static final int INIV55 = 55;
260     public static final int INIV56 = 56;
261     public static final int INIV57 = 57;
262     public static final int INIV58 = 58;
263     public static final int INIV59 = 59;
264     public static final int INIV60 = 60;
265 
266 
267     public static final int  data_offset = 632;
268 
269     public boolean byteOrder = SunByteOrder;
270     public static final boolean SunByteOrder = true;
271     public static final boolean IntelByteOrder = false;
272 
273     /*** reads the sac file specified by the filename. Only a very simple
274      * check is made
275      * to be sure the file really is a sac file.
276      * @throws FileNotFoundException if the file cannot be found
277      * @throws IOException if it isn't a sac file or if it happens :)
278      */
279     public void read(String filename)
280         throws FileNotFoundException, IOException {
281         File sacFile = new File(filename);
282         FileInputStream fis = new FileInputStream(filename);
283         BufferedInputStream buf = new BufferedInputStream(fis);
284         DataInputStream dis = new DataInputStream( buf);
285         readHeader(dis);
286         if (sacFile.length() != npts*4+data_offset) {
287             if (sacFile.length() == swapBytes(npts)*4+data_offset ) {
288                 // must be little endian
289                 System.out.println("Swap bytes for linux: swapBytes(npts) ("+swapBytes(npts)+"*4) + header("+data_offset+") =  file length="+sacFile.length());
290                 byteOrder = IntelByteOrder;
291                 swapHeader();
292             } else {
293                 throw new IOException(filename+
294                                           " does not appear to be a sac file! npts("+npts+") + header("+data_offset+") !=  file length="+sacFile.length()+"\n  as linux: npts("+swapBytes(npts)+") + header("+data_offset+") !=  file length="+sacFile.length());
295             } // end of else
296         }
297         readData(dis);
298         dis.close();
299     }
300 
301     public final static short swapBytes(short val){
302         return (short)(((val & 0xff00) >> 8) + ((val & 0x00ff) << 8));
303     }
304 
305     public final static float swapBytes(float val) {
306         return Float.intBitsToFloat(swapBytes(Float.floatToRawIntBits(val)));
307     }
308 
309     public final static int swapBytes(int val) {
310         return ((val & 0xff000000) >>> 24 ) +
311             ((val & 0x00ff0000) >> 8 ) +
312             ((val & 0x0000ff00) << 8 ) +
313             ((val & 0x000000ff) << 24);
314 
315     }
316 
317     public final static double swapBytes(double val){
318         return Double.longBitsToDouble(swapBytes(Double.doubleToRawLongBits(val)));
319     }
320 
321     public final static long swapBytes(long val){
322         return ((val & 0xffl << 56) >>> 56) +
323             ((val & 0xffl << 48) >> 40) +
324             ((val & 0xffl << 40) >> 24) +
325             ((val & 0xffl << 32) >> 8) +
326             ((val & 0xffl << 24) << 8) +
327             ((val & 0xffl << 16) << 24) +
328             ((val & 0xffl << 8) << 40) +
329             ((val & 0xffl) << 56);
330     }
331 
332     public void read(DataInputStream dis)
333         throws IOException {
334         readHeader(dis);
335         readData(dis);
336     }
337 
338     /*** reads just the sac header specified by the filename. No checks are
339      * made to be sure the file really is a sac file.
340      */
341     public void readHeader(String filename)
342         throws FileNotFoundException, IOException {
343         DataInputStream dis = new DataInputStream(
344             new BufferedInputStream(
345                                                      new FileInputStream(filename)));
346         readHeader(dis);
347         dis.close();
348     }
349 
350     /*** Swaps the byte order of the header values. */
351     protected  void swapHeader() {
352         delta = swapBytes(delta);
353         depmin = swapBytes(depmin);
354         depmax = swapBytes(depmax);
355         scale = swapBytes(scale);
356         odelta = swapBytes(odelta);
357 
358         b = swapBytes(b);
359         e = swapBytes(e);
360         o = swapBytes(o);
361         a = swapBytes(a);
362         fmt = swapBytes(fmt);
363 
364         t0 = swapBytes(t0);
365         t1 = swapBytes(t1);
366         t2 = swapBytes(t2);
367         t3 = swapBytes(t3);
368         t4 = swapBytes(t4);
369 
370         t5 = swapBytes(t5);
371         t6 = swapBytes(t6);
372         t7 = swapBytes(t7);
373         t8 = swapBytes(t8);
374         t9 = swapBytes(t9);
375 
376         f = swapBytes(f);
377         resp0 = swapBytes(resp0);
378         resp1 = swapBytes(resp1);
379         resp2 = swapBytes(resp2);
380         resp3 = swapBytes(resp3);
381 
382         resp4 = swapBytes(resp4);
383         resp5 = swapBytes(resp5);
384         resp6 = swapBytes(resp6);
385         resp7 = swapBytes(resp7);
386         resp8 = swapBytes(resp8);
387 
388         resp9 = swapBytes(resp9);
389         stla = swapBytes(stla);
390         stlo = swapBytes(stlo);
391         stel = swapBytes(stel);
392         stdp = swapBytes(stdp);
393 
394         evla = swapBytes(evla);
395         evlo = swapBytes(evlo);
396         evel = swapBytes(evel);
397         evdp = swapBytes(evdp);
398         mag = swapBytes(mag);
399 
400         user0 = swapBytes(user0);
401         user1 = swapBytes(user1);
402         user2 = swapBytes(user2);
403         user3 = swapBytes(user3);
404         user4 = swapBytes(user4);
405 
406         user5 = swapBytes(user5);
407         user6 = swapBytes(user6);
408         user7 = swapBytes(user7);
409         user8 = swapBytes(user8);
410         user9 = swapBytes(user9);
411 
412         dist = swapBytes(dist);
413         az = swapBytes(az);
414         baz = swapBytes(baz);
415         gcarc = swapBytes(gcarc);
416         sb = swapBytes(sb);
417 
418         sdelta = swapBytes(sdelta);
419         depmen = swapBytes(depmen);
420         cmpaz = swapBytes(cmpaz);
421         cmpinc = swapBytes(cmpinc);
422         xminimum = swapBytes(xminimum);
423 
424         xmaximum = swapBytes(xmaximum);
425         yminimum = swapBytes(yminimum);
426         ymaximum = swapBytes(ymaximum);
427         unused6 = swapBytes(unused6);
428         unused7 = swapBytes(unused7);
429 
430         unused8 = swapBytes(unused8);
431         unused9 = swapBytes(unused9);
432         unused10 = swapBytes(unused10);
433         unused11 = swapBytes(unused11);
434         unused12 = swapBytes(unused12);
435 
436         nzyear = swapBytes(nzyear);
437         nzjday = swapBytes(nzjday);
438         nzhour = swapBytes(nzhour);
439         nzmin = swapBytes(nzmin);
440         nzsec = swapBytes(nzsec);
441 
442         nzmsec = swapBytes(nzmsec);
443         nvhdr = swapBytes(nvhdr);
444         norid = swapBytes(norid);
445         nevid = swapBytes(nevid);
446         npts = swapBytes(npts);
447 
448         nsnpts = swapBytes(nsnpts);
449         nwfid = swapBytes(nwfid);
450         nxsize = swapBytes(nxsize);
451         nysize = swapBytes(nysize);
452         unused15 = swapBytes(unused15);
453 
454         iftype = swapBytes(iftype);
455         idep = swapBytes(idep);
456         iztype = swapBytes(iztype);
457         unused16 = swapBytes(unused16);
458         iinst = swapBytes(iinst);
459 
460         istreg = swapBytes(istreg);
461         ievreg = swapBytes(ievreg);
462         ievtyp = swapBytes(ievtyp);
463         iqual = swapBytes(iqual);
464         isynth = swapBytes(isynth);
465 
466         imagtyp = swapBytes(imagtyp);
467         imagsrc = swapBytes(imagsrc);
468         unused19 = swapBytes(unused19);
469         unused20 = swapBytes(unused20);
470         unused21 = swapBytes(unused21);
471 
472         unused22 = swapBytes(unused22);
473         unused23 = swapBytes(unused23);
474         unused24 = swapBytes(unused24);
475         unused25 = swapBytes(unused25);
476         unused26 = swapBytes(unused26);
477 
478         leven = swapBytes(leven);
479         lpspol = swapBytes(lpspol);
480         lovrok = swapBytes(lovrok);
481         lcalda = swapBytes(lcalda);
482         unused27 = swapBytes(unused27);
483     }
484 
485     /*** reads the header from the given stream. */
486     public void readHeader(DataInputStream dis)
487         throws FileNotFoundException, IOException {
488         delta = dis.readFloat();
489         depmin = dis.readFloat();
490         depmax = dis.readFloat();
491         scale = dis.readFloat();
492         odelta = dis.readFloat();
493 
494         b = dis.readFloat();
495         e = dis.readFloat();
496         o = dis.readFloat();
497         a = dis.readFloat();
498         fmt = dis.readFloat();
499 
500         t0 = dis.readFloat();
501         t1 = dis.readFloat();
502         t2 = dis.readFloat();
503         t3 = dis.readFloat();
504         t4 = dis.readFloat();
505 
506         t5 = dis.readFloat();
507         t6 = dis.readFloat();
508         t7 = dis.readFloat();
509         t8 = dis.readFloat();
510         t9 = dis.readFloat();
511 
512         f = dis.readFloat();
513         resp0 = dis.readFloat();
514         resp1 = dis.readFloat();
515         resp2 = dis.readFloat();
516         resp3 = dis.readFloat();
517 
518         resp4 = dis.readFloat();
519         resp5 = dis.readFloat();
520         resp6 = dis.readFloat();
521         resp7 = dis.readFloat();
522         resp8 = dis.readFloat();
523 
524         resp9 = dis.readFloat();
525         stla = dis.readFloat();
526         stlo = dis.readFloat();
527         stel = dis.readFloat();
528         stdp = dis.readFloat();
529 
530         evla = dis.readFloat();
531         evlo = dis.readFloat();
532         evel = dis.readFloat();
533         evdp = dis.readFloat();
534         mag = dis.readFloat();
535 
536         user0 = dis.readFloat();
537         user1 = dis.readFloat();
538         user2 = dis.readFloat();
539         user3 = dis.readFloat();
540         user4 = dis.readFloat();
541 
542         user5 = dis.readFloat();
543         user6 = dis.readFloat();
544         user7 = dis.readFloat();
545         user8 = dis.readFloat();
546         user9 = dis.readFloat();
547 
548         dist = dis.readFloat();
549         az = dis.readFloat();
550         baz = dis.readFloat();
551         gcarc = dis.readFloat();
552         sb = dis.readFloat();
553 
554         sdelta = dis.readFloat();
555         depmen = dis.readFloat();
556         cmpaz = dis.readFloat();
557         cmpinc = dis.readFloat();
558         xminimum = dis.readFloat();
559 
560         xmaximum = dis.readFloat();
561         yminimum = dis.readFloat();
562         ymaximum = dis.readFloat();
563         unused6 = dis.readFloat();
564         unused7 = dis.readFloat();
565 
566         unused8 = dis.readFloat();
567         unused9 = dis.readFloat();
568         unused10 = dis.readFloat();
569         unused11 = dis.readFloat();
570         unused12 = dis.readFloat();
571 
572         nzyear = dis.readInt();
573         nzjday = dis.readInt();
574         nzhour = dis.readInt();
575         nzmin = dis.readInt();
576         nzsec = dis.readInt();
577 
578         nzmsec = dis.readInt();
579         nvhdr = dis.readInt();
580         norid = dis.readInt();
581         nevid = dis.readInt();
582         npts = dis.readInt();
583 
584         nsnpts = dis.readInt();
585         nwfid = dis.readInt();
586         nxsize = dis.readInt();
587         nysize = dis.readInt();
588         unused15 = dis.readInt();
589 
590         iftype = dis.readInt();
591         idep = dis.readInt();
592         iztype = dis.readInt();
593         unused16 = dis.readInt();
594         iinst = dis.readInt();
595 
596         istreg = dis.readInt();
597         ievreg = dis.readInt();
598         ievtyp = dis.readInt();
599         iqual = dis.readInt();
600         isynth = dis.readInt();
601 
602         imagtyp = dis.readInt();
603         imagsrc = dis.readInt();
604         unused19 = dis.readInt();
605         unused20 = dis.readInt();
606         unused21 = dis.readInt();
607 
608         unused22 = dis.readInt();
609         unused23 = dis.readInt();
610         unused24 = dis.readInt();
611         unused25 = dis.readInt();
612         unused26 = dis.readInt();
613 
614         leven = dis.readInt();
615         lpspol = dis.readInt();
616         lovrok = dis.readInt();
617         lcalda = dis.readInt();
618         unused27 = dis.readInt();
619 
620         byte[] eightBytes = new byte[8];
621         byte[] sixteenBytes = new byte[16];
622 
623         dis.readFully(eightBytes);   kstnm = new String(eightBytes);
624         dis.readFully(sixteenBytes); kevnm = new String(sixteenBytes);
625 
626         dis.readFully(eightBytes);   khole = new String(eightBytes);
627         dis.readFully(eightBytes);   ko = new String(eightBytes);
628         dis.readFully(eightBytes);   ka = new String(eightBytes);
629 
630         dis.readFully(eightBytes);   kt0 = new String(eightBytes);
631         dis.readFully(eightBytes);   kt1 = new String(eightBytes);
632         dis.readFully(eightBytes);   kt2 = new String(eightBytes);
633 
634         dis.readFully(eightBytes);   kt3 = new String(eightBytes);
635         dis.readFully(eightBytes);   kt4 = new String(eightBytes);
636         dis.readFully(eightBytes);   kt5 = new String(eightBytes);
637 
638         dis.readFully(eightBytes);   kt6 = new String(eightBytes);
639         dis.readFully(eightBytes);   kt7 = new String(eightBytes);
640         dis.readFully(eightBytes);   kt8 = new String(eightBytes);
641 
642         dis.readFully(eightBytes);   kt9 = new String(eightBytes);
643         dis.readFully(eightBytes);   kf = new String(eightBytes);
644         dis.readFully(eightBytes);   kuser0 = new String(eightBytes);
645 
646         dis.readFully(eightBytes);   kuser1 = new String(eightBytes);
647         dis.readFully(eightBytes);   kuser2 = new String(eightBytes);
648         dis.readFully(eightBytes);   kcmpnm = new String(eightBytes);
649 
650         dis.readFully(eightBytes);   knetwk = new String(eightBytes);
651         dis.readFully(eightBytes);   kdatrd = new String(eightBytes);
652         dis.readFully(eightBytes);   kinst = new String(eightBytes);
653     }
654 
655     /*** read the data portion of the given File */
656     public void readData(DataInputStream fis) throws IOException {
657         InputStream in = fis;
658 
659         y = new float[npts];
660 
661         int numAdded = 0;
662         int numRead;
663         int i;
664         byte[] overflow = new byte[4];
665         byte[] prevoverflow = new byte[4];
666         int overflowBytes=0;
667         int prevoverflowBytes=0;
668         byte[] buf = new byte[4096];  // buf length must be == 0 % 4
669         // and for efficiency, should be
670         // a multiple of the disk sector size
671         while (numAdded < npts) {
672             if ((numRead = in.read(buf)) == 0) {
673                 continue;
674             } else if (numRead == -1) {
675                 // EOF
676                 throw new EOFException();
677             }
678             overflowBytes = (numRead+prevoverflowBytes) % 4;
679             if (overflowBytes != 0) {
680                 // partial read of bytes for last value
681                 // save in overflow
682                 System.arraycopy(buf, numRead - overflowBytes,
683                                  overflow, 0,
684                                  overflowBytes);
685             }
686             i=0;
687             if (prevoverflowBytes != 0) {
688                 int temp = 0;
689                 // use leftover bytes
690                 for(i=0;i<prevoverflowBytes; i++) {
691                     temp <<= 8;
692                     temp += (prevoverflow[i] & 0xff);
693                 }
694                 // use first new bytes as needed
695                 for(i=0;i<4-prevoverflowBytes; i++) {
696                     temp <<= 8;
697                     temp += (buf[i] & 0xff);
698                 }
699                 y[numAdded] = Float.intBitsToFloat(temp);
700 
701                 numAdded++;
702             }
703             // i is now set to first unused byte in buf
704             while ( i<= numRead-4) {
705                 y[numAdded++] =
706                     Float.intBitsToFloat( ((buf[i++] & 0xff) << 24) +
707                                              ((buf[i++] & 0xff) << 16) +
708                                              ((buf[i++] & 0xff) << 8) +
709                                              ((buf[i++] & 0xff) << 0));
710             }
711             System.arraycopy(overflow, 0,
712                              prevoverflow, 0,
713                              overflowBytes);
714             prevoverflowBytes = overflowBytes;
715         }
716 
717         if ( byteOrder == IntelByteOrder) {
718             for ( int j=0; j<y.length; j++) {
719                 y[j] = swapBytes(y[j]);
720             } // end of for ()
721 
722         } // end of if ()
723 
724     }
725 
726     /*** reads the data portion from the given stream.
727      *  Uses readFloat repeatedly resulting in MUCH slower read
728      *  times than the slightly more confusing method above. */
729     protected void readDataOld(DataInputStream dis)
730         throws FileNotFoundException, IOException {
731 
732         y = new float[npts];
733         for (int i=0; i<npts; i++) {
734             y[i] = dis.readFloat();
735         }
736 
737 
738         if (leven == SacTimeSeries.FALSE ||
739             iftype == SacTimeSeries.IRLIM ||
740             iftype == SacTimeSeries.IAMPH) {
741             x = new float[npts];
742             for (int i=0; i<npts; i++) {
743                 x[i] = dis.readFloat();
744             }
745             if (iftype == SacTimeSeries.IRLIM) {
746                 real = y;
747                 imaginary = x;
748             }
749             if (iftype == SacTimeSeries.IAMPH) {
750                 amp = y;
751                 phase = x;
752             }
753         }
754     }
755 
756     /*** writes this object out as a sac file. */
757     public void write(String filename)
758         throws FileNotFoundException, IOException {
759         File f = new File(filename);
760         write(f);
761     }
762 
763     /*** writes this object out as a sac file. */
764     public void write(File file)
765         throws FileNotFoundException, IOException {
766         DataOutputStream dos = new DataOutputStream(
767             new BufferedOutputStream(
768                                                        new FileOutputStream(file)));
769         writeHeader(dos);
770         writeData(dos);
771         dos.close();
772     }
773 
774     /*** write the float to the stream, swapping bytes if needed. */
775     private final void writeFloat(DataOutputStream dos, float val) throws IOException {
776         if ( byteOrder == IntelByteOrder) {
777             dos.writeFloat(swapBytes(val));
778         } else {
779             dos.writeFloat(val);
780         } // end of else
781     }
782 
783 
784     /*** write the float to the stream, swapping bytes if needed. */
785     private final void writeInt(DataOutputStream dos, int val) throws IOException {
786         if ( byteOrder == IntelByteOrder) {
787             dos.writeInt(swapBytes(val));
788         } else {
789             dos.writeInt(val);
790         } // end of else
791     }
792 
793     public void writeHeader(DataOutputStream dos)
794         throws IOException {
795         writeFloat(dos, delta);
796         writeFloat(dos, depmin);
797         writeFloat(dos, depmax);
798         writeFloat(dos, scale);
799         writeFloat(dos, odelta);
800 
801         writeFloat(dos, b);
802         writeFloat(dos, e);
803         writeFloat(dos, o);
804         writeFloat(dos, a);
805         writeFloat(dos, fmt);
806 
807         writeFloat(dos, t0);
808         writeFloat(dos, t1);
809         writeFloat(dos, t2);
810         writeFloat(dos, t3);
811         writeFloat(dos, t4);
812 
813         writeFloat(dos, t5);
814         writeFloat(dos, t6);
815         writeFloat(dos, t7);
816         writeFloat(dos, t8);
817         writeFloat(dos, t9);
818 
819         writeFloat(dos, f);
820         writeFloat(dos, resp0);
821         writeFloat(dos, resp1);
822         writeFloat(dos, resp2);
823         writeFloat(dos, resp3);
824 
825         writeFloat(dos, resp4);
826         writeFloat(dos, resp5);
827         writeFloat(dos, resp6);
828         writeFloat(dos, resp7);
829         writeFloat(dos, resp8);
830 
831         writeFloat(dos, resp9);
832         writeFloat(dos, stla);
833         writeFloat(dos, stlo);
834         writeFloat(dos, stel);
835         writeFloat(dos, stdp);
836 
837         writeFloat(dos, evla);
838         writeFloat(dos, evlo);
839         writeFloat(dos, evel);
840         writeFloat(dos, evdp);
841         writeFloat(dos, mag);
842 
843         writeFloat(dos, user0);
844         writeFloat(dos, user1);
845         writeFloat(dos, user2);
846         writeFloat(dos, user3);
847         writeFloat(dos, user4);
848 
849         writeFloat(dos, user5);
850         writeFloat(dos, user6);
851         writeFloat(dos, user7);
852         writeFloat(dos, user8);
853         writeFloat(dos, user9);
854 
855         writeFloat(dos, dist);
856         writeFloat(dos, az);
857         writeFloat(dos, baz);
858         writeFloat(dos, gcarc);
859         writeFloat(dos, sb);
860 
861         writeFloat(dos, sdelta);
862         writeFloat(dos, depmen);
863         writeFloat(dos, cmpaz);
864         writeFloat(dos, cmpinc);
865         writeFloat(dos, xminimum);
866 
867         writeFloat(dos, xmaximum);
868         writeFloat(dos, yminimum);
869         writeFloat(dos, ymaximum);
870         writeFloat(dos, unused6);
871         writeFloat(dos, unused7);
872 
873         writeFloat(dos, unused8);
874         writeFloat(dos, unused9);
875         writeFloat(dos, unused10);
876         writeFloat(dos, unused11);
877         writeFloat(dos, unused12);
878 
879         writeInt(dos, nzyear);
880         writeInt(dos, nzjday);
881         writeInt(dos, nzhour);
882         writeInt(dos, nzmin);
883         writeInt(dos, nzsec);
884 
885         writeInt(dos, nzmsec);
886         writeInt(dos, nvhdr);
887         writeInt(dos, norid);
888         writeInt(dos, nevid);
889         writeInt(dos, npts);
890 
891         writeInt(dos, nsnpts);
892         writeInt(dos, nwfid);
893         writeInt(dos, nxsize);
894         writeInt(dos, nysize);
895         writeInt(dos, unused15);
896 
897         writeInt(dos, iftype);
898         writeInt(dos, idep);
899         writeInt(dos, iztype);
900         writeInt(dos, unused16);
901         writeInt(dos, iinst);
902 
903         writeInt(dos, istreg);
904         writeInt(dos, ievreg);
905         writeInt(dos, ievtyp);
906         writeInt(dos, iqual);
907         writeInt(dos, isynth);
908 
909         writeInt(dos, imagtyp);
910         writeInt(dos, imagsrc);
911         writeInt(dos, unused19);
912         writeInt(dos, unused20);
913         writeInt(dos, unused21);
914 
915         writeInt(dos, unused22);
916         writeInt(dos, unused23);
917         writeInt(dos, unused24);
918         writeInt(dos, unused25);
919         writeInt(dos, unused26);
920 
921         writeInt(dos, leven);
922         writeInt(dos, lpspol);
923         writeInt(dos, lovrok);
924         writeInt(dos, lcalda);
925         writeInt(dos, unused27);
926 
927         if (kstnm.length() > 8) {kstnm = kstnm.substring(0,7); }
928         while (kstnm.length() < 8) {kstnm += " "; }
929         dos.writeBytes(kstnm);
930         if (kevnm.length() > 16) {kevnm = kevnm.substring(0,15); }
931         while (kevnm.length() < 16) { kevnm += " "; }
932         dos.writeBytes(kevnm);
933 
934         if (khole.length() > 8) {khole = khole.substring(0,7); }
935         while (khole.length() < 8) {khole += " "; }
936         dos.writeBytes(khole);
937         if (ko.length() > 8) {ko = ko.substring(0,7); }
938         while (ko.length() < 8) {ko += " "; }
939         dos.writeBytes(ko);
940         if (ka.length() > 8) {ka = ka.substring(0,7); }
941         while (ka.length() < 8) {ka += " "; }
942         dos.writeBytes(ka);
943 
944         if (kt0.length() > 8) {kt0 = kt0.substring(0,7); }
945         while (kt0.length() < 8) {kt0 += " "; }
946         dos.writeBytes(kt0);
947         if (kt1.length() > 8) {kt1 = kt1.substring(0,7); }
948         while (kt1.length() < 8) {kt1 += " "; }
949         dos.writeBytes(kt1);
950         if (kt2.length() > 8) {kt2 = kt2.substring(0,7); }
951         while (kt2.length() < 8) {kt2 += " "; }
952         dos.writeBytes(kt2);
953 
954         if (kt3.length() > 8) {kt3 = kt3.substring(0,7); }
955         while (kt3.length() < 8) {kt3 += " "; }
956         dos.writeBytes(kt3);
957         if (kt4.length() > 8) {kt4 = kt4.substring(0,7); }
958         while (kt4.length() < 8) {kt4 += " "; }
959         dos.writeBytes(kt4);
960         if (kt5.length() > 8) {kt5 = kt5.substring(0,7); }
961         while (kt5.length() < 8) {kt5 += " "; }
962         dos.writeBytes(kt5);
963 
964         if (kt6.length() > 8) {kt6 = kt6.substring(0,7); }
965         while (kt6.length() < 8) {kt6 += " "; }
966         dos.writeBytes(kt6);
967         if (kt7.length() > 8) {kt7 = kt7.substring(0,7); }
968         while (kt7.length() < 8) {kt7 += " "; }
969         dos.writeBytes(kt7);
970         if (kt8.length() > 8) {kt8 = kt8.substring(0,7); }
971         while (kt8.length() < 8) {kt8 += " "; }
972         dos.writeBytes(kt8);
973 
974         if (kt9.length() > 8) {kt9 = kt9.substring(0,7); }
975         while (kt9.length() < 8) {kt9 += " "; }
976         dos.writeBytes(kt9);
977         if (kf.length() > 8) {kf = kf.substring(0,7); }
978         while (kf.length() < 8) {kf += " "; }
979         dos.writeBytes(kf);
980         if (kuser0.length() > 8) {kuser0 = kuser0.substring(0,7); }
981         while (kuser0.length() < 8) {kuser0 += " "; }
982         dos.writeBytes(kuser0);
983 
984         if (kuser1.length() > 8) {kuser1 = kuser1.substring(0,7); }
985         while (kuser1.length() < 8) {kuser1 += " "; }
986         dos.writeBytes(kuser1);
987         if (kuser2.length() > 8) {kuser2 = kuser2.substring(0,7); }
988         while (kuser2.length() < 8) {kuser2 += " "; }
989         dos.writeBytes(kuser2);
990         if (kcmpnm.length() > 8) {kcmpnm = kcmpnm.substring(0,7); }
991         while (kcmpnm.length() < 8) {kcmpnm += " "; }
992         dos.writeBytes(kcmpnm);
993 
994         if (knetwk.length() > 8) {knetwk = knetwk.substring(0,7); }
995         while (knetwk.length() < 8) {knetwk += " "; }
996         dos.writeBytes(knetwk);
997         if (kdatrd.length() > 8) {kdatrd = kdatrd.substring(0,7); }
998         while (kdatrd.length() < 8) {kdatrd += " "; }
999         dos.writeBytes(kdatrd);
1000         if (kinst.length() > 8) {kinst = kinst.substring(0,7); }
1001         while (kinst.length() < 8) {kinst += " "; }
1002         dos.writeBytes(kinst);
1003 
1004     }
1005 
1006     public void writeData(DataOutputStream dos)
1007         throws IOException {
1008 
1009         for (int i=0; i<npts; i++) {
1010             writeFloat(dos, y[i]);
1011         }
1012 
1013 
1014         if (leven == SacTimeSeries.FALSE ||
1015             iftype == SacTimeSeries.IRLIM ||
1016             iftype == SacTimeSeries.IAMPH) {
1017             for (int i=0; i<npts; i++) {
1018                 writeFloat(dos, x[i]);
1019             }
1020         }
1021     }
1022 
1023 
1024     public static final DecimalFormat decimalFormat = new DecimalFormat("#####.####");
1025 
1026     public static String format(String label, float f) {
1027         String s = label+" = ";
1028         String fString = decimalFormat.format(f);
1029         while (fString.length() < 8) {
1030             fString = " "+fString;
1031         }
1032         s = s+fString;
1033         while (s.length() < 21) {
1034             s = " "+s;
1035         }
1036         return s;
1037     }
1038 
1039     public static String formatLine(String s1, float f1, String s2, float f2, String s3, float f3, String s4, float f4, String s5, float f5) {
1040 
1041         return format(s1,f1)+
1042             format(s2,f2)+
1043             format(s3,f3)+
1044             format(s4,f4)+
1045             format(s5,f5);
1046     }
1047 
1048     public void printHeader() {
1049         System.out.println(formatLine("delta",delta,
1050                                       "depmin",depmin,
1051                                       "depmax",depmax,
1052                                       "scale",scale,
1053                                       "odelta",odelta));
1054 
1055         System.out.println(formatLine("b",b,
1056                                       "e",e,
1057                                       "o",o,
1058                                       "a",a,
1059                                       "fmt",fmt));
1060 
1061         System.out.println(formatLine("t0",t0,
1062                                       "t1",t1,
1063                                       "t2",t2,
1064                                       "t3",t3,
1065                                       "t4",t4));
1066 
1067         System.out.println(formatLine("t5",t5,
1068                                       "t6",t6,
1069                                       "t7",t7,
1070                                       "t8",t8,
1071                                       "t9",t9));
1072 
1073         System.out.println(formatLine("f",f,
1074                                       "resp0",resp0,
1075                                       "resp1",resp1,
1076                                       "resp2",resp2,
1077                                       "resp3",resp3));
1078 
1079         System.out.println(formatLine("resp4",resp4,
1080                                       "resp5",resp5,
1081                                       "resp6",resp6,
1082                                       "resp7",resp7,
1083                                       "resp8",resp8));
1084 
1085         System.out.println(formatLine("resp9",resp9,
1086                                       "stla",stla,
1087                                       "stlo",stlo,
1088                                       "stel",stel,
1089                                       "stdp",stdp));
1090 
1091         System.out.println(formatLine("evla",evla,
1092                                       "evlo",evlo,
1093                                       "evel",evel,
1094                                       "evdp",evdp,
1095                                       "mag",mag));
1096 
1097         System.out.println(formatLine("user0",user0,
1098                                       "user1",user1,
1099                                       "user2",user2,
1100                                       "user3",user3,
1101                                       "user4",user4));
1102 
1103         System.out.println(formatLine("user5",user5,
1104                                       "user6",user6,
1105                                       "user7",user7,
1106                                       "user8",user8,
1107                                       "user9",user9));
1108 
1109         System.out.println(formatLine("dist",dist,
1110                                       "az",az,
1111                                       "baz",baz,
1112                                       "gcarc",gcarc,
1113                                       "sb",sb));
1114 
1115         System.out.println(formatLine("sdelta",sdelta,
1116                                       "depmen",depmen,
1117                                       "cmpaz",cmpaz,
1118                                       "cmpinc",cmpinc,
1119                                       "xminimum",xminimum));
1120 
1121         System.out.println(formatLine("xmaximum",xmaximum,
1122                                       "yminimum",yminimum,
1123                                       "ymaximum",ymaximum,
1124                                       "unused6",unused6,
1125                                       "unused7",unused7));
1126 
1127         System.out.println(formatLine("unused8",unused8,
1128                                       "unused9",unused9,
1129                                       "unused10",unused10,
1130                                       "unused11",unused11,
1131                                       "unused12",unused12));
1132 
1133         System.out.println(formatLine("nzyear",nzyear,
1134                                       "nzjday",nzjday,
1135                                       "nzhour",nzhour,
1136                                       "nzmin",nzmin,
1137                                       "nzsec",nzsec));
1138 
1139         System.out.println(formatLine("nzmsec",nzmsec,
1140                                       "nvhdr",nvhdr,
1141                                       "norid",norid,
1142                                       "nevid",nevid,
1143                                       "npts",npts));
1144 
1145         System.out.println(formatLine("nsnpts",nsnpts,
1146                                       "nwfid",nwfid,
1147                                       "nxsize",nxsize,
1148                                       "nysize",nysize,
1149                                       "unused15",unused15));
1150 
1151         System.out.println(formatLine("iftype",iftype,
1152                                       "idep",idep,
1153                                       "iztype",iztype,
1154                                       "unused16",unused16,
1155                                       "iinst",iinst));
1156 
1157         System.out.println(formatLine("istreg",istreg,
1158                                       "ievreg",ievreg,
1159                                       "ievtyp",ievtyp,
1160                                       "iqual",iqual,
1161                                       "isynth",isynth));
1162 
1163         System.out.println(formatLine("imagtyp",imagtyp,
1164                                       "imagsrc",imagsrc,
1165                                       "unused19",unused19,
1166                                       "unused20",unused20,
1167                                       "unused21",unused21));
1168 
1169         System.out.println(formatLine("unused22",unused22,
1170                                       "unused23",unused23,
1171                                       "unused24",unused24,
1172                                       "unused25",unused25,
1173                                       "unused26",unused26));
1174 
1175         System.out.println(formatLine("leven",leven,
1176                                       "lpspol",lpspol,
1177                                       "lovrok",lovrok,
1178                                       "lcalda",lcalda,
1179                                       "unused27",unused27));
1180 
1181         System.out.println(
1182             " kstnm = "+kstnm+
1183                 " kevnm = "+kevnm+
1184                 " khole = "+khole+
1185                 " ko = "+ko);
1186 
1187         System.out.println(
1188             " ka = "+ka+
1189                 " kt0 = "+kt0+
1190                 " kt1 = "+kt1+
1191                 " kt2 = "+kt2);
1192         System.out.println(
1193             " kt3 = "+kt3+
1194                 " kt4 = "+kt4+
1195                 " kt5 = "+kt5+
1196                 " kt6 = "+kt6);
1197         System.out.println(
1198             " kt7 = "+kt7+
1199                 " kt8 = "+kt8+
1200                 " kt9 = "+kt9+
1201 
1202                 " kf = "+kf);
1203         System.out.println(
1204             " kuser0 = "+kuser0+
1205                 " kuser1 = "+kuser1+
1206                 " kuser2 = "+kuser2+
1207                 " kcmpnm = "+kcmpnm);
1208         System.out.println(
1209             " knetwk = "+knetwk+
1210                 " kdatrd = "+kdatrd+
1211                 " kinst = "+kinst);
1212     }
1213 
1214     /*** just for testing. Reads the filename given as the argument,
1215      *  writes out some header variables and then
1216      *  writes it back out as "outsacfile".
1217      */
1218     public static void main(String[] args) {
1219         SacTimeSeries data = new SacTimeSeries();
1220 
1221         if (args.length != 1) {
1222             System.out.println("Usage: java SacTimeSeries sacsourcefile ");
1223             return;
1224         }
1225 
1226         try {
1227 
1228             data.read(args[0]);
1229             //    data.y = new float[100000];
1230             //     for (int i=0; i<100000; i++) {
1231             //         data.y[i] = (float)Math.sin(Math.PI*i/18000)/1000000.0f;
1232             //         data.y[i] = (float)Math.sin(Math.PI*i/18000);
1233             //         //System.out.println("point is " + data.y[i]);
1234             //     }
1235             //     data.npts = data.y.length;
1236 
1237             data.printHeader();
1238             data.byteOrder = IntelByteOrder;
1239             data.write("outsacfile");
1240             System.out.println("Done writing");
1241         } catch (FileNotFoundException e) {
1242             System.out.println("File "+args[0]+" doesn't exist.");
1243         } catch (IOException e) {
1244             System.out.println("IOException: "+e.getMessage());
1245         }
1246     }
1247 }