Example #1
0
 Date exportChannel(
     WinstonUtil winston, WinstonSCNL channel, Date begin, Date end, EarthwormExport exporter)
     throws SeisFileException, SQLException, DataFormatException, FileNotFoundException,
         IOException, URISyntaxException {
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
   sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
   List<TraceBuf2> tbList = winston.extractData(channel, begin, end);
   Date lastSentEnd = end;
   double sampRate = 1;
   TraceBuf2 prev = null;
   for (TraceBuf2 traceBuf2 : tbList) {
     if (params.isVerbose()) {
       System.out.println(
           "Tracebuf: "
               + traceBuf2.getNetwork()
               + "."
               + traceBuf2.getStation()
               + "."
               + traceBuf2.getLocId()
               + "."
               + traceBuf2.getChannel()
               + " "
               + sdf.format(traceBuf2.getStartDate())
               + " "
               + traceBuf2.getNumSamples()
               + " "
               + sdf.format(traceBuf2.getEndDate()));
     }
     if (prev != null && prev.getEndDate().after(traceBuf2.getStartDate())) {
       System.out.println("WARNING: current tracebuf overlaps previous: ");
       System.out.println("  prev: " + prev);
       System.out.println("  curr: " + traceBuf2);
     }
     boolean notSent = true;
     while (notSent) {
       try {
         exporter.export(traceBuf2);
         notSent = false;
       } catch (IOException e) {
         exporter.closeClient();
         if (params.isVerbose()) {
           System.out.println("Caught exception, waiting for reconnect, will resend tracebuf" + e);
         }
         logger.warn("Caught exception, waiting for reconnect, will resend tracebuf", e);
         exporter.waitForClient();
         if (params.isVerbose()) {
           System.out.println(
               "Resend Tracebuf: "
                   + traceBuf2.getNetwork()
                   + "."
                   + traceBuf2.getStation()
                   + "."
                   + traceBuf2.getLocId()
                   + "."
                   + traceBuf2.getChannel()
                   + " "
                   + sdf.format(traceBuf2.getStartDate())
                   + " "
                   + traceBuf2.getNumSamples()
                   + " "
                   + sdf.format(traceBuf2.getEndDate()));
         }
       }
     }
     if (lastSentEnd.before(traceBuf2.getPredictedNextStartDate())) {
       lastSentEnd = traceBuf2.getPredictedNextStartDate();
       sampRate = traceBuf2.getSampleRate();
     }
     if (params.isVerbose()) {
       System.out.print(
           "sleep: " + sleepMillis + " milliseconds " + sdf.format(new Date()) + " ...");
     }
     try {
       Thread.sleep(sleepMillis);
     } catch (InterruptedException e) {
     }
     if (params.isVerbose()) {
       System.out.println("...back to work at " + sdf.format(new Date()) + ".");
     }
   }
   return lastSentEnd;
 }
Example #2
0
 public void readData()
     throws SeisFileException, SQLException, DataFormatException, FileNotFoundException,
         IOException, URISyntaxException {
   if (params.isPrintHelp()) {
     System.out.println(getHelp());
     return;
   } else if (params.isPrintVersion()) {
     System.out.println("Version: " + BuildVersion.getDetailedVersion());
     return;
   } else if (params.getNetwork() == null
       || params.getStation() == null
       || params.getChannel() == null) {
     System.out.println(
         BuildVersion.getDetailedVersion()
             + " one of scnl is null: n="
             + params.getNetwork()
             + " s="
             + params.getStation()
             + " l="
             + params.getLocation()
             + " c="
             + params.getChannel());
     System.out.println("LocId null is ok for scn, but needed for scnl");
     return;
   }
   if (params.isVerbose()) {
     WinstonUtil.setVerbose(true);
   }
   WinstonUtil winston =
       new WinstonUtil(
           getDbURL(), getUser(), getPassword(), winstonConfig.getProperty("winston.prefix"));
   List<WinstonSCNL> allChannels = winston.listChannelDatabases();
   Pattern staPattern =
       Pattern.compile("*".equals(params.getStation()) ? ".*" : params.getStation());
   Pattern chanPattern =
       Pattern.compile("*".equals(params.getChannel()) ? ".*" : params.getChannel());
   Pattern netPattern =
       Pattern.compile("*".equals(params.getNetwork()) ? ".*" : params.getNetwork());
   Pattern locPattern =
       Pattern.compile("*".equals(params.getLocation()) ? ".*" : params.getLocation());
   if (doSync) {
     PrintWriter out =
         new PrintWriter(new BufferedWriter(new OutputStreamWriter(params.getDataOutputStream())));
     SyncFileWriter syncOut = new SyncFileWriter("winston", out);
     for (WinstonSCNL scnl : allChannels) {
       if (staPattern.matcher(scnl.getStation()).matches()
           && chanPattern.matcher(scnl.getChannel()).matches()
           && netPattern.matcher(scnl.getNetwork()).matches()
           && locPattern.matcher(scnl.getLocId() == null ? "" : scnl.getLocId()).matches()) {
         syncChannel(winston, scnl, syncOut);
       }
     }
     syncOut.close();
   } else if (doTbZip) {
     File f = new File(params.getOutFile());
     String dirName = f.getName();
     if (dirName.endsWith(".zip")) {
       dirName = dirName.substring(0, dirName.length() - 4);
     } else {
       dirName = dirName + "_TraceBufs";
     }
     ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
     ZipEntry tbzip = new ZipEntry(dirName + "/");
     zip.putNextEntry(tbzip);
     zip.closeEntry();
     for (WinstonSCNL scnl : allChannels) {
       if (staPattern.matcher(scnl.getStation()).matches()
           && chanPattern.matcher(scnl.getChannel()).matches()
           && netPattern.matcher(scnl.getNetwork()).matches()
           && locPattern.matcher(scnl.getLocId() == null ? "" : scnl.getLocId()).matches()) {
         outputRawTraceBuf2s(winston, scnl, zip, dirName);
       }
     }
     zip.close();
   } else if (doExport) {
     EarthwormExport exporter =
         new EarthwormExport(exportPort, module, institution, "heartbeat", heartbeat);
     if (heartbeatverbose) {
       exporter.getHeartbeater().setVerbose(heartbeatverbose);
     }
     if (params.isVerbose()) {
       exporter.setVerbose(true);
       System.out.println("Waiting for client connect, port: " + exportPort);
     }
     exporter.waitForClient();
     Date startTime = params.getBegin();
     Date chunkBegin, chunkEnd;
     HashMap<WinstonSCNL, Date> lastSent = new HashMap<WinstonSCNL, Date>();
     for (WinstonSCNL scnl : allChannels) {
       if (staPattern.matcher(scnl.getStation()).matches()
           && chanPattern.matcher(scnl.getChannel()).matches()
           && netPattern.matcher(scnl.getNetwork()).matches()
           && locPattern.matcher(scnl.getLocId() == null ? "" : scnl.getLocId()).matches()) {
         lastSent.put(scnl, startTime);
       }
     }
     while (startTime.before(params.getEnd())) {
       chunkEnd = new Date(startTime.getTime() + chunkSeconds * 1000);
       for (WinstonSCNL scnl : lastSent.keySet()) {
         chunkBegin = lastSent.get(scnl);
         if (chunkBegin.before(chunkEnd)) {
           Date sentEnd = exportChannel(winston, scnl, chunkBegin, chunkEnd, exporter);
           // sendEnd is expected time of next sample, ie 1 samp period after end time of last tb
           lastSent.put(scnl, new Date(sentEnd.getTime() + 1));
         }
       }
       startTime = new Date(chunkEnd.getTime() + 1);
     }
     exporter.closeSocket();
     if (params.isVerbose()) {
       System.out.println(
           "Done sending, "
               + exporter.getNumTraceBufSent()
               + " ("
               + exporter.getNumSplitTraceBufSent()
               + " too big so split)");
     }
   } else {
     for (WinstonSCNL scnl : allChannels) {
       if (staPattern.matcher(scnl.getStation()).matches()
           && chanPattern.matcher(scnl.getChannel()).matches()
           && netPattern.matcher(scnl.getNetwork()).matches()
           && locPattern.matcher(scnl.getLocId() == null ? "" : scnl.getLocId()).matches()) {
         processChannel(winston, scnl);
       }
     }
     params.getDataOutputStream().close();
   }
   winston.close();
 }