/* JRG, the Resource Geology Seismic Processing System for Java, and Viewmat for Java are Copyright 1998-2007 by John N. Louie License is hereby granted for anyone to make any use of this software and its associated source and compiled binary codes, including free re-distribution, and incorporation into commercial products, provided this copyright notice is retained within all files, versions, and distribution media substantially incorporating John Louie's original work. The software and methods here are the subject of academic research, not commercial products. I would like to know what use you make of my methods, and have your feedback on their success or failure. Also, by letting me know who you are, I can inform you if bugs or errors are discovered. Please send me an email message with: your name and email address; whether you are a student or faculty member, consultant or employee; the name of your university or company, and department; and a sentence or two describing what use you intend to make of these methods. Send your message to louie@seismo.unr.edu */ import java.io.*; public class SACTraceHead { public int nt; /* number of time-points in trace */ public float dt; /* time sample interval, second */ static final int SACTRACEHEADWORDS = 158; static final int SACTRACEHEADBYTES = SACTRACEHEADWORDS*4; public boolean isValid() { if (nt > 0 && nt < 1000000000 && dt > 0F && dt < 1e20) return true; return false; } public boolean equals(SACTraceHead thd) { if (this.nt == thd.nt && this.dt == thd.dt) return true; return false; } public void copy(SACTraceHead hd) { this.nt = hd.nt; /* number of time-points in trace */ this.dt = (float)(hd.dt); /* time sample interval, second */ } public void read(RandomAccessFile dis) throws IOException, EOFException { int i, tmp; dt = dis.readFloat(); for (i=1; i<79; i++) tmp = dis.readInt(); nt = dis.readInt(); for (i++; i< SACTRACEHEADWORDS; i++) tmp = dis.readInt(); } public void write(DataOutputStream dos) throws IOException { int i, tmp; tmp = 0; dos.writeFloat(dt); for (i=1; i<79; i++) dos.writeInt(tmp); dos.writeInt(nt); for (i++; i< SACTRACEHEADWORDS; i++) dos.writeInt(tmp); } public void readIntel(RandomAccessFile dis) throws IOException, EOFException { int i, tmp; dt = FltVec.readRAFIntelFloat(dis); for (i=1; i<79; i++) tmp = dis.readInt(); nt = FltVec.readRAFIntelInt(dis); for (i++; i< SACTRACEHEADWORDS; i++) tmp = dis.readInt(); } public String toString() { return "nt=" + nt + " dt=" + Viewmat.getSignif("" + dt); } public static RgFileHead scanSACFile(String path, int np, boolean isIntel) { int count, i; long bytes; boolean eof; RandomAccessFile raf; SACTraceHead thd = new SACTraceHead(); SACTraceHead temphd = new SACTraceHead(); RgFileHead fhd = new RgFileHead(); fhd.ntrace = 0; try { raf = new RandomAccessFile(path, "r"); } catch(IOException ioe) { System.out.println("Cannot open file " + path + "\nfor random read access: " + ioe); return fhd; } catch(SecurityException se) { System.out.println("Read access to file " + path + "\nnot permitted: " + se); return fhd; } bytes = (long)(0); try { raf.seek(bytes); } catch(IOException ioe) { System.out.println("Cannot seek in SAC file " + path + "\nto " + bytes + " bytes: " + ioe); return fhd; } eof = false; count = 0; /* Read SAC trace headers to EOF */ for (i=0; !eof && i<1000000; i++) { try { if (isIntel) temphd.readIntel(raf); else temphd.read(raf); } catch(EOFException eofe) { eof = true; break; } catch(IOException ioe) { System.out.println("IO Exception reading file " + path + "\nfor SAC trace header, i=" + i + ": " + ioe); return fhd; } if (temphd.isValid()) { if (i==0) thd.copy(temphd); if (temphd.equals(thd)) count++; else System.out.println("SAC file " + path + "\ntrace header at " + bytes + " bytes offset: header " + temphd + " varies from leading trace header: " + thd); } else { System.out.println("SAC file " + path + "\ndoes not have a valid SAC trace header at " + bytes + " bytes offset: only " + count + " traces identified."); break; } bytes += SACTRACEHEADBYTES; bytes += temphd.nt*4; try { raf.seek(bytes); } catch(IOException ioe) { System.out.println("Cannot seek in SAC file " + path + "\nto " + bytes + " bytes: " + ioe); return fhd; } if (i==0 || i==1 || i==9 || i==99 || i%1000 == 999) System.out.println("Read SAC trace header " + (i+1)); } System.out.println("" + i + " trace headers, with " + count + " matching the first read from SAC file " + path); try { raf.close(); } catch(IOException ioe) { System.out.println("Could not close file " + path + "\nafter random read access."); } fhd.ntrace = count; fhd.dt = thd.dt; fhd.nt = thd.nt; fhd.starttime = 0F; return fhd; } public static void readSACFiletoFP(FltPlane fp, String path, RgFileHead fhd, boolean isIntel) { int count, i, j; long bytes; boolean eof; RandomAccessFile raf; SACTraceHead thd = new SACTraceHead(); SACTraceHead temphd = new SACTraceHead(); try { raf = new RandomAccessFile(path, "r"); } catch(IOException ioe) { System.out.println("Cannot open file " + path + "\nfor random read access: " + ioe); return; } catch(SecurityException se) { System.out.println("Read access to file " + path + "\nnot permitted: " + se); return; } bytes = (long)(0); try { raf.seek(bytes); } catch(IOException ioe) { System.out.println("Cannot seek in SAC file " + path + "\nto " + bytes + " bytes: " + ioe); return; } eof = false; count = 0; /* Read SAC trace headers to EOF */ for (i=0; !eof && i<1000000; i++) { try { if (isIntel) temphd.readIntel(raf); else temphd.read(raf); } catch(EOFException eofe) { eof = true; break; } catch(IOException ioe) { System.out.println("IO Exception reading file " + path + "\nfor SAC trace header, i=" + i + ": " + ioe); return; } if (temphd.isValid()) { if (i==0) thd.copy(temphd); } else { System.out.println("SAC file " + path + "\ndoes not have a valid trace header at " + bytes + " bytes offset: only " + count + " traces read."); break; } bytes += SACTRACEHEADBYTES; bytes += temphd.nt*4; if (temphd.equals(thd)) { try { if (isIntel) for (j=0; j