/**
   * 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 fromGTSToGTSWrapper(
      GeoTimeSerie gts, boolean compress, double compratio) {

    GTSEncoder encoder = new GTSEncoder(0L);
    encoder.setMetadata(gts.getMetadata());

    try {
      encoder.encode(gts);
    } catch (IOException ioe) {
    }

    GTSWrapper wrapper = fromGTSEncoderToGTSWrapper(encoder, compress, compratio);

    if (GTSHelper.isBucketized(gts)) {
      wrapper.setBucketcount(gts.bucketcount);
      wrapper.setBucketspan(gts.bucketspan);
      wrapper.setLastbucket(gts.lastbucket);
    }

    return wrapper;
  }