AberrationModifier.java

  1. /* Copyright 2023 Mark Rutten
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * Mark Rutten licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.estimation.measurements.modifiers;

  18. import java.util.Collections;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;

  22. import org.hipparchus.Field;
  23. import org.hipparchus.analysis.differentiation.Gradient;
  24. import org.hipparchus.analysis.differentiation.GradientField;
  25. import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
  26. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  27. import org.hipparchus.util.FastMath;
  28. import org.hipparchus.util.MathUtils;
  29. import org.orekit.annotation.DefaultDataContext;
  30. import org.orekit.bodies.CelestialBodyFactory;
  31. import org.orekit.errors.OrekitException;
  32. import org.orekit.errors.OrekitMessages;
  33. import org.orekit.estimation.measurements.AngularRaDec;
  34. import org.orekit.estimation.measurements.EstimatedMeasurement;
  35. import org.orekit.estimation.measurements.EstimatedMeasurementBase;
  36. import org.orekit.estimation.measurements.EstimationModifier;
  37. import org.orekit.estimation.measurements.GroundStation;
  38. import org.orekit.frames.FieldTransform;
  39. import org.orekit.frames.Frame;
  40. import org.orekit.time.AbsoluteDate;
  41. import org.orekit.time.FieldAbsoluteDate;
  42. import org.orekit.utils.Constants;
  43. import org.orekit.utils.PVCoordinates;
  44. import org.orekit.utils.ParameterDriver;
  45. import org.orekit.utils.TimeSpanMap;
  46. import org.orekit.utils.TimeStampedFieldPVCoordinates;


  47. /**
  48.  * Class modifying theoretical angular measurement with (the inverse of) stellar aberration.
  49.  * <p>
  50.  * This class implements equation 3.252-3 from Seidelmann, "Explanatory Supplement to the Astronmical Almanac", 1992.
  51.  *
  52.  * @author Mark Rutten
  53.  */
  54. public class AberrationModifier implements EstimationModifier<AngularRaDec> {

  55.     /** Empty constructor.
  56.      * <p>
  57.      * This constructor is not strictly necessary, but it prevents spurious
  58.      * javadoc warnings with JDK 18 and later.
  59.      * </p>
  60.      * @since 12.0
  61.      */
  62.     public AberrationModifier() {
  63.         // nothing to do
  64.     }

  65.     /**
  66.      * Natural to proper correction for aberration of light.
  67.      *
  68.      * @param naturalRaDec the "natural" direction (in barycentric coordinates)
  69.      * @param station      the observer ground station
  70.      * @param date         the date of the measurement
  71.      * @param frame        the frame of the measurement
  72.      * @return the "proper" direction (station-relative coordinates)
  73.      */
  74.     @DefaultDataContext
  75.     public static double[] naturalToProper(final double[] naturalRaDec, final GroundStation station,
  76.                                            final AbsoluteDate date, final Frame frame) {

  77.         // Check measurement frame is inertial
  78.         if (!frame.isPseudoInertial()) {
  79.             throw new OrekitException(OrekitMessages.NON_PSEUDO_INERTIAL_FRAME, frame.getName());
  80.         }

  81.         // Velocity of station relative to barycentre (units of c)
  82.         final PVCoordinates baryPV = CelestialBodyFactory.getSolarSystemBarycenter()
  83.                 .getPVCoordinates(date, frame);
  84.         final PVCoordinates stationPV = station.getBaseFrame().getPVCoordinates(date, frame);
  85.         final Vector3D stationBaryVel = stationPV.getVelocity()
  86.                 .subtract(baryPV.getVelocity())
  87.                 .scalarMultiply(1.0 / Constants.SPEED_OF_LIGHT);

  88.         // Delegate to private method
  89.         return lorentzVelocitySum(naturalRaDec, stationBaryVel);
  90.     }

  91.     /**
  92.      * Proper to natural correction for aberration of light.
  93.      *
  94.      * @param properRaDec the "proper" direction (station-relative coordinates)
  95.      * @param station     the observer ground station
  96.      * @param date        the date of the measurement
  97.      * @param frame       the frame of the measurement
  98.      * @return the "natural" direction (in barycentric coordinates)
  99.      */
  100.     @DefaultDataContext
  101.     public static double[] properToNatural(final double[] properRaDec, final GroundStation station,
  102.                                            final AbsoluteDate date, final Frame frame) {

  103.         // Check measurement frame is inertial
  104.         if (!frame.isPseudoInertial()) {
  105.             throw new OrekitException(OrekitMessages.NON_PSEUDO_INERTIAL_FRAME, frame.getName());
  106.         }

  107.         // Velocity of barycentre relative to station (units of c)
  108.         final PVCoordinates baryPV = CelestialBodyFactory.getSolarSystemBarycenter()
  109.                 .getPVCoordinates(date, frame);
  110.         final PVCoordinates stationPV = station.getBaseFrame().getPVCoordinates(date, frame);
  111.         final Vector3D baryStationVel = baryPV.getVelocity()
  112.                 .subtract(stationPV.getVelocity())
  113.                 .scalarMultiply(1.0 / Constants.SPEED_OF_LIGHT);

  114.         // Delegate to private method
  115.         return lorentzVelocitySum(properRaDec, baryStationVel);
  116.     }

  117.     /**
  118.      * Relativistic sum of velocities.
  119.      * This is based on equation 3.252-3 from Seidelmann, "Explanatory Supplement to the Astronmical Almanac", 1992.
  120.      *
  121.      * @param raDec    the direction to transform
  122.      * @param velocity the velocity (units of c)
  123.      * @return the transformed direction
  124.      */
  125.     private static double[] lorentzVelocitySum(final double[] raDec, final Vector3D velocity) {

  126.         // Measurement as unit vector
  127.         final Vector3D direction = new Vector3D(raDec[0], raDec[1]);

  128.         // Coefficients for calculations
  129.         final double inverseBeta = FastMath.sqrt(1.0 - velocity.getNormSq());
  130.         final double velocityScale = 1.0 + direction.dotProduct(velocity) / (1.0 + inverseBeta);

  131.         // From Seidelmann, equation 3.252-3 (unnormalised)
  132.         final Vector3D transformDirection = (direction.scalarMultiply(inverseBeta))
  133.                 .add(velocity.scalarMultiply(velocityScale));
  134.         return new double[] {transformDirection.getAlpha(), transformDirection.getDelta()};
  135.     }

  136.     /**
  137.      * Natural to proper correction for aberration of light.
  138.      *
  139.      * @param naturalRaDec      the "natural" direction (in barycentric coordinates)
  140.      * @param stationToInertial the transform from station to inertial coordinates
  141.      * @param frame             the frame of the measurement
  142.      * @return the "proper" direction (station-relative coordinates)
  143.      */
  144.     @DefaultDataContext
  145.     public static Gradient[] fieldNaturalToProper(final Gradient[] naturalRaDec,
  146.                                                   final FieldTransform<Gradient> stationToInertial,
  147.                                                   final Frame frame) {

  148.         // Check measurement frame is inertial
  149.         if (!frame.isPseudoInertial()) {
  150.             throw new OrekitException(OrekitMessages.NON_PSEUDO_INERTIAL_FRAME, frame.getName());
  151.         }

  152.         // Set up field
  153.         final Field<Gradient> field = naturalRaDec[0].getField();
  154.         final FieldVector3D<Gradient> zero = FieldVector3D.getZero(field);
  155.         final FieldAbsoluteDate<Gradient> date = stationToInertial.getFieldDate();

  156.         // Barycentre in inertial coordinates
  157.         final PVCoordinates baryPV = CelestialBodyFactory.getSolarSystemBarycenter()
  158.                 .getPVCoordinates(date.toAbsoluteDate(), frame);

  159.         // Station in inertial coordinates
  160.         final TimeStampedFieldPVCoordinates<Gradient> stationPV =
  161.                 stationToInertial.transformPVCoordinates(new TimeStampedFieldPVCoordinates<>(date, zero, zero, zero));

  162.         // Velocity of station relative to barycentre (units of c)
  163.         final FieldVector3D<Gradient> stationBaryVel = stationPV.getVelocity()
  164.                 .subtract(baryPV.getVelocity())
  165.                 .scalarMultiply(1.0 / Constants.SPEED_OF_LIGHT);

  166.         return fieldLorentzVelocitySum(naturalRaDec, stationBaryVel);
  167.     }


  168.     /**
  169.      * Proper to natural correction for aberration of light.
  170.      *
  171.      * @param properRaDec       the "proper" direction (station-relative coordinates)
  172.      * @param stationToInertial the transform from station to inertial coordinates
  173.      * @param frame             the frame of the measurement
  174.      * @return the "natural" direction (in barycentric coordinates)
  175.      */
  176.     @DefaultDataContext
  177.     public static Gradient[] fieldProperToNatural(final Gradient[] properRaDec,
  178.                                                   final FieldTransform<Gradient> stationToInertial,
  179.                                                   final Frame frame) {

  180.         // Check measurement frame is inertial
  181.         if (!frame.isPseudoInertial()) {
  182.             throw new OrekitException(OrekitMessages.NON_PSEUDO_INERTIAL_FRAME, frame.getName());
  183.         }

  184.         // Set up field
  185.         final Field<Gradient> field = properRaDec[0].getField();
  186.         final FieldVector3D<Gradient> zero = FieldVector3D.getZero(field);
  187.         final FieldAbsoluteDate<Gradient> date = stationToInertial.getFieldDate();

  188.         // Barycentre in inertial coordinates
  189.         final PVCoordinates baryPV = CelestialBodyFactory.getSolarSystemBarycenter()
  190.                 .getPVCoordinates(date.toAbsoluteDate(), frame);

  191.         // Station in inertial coordinates
  192.         final TimeStampedFieldPVCoordinates<Gradient> stationPV =
  193.                 stationToInertial.transformPVCoordinates(new TimeStampedFieldPVCoordinates<>(date, zero, zero, zero));

  194.         // Velocity of barycentre relative to station (units of c)
  195.         final FieldVector3D<Gradient> stationBaryVel = stationPV.getVelocity()
  196.                 .negate()
  197.                 .add(baryPV.getVelocity())
  198.                 .scalarMultiply(1.0 / Constants.SPEED_OF_LIGHT);

  199.         return fieldLorentzVelocitySum(properRaDec, stationBaryVel);
  200.     }

  201.     /**
  202.      * Relativistic sum of velocities.
  203.      * This is based on equation 3.252-3 from Seidelmann, "Explanatory Supplement to the Astronmical Almanac", 1992.
  204.      *
  205.      * @param raDec    the direction to transform
  206.      * @param velocity the velocity (units of c)
  207.      * @return the transformed direction
  208.      */
  209.     private static Gradient[] fieldLorentzVelocitySum(final Gradient[] raDec,
  210.                                                       final FieldVector3D<Gradient> velocity) {

  211.         // Measurement as unit vector
  212.         final FieldVector3D<Gradient> direction = new FieldVector3D<>(raDec[0], raDec[1]);

  213.         // Coefficients for calculations
  214.         final Gradient inverseBeta = (velocity.getNormSq().negate().add(1.0)).sqrt();
  215.         final Gradient velocityScale = (direction.dotProduct(velocity)).divide(inverseBeta.add(1.0)).add(1.0);

  216.         // From Seidelmann, equation 3.252-3 (unnormalised)
  217.         final FieldVector3D<Gradient> transformDirection = (direction.scalarMultiply(inverseBeta))
  218.                 .add(velocity.scalarMultiply(velocityScale));
  219.         return new Gradient[] {transformDirection.getAlpha(), transformDirection.getDelta()};
  220.     }


  221.     /**
  222.      * {@inheritDoc}
  223.      */
  224.     @Override
  225.     public List<ParameterDriver> getParametersDrivers() {
  226.         return Collections.emptyList();
  227.     }


  228.     @Override
  229.     @DefaultDataContext
  230.     public void modifyWithoutDerivatives(final EstimatedMeasurementBase<AngularRaDec> estimated) {

  231.         // Observation date
  232.         final AbsoluteDate date = estimated.getDate();

  233.         // Observation station
  234.         final GroundStation station = estimated.getObservedMeasurement().getStation();

  235.         // Observation frame
  236.         final Frame frame = estimated.getObservedMeasurement().getReferenceFrame();

  237.         // Convert measurement to natural direction
  238.         final double[] estimatedRaDec = estimated.getEstimatedValue();
  239.         final double[] naturalRaDec = properToNatural(estimatedRaDec, station, date, frame);

  240.         // Normalise RA
  241.         final double[] observed           = estimated.getObservedValue();
  242.         final double   baseRightAscension = naturalRaDec[0];
  243.         final double   twoPiWrap          = MathUtils.normalizeAngle(baseRightAscension, observed[0]) - baseRightAscension;
  244.         final double   rightAscension     = baseRightAscension + twoPiWrap;

  245.         // New estimated values
  246.         estimated.setEstimatedValue(rightAscension, naturalRaDec[1]);

  247.     }

  248.     @Override
  249.     @DefaultDataContext
  250.     public void modify(final EstimatedMeasurement<AngularRaDec> estimated) {

  251.         // Observation date
  252.         final AbsoluteDate date = estimated.getDate();

  253.         // Observation frame
  254.         final Frame frame = estimated.getObservedMeasurement().getReferenceFrame();

  255.         // Extract RA/Dec parameters (no state derivatives)
  256.         int nbParams = 6;
  257.         final Map<String, Integer> indices = new HashMap<>();
  258.         for (ParameterDriver driver : estimated.getObservedMeasurement().getParametersDrivers()) {
  259.             if (driver.isSelected()) {
  260.                 for (TimeSpanMap.Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
  261.                     if (!indices.containsKey(span.getData())) {
  262.                         indices.put(span.getData(), nbParams++);
  263.                     }
  264.                 }
  265.             }
  266.         }
  267.         final Field<Gradient> field = GradientField.getField(nbParams);

  268.         // Observation location
  269.         final FieldTransform<Gradient> stationToInertial =
  270.                 estimated.getObservedMeasurement().getStation().getOffsetToInertial(frame, date, nbParams, indices);

  271.         // Convert measurement to natural direction
  272.         final double[] estimatedRaDec = estimated.getEstimatedValue();
  273.         final Gradient[] estimatedRaDecDS = new Gradient[] {
  274.                 field.getZero().add(estimatedRaDec[0]),
  275.                 field.getZero().add(estimatedRaDec[1])
  276.         };
  277.         final Gradient[] naturalRaDec = fieldProperToNatural(estimatedRaDecDS, stationToInertial, frame);

  278.         // Normalise RA
  279.         final double[] observed = estimated.getObservedValue();
  280.         final Gradient baseRightAscension = naturalRaDec[0];
  281.         final double twoPiWrap = MathUtils.normalizeAngle(baseRightAscension.getReal(),
  282.                 observed[0]) - baseRightAscension.getReal();
  283.         final Gradient rightAscension = baseRightAscension.add(twoPiWrap);

  284.         // New estimated values
  285.         estimated.setEstimatedValue(rightAscension.getValue(), naturalRaDec[1].getValue());

  286.         // Derivatives (only parameter, no state)
  287.         final double[] raDerivatives = rightAscension.getGradient();
  288.         final double[] decDerivatives = naturalRaDec[1].getGradient();

  289.         for (final ParameterDriver driver : estimated.getObservedMeasurement().getParametersDrivers()) {
  290.             for (TimeSpanMap.Span<String> span = driver.getNamesSpanMap().getFirstSpan(); span != null; span = span.next()) {
  291.                 final Integer index = indices.get(span.getData());
  292.                 if (index != null) {
  293.                     final double[] parameterDerivative = estimated.getParameterDerivatives(driver);
  294.                     parameterDerivative[0] += raDerivatives[index];
  295.                     parameterDerivative[1] += decDerivatives[index];
  296.                     estimated.setParameterDerivatives(driver, span.getStart(), parameterDerivative[0], parameterDerivative[1]);
  297.                 }
  298.             }
  299.         }
  300.     }

  301. }