import java.io.*; import java.util.*; class SEGYTexans { static boolean P3=false; public static void main(String[] argv) { if (argv.length < 5 || argv.length > 6) { System.err.println("Usage: java SEGYTexans serials.txt records.txt" + " year startsec intersec [-P3]\n" + "Run above the following default directory structure,\n" + "generated by placing raw files in daily subdirectories\n" + "and there running:\n" + " 125_segy InnnnRAW.TRD -P1\n" + "for each raw TRD file:\n" + "./day/SEGY/Ryear_day/year_day_hr_mn_sc_1nnnn_1.RSY\n" + "With the -P3 option at the end the run and directory structure are:\n" + " 125_segy InnnnRAW.TRD -P3\n" + "./year-day/Ryear_day/hr_mn/year_day_hr_mn_sc_1nnnn_1.RSY"); return; } int year = Integer.parseInt(argv[2]); if (year < 1985 || year > 2020) { System.err.println("SEGYTexans Aborting: the year must be\n" + "between 1985 and 2020 (simple to rewire in java source)."); return; } int startsec = Integer.parseInt(argv[3]); int intersec = Integer.parseInt(argv[4]); if (startsec < 0 || intersec < 0 || startsec > intersec || intersec < 4 || intersec > 3600) { System.err.println("SEGYTexans Aborting: startsec is number\n" + "of seconds after each even hour that records begin;\n" + "intersec is seconds between records.\n" + "Usually startsec=0 and intersec=150\n" + "(for 120-second records)."); return; } String reclines = readRecords(argv[1]); if (reclines == null) { System.err.println("SEGYTexans: Aborting."); return; } int serials[] = readSerials(argv[0]); if (serials == null) { System.err.println("SEGYTexans: Aborting."); return; } if (argv.length == 6) if (argv[5].equals("-P3")) SEGYTexans.P3 = true; StringBufferInputStream sbis = new StringBufferInputStream(reclines); InputStreamReader isr = new InputStreamReader(sbis); BufferedReader br = new BufferedReader(isr); StringTokenizer st; int lines = 0; String line = null; int sta, nflags, serial0, serialf, day, hr0, min0, hrf, minf; try { while ((line = br.readLine()) != null) { if (line.length() < 17) continue; st = new StringTokenizer(line); if (st.hasMoreTokens()) { try {sta = Integer.valueOf(st.nextToken()).intValue(); } catch (NumberFormatException nfe) {continue; } } else continue; if (st.hasMoreTokens()) { try {nflags = Integer.valueOf(st.nextToken()).intValue(); } catch (NumberFormatException nfe) {continue; } } else continue; if (st.hasMoreTokens()) { try {serial0 = Integer.valueOf(st.nextToken()).intValue(); } catch (NumberFormatException nfe) {continue; } } else continue; if (st.hasMoreTokens()) { try {serialf = Integer.valueOf(st.nextToken()).intValue(); } catch (NumberFormatException nfe) {continue; } } else continue; if (st.hasMoreTokens()) { try {day = Integer.valueOf(st.nextToken()).intValue(); } catch (NumberFormatException nfe) {continue; } } else continue; if (st.hasMoreTokens()) { try {hr0 = Integer.valueOf(st.nextToken()).intValue(); } catch (NumberFormatException nfe) {continue; } } else continue; if (st.hasMoreTokens()) { try {min0 = Integer.valueOf(st.nextToken()).intValue(); } catch (NumberFormatException nfe) {continue; } } else continue; if (st.hasMoreTokens()) { try {hrf = Integer.valueOf(st.nextToken()).intValue(); } catch (NumberFormatException nfe) {continue; } } else continue; if (st.hasMoreTokens()) { try {minf = Integer.valueOf(st.nextToken()).intValue(); } catch (NumberFormatException nfe) {continue; } } else continue; if (makeRec(serials, year, startsec, intersec, serial0, serialf, sta, nflags, day, hr0, min0, hrf, minf)) lines++; else System.err.println("Could not assemble record for entry:\n" + sta + " " + nflags + " " + serial0 + " " + serialf + " " + day + " " + hr0 + " " + min0 + " " + hrf + " " + minf + "\nin records file " + argv[1]); } } catch (IOException ioe) { System.err.println("Internal IOException parsing record file text:\n" + ioe); } System.err.println("Assembled " + lines + " records based on file " + argv[1] + " Done."); return; } static int getNTraces(int sta, int serials[], int nflags, int serial0, int serialf, int year, int day, int hr, int min, int sec) { int ntraces=0, index, serial; File trace; index = getSerialIndex(serials, serial0); int i; for (i=index; i<(index+nflags); i++) { serial = serials[i]; trace = new File(makePath(serial, year, day, hr, min, sec)); /*System.err.println("Checking " + trace.getAbsolutePath());*/ if (trace.exists()) if (trace.canRead() && trace.isFile() && trace.length() > 240) ntraces++; } if (serials[i-1] != serialf) { System.err.println("Entry at index " + (i-1) + " in serials list is " + serials[i-1] + " but final serial number in record is " + serialf); System.err.println("" + sta + " " + nflags + " " + serial0 + " " + serialf + " " + day); return -1; } return ntraces; } static int getSerialIndex(int serials[], int number) { if (serials == null) return -1; int i; for (i=0; i 240) { traces[i] = hdrarray[i].importSEGYFile (makePath(serial, year, day, hr, min, sec), false, false, 1, 111111, 2); if (traces[i] == null || traces[i].headers[0] == null) { traces[i] = null; break; } mindelays[i] = 0; ntraces++; } else /* Search back in 2-min increments, three times */ { tyear = year; tday = day; thr = hr; tmin = min; /* Going back 2 minutes might require decrementing the hour, day, and year (not valid for year 2100 */ for (int t=0; t<3; t++) { if (tmin < 2) { tmin = 60 + tmin - 2; if (thr < 1) { thr = 24 + thr - 1; if (tday < 1) { tday = (tyear-1)%4 == 0 ? 366 : 365; tyear = tyear - 1; } else tday = tday - 1; } else thr = thr - 1; } else tmin = tmin - 2; /* Compose file path */ trace = new File( makePath(serial, tyear, tday, thr, tmin, sec)); /* Read SEG-Y single trace if exists */ if (trace.exists() && trace.canRead() && trace.isFile() && trace.length() > 240) { traces[i] = hdrarray[i].importSEGYFile (makePath(serial, tyear, tday, thr, tmin, sec), false, false, 1, 111111, 2); if (traces[i] == null || traces[i].headers[0] == null) { traces[i] = null; break; } mindelays[i] = (t+1)*2; ntraces++; break; } } } } int missing; missing = 0; if (ntraces < nflags && ntraces != 0) missing = nflags - ntraces; if (missing > 0) System.err.println("Rec " + year + " " + day + " " + hr + " " + min + " " + sec + " is missing " + missing + " of " + nflags + " possible traces."); /* create record for output and save */ int maxsamp, minsamp, validindex; maxsamp = 0; minsamp = (int)(1e11); validindex = -1; for (int i=0; i 0) continue; if (validindex < 0) validindex = i; if (traces[i].ne > maxsamp) maxsamp = traces[i].ne; if (traces[i].ne < minsamp) minsamp = traces[i].ne; } if (maxsamp < 1 || maxsamp > 1e10) { System.err.println("Max samples/trace=" + maxsamp + " improper, no record from " + year + " " + day + " " + hr + " " + min + " " + sec); return; } float dt; dt = traces[validindex].DElem; System.err.println("Max samples/trace=" + maxsamp + ", Min=" + minsamp + "; intersec/dt=" + (int)(intersec/dt)); /* Establish blank record, set units */ if (maxsamp > intersec/dt) maxsamp = (int)(intersec/dt); System.err.println("Allocating FltPlane with " + nflags + " traces and " + maxsamp + " samples/trace"); FltPlane record = new FltPlane(nflags, maxsamp); record.headers = new RgHead[nflags]; record.copyunits(traces[validindex]); record.DElem = dt; System.err.println("Assembling FltPlane with " + record.vecs + " traces, " + record.ne + " samples, for " + record.vec.length + " samples total"); int t, t1, ffid; for (int i=0; i 3600/4) return false; int winmin[] = new int[hourlywindows]; int winsec[] = new int[hourlywindows]; int hrsec, i; for (i=0, hrsec=startsec; i 4000 || hrf < hr0) return false; int planes = 0; int tracespresent; int hr, min, iwin; for (hr=hr0; hr<=hrf; hr++) { for (min=(hr==hr0?min0:0); min<=(hr==hrf?minf:59); min++) { for (i=0; i nflags/4) { writeRecord(planes, sta, serials, nflags, serial0, serialf, year, day, hr, min, winsec[i], intersec); planes++; } else continue; } else break; } } } if (planes == 0) { System.err.println("Could not assemble any records from entry:\n" + sta + " " + nflags + " " + serial0 + " " + serialf + " " + day + " " + hr0 + " " + min0 + " " + hrf + " " + minf); return false; } else System.err.println("Assembled " + planes + " records of " + nflags + " traces each from entry:\n" + sta + " " + nflags + " " + serial0 + " " + serialf + " " + day + " " + hr0 + " " + min0 + " " + hrf + " " + minf); return true; } static String readRecords(String records) { FileReader fr; try {fr = new FileReader(records); } catch(FileNotFoundException fnf) { System.err.println("Can't find records file " + records); return null; } String buf = ""; String temp = ""; BufferedReader in; try { in = new BufferedReader(fr); while (temp != null) { temp = in.readLine(); if (temp != null) buf += temp + "\n"; } } catch(IOException ioe) { System.err.println("Can't read records file " + records); return null; } try { in.close(); fr.close(); } catch(IOException ioe) { System.err.println("Can't close records file " + records + " after reading."); } if (buf.length() <= 0) { System.err.println("File " + records + " does not appear to contain any text."); return null; } return buf; } static int[] readSerials(String serialsfile) { FileReader fr; try {fr = new FileReader(serialsfile); } catch(FileNotFoundException fnf) { System.err.println("Can't find serial-number list file " + serialsfile); return null; } String buf = ""; String temp = ""; BufferedReader in; try { in = new BufferedReader(fr); while (temp != null) { temp = in.readLine(); if (temp != null) buf += temp + "\n"; } } catch(IOException ioe) { System.err.println("Can't read serial-number list file " + serialsfile); return null; } try { in.close(); fr.close(); } catch(IOException ioe) { System.err.println("Can't close serial-number list file " + serialsfile + " after reading."); } if (buf.length() <= 0) { System.err.println("File " + serialsfile + " does not appear to contain any text."); return null; } StringBufferInputStream sbis = new StringBufferInputStream(buf); InputStreamReader isr = new InputStreamReader(sbis); BufferedReader br = new BufferedReader(isr); StringTokenizer st; int lines = 0; String line = null; int serial, nserials; nserials = 0; try { while ((line = br.readLine()) != null) { if (line.length() < 2) continue; st = new StringTokenizer(line); while (st.hasMoreTokens()) { try {serial = Integer.valueOf(st.nextToken()).intValue(); } catch (NumberFormatException nfe) {continue; } nserials++; } } } catch (IOException ioe) { System.err.println("Internal IOException parsing serial-number list file text:\n" + ioe); } if (nserials < 1 || nserials > 10000) { System.err.println("" + nserials + " serial numbers found in file " + serialsfile + ", but there are not more than a few thousand instruments!"); return null; } int serials[] = new int[nserials]; int index; index = 0; sbis = new StringBufferInputStream(buf); isr = new InputStreamReader(sbis); br = new BufferedReader(isr); try { while ((line = br.readLine()) != null) { if (line.length() < 2) continue; st = new StringTokenizer(line); while (st.hasMoreTokens()) { try {serials[index] = Integer.valueOf(st.nextToken()).intValue(); } catch (NumberFormatException nfe) {continue; } index++; } } } catch (IOException ioe) { System.err.println("Internal IOException parsing serial-number list file text:\n" + ioe); } System.err.println("found " + nserials + " serial numbers based on file " + serialsfile + " ."); return serials; } /* End of SEGYTexans class */ }