/**
   * Produces a GTSWrapper whose values are those at ticks from the argument only clipped to
   * [from,to]. The bucketization parameters are not modified
   *
   * @param wrapper Source wrapper
   * @return A new wrapper
   */
  public static GTSWrapper clip(GTSWrapper wrapper, long from, long to) {
    GTSDecoder decoder = new GTSDecoder(wrapper.getBase(), ByteBuffer.wrap(unwrapEncoded(wrapper)));
    GTSEncoder encoder =
        wrapper.isSetKey()
            ? new GTSEncoder(wrapper.getBase(), wrapper.getKey())
            : new GTSEncoder(wrapper.getBase());

    while (decoder.next()) {
      if (decoder.getTimestamp() >= from && decoder.getTimestamp() <= to) {
        try {
          encoder.addValue(
              decoder.getTimestamp(),
              decoder.getLocation(),
              decoder.getElevation(),
              decoder.getValue());
        } catch (IOException ioe) {
          return null;
        }
      }
    }

    GTSWrapper clipped = new GTSWrapper();
    clipped.setBase(wrapper.getBase());
    clipped.setBucketcount(wrapper.getBucketcount());
    clipped.setBucketspan(wrapper.getBucketspan());
    clipped.setCount(encoder.getCount());
    clipped.setEncoded(encoder.getBytes());
    clipped.setLastbucket(wrapper.getLastbucket());
    clipped.setMetadata(new Metadata(wrapper.getMetadata()));
    if (wrapper.isSetKey()) {
      clipped.setKey(wrapper.getKey());
    }

    return clipped;
  }
  public static GTSWrapper fromGTSEncoderToGTSWrapper(
      GTSEncoder encoder, boolean compress, double compratio) {

    if (compratio < 1.0D) {
      compratio = 1.0D;
    }

    GTSWrapper wrapper = new GTSWrapper();

    try {
      if (!compress) {
        wrapper.setEncoded(encoder.getBytes());
      } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] bytes = encoder.getBytes();

        double ratio = 0.0D;

        int pass = 0;

        //
        // We compress the data once, if the compression ratio is greater
        // than 'compratio', we consider that the resulting compressed data will
        // probably still have lots of repetitions since we will have
        // overflowed the gzip sliding window several times, we therefore
        // enter a loop to compress the data until the compression ratio
        // falls below 'compratio'
        // We then store the number of compression passes in the GTSWrapper
        // so we can apply a matching number of decompression ops
        //
        // For ultimate compression, set 'compratio' to 1.0
        //

        byte[] encoded = null;

        do {
          encoded = bytes;
          ratio = bytes.length;
          GZIPOutputStream gzos = new GZIPOutputStream(baos);
          gzos.write(bytes);
          gzos.close();
          bytes = baos.toByteArray();
          baos.reset();
          ratio = ratio / bytes.length;
          pass++;
        } while (ratio > compratio);

        if (ratio > 1.0D) {
          // The last compression pass improved the footprint, so use the compressed data
          wrapper.setEncoded(bytes);
        } else {
          // The last pass added some overhead, ignore it
          pass = pass - 1;
          wrapper.setEncoded(encoded);
        }

        if (pass > 0) {
          wrapper.setCompressed(true);
          if (pass > 1) {
            // Only store number of passes if it is > 1 as 1 is the default value
            wrapper.setCompressionPasses(pass);
          }
        } else {
          wrapper.setCompressed(false);
        }
      }

      wrapper.setBase(encoder.getBaseTimestamp());
      wrapper.setCount(encoder.getCount());
      wrapper.setMetadata(encoder.getMetadata());

    } catch (IOException e) {
      e.printStackTrace();
    }

    return wrapper;
  }