public ReferenceWaypoint(Reference ref) {
    try {
      this.reference = Reference.clone(ref);
    } catch (Exception e) {
      e.printStackTrace();
    }

    loc = new ManeuverLocation();
    loc.setLatitudeRads(ref.getLat());
    loc.setLongitudeRads(ref.getLon());
    if (ref.getZ() != null) {
      loc.setZ(ref.getZ().getValue());
      loc.setZUnits(ManeuverLocation.Z_UNITS.valueOf(ref.getZ().getZUnits().name()));
    }
    boolean defineZ = (ref.getFlags() & Reference.FLAG_Z) != 0;
    if (!defineZ) loc.setZUnits(ManeuverLocation.Z_UNITS.NONE);

    loiter = (ref.getFlags() & Reference.FLAG_RADIUS) != 0;
    this.loiterRadius = ref.getRadius();

    this.latitude = loc.getLatitudeAsDoubleValue();
    this.longitude = loc.getLongitudeAsDoubleValue();
    this.z = loc.getZ();
    this.zUnits = loc.getZUnits();
    if (ref.getSpeed() != null) {
      this.speedUnits = ref.getSpeed().getSpeedUnits();
      this.speed = ref.getSpeed().getValue();
    }
    defineSpeed = (ref.getFlags() & Reference.FLAG_SPEED) != 0;
  }
  public ReferenceWaypoint(ManeuverLocation loc, double speed) {
    loc.convertToAbsoluteLatLonDepth();
    this.loc = loc.clone();
    this.latitude = loc.getLatitudeAsDoubleValue();
    this.longitude = loc.getLongitudeAsDoubleValue();
    this.speed = speed;
    this.speedUnits = SPEED_UNITS.METERS_PS;
    this.z = loc.getZ();
    this.zUnits = loc.getZUnits();

    reference = new Reference();
    reference.setLat(loc.getLatitudeAsDoubleValueRads());
    reference.setLon(loc.getLongitudeAsDoubleValueRads());
    reference.setZ(new DesiredZ((float) loc.getZ(), Z_UNITS.valueOf(loc.getZUnits().name())));
    reference.setSpeed(new DesiredSpeed(speed, SPEED_UNITS.METERS_PS));
    reference.setFlags((short) (Reference.FLAG_LOCATION | Reference.FLAG_SPEED | Reference.FLAG_Z));
  }