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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;

import javax.swing.SwingWorker;

import org.helioviewer.base.DownloadStream;
import org.helioviewer.base.logging.Log;
import org.helioviewer.base.math.GL3DVec3d;
import org.helioviewer.base.physics.Constants;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class GL3DPositionLoading {

    private final String LOADEDSTATE = "Loaded";
    private final String FAILEDSTATE = "Failed";
    private final String PARTIALSTATE = "Partial";
    private boolean running = false;

    private boolean isLoaded = false;
    private URL url;
    private JSONArray jsonResult;
    private GL3DPositionDateTime[] positionDateTime;
    private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    private String beginDate = "2014-05-28T00:00:00";
    private String endDate = "2014-05-28T00:00:00";
    private final String target = "SUN";
    private String observer = "Earth";
    private final String baseUrl = "http://swhv.oma.be/position?";
    private int deltat = 45;
    private final ArrayList<GL3DPositionLoadingListener> listeners = new ArrayList<GL3DPositionLoadingListener>();
    private Date beginDatems = new Date(0);
    private Date endDatems = new Date();
    private SwingWorker<Integer, Integer> worker;

    private void buildRequestURL() {
        try {
            url = new URL(baseUrl + "abcorr=LT%2BS&utc=" + this.beginDate + "&utc_end=" + this.endDate + "&deltat=" + deltat + "&observer=" + observer + "&target=" + target + "&ref=HEEQ&kind=latitudinal");
        } catch (MalformedURLException e) {
            Log.error("A wrong url is given.", e);
        }
    }

    public void requestData() {
        if (worker != null) {
            worker.cancel(false);
        }
        fireLoaded("Loading...");

        worker = new SwingWorker<Integer, Integer>() {
            private String report = null;

            @Override
            protected Integer doInBackground() throws Exception {
                Thread.currentThread().setName("GL3DPositionLoading--Main");
                running = true;
                try {
                    if (endDatems.getTime() - beginDatems.getTime() < 1000 * 60 * 60 * 24 * 20) {
                        deltat = 60 * 60 / 64;
                    } else {
                        deltat = 60 * 60 * 24;
                    }
                    buildRequestURL();

                    DownloadStream ds = new DownloadStream(url.toURI(), 30000, 30000, true);
                    Reader reader = new BufferedReader(new InputStreamReader(ds.getInput(), "UTF-8"));
                    if (!ds.getResponse400()) {
                        jsonResult = new JSONArray(new JSONTokener(reader));
                    } else {
                        JSONObject jsonObject = new JSONObject(new JSONTokener(reader));
                        if (jsonObject.has("faultstring")) {
                            String faultstring = jsonObject.getString("faultstring");
                            report = faultstring;
                        } else {
                            report = "Invalid network response.";
                        }
                    }
                } catch (UnknownHostException e) {
                    Log.debug("Unknown host, network down?", e);
                } catch (final IOException e1) {
                    report = FAILEDSTATE + ": server problem";
                } catch (JSONException e2) {
                    report = FAILEDSTATE + ": JSON parse problem";
                } catch (URISyntaxException e) {
                    report = FAILEDSTATE + ": wrong URI";
                }

                return 1;
            }

            @Override
            public void process(List<Integer> chunks) {
            }

            @Override
            public void done() {
                if (!this.isCancelled()) {
                    if (report == null && jsonResult != null) {
                        parseData();
                        jsonResult = null;
                        if (positionDateTime != null && positionDateTime.length > 0) {
                            setLoaded(true);
                        } else if (positionDateTime == null) {
                            report = "response is void";
                        } else {
                            report = "response is zero length array";
                        }
                    }
                    if (report != null) {
                        fireLoaded(report);
                    }
                }
            }
        };
        worker.execute();
    }

    private void setLoaded(boolean isLoaded) {
        this.isLoaded = isLoaded;
        if (isLoaded) {
            this.fireLoaded(this.LOADEDSTATE);
        }
    }

    private void parseData() {
        try {
            GregorianCalendar calendar = new GregorianCalendar();
            GL3DPositionDateTime[] positionDateTimehelper = new GL3DPositionDateTime[jsonResult.length()];

            for (int j = 0; j < jsonResult.length(); j++) {
                JSONObject positionObject = jsonResult.getJSONObject(j);
                Iterator<String> iteratorKeys = positionObject.keys();
                if (!iteratorKeys.hasNext())
                    continue;

                String dateString = iteratorKeys.next();
                JSONArray positionArray = positionObject.getJSONArray(dateString);

                Date date = format.parse(dateString);
                calendar.setTime(date);

                double x, y, z, jy;
                x = positionArray.getDouble(0);
                jy = positionArray.getDouble(1);
                y = jy + (jy > 0 ? -Math.PI : Math.PI);
                z = -positionArray.getDouble(2);

                positionDateTimehelper[j] = new GL3DPositionDateTime(calendar.getTimeInMillis(), x, y, z);
            }
            this.positionDateTime = positionDateTimehelper;
        } catch (JSONException e) {
            this.fireLoaded(this.PARTIALSTATE);
            Log.warn("JSON response parse failure", e);
        } catch (ParseException e) {
            this.fireLoaded(this.PARTIALSTATE);
            Log.warn("JSON response parse failure", e);
        } catch (NumberFormatException e) {
            this.fireLoaded(this.PARTIALSTATE);
            Log.warn("JSON response parse failure", e);
        }
    }

    public boolean isLoaded() {
        return this.isLoaded;
    }

    private void applyChanges() {
        this.setLoaded(false);
        this.positionDateTime = null;
        this.requestData();
    }<commit_msg>Adapt format for geometry server responses<commit_after>package org.helioviewer.jhv.camera;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;

import javax.swing.SwingWorker;

import org.helioviewer.base.DownloadStream;
import org.helioviewer.base.logging.Log;
import org.helioviewer.base.math.GL3DVec3d;
import org.helioviewer.base.physics.Constants;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class GL3DPositionLoading {

    private final String LOADEDSTATE = "Loaded";
    private final String FAILEDSTATE = "Failed";
    private final String PARTIALSTATE = "Partial";
    private boolean running = false;

    private boolean isLoaded = false;
    private URL url;
    private JSONObject jsonResult;
    private GL3DPositionDateTime[] positionDateTime;
    private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
    private String beginDate = "2014-05-28T00:00:00";
    private String endDate = "2014-05-28T00:00:00";
    private final String target = "SUN";
    private String observer = "Earth";
    private final String baseUrl = "http://swhv.oma.be/position?";
    private int deltat = 45;
    private final ArrayList<GL3DPositionLoadingListener> listeners = new ArrayList<GL3DPositionLoadingListener>();
    private Date beginDatems = new Date(0);
    private Date endDatems = new Date();
    private SwingWorker<Integer, Integer> worker;

    private void buildRequestURL() {
        try {
            url = new URL(baseUrl + "abcorr=LT%2BS&utc=" + this.beginDate + "&utc_end=" + this.endDate + "&deltat=" + deltat + "&observer=" + observer + "&target=" + target + "&ref=HEEQ&kind=latitudinal");
        } catch (MalformedURLException e) {
            Log.error("A wrong url is given.", e);
        }
    }

    public void requestData() {
        if (worker != null) {
            worker.cancel(false);
        }
        fireLoaded("Loading...");

        worker = new SwingWorker<Integer, Integer>() {
            private String report = null;

            @Override
            protected Integer doInBackground() throws Exception {
                Thread.currentThread().setName("GL3DPositionLoading--Main");
                running = true;
                try {
                    if (endDatems.getTime() - beginDatems.getTime() < 1000 * 60 * 60 * 24 * 20) {
                        deltat = 60 * 60 / 64;
                    } else {
                        deltat = 60 * 60 * 24;
                    }
                    buildRequestURL();

                    DownloadStream ds = new DownloadStream(url.toURI(), 30000, 30000, true);
                    Reader reader = new BufferedReader(new InputStreamReader(ds.getInput(), "UTF-8"));
                    if (!ds.getResponse400()) {
                        jsonResult = new JSONObject(new JSONTokener(reader));
                    } else {
                        JSONObject jsonObject = new JSONObject(new JSONTokener(reader));
                        if (jsonObject.has("faultstring")) {
                            String faultstring = jsonObject.getString("faultstring");
                            report = faultstring;
                        } else {
                            report = "Invalid network response";
                        }
                    }
                } catch (UnknownHostException e) {
                    Log.debug("Unknown host, network down?", e);
                } catch (IOException e) {
                    report = FAILEDSTATE + ": server problem";
                } catch (JSONException e) {
                    report = FAILEDSTATE + ": JSON parse problem";
                } catch (URISyntaxException e) {
                    report = FAILEDSTATE + ": wrong URI";
                }

                return 1;
            }

            @Override
            public void process(List<Integer> chunks) {
            }

            @Override
            public void done() {
                if (!this.isCancelled()) {
                    if (report == null && jsonResult != null) {
                        parseData();
                        jsonResult = null;
                        if (positionDateTime != null && positionDateTime.length > 0) {
                            setLoaded(true);
                        } else if (positionDateTime == null) {
                            report = "response is void";
                        } else {
                            report = "response is zero length array";
                        }
                    }
                    if (report != null) {
                        fireLoaded(report);
                    }
                }
            }
        };
        worker.execute();
    }

    private void setLoaded(boolean isLoaded) {
        this.isLoaded = isLoaded;
        if (isLoaded) {
            this.fireLoaded(this.LOADEDSTATE);
        }
    }

    private void parseData() {
        try {
            GregorianCalendar calendar = new GregorianCalendar();
            JSONArray resArray = jsonResult.getJSONArray("result");
            GL3DPositionDateTime[] positionDateTimehelper = new GL3DPositionDateTime[resArray.length()];

            for (int j = 0; j < resArray.length(); j++) {
                JSONObject posObject = resArray.getJSONObject(j);
                Iterator<String> iterKeys = posObject.keys();
                if (!iterKeys.hasNext())
                    throw new JSONException("unexpected format");

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

                Date date = format.parse(dateString);
                calendar.setTime(date);

                double x, y, z, jy;
                x = posArray.getDouble(0);
                jy = posArray.getDouble(1);
                y = jy + (jy > 0 ? -Math.PI : Math.PI);
                z = -posArray.getDouble(2);

                positionDateTimehelper[j] = new GL3DPositionDateTime(calendar.getTimeInMillis(), x, y, z);
            }
            this.positionDateTime = positionDateTimehelper;
        } catch (JSONException e) {
            this.fireLoaded(this.PARTIALSTATE);
            Log.warn("JSON response parse failure", e);
        } catch (ParseException e) {
            this.fireLoaded(this.PARTIALSTATE);
            Log.warn("JSON response parse failure", e);
        } catch (NumberFormatException e) {
            this.fireLoaded(this.PARTIALSTATE);
            Log.warn("JSON response parse failure", e);
        }
    }

    public boolean isLoaded() {
        return this.isLoaded;
    }

    private void applyChanges() {
        this.setLoaded(false);
        this.positionDateTime = null;
        this.requestData();
    }
{'fraction_non_alphanumeric': 0.10034143962093234, 'fraction_numerical': 0.012263953731447286, 'mean_word_length': 1.7568190549366116, 'pattern_counts': {'":': 6, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4332268', 'n_tokens_mistral': 3881, 'n_tokens_neox': 3583, 'n_words': 878}