/**
   * convert a GTSWrapper into GeoTimeSerie
   *
   * @param wrapper
   * @return GeoTimeSerie
   */
  public static GeoTimeSerie fromGTSWrapperToGTS(GTSWrapper wrapper) {

    Metadata metadata = wrapper.getMetadata();
    GeoTimeSerie gts = null;

    if (null != wrapper.getEncoded()) {

      byte[] bytes = null;

      if (wrapper.isCompressed()) {
        bytes = unwrapEncoded(wrapper);
      } else {
        bytes = wrapper.getEncoded();
      }

      ByteBuffer bb = ByteBuffer.wrap(bytes);

      GTSDecoder decoder = new GTSDecoder(wrapper.getBase(), bb);

      decoder.setCount(0 != wrapper.getCount() ? wrapper.getCount() : bytes.length / 10);

      gts = decoder.decode();

    } else {
      gts = new GeoTimeSerie();
    }

    if (null == metadata) {
      metadata = new Metadata();
    }

    gts.setMetadata(metadata);

    return gts;
  }
  /** Return an ID for a wrapper */
  public static byte[] getId(GTSWrapper wrapper) {
    byte[] id = new byte[16];

    long classId = wrapper.getMetadata().getClassId();
    long labelsId = wrapper.getMetadata().getLabelsId();

    for (int i = 7; i >= 0; i--) {
      id[i] = (byte) (classId & 0xFFL);
      classId >>>= 8;

      id[8 + i] = (byte) (labelsId & 0xFFL);
      labelsId >>>= 8;
    }

    return id;
  }
  /**
   * 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 GTSDecoder fromGTSWrapperToGTSDecoder(GTSWrapper wrapper) {

    byte[] unwrapped = unwrapEncoded(wrapper);

    GTSDecoder decoder =
        new GTSDecoder(wrapper.getBase(), ByteBuffer.wrap(unwrapped).order(ByteOrder.BIG_ENDIAN));
    decoder.setMetadata(wrapper.getMetadata());
    decoder.setCount(wrapper.getCount());

    return decoder;
  }