Skip to content

Commit caf89e3

Browse files
committed
major upgrade to the Merger + OIFits processor (list, merge commands) to use unified OIFitsCollection (filter)
1 parent 34278d9 commit caf89e3

20 files changed

+1149
-707
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright (C) 2018 CNRS - JMMC project ( http://www.jmmc.fr )
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
/*******************************************************************************
18+
* JMMC project ( http://www.jmmc.fr ) - Copyright (C) CNRS.
19+
******************************************************************************/
20+
package fr.jmmc.oitools;
21+
22+
import fr.jmmc.oitools.model.CsvOutputVisitor;
23+
import fr.jmmc.oitools.model.Granule;
24+
import fr.jmmc.oitools.model.InstrumentMode;
25+
import fr.jmmc.oitools.model.OIData;
26+
import fr.jmmc.oitools.model.OIFitsCollection;
27+
import fr.jmmc.oitools.model.OIT3;
28+
import fr.jmmc.oitools.model.OIVis;
29+
import fr.jmmc.oitools.model.OIVis2;
30+
import fr.jmmc.oitools.model.Target;
31+
import fr.jmmc.oitools.model.TargetIdMatcher;
32+
import java.util.List;
33+
import java.util.Map;
34+
import java.util.Set;
35+
36+
/**
37+
* This utility class loads OIFits files given as arguments
38+
* and print their XML or simple CSV description in the system out stream
39+
* @author bourgesl, mella
40+
*/
41+
public final class OIFitsCollectionViewer {
42+
43+
private OIFitsCollectionViewer() {
44+
super();
45+
}
46+
47+
public static void process(final OIFitsCollection oiFitsCollection) {
48+
final StringBuilder buffer = new StringBuilder(4 * 1024);
49+
CsvOutputVisitor.appendHeader(buffer);
50+
targetMetadata(oiFitsCollection, buffer);
51+
OIFitsCommand.info(buffer.toString());
52+
}
53+
54+
private static void targetMetadata(final OIFitsCollection oiFitsCollection, final StringBuilder sb) {
55+
56+
final List<Granule> granules = oiFitsCollection.getSortedGranules();
57+
58+
final Map<Granule, Set<OIData>> oiDataPerGranule = oiFitsCollection.getOiDataPerGranule();
59+
60+
for (Granule granule : granules) {
61+
final Target target = granule.getTarget();
62+
// Target info
63+
final String targetName = target.getTarget(); // global UID
64+
final double targetRa = target.getRaEp0();
65+
final double targetDec = target.getDecEp0();
66+
67+
final InstrumentMode insMode = granule.getInsMode();
68+
// OIWavelength info
69+
final String insName = insMode.getInsName(); // global UID
70+
final float minWavelength = insMode.getLambdaMin();
71+
final float maxWavelength = insMode.getLambdaMax();
72+
final int nbChannels = insMode.getNbChannels();
73+
// Resolution = lambda / delta_lambda
74+
final float resPower = insMode.getResPower();
75+
76+
// night
77+
final double nightId = granule.getNight().getNightId();
78+
79+
final Set<OIData> oiDatas = oiDataPerGranule.get(granule);
80+
if (oiDatas != null) {
81+
// Statistics per granule:
82+
int nbVis = 0, nbVis2 = 0, nbT3 = 0;
83+
double tMin = Double.POSITIVE_INFINITY, tMax = Double.NEGATIVE_INFINITY;
84+
double intTime = Double.POSITIVE_INFINITY;
85+
String facilityName = "";
86+
87+
for (OIData oiData : oiDatas) {
88+
final TargetIdMatcher targetIdMatcher = oiData.getTargetIdMatcher(target);
89+
90+
if (targetIdMatcher != null) {
91+
/* one oiData table, search for target by targetid (and nightid) */
92+
final short[] targetIds = oiData.getTargetId();
93+
final double[] nightIds = oiData.getNightId();
94+
final double[] mjds = oiData.getMJD();
95+
final double[] intTimes = oiData.getIntTime();
96+
97+
for (int i = 0; i < targetIds.length; i++) {
98+
// same target and same night:
99+
if (targetIdMatcher.match(targetIds[i]) && (nightId == nightIds[i])) {
100+
// TODO: count flag? what to do with flagged measures?
101+
// TODO: check for NaN values ?
102+
// number of rows in data tables:
103+
if (oiData instanceof OIVis) {
104+
nbVis += 1;
105+
} else if (oiData instanceof OIVis2) {
106+
nbVis2 += 1;
107+
} else if (oiData instanceof OIT3) {
108+
nbT3 += 1;
109+
}
110+
// TODO: add OIFlux ?
111+
112+
/* search for minimal and maximal MJD for target */
113+
/* TODO: make use of DATE-OBS+TIME[idx] if no MJD */
114+
final double mjd = mjds[i];
115+
if (mjd < tMin) {
116+
tMin = mjd;
117+
}
118+
if (mjd > tMax) {
119+
tMax = mjd;
120+
}
121+
122+
/* search for minimal (?) INT_TIME for target */
123+
final double t = intTimes[i];
124+
if (t < intTime) {
125+
intTime = t;
126+
}
127+
}
128+
}
129+
130+
if (facilityName.isEmpty() && oiData.getArrName() != null) {
131+
facilityName = oiData.getArrName(); // potential multiple ARRNAME values !
132+
}
133+
}
134+
}
135+
136+
CsvOutputVisitor.appendRecord(sb, targetName, targetRa,
137+
targetDec, intTime, tMin, tMax, resPower,
138+
minWavelength, maxWavelength, facilityName,
139+
insName, nbVis, nbVis2, nbT3, nbChannels);
140+
}
141+
}
142+
}
143+
144+
}

src/main/java/fr/jmmc/oitools/OIFitsCommand.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,23 @@ static boolean hasOptionArg(final String[] args, final String opt, final String
107107
}
108108

109109
/**
110-
* Get position of an option in args.
110+
* Get the value of an option in given args.
111111
* @param args
112112
* @param opt
113-
* @param longOpt
114-
* @return index of the option in args if found, -1 else.
113+
* @return value of the option in args if found, null otherwise.
115114
*/
116-
static int getOptionArgPosition(final String[] args, final String opt, final String longOpt) {
115+
static String getOptionArgValue(final String[] args, final String opt) {
116+
int pos = -1;
117117
for (int i = 0; i < args.length; i++) {
118-
if (args[i].equals(opt) || args[i].equals(longOpt)) {
119-
return i;
118+
if (args[i].equals(opt)) {
119+
pos = i;
120+
break;
120121
}
121122
}
122-
return -1;
123+
if (pos != -1 && (pos + 1) < args.length) {
124+
return args[pos + 1];
125+
}
126+
return null;
123127
}
124128

125129
}

src/main/java/fr/jmmc/oitools/OIFitsProcessor.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import fr.jmmc.oitools.fits.FitsUtils;
2020
import fr.jmmc.oitools.model.OIFitsChecker;
21+
import fr.jmmc.oitools.model.OIFitsCollection;
2122
import fr.jmmc.oitools.model.OIFitsFile;
2223
import fr.jmmc.oitools.model.OIFitsLoader;
2324
import fr.jmmc.oitools.model.OIFitsWriter;
@@ -37,9 +38,11 @@ public class OIFitsProcessor extends OIFitsCommand {
3738
private static final String COMMAND_HELP = "help";
3839
private static final String COMMAND_LIST = "list";
3940
private static final String COMMAND_CONVERT = "convert";
41+
private static final String COMMAND_DUMP = "dump";
4042
private static final String COMMAND_MERGE = "merge";
4143

4244
private static final String OPTION_OUTPUT = "-output";
45+
private static final String OPTION_TARGET = "-target";
4346
private static final String OPTION_INSNAME = "-insname";
4447

4548
/**
@@ -62,7 +65,7 @@ public static void main(final String[] args) {
6265
// command processing
6366
if (COMMAND_HELP.equals(command)) {
6467
showArgumentsHelp();
65-
} else if ("dump".equals(command)) {
68+
} else if (COMMAND_DUMP.equals(command)) {
6669
dump(args);
6770
} else if (COMMAND_LIST.equals(command)) {
6871
list(args);
@@ -88,9 +91,17 @@ public static void main(final String[] args) {
8891
*/
8992
private static void list(final String[] args) throws FitsException, IOException {
9093
final List<String> fileLocations = getInputFiles(args);
94+
final boolean check = hasOptionArg(args, "-c", "-check");
95+
96+
final OIFitsChecker checker = new OIFitsChecker();
97+
98+
final OIFitsCollection oiFitsCollection = OIFitsCollection.create(checker, fileLocations);
99+
100+
if (check) {
101+
info("validation results:\n" + checker.getCheckReport());
102+
}
91103

92-
// TODO: implement later a simplified output
93-
OIFitsViewer.process(false, true, false, false, fileLocations);
104+
OIFitsCollectionViewer.process(oiFitsCollection);
94105
}
95106

96107
/**
@@ -101,18 +112,18 @@ private static void list(final String[] args) throws FitsException, IOException
101112
private static void dump(final String[] args) throws FitsException, IOException {
102113
final List<String> fileLocations = getInputFiles(args);
103114

104-
FitsUtils.setup();
105-
115+
FitsUtils.setup();
116+
106117
final StringBuilder sb = new StringBuilder(16 * 1024);
107-
118+
108119
for (String fileLocation : fileLocations) {
109120
info("Processing: " + fileLocation);
110121
try {
111122
FitsUtils.dumpFile(fileLocation, false, sb);
112-
123+
113124
info(sb.toString());
114125
sb.setLength(0); // reset
115-
126+
116127
} catch (Exception e) {
117128
error("Error reading file '" + fileLocation + "'", e);
118129
}
@@ -153,29 +164,28 @@ private static void merge(final String[] args) throws FitsException, IOException
153164
final String outputFilePath = getOutputFilepath(args);
154165
final boolean check = hasOptionArg(args, "-c", "-check");
155166

156-
final OIFitsFile[] inputs = new OIFitsFile[fileLocations.size()];
167+
// Optional filters:
168+
final String targetUID = getOptionArgValue(args, OPTION_TARGET);
169+
final String insModeUID = getOptionArgValue(args, OPTION_INSNAME);
157170

158-
// Get input files
159-
for (int i = 0; i < fileLocations.size(); i++) {
160-
inputs[i] = OIFitsLoader.loadOIFits(fileLocations.get(i));
161-
}
171+
final OIFitsCollection oiFitsCollection = OIFitsCollection.create(null, fileLocations);
162172

163-
Selector selector = null;
164-
int positionOptionFilter = getOptionArgPosition(args, OPTION_INSNAME, OPTION_INSNAME);
165-
if (positionOptionFilter > -1 && args.length > positionOptionFilter + 1) {
166-
selector = new Selector();
167-
selector.addPattern(Selector.INSTRUMENT_FILTER, args[positionOptionFilter + 1]);
173+
final Selector selector = new Selector();
174+
if (targetUID != null) {
175+
selector.setTargetUID(targetUID);
176+
}
177+
if (insModeUID != null) {
178+
selector.setInsModeUID(insModeUID);
168179
}
169180

170181
// Call merge
171-
final OIFitsFile result = Merger.process(selector, inputs);
182+
final OIFitsFile result = Merger.process(oiFitsCollection, selector);
172183
if (result.hasOiData()) {
173184
// Store result
174185
write(outputFilePath, result, check);
175186
} else {
176187
info("Result is empty, no file created.");
177188
}
178-
179189
}
180190

181191
private static void write(final String outputFilePath, final OIFitsFile result, final boolean check) throws IOException, FitsException {
@@ -185,6 +195,7 @@ private static void write(final String outputFilePath, final OIFitsFile result,
185195
info("validation results:\n" + checker.getCheckReport());
186196
}
187197

198+
info("Writing: " + outputFilePath);
188199
// Store result
189200
OIFitsWriter.writeOIFits(outputFilePath, result);
190201
}
@@ -221,7 +232,9 @@ private static List<String> getInputFiles(String[] args) {
221232

222233
for (int i = 1; i < args.length; i++) {
223234
// note: should be generalized to any argument having value(s):
224-
if (OPTION_OUTPUT.substring(0, 2).equals(args[i]) || OPTION_OUTPUT.equals(args[i])
235+
if (OPTION_OUTPUT.substring(0, 2).equals(args[i])
236+
|| OPTION_OUTPUT.equals(args[i])
237+
|| OPTION_TARGET.equals(args[i])
225238
|| OPTION_INSNAME.equals(args[i])) {
226239
i++; // skip next parameter which is the output file
227240
} else if (args[i].startsWith("-")) {
@@ -248,13 +261,15 @@ protected static void showArgumentsHelp() {
248261
info("|------------------------------------------------------------------------------------|");
249262
info("| command " + COMMAND_HELP + " Show this help |");
250263
info("| command " + COMMAND_LIST + " List content of several oifits files |");
264+
info("| command " + COMMAND_DUMP + " Dump the given oifits files |");
251265
info("| command " + COMMAND_CONVERT + " Convert the given input file |");
252266
info("| command " + COMMAND_MERGE + " Merge several oifits files |");
253267
info("| " + OPTION_OUTPUT.substring(0, 2) + " or " + OPTION_OUTPUT
254268
+ " <file_path> Complete path, absolute or relative, for output file |");
255269
info("| [-l] or [-log] Enable logging (quiet by default) |");
256270
info("| [-c] or [-check] Check output file before writing |");
257-
info("| [-insname] <insname_value> Filter result on given insname |");
271+
info("| [-target] <target value> Filter result on given target |");
272+
info("| [-insname] <insname value> Filter result on given insname |");
258273
info("--------------------------------------------------------------------------------------");
259274
}
260275

0 commit comments

Comments
 (0)