View Javadoc
1   /**
2    * I waive copyright and related rights in the this work worldwide through the CC0 1.0 Universal
3    * public domain dedication. https://creativecommons.org/publicdomain/zero/1.0/legalcode
4    */
5   
6   package gov.usgs.volcanoes.winston.server.wws.cmd;
7   
8   import java.nio.ByteBuffer;
9   
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  import gov.usgs.math.DownsamplingType;
14  import gov.usgs.plot.data.RSAMData;
15  import gov.usgs.volcanoes.core.Zip;
16  import gov.usgs.volcanoes.core.util.UtilException;
17  import gov.usgs.volcanoes.winston.db.Data;
18  import gov.usgs.volcanoes.winston.db.WinstonDatabase;
19  import gov.usgs.volcanoes.winston.server.MalformedCommandException;
20  import gov.usgs.volcanoes.winston.server.wws.WinstonConsumer;
21  import gov.usgs.volcanoes.winston.server.wws.WwsBaseCommand;
22  import gov.usgs.volcanoes.winston.server.wws.WwsCommandString;
23  import io.netty.channel.ChannelHandlerContext;
24  
25  /**
26   * Return Channel details.
27   * 
28   * @author Dan Cervelli
29   * @author Tom Parker
30   */
31  public class GetScnlRsamRawCommand extends WwsBaseCommand {
32    private static final Logger LOGGER = LoggerFactory.getLogger(GetScnlRsamRawCommand.class);
33  
34    /**
35     * Constructor.
36     */
37    public GetScnlRsamRawCommand() {
38      super();
39    }
40  
41    public void doCommand(ChannelHandlerContext ctx, WwsCommandString cmd)
42        throws MalformedCommandException, UtilException {
43  
44      if (!cmd.isLegalSCNLTT(10) || Double.isNaN(cmd.getDouble(8))
45          || cmd.getInt(9) == Integer.MIN_VALUE) {
46        throw new MalformedCommandException();
47      }
48      final double t1 = cmd.getT1(true);
49      final double t2 = cmd.getT2(true);
50      final int ds = (int) cmd.getDouble(8);
51      final String scnl = cmd.getWinstonSCNL();
52      final DownsamplingType dst = (ds < 2) ? DownsamplingType.NONE : DownsamplingType.MEAN;
53  
54      RSAMData rsam;
55      try {
56        rsam = databasePool.doCommand(new WinstonConsumer<RSAMData>() {
57          public RSAMData execute(WinstonDatabase winston) throws UtilException {
58            return new Data(winston).getRSAMData(scnl, t1, t2, 0, dst, ds);
59          }
60  
61        });
62      } catch (Exception e) {
63        throw new UtilException(e.getMessage());
64      }
65  
66      ByteBuffer bb = null;
67      if (rsam != null && rsam.rows() > 0)
68        bb = (ByteBuffer) rsam.toBinary().flip();
69  
70      if (cmd.getInt(9) == 1)
71        bb = ByteBuffer.wrap(Zip.compress(bb.array()));
72  
73      if (bb != null) {
74        LOGGER.debug("returning {} rsam bytes", bb.limit());
75        ctx.write(cmd.getID() + " " + bb.limit() + '\n');
76        ctx.writeAndFlush(bb.array());
77      }
78    }
79  }