<commit_before>package org.helioviewer.jhv.astronomy;

import java.util.Iterator;

import org.helioviewer.jhv.math.MathUtils;
import org.helioviewer.jhv.time.JHVTime;
import org.helioviewer.jhv.time.TimeUtils;
import org.json.JSONArray;
import org.json.JSONObject;

public class PositionResponse {

    private final PositionCartesian[] position;
    private final long positionStart;
    private final long positionEnd;

    PositionResponse(PositionCartesian[] _position) throws Exception {
        int len = _position.length;
        if (len == 0)
            throw new Exception("Empty response");
        position = _position;
        positionStart = position[0].milli;
        positionEnd = position[len - 1].milli;
    }

    PositionResponse(JSONObject jo) throws Exception {
        JSONArray res = jo.getJSONArray("result");
        int len = res.length();
        if (len == 0)
            throw new Exception("Empty response");

        position = new PositionCartesian[len];
        for (int i = 0; i < len; i++) {
            JSONObject posObject = res.getJSONObject(i);
            Iterator<String> iterKeys = posObject.keys();
            if (!iterKeys.hasNext())
                throw new Exception("Unexpected format");

            String date = iterKeys.next();
            JSONArray posArray = posObject.getJSONArray(date);

            double x = posArray.getDouble(0) * Sun.RadiusKMeterInv;
            double y = posArray.getDouble(1) * Sun.RadiusKMeterInv;
            double z = posArray.getDouble(2) * Sun.RadiusKMeterInv;
            position[i] = new PositionCartesian(TimeUtils.parse(date), x, y, z);
        }
        positionStart = position[0].milli;
        positionEnd = position[len - 1].milli;
    }

    long interpolateTime(long t, long start, long end) {
        if (start >= end)
            return positionEnd;
        else {
            double f = (t - start) / (double) (end - start); //!
            long time = (long) (positionStart + f * (positionEnd - positionStart) + .5);
            return MathUtils.clip(time, positionStart, positionEnd);
        }
    }

    public long interpolateLatitudinal(long t, long start, long end, double[] lati) {
        long time = interpolateTime(t, start, end);

        double x, y, z;
        if (positionStart == positionEnd) {
            x = position[0].x;
            y = position[0].y;
            z = position[0].z;
        } else {
            double interpolatedIndex = (time - positionStart) / (double) (positionEnd - positionStart) * position.length;
            int i = (int) interpolatedIndex;
            i = MathUtils.clip(i, 0, position.length - 1);
            int inext = Math.min(i + 1, position.length - 1);

            long tstart = position[i].milli;
            long tend = position[inext].milli;

            double alpha = tend == tstart ? 1. : ((time - tstart) / (double) (tend - tstart)) % 1.;
            x = (1. - alpha) * position[i].x + alpha * position[inext].x;
            y = (1. - alpha) * position[i].y + alpha * position[inext].y;
            z = (1. - alpha) * position[i].z + alpha * position[inext].z;
        }

        double dist, hgln, hglt;
        dist = Math.sqrt(x * x + y * y + z * z);
        if (dist == 0) {
            hgln = 0;
            hglt = 0;
        } else {
            hgln = Math.atan2(y, x);
            hglt = Math.asin(z / dist);
        }
        lati[0] = dist;
        lati[1] = hgln;
        lati[2] = hglt;
        return time;
    }

    Position interpolateCarrington(long t, long start, long end) {
        long time = interpolateTime(t, start, end);

        double x, y, z;
        if (positionStart == positionEnd) {
            x = position[0].x;
            y = position[0].y;
            z = position[0].z;
        } else {
            double interpolatedIndex = (time - positionStart) / (double) (positionEnd - positionStart) * position.length;
            int i = (int) interpolatedIndex;
            i = MathUtils.clip(i, 0, position.length - 1);
            int inext = Math.min(i + 1, position.length - 1);

            long tstart = position[i].milli;
            long tend = position[inext].milli;

            double alpha = tend == tstart ? 1. : ((time - tstart) / (double) (tend - tstart)) % 1.;
            x = (1. - alpha) * position[i].x + alpha * position[inext].x;
            y = (1. - alpha) * position[i].y + alpha * position[inext].y;
            z = (1. - alpha) * position[i].z + alpha * position[inext].z;
        }

        double dist, hgln, hglt;
        dist = Math.sqrt(x * x + y * y + z * z);
        if (dist == 0) {
            hgln = 0;
            hglt = 0;
        } else {
            hgln = -Math.atan2(y, x); // like Carrington
            hglt = Math.asin(z / dist);
        }
        return new Position(new JHVTime(time), dist, hgln, hglt);
    }

    public double interpolateRectangular(long t, long start, long end, float[] xyz) {<commit_msg>Clean<commit_after>package org.helioviewer.jhv.astronomy;

import java.util.Iterator;

import org.helioviewer.jhv.math.MathUtils;
import org.helioviewer.jhv.time.JHVTime;
import org.helioviewer.jhv.time.TimeUtils;
import org.json.JSONArray;
import org.json.JSONObject;

public class PositionResponse {

    private final PositionCartesian[] position;
    private final long positionStart;
    private final long positionEnd;

    PositionResponse(PositionCartesian[] _position) throws Exception {
        int len = _position.length;
        if (len == 0)
            throw new Exception("Empty response");
        position = _position;
        positionStart = position[0].milli;
        positionEnd = position[len - 1].milli;
    }

    PositionResponse(JSONObject jo) throws Exception {
        JSONArray res = jo.getJSONArray("result");
        int len = res.length();
        if (len == 0)
            throw new Exception("Empty response");

        position = new PositionCartesian[len];
        for (int i = 0; i < len; i++) {
            JSONObject posObject = res.getJSONObject(i);
            Iterator<String> iterKeys = posObject.keys();
            if (!iterKeys.hasNext())
                throw new Exception("Unexpected format");

            String date = iterKeys.next();
            JSONArray posArray = posObject.getJSONArray(date);

            double x = posArray.getDouble(0) * Sun.RadiusKMeterInv;
            double y = posArray.getDouble(1) * Sun.RadiusKMeterInv;
            double z = posArray.getDouble(2) * Sun.RadiusKMeterInv;
            position[i] = new PositionCartesian(TimeUtils.parse(date), x, y, z);
        }
        positionStart = position[0].milli;
        positionEnd = position[len - 1].milli;
    }

    long interpolateTime(long t, long start, long end) {
        if (start >= end)
            return positionEnd;
        else {
            double f = (t - start) / (double) (end - start); //!
            long time = (long) (positionStart + f * (positionEnd - positionStart) + .5);
            return MathUtils.clip(time, positionStart, positionEnd);
        }
    }

    public long interpolateLatitudinal(long t, long start, long end, double[] lati) {
        long time = interpolateTime(t, start, end);

        double x, y, z;
        if (positionStart == positionEnd) {
            x = position[0].x;
            y = position[0].y;
            z = position[0].z;
        } else {
            double interpolatedIndex = (time - positionStart) / (double) (positionEnd - positionStart) * position.length;
            int i = (int) interpolatedIndex;
            i = MathUtils.clip(i, 0, position.length - 1);
            int inext = Math.min(i + 1, position.length - 1);

            long tstart = position[i].milli;
            long tend = position[inext].milli;

            double alpha = tend == tstart ? 1. : ((time - tstart) / (double) (tend - tstart)) % 1.;
            x = (1. - alpha) * position[i].x + alpha * position[inext].x;
            y = (1. - alpha) * position[i].y + alpha * position[inext].y;
            z = (1. - alpha) * position[i].z + alpha * position[inext].z;
        }

        double dist, hgln, hglt;
        dist = Math.sqrt(x * x + y * y + z * z);
        if (dist == 0) {
            hgln = 0;
            hglt = 0;
        } else {
            hgln = Math.atan2(y, x);
            hglt = Math.asin(z / dist);
        }
        lati[0] = dist;
        lati[1] = hgln;
        lati[2] = hglt;
        return time;
    }

    Position interpolateCarrington(long t, long start, long end) {
        long time = interpolateTime(t, start, end);

        double x, y, z;
        if (positionStart == positionEnd) {
            x = position[0].x;
            y = position[0].y;
            z = position[0].z;
        } else {
            double interpolatedIndex = (time - positionStart) / (double) (positionEnd - positionStart) * position.length;
            int i = (int) interpolatedIndex;
            i = MathUtils.clip(i, 0, position.length - 1);
            int inext = Math.min(i + 1, position.length - 1);

            long tstart = position[i].milli;
            long tend = position[inext].milli;

            double alpha = tend == tstart ? 1. : ((time - tstart) / (double) (tend - tstart)) % 1.;
            x = (1. - alpha) * position[i].x + alpha * position[inext].x;
            y = (1. - alpha) * position[i].y + alpha * position[inext].y;
            z = (1. - alpha) * position[i].z + alpha * position[inext].z;
        }

        double dist, hgln, hglt;
        dist = Math.sqrt(x * x + y * y + z * z);
        if (dist == 0) {
            hgln = 0;
            hglt = 0;
        } else {
            hgln = Math.atan2(y, x);
            if (hgln < 0)
                hgln += 2 * Math.PI;
            hglt = Math.asin(z / dist);
        }
        return new Position(new JHVTime(time), dist, -hgln, hglt);
    }

    public double interpolateRectangular(long t, long start, long end, float[] xyz) {
{'fraction_non_alphanumeric': 0.1283443975055321, 'fraction_numerical': 0.009454838060752363, 'mean_word_length': 1.8786913723219456, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6342257', 'n_tokens_mistral': 3159, 'n_tokens_neox': 3026, 'n_words': 800}