import java.io.*; import java.util.*; class AssembleTexans { static boolean P3=false; public static void main(String[] argv) { if (argv.length < 5 || argv.length > 6) { System.out.println("Usage: java AssembleTexans 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" + "./day/SEGY/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.out.println("AssembleTexans 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.out.println("AssembleTexans 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.out.println("AssembleTexans: Aborting."); return; } int serials[] = readSerials(argv[0]); if (serials == null) { System.out.println("AssembleTexans: Aborting."); return; } if (argv.length == 6) if (argv[5].equals("-P3")) AssembleTexans.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.out.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.out.println("Internal IOException parsing record file text:\n" + ioe); } System.out.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.out.println("Checking " + trace.getAbsolutePath());*/ if (trace.exists()) if (trace.canRead() && trace.isFile() && trace.length() > 240) ntraces++; } if (serials[i-1] != serialf) { System.out.println("Entry at index " + (i-1) + " in serials list is " + serials[i-1] + " but final serial number in record is " + serialf); System.out.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) { tracesize = (int)(trace.length()); buf = new byte[tracesize]; try { fis = new FileInputStream(trace); fis.read(buf); fis.close(); } catch (IOException ioe) { System.out.println("AssembleTexans Aborting:\n" + "Could not read " + tracesize + " bytes from trace: " + trace.getAbsolutePath() + "\n" + ioe); System.exit(-1); } if (planes == 0 && ntraces == 0) { try { fos = new FileOutputStream( makeOutPath(sta, day)); fos.write(buf); fos.close(); } catch (IOException ioe) { System.out.println("AssembleTexans Aborting:\n" + "Could not create " + tracesize + "-byte output file" + makeOutPath(sta, day) + "\n" + ioe); System.exit(-1); } } else { try { raf = new RandomAccessFile(makeOutPath(sta, day), "rw"); raf.seek(raf.length()); raf.write(buf); raf.close(); } catch (IOException ioe) { System.out.println("AssembleTexans Aborting:\n" + "Could not append " + tracesize + " bytes to output file" + makeOutPath(sta, day) + "\n" + ioe); System.exit(-1); } } ntraces++; } else if (ntraces!=0) { try { raf = new RandomAccessFile(makeOutPath(sta, day), "rw"); raf.seek(raf.length()); raf.write(buf); raf.close(); } catch (IOException ioe) { System.out.println("AssembleTexans Aborting:\n" + "Could not append " + tracesize + " bytes to output file" + makeOutPath(sta, day) + "\n" + ioe); System.exit(-1); } System.out.println("Copied serial no. " + (serial-1) + " trace to missing serial no. " + serial + " in output " + makeOutPath(sta, day)); ntraces++; } } int missing; if (ntraces < nflags && ntraces != 0) { missing = nflags - ntraces; try { raf = new RandomAccessFile(makeOutPath(sta, day), "rw"); raf.seek(raf.length()); for (; ntraces 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/2) { writeRecord(planes, sta, serials, nflags, serial0, serialf, year, day, hr, min, winsec[i]); planes++; } else continue; } else break; } } } if (planes == 0) { System.out.println("Could not assemble any records from entry:\n" + sta + " " + nflags + " " + serial0 + " " + serialf + " " + day + " " + hr0 + " " + min0 + " " + hrf + " " + minf); return false; } else System.out.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.out.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.out.println("Can't read records file " + records); return null; } try { in.close(); fr.close(); } catch(IOException ioe) { System.out.println("Can't close records file " + records + " after reading."); } if (buf.length() <= 0) { System.out.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.out.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.out.println("Can't read serial-number list file " + serialsfile); return null; } try { in.close(); fr.close(); } catch(IOException ioe) { System.out.println("Can't close serial-number list file " + serialsfile + " after reading."); } if (buf.length() <= 0) { System.out.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.out.println("Internal IOException parsing serial-number list file text:\n" + ioe); } if (nserials < 1 || nserials > 10000) { System.out.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.out.println("Internal IOException parsing serial-number list file text:\n" + ioe); } System.out.println("found " + nserials + " serial numbers based on file " + serialsfile + " ."); return serials; } /* End of AssembleTexans class */ }