Blame view

src/main/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrl.java 8.24 KB
cfbb6d07   Nathanael Jourdane   Implement most of...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * This file is a part of EpnTAPClient.
 * This program aims to provide EPN-TAP support for software clients, like CASSIS spectrum analyzer.
 * See draft specifications: https://voparis-confluence.obspm.fr/pages/viewpage.action?pageId=559861
 * Copyright (C) 2016 Institut de Recherche en Astrophysique et Planétologie.
 *
 * This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version. This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE. See the GNU General Public License for more details. You should have received a copy of
 * the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package eu.omp.irap.vespa.epntapclient.granule;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
6a2d3842   Nathanael Jourdane   Use getters and s...
24
import java.util.logging.Level;
cfbb6d07   Nathanael Jourdane   Implement most of...
25
26
import java.util.logging.Logger;

cfbb6d07   Nathanael Jourdane   Implement most of...
27
28
29
30
31
32
33
34
import eu.omp.irap.vespa.votable.votabledata.VOTableData;

/**
 * @author N. Jourdane
 */
public class GranuleCtrl {

	/** The logger for the class GranuleCtrl. */
fd93317f   Nathanael Jourdane   Solve Sonar/Eclip...
35
	private static final Logger LOGGER = Logger.getLogger(GranuleCtrl.class.getName());
cfbb6d07   Nathanael Jourdane   Implement most of...
36

30e4599b   Nathanael Jourdane   Add Javadoc
37
	/** The data to parse */
cfbb6d07   Nathanael Jourdane   Implement most of...
38
39
	private VOTableData data;

30e4599b   Nathanael Jourdane   Add Javadoc
40
	/** The error message when the specified row is not found. */
6a2d3842   Nathanael Jourdane   Use getters and s...
41
42
	private static final String ERROR_MSG = "%s not found in the rowId %s: return %s.";

cfbb6d07   Nathanael Jourdane   Implement most of...
43

30e4599b   Nathanael Jourdane   Add Javadoc
44
45
46
47
48
	/**
	 * Constructor of GranuleCtrl
	 *
	 * @param data The VOTable data to parse.
	 */
cfbb6d07   Nathanael Jourdane   Implement most of...
49
50
51
52
	public GranuleCtrl(VOTableData data) {
		this.data = data;
	}

30e4599b   Nathanael Jourdane   Add Javadoc
53
54
55
56
57
58
59
	/**
	 * Parse a String value in the VOTable data at the specified row and column.
	 *
	 * @param rowId The row identifier
	 * @param granule The Granule enumeration, representing the column name.
	 * @return The value as String.
	 */
6a2d3842   Nathanael Jourdane   Use getters and s...
60
	private String parseString(int rowId, GranuleEnum granule) {
b021ed9a   Jean-Michel Glorian   catch the excepti...
61
62
		String res = "";
		try {
6a2d3842   Nathanael Jourdane   Use getters and s...
63
			res = (String) data.getCell(rowId, granule.toString());
30e4599b   Nathanael Jourdane   Add Javadoc
64
		} catch (IllegalArgumentException e) {
fd93317f   Nathanael Jourdane   Solve Sonar/Eclip...
65
			LOGGER.log(Level.WARNING, String.format(ERROR_MSG, granule, rowId, "empty string"), e);
b021ed9a   Jean-Michel Glorian   catch the excepti...
66
67
		}
		return res;
cfbb6d07   Nathanael Jourdane   Implement most of...
68
69
	}

30e4599b   Nathanael Jourdane   Add Javadoc
70
71
72
73
74
75
76
77
	/**
	 * Parse a Date value in the VOTable data at the specified row and column.
	 *
	 * @param rowId The row identifier
	 * @param granule The Granule enumeration, representing the column name.
	 * @return The value as Date.
	 * @throws ParseException The date format is not correct.
	 */
6a2d3842   Nathanael Jourdane   Use getters and s...
78
	private Date parseDate(int rowId, GranuleEnum granule) throws ParseException {
cfbb6d07   Nathanael Jourdane   Implement most of...
79
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
b021ed9a   Jean-Michel Glorian   catch the excepti...
80
81
		Date date = new Date();
		try {
6a2d3842   Nathanael Jourdane   Use getters and s...
82
			date = sdf.parse((String) data.getCell(rowId, granule.toString()));
30e4599b   Nathanael Jourdane   Add Javadoc
83
		} catch (IllegalArgumentException e) {
fd93317f   Nathanael Jourdane   Solve Sonar/Eclip...
84
			LOGGER.log(Level.WARNING, String.format(ERROR_MSG, granule, rowId, "empty date"), e);
b021ed9a   Jean-Michel Glorian   catch the excepti...
85
		}
6a2d3842   Nathanael Jourdane   Use getters and s...
86

b021ed9a   Jean-Michel Glorian   catch the excepti...
87
		return date;
cfbb6d07   Nathanael Jourdane   Implement most of...
88
89
	}

30e4599b   Nathanael Jourdane   Add Javadoc
90
91
92
93
94
95
96
	/**
	 * Parse a Double value in the VOTable data at the specified row and column.
	 *
	 * @param rowId The row identifier
	 * @param granule The Granule enumeration, representing the column name.
	 * @return The value as Double.
	 */
6a2d3842   Nathanael Jourdane   Use getters and s...
97
	private Double parseDouble(int rowId, GranuleEnum granule) {
b021ed9a   Jean-Michel Glorian   catch the excepti...
98
99
		Double d = null;
		try {
6a2d3842   Nathanael Jourdane   Use getters and s...
100
			d = (Double) data.getCell(rowId, granule.toString());
30e4599b   Nathanael Jourdane   Add Javadoc
101
		} catch (IllegalArgumentException e) {
fd93317f   Nathanael Jourdane   Solve Sonar/Eclip...
102
			LOGGER.log(Level.WARNING, String.format(ERROR_MSG, granule, rowId, "double.NaN"), e);
b021ed9a   Jean-Michel Glorian   catch the excepti...
103
		}
cfbb6d07   Nathanael Jourdane   Implement most of...
104

6a2d3842   Nathanael Jourdane   Use getters and s...
105
		return d == null ? Double.NaN : d;
cfbb6d07   Nathanael Jourdane   Implement most of...
106
107
	}

b021ed9a   Jean-Michel Glorian   catch the excepti...
108
	/**
30e4599b   Nathanael Jourdane   Add Javadoc
109
110
111
112
113
	 * Parse a Double value in the VOTable data at the specified row and column.
	 *
	 * @param rowId The row identifier
	 * @param granule The Granule enumeration, representing the column name.
	 * @return The value as Double.
b021ed9a   Jean-Michel Glorian   catch the excepti...
114
	 */
30e4599b   Nathanael Jourdane   Add Javadoc
115
	public Integer parseInteger(int rowId, GranuleEnum granule) {
b021ed9a   Jean-Michel Glorian   catch the excepti...
116
117
		Integer val = 0;
		try {
30e4599b   Nathanael Jourdane   Add Javadoc
118
119
			val = (Integer) data.getCell(rowId, granule.toString());
		} catch (IllegalArgumentException e) {
fd93317f   Nathanael Jourdane   Solve Sonar/Eclip...
120
			LOGGER.log(Level.WARNING, String.format(ERROR_MSG, granule, rowId, "0"), e);
b021ed9a   Jean-Michel Glorian   catch the excepti...
121
122
123
124
		}
		return val;
	}

30e4599b   Nathanael Jourdane   Add Javadoc
125
126
127
128
129
130
131
	/**
	 * Get a Granule object from a VOTable data row.
	 *
	 * @param rowId the row identified
	 * @return the granule at the position rowId in the VOTable data.
	 * @throws ParseException If an element can not be parsed (ie., a date).
	 */
6a2d3842   Nathanael Jourdane   Use getters and s...
132
	public Granule getGranuleFromVOTableRow(int rowId) throws ParseException {
6a2d3842   Nathanael Jourdane   Use getters and s...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
		//@noformat
		Granule g = new Granule(parseString(rowId, GranuleEnum.GRANULE_UID));
		g.setGranuleGid(parseString(rowId, GranuleEnum.GRANULE_GID));
		g.setObsId(parseString(rowId, GranuleEnum.OBS_ID));
		g.setDataproductType(parseString(rowId, GranuleEnum.DATAPRODUCT_TYPE));
		g.setTargetName(parseString(rowId, GranuleEnum.TARGET_NAME));
		g.setTargetClass(parseString(rowId, GranuleEnum.TARGET_CLASS));
		g.setTimeMin(parseDouble(rowId, GranuleEnum.TIME_MIN));
		g.setTimeMax(parseDouble(rowId, GranuleEnum.TIME_MAX));
		g.setTimeSamplingStepMin(parseDouble(rowId, GranuleEnum.TIME_SAMPLING_STEP_MIN));
		g.setTimeSamplingStepMax(parseDouble(rowId, GranuleEnum.TIME_SAMPLING_STEP_MAX));
		g.setTimeExpMin(parseDouble(rowId, GranuleEnum.TIME_EXP_MIN));
		g.setTimeExpMax(parseDouble(rowId, GranuleEnum.TIME_EXP_MAX));
		g.setSpectralRangeMin(parseDouble(rowId, GranuleEnum.SPECTRAL_RANGE_MIN));
		g.setSpectralRangeMax(parseDouble(rowId, GranuleEnum.SPECTRAL_RANGE_MAX));
		g.setTimeSamplingStepMin(parseDouble(rowId, GranuleEnum.TIME_SAMPLING_STEP_MIN));
		g.setTimeSamplingStepMax(parseDouble(rowId, GranuleEnum.TIME_SAMPLING_STEP_MAX));
		g.setSpectralResolutionMin(parseDouble(rowId, GranuleEnum.SPECTRAL_RESOLUTION_MIN));
		g.setSpectralResolutionMax(parseDouble(rowId, GranuleEnum.SPECTRAL_RESOLUTION_MAX));
		g.setC1Min(parseDouble(rowId, GranuleEnum.C1MIN));
		g.setC1Max(parseDouble(rowId, GranuleEnum.C1MAX));
		g.setC2Min(parseDouble(rowId, GranuleEnum.C2MIN));
		g.setC2Max(parseDouble(rowId, GranuleEnum.C2MAX));
		g.setC3Min(parseDouble(rowId, GranuleEnum.C3MIN));
		g.setC3Max(parseDouble(rowId, GranuleEnum.C3MAX));
		g.setsRegion(parseString(rowId, GranuleEnum.S_REGION));
		g.setC1ResolMin(parseDouble(rowId, GranuleEnum.C1_RESOL_MIN));
		g.setC1ResolMax(parseDouble(rowId, GranuleEnum.C1_RESOL_MAX));
		g.setC2ResolMin(parseDouble(rowId, GranuleEnum.C2_RESOL_MIN));
		g.setC2ResolMax(parseDouble(rowId, GranuleEnum.C2_RESOL_MAX));
		g.setC3ResolMin(parseDouble(rowId, GranuleEnum.C3_RESOL_MIN));
		g.setC3ResolMax(parseDouble(rowId, GranuleEnum.C3_RESOL_MAX));
		g.setSpatialFrameType(parseString(rowId, GranuleEnum.SPATIAL_FRAME_TYPE));
		g.setIncidenceMin(parseDouble(rowId, GranuleEnum.INCIDENCE_MIN));
		g.setIncidenceMax(parseDouble(rowId, GranuleEnum.INCIDENCE_MAX));
		g.setEmergenceMin(parseDouble(rowId, GranuleEnum.EMERGENCE_MIN));
		g.setEmergenceMax(parseDouble(rowId, GranuleEnum.EMERGENCE_MAX));
		g.setPhaseMin(parseDouble(rowId, GranuleEnum.PHASE_MIN));
		g.setPhaseMax(parseDouble(rowId, GranuleEnum.PHASE_MAX));
		g.setInstrumentHostName(parseString(rowId, GranuleEnum.INSTRUMENT_HOST_NAME));
		g.setInstrumentName(parseString(rowId, GranuleEnum.INSTRUMENT_NAME));
		g.setMeasurementType(parseString(rowId, GranuleEnum.MEASUREMENT_TYPE));
30e4599b   Nathanael Jourdane   Add Javadoc
175
		g.setProcessingLevel(parseInteger(rowId, GranuleEnum.PROCESSING_LEVEL));
6a2d3842   Nathanael Jourdane   Use getters and s...
176
177
178
179
180
181
182
183
184
185
186
187
		g.setCreationDate(parseDate(rowId, GranuleEnum.CREATION_DATE));
		g.setModificationDate(parseDate(rowId, GranuleEnum.MODIFICATION_DATE) );
		g.setReleaseDate(parseDate(rowId, GranuleEnum.RELEASE_DATE) );
		g.setServiceTitle(parseString(rowId, GranuleEnum.SERVICE_TITLE));
		//@format

		if (!g.isValid()) {
			throw new IllegalArgumentException("One or more EPN parameter is null.");
		}
		return g;
	}

30e4599b   Nathanael Jourdane   Add Javadoc
188
189
190
191
	/**
	 * @return true if the VOTable data implements the EpnCoreV2.
	 */
	public boolean isV2() {
35daa117   Nathanael Jourdane   Improve JUnits te...
192
		return !data.isContainingColumnName("index");
cfbb6d07   Nathanael Jourdane   Implement most of...
193
194
	}

30e4599b   Nathanael Jourdane   Add Javadoc
195
196
197
198
199
200
	/**
	 * @return A list of Granules from a VOTable, where each Granule is a row of the VOTable data.
	 * @throws ParseException If the granule can not be parsed.
	 */
	public List<Granule> getGranules() throws ParseException {
		if (!isV2()) {
cfbb6d07   Nathanael Jourdane   Implement most of...
201
202
203
204
205
			throw new IllegalArgumentException(
					"The EPN-CORE is not v2, which is the only suported version");
		}
		List<Granule> granules = new ArrayList<>();

6a2d3842   Nathanael Jourdane   Use getters and s...
206
		if (data != null) {
b021ed9a   Jean-Michel Glorian   catch the excepti...
207
208
209
			for (int rowId = 0; rowId < data.getNbRows(); rowId++) {
				granules.add(getGranuleFromVOTableRow(rowId));
			}
cfbb6d07   Nathanael Jourdane   Implement most of...
210
211
212
213
214
		}
		return granules;
	}

}