Search in sources :

Example 1 with OrekitParseException

use of org.orekit.errors.OrekitParseException in project Orekit by CS-SI.

the class ICGEMFormatReader method loadData.

/**
 * {@inheritDoc}
 */
public void loadData(final InputStream input, final String name) throws IOException, ParseException, OrekitException {
    // reset the indicator before loading any data
    setReadComplete(false);
    referenceDate = null;
    cTrend.clear();
    sTrend.clear();
    cCos.clear();
    cSin.clear();
    sCos.clear();
    sSin.clear();
    // by default, the field is normalized with unknown tide system
    // (will be overridden later if non-default)
    normalized = true;
    tideSystem = TideSystem.UNKNOWN;
    final BufferedReader r = new BufferedReader(new InputStreamReader(input, "UTF-8"));
    boolean inHeader = true;
    double[][] c = null;
    double[][] s = null;
    boolean okCoeffs = false;
    int lineNumber = 0;
    for (String line = r.readLine(); line != null; line = r.readLine()) {
        try {
            ++lineNumber;
            if (line.trim().length() == 0) {
                continue;
            }
            final String[] tab = line.split("\\s+");
            if (inHeader) {
                if ((tab.length == 2) && PRODUCT_TYPE.equals(tab[0])) {
                    if (!GRAVITY_FIELD.equals(tab[1])) {
                        throw new OrekitParseException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE, lineNumber, name, line);
                    }
                } else if ((tab.length == 2) && tab[0].endsWith(GRAVITY_CONSTANT)) {
                    setMu(parseDouble(tab[1]));
                } else if ((tab.length == 2) && REFERENCE_RADIUS.equals(tab[0])) {
                    setAe(parseDouble(tab[1]));
                } else if ((tab.length == 2) && MAX_DEGREE.equals(tab[0])) {
                    final int degree = FastMath.min(getMaxParseDegree(), Integer.parseInt(tab[1]));
                    final int order = FastMath.min(getMaxParseOrder(), degree);
                    c = buildTriangularArray(degree, order, missingCoefficientsAllowed() ? 0.0 : Double.NaN);
                    s = buildTriangularArray(degree, order, missingCoefficientsAllowed() ? 0.0 : Double.NaN);
                } else if ((tab.length == 2) && TIDE_SYSTEM_INDICATOR.equals(tab[0])) {
                    if (ZERO_TIDE.equals(tab[1])) {
                        tideSystem = TideSystem.ZERO_TIDE;
                    } else if (TIDE_FREE.equals(tab[1])) {
                        tideSystem = TideSystem.TIDE_FREE;
                    } else if (TIDE_UNKNOWN.equals(tab[1])) {
                        tideSystem = TideSystem.UNKNOWN;
                    } else {
                        throw new OrekitParseException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE, lineNumber, name, line);
                    }
                } else if ((tab.length == 2) && NORMALIZATION_INDICATOR.equals(tab[0])) {
                    if (NORMALIZED.equals(tab[1])) {
                        normalized = true;
                    } else if (UNNORMALIZED.equals(tab[1])) {
                        normalized = false;
                    } else {
                        throw new OrekitParseException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE, lineNumber, name, line);
                    }
                } else if ((tab.length == 2) && END_OF_HEADER.equals(tab[0])) {
                    inHeader = false;
                }
            } else {
                if ((tab.length == 7 && GFC.equals(tab[0])) || (tab.length == 8 && GFCT.equals(tab[0]))) {
                    final int i = Integer.parseInt(tab[1]);
                    final int j = Integer.parseInt(tab[2]);
                    if (i < c.length && j < c[i].length) {
                        parseCoefficient(tab[3], c, i, j, "C", name);
                        parseCoefficient(tab[4], s, i, j, "S", name);
                        okCoeffs = true;
                        if (tab.length == 8) {
                            // check the reference date (format yyyymmdd)
                            final DateComponents localRef = new DateComponents(Integer.parseInt(tab[7].substring(0, 4)), Integer.parseInt(tab[7].substring(4, 6)), Integer.parseInt(tab[7].substring(6, 8)));
                            if (referenceDate == null) {
                                // first reference found, store it
                                referenceDate = localRef;
                            } else if (!referenceDate.equals(localRef)) {
                                throw new OrekitException(OrekitMessages.SEVERAL_REFERENCE_DATES_IN_GRAVITY_FIELD, referenceDate, localRef, name);
                            }
                        }
                    }
                } else if (tab.length == 7 && (DOT.equals(tab[0]) || TRND.equals(tab[0]))) {
                    final int i = Integer.parseInt(tab[1]);
                    final int j = Integer.parseInt(tab[2]);
                    if (i < c.length && j < c[i].length) {
                        // store the secular trend coefficients
                        extendListOfLists(cTrend, i, j, 0.0);
                        extendListOfLists(sTrend, i, j, 0.0);
                        parseCoefficient(tab[3], cTrend, i, j, "Ctrend", name);
                        parseCoefficient(tab[4], sTrend, i, j, "Strend", name);
                    }
                } else if (tab.length == 8 && (ASIN.equals(tab[0]) || ACOS.equals(tab[0]))) {
                    final int i = Integer.parseInt(tab[1]);
                    final int j = Integer.parseInt(tab[2]);
                    if (i < c.length && j < c[i].length) {
                        // extract arrays corresponding to period
                        final Double period = Double.valueOf(tab[7]);
                        if (!cCos.containsKey(period)) {
                            cCos.put(period, new ArrayList<List<Double>>());
                            cSin.put(period, new ArrayList<List<Double>>());
                            sCos.put(period, new ArrayList<List<Double>>());
                            sSin.put(period, new ArrayList<List<Double>>());
                        }
                        final List<List<Double>> cCosPeriod = cCos.get(period);
                        final List<List<Double>> cSinPeriod = cSin.get(period);
                        final List<List<Double>> sCosPeriod = sCos.get(period);
                        final List<List<Double>> sSinPeriod = sSin.get(period);
                        // store the pulsation coefficients
                        extendListOfLists(cCosPeriod, i, j, 0.0);
                        extendListOfLists(cSinPeriod, i, j, 0.0);
                        extendListOfLists(sCosPeriod, i, j, 0.0);
                        extendListOfLists(sSinPeriod, i, j, 0.0);
                        if (ACOS.equals(tab[0])) {
                            parseCoefficient(tab[3], cCosPeriod, i, j, "Ccos", name);
                            parseCoefficient(tab[4], sCosPeriod, i, j, "SCos", name);
                        } else {
                            parseCoefficient(tab[3], cSinPeriod, i, j, "Csin", name);
                            parseCoefficient(tab[4], sSinPeriod, i, j, "Ssin", name);
                        }
                    }
                } else {
                    throw new OrekitParseException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE, lineNumber, name, line);
                }
            }
        } catch (NumberFormatException nfe) {
            final OrekitParseException pe = new OrekitParseException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE, lineNumber, name, line);
            pe.initCause(nfe);
            throw pe;
        }
    }
    if (missingCoefficientsAllowed() && c.length > 0 && c[0].length > 0) {
        // ensure at least the (0, 0) element is properly set
        if (Precision.equals(c[0][0], 0.0, 0)) {
            c[0][0] = 1.0;
        }
    }
    if (Double.isNaN(getAe()) || Double.isNaN(getMu()) || !okCoeffs) {
        String loaderName = getClass().getName();
        loaderName = loaderName.substring(loaderName.lastIndexOf('.') + 1);
        throw new OrekitException(OrekitMessages.UNEXPECTED_FILE_FORMAT_ERROR_FOR_LOADER, name, loaderName);
    }
    setRawCoefficients(normalized, c, s, name);
    setTideSystem(tideSystem);
    setReadComplete(true);
}
Also used : InputStreamReader(java.io.InputStreamReader) DateComponents(org.orekit.time.DateComponents) OrekitParseException(org.orekit.errors.OrekitParseException) BufferedReader(java.io.BufferedReader) OrekitException(org.orekit.errors.OrekitException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with OrekitParseException

use of org.orekit.errors.OrekitParseException in project Orekit by CS-SI.

the class GRGSFormatReader method loadData.

/**
 * {@inheritDoc}
 */
public void loadData(final InputStream input, final String name) throws IOException, ParseException, OrekitException {
    // reset the indicator before loading any data
    setReadComplete(false);
    referenceDate = null;
    cDot.clear();
    sDot.clear();
    // FIELD - GRIM5, VERSION : C1, november 1999
    // AE                  1/F                 GM                 OMEGA
    // 0.63781364600000E+070.29825765000000E+030.39860044150000E+150.72921150000000E-04
    // REFERENCE DATE : 1997.00
    // MAXIMAL DEGREE : 120     Sigmas calibration factor : .5000E+01 (applied)
    // L  M DOT         CBAR                SBAR             SIGMA C      SIGMA S
    // 2  0DOT 0.13637590952454E-10 0.00000000000000E+00  .143968E-11  .000000E+00
    // 3  0DOT 0.28175700027753E-11 0.00000000000000E+00  .496704E-12  .000000E+00
    // 4  0DOT 0.12249148508277E-10 0.00000000000000E+00  .129977E-11  .000000E+00
    // 0  0     .99999999988600E+00  .00000000000000E+00  .153900E-09  .000000E+00
    // 2  0   -0.48416511550920E-03 0.00000000000000E+00  .204904E-10  .000000E+00
    final BufferedReader r = new BufferedReader(new InputStreamReader(input, "UTF-8"));
    int lineNumber = 0;
    double[][] c = null;
    double[][] s = null;
    for (String line = r.readLine(); line != null; line = r.readLine()) {
        ++lineNumber;
        // match current header or data line
        final Matcher matcher = LINES[FastMath.min(LINES.length, lineNumber) - 1].matcher(line);
        if (!matcher.matches()) {
            throw new OrekitParseException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE, lineNumber, name, line);
        }
        if (lineNumber == 3) {
            // header line defining ae, 1/f, GM and Omega
            setAe(parseDouble(matcher.group(1)));
            setMu(parseDouble(matcher.group(3)));
        } else if (lineNumber == 4) {
            // header line containing the reference date
            referenceDate = new DateComponents(Integer.parseInt(matcher.group(1)), 1, 1);
        } else if (lineNumber == 5) {
            // header line defining max degree
            final int degree = FastMath.min(getMaxParseDegree(), Integer.parseInt(matcher.group(1)));
            final int order = FastMath.min(getMaxParseOrder(), degree);
            c = buildTriangularArray(degree, order, missingCoefficientsAllowed() ? 0.0 : Double.NaN);
            s = buildTriangularArray(degree, order, missingCoefficientsAllowed() ? 0.0 : Double.NaN);
        } else if (lineNumber > 6) {
            // data line
            final int i = Integer.parseInt(matcher.group(1).trim());
            final int j = Integer.parseInt(matcher.group(2).trim());
            if (i < c.length && j < c[i].length) {
                if ("DOT".equals(matcher.group(3).trim())) {
                    // store the secular drift coefficients
                    extendListOfLists(cDot, i, j, 0.0);
                    extendListOfLists(sDot, i, j, 0.0);
                    parseCoefficient(matcher.group(4), cDot, i, j, "Cdot", name);
                    parseCoefficient(matcher.group(5), sDot, i, j, "Sdot", name);
                } else {
                    // store the constant coefficients
                    parseCoefficient(matcher.group(4), c, i, j, "C", name);
                    parseCoefficient(matcher.group(5), s, i, j, "S", name);
                }
            }
        }
    }
    if (missingCoefficientsAllowed() && c.length > 0 && c[0].length > 0) {
        // ensure at least the (0, 0) element is properly set
        if (Precision.equals(c[0][0], 0.0, 0)) {
            c[0][0] = 1.0;
        }
    }
    setRawCoefficients(true, c, s, name);
    setTideSystem(TideSystem.UNKNOWN);
    setReadComplete(true);
}
Also used : InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) OrekitParseException(org.orekit.errors.OrekitParseException) BufferedReader(java.io.BufferedReader) DateComponents(org.orekit.time.DateComponents)

Aggregations

BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 OrekitParseException (org.orekit.errors.OrekitParseException)2 DateComponents (org.orekit.time.DateComponents)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 OrekitException (org.orekit.errors.OrekitException)1