Skip to content

Commit

Permalink
major upgrade to the Merger + OIFits processor (list, merge commands)…
Browse files Browse the repository at this point in the history
… to use unified OIFitsCollection (filter)
  • Loading branch information
bourgesl committed Jul 4, 2018
1 parent 34278d9 commit caf89e3
Show file tree
Hide file tree
Showing 20 changed files with 1,149 additions and 707 deletions.
144 changes: 144 additions & 0 deletions src/main/java/fr/jmmc/oitools/OIFitsCollectionViewer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright (C) 2018 CNRS - JMMC project ( http://www.jmmc.fr )
*
* 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/>.
*/
/*******************************************************************************
* JMMC project ( http://www.jmmc.fr ) - Copyright (C) CNRS.
******************************************************************************/
package fr.jmmc.oitools;

import fr.jmmc.oitools.model.CsvOutputVisitor;
import fr.jmmc.oitools.model.Granule;
import fr.jmmc.oitools.model.InstrumentMode;
import fr.jmmc.oitools.model.OIData;
import fr.jmmc.oitools.model.OIFitsCollection;
import fr.jmmc.oitools.model.OIT3;
import fr.jmmc.oitools.model.OIVis;
import fr.jmmc.oitools.model.OIVis2;
import fr.jmmc.oitools.model.Target;
import fr.jmmc.oitools.model.TargetIdMatcher;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* This utility class loads OIFits files given as arguments
* and print their XML or simple CSV description in the system out stream
* @author bourgesl, mella
*/
public final class OIFitsCollectionViewer {

private OIFitsCollectionViewer() {
super();
}

public static void process(final OIFitsCollection oiFitsCollection) {
final StringBuilder buffer = new StringBuilder(4 * 1024);
CsvOutputVisitor.appendHeader(buffer);
targetMetadata(oiFitsCollection, buffer);
OIFitsCommand.info(buffer.toString());
}

private static void targetMetadata(final OIFitsCollection oiFitsCollection, final StringBuilder sb) {

final List<Granule> granules = oiFitsCollection.getSortedGranules();

final Map<Granule, Set<OIData>> oiDataPerGranule = oiFitsCollection.getOiDataPerGranule();

for (Granule granule : granules) {
final Target target = granule.getTarget();
// Target info
final String targetName = target.getTarget(); // global UID
final double targetRa = target.getRaEp0();
final double targetDec = target.getDecEp0();

final InstrumentMode insMode = granule.getInsMode();
// OIWavelength info
final String insName = insMode.getInsName(); // global UID
final float minWavelength = insMode.getLambdaMin();
final float maxWavelength = insMode.getLambdaMax();
final int nbChannels = insMode.getNbChannels();
// Resolution = lambda / delta_lambda
final float resPower = insMode.getResPower();

// night
final double nightId = granule.getNight().getNightId();

final Set<OIData> oiDatas = oiDataPerGranule.get(granule);
if (oiDatas != null) {
// Statistics per granule:
int nbVis = 0, nbVis2 = 0, nbT3 = 0;
double tMin = Double.POSITIVE_INFINITY, tMax = Double.NEGATIVE_INFINITY;
double intTime = Double.POSITIVE_INFINITY;
String facilityName = "";

for (OIData oiData : oiDatas) {
final TargetIdMatcher targetIdMatcher = oiData.getTargetIdMatcher(target);

if (targetIdMatcher != null) {
/* one oiData table, search for target by targetid (and nightid) */
final short[] targetIds = oiData.getTargetId();
final double[] nightIds = oiData.getNightId();
final double[] mjds = oiData.getMJD();
final double[] intTimes = oiData.getIntTime();

for (int i = 0; i < targetIds.length; i++) {
// same target and same night:
if (targetIdMatcher.match(targetIds[i]) && (nightId == nightIds[i])) {
// TODO: count flag? what to do with flagged measures?
// TODO: check for NaN values ?
// number of rows in data tables:
if (oiData instanceof OIVis) {
nbVis += 1;
} else if (oiData instanceof OIVis2) {
nbVis2 += 1;
} else if (oiData instanceof OIT3) {
nbT3 += 1;
}
// TODO: add OIFlux ?

/* search for minimal and maximal MJD for target */
/* TODO: make use of DATE-OBS+TIME[idx] if no MJD */
final double mjd = mjds[i];
if (mjd < tMin) {
tMin = mjd;
}
if (mjd > tMax) {
tMax = mjd;
}

/* search for minimal (?) INT_TIME for target */
final double t = intTimes[i];
if (t < intTime) {
intTime = t;
}
}
}

if (facilityName.isEmpty() && oiData.getArrName() != null) {
facilityName = oiData.getArrName(); // potential multiple ARRNAME values !
}
}
}

CsvOutputVisitor.appendRecord(sb, targetName, targetRa,
targetDec, intTime, tMin, tMax, resPower,
minWavelength, maxWavelength, facilityName,
insName, nbVis, nbVis2, nbT3, nbChannels);
}
}
}

}
18 changes: 11 additions & 7 deletions src/main/java/fr/jmmc/oitools/OIFitsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,23 @@ static boolean hasOptionArg(final String[] args, final String opt, final String
}

/**
* Get position of an option in args.
* Get the value of an option in given args.
* @param args
* @param opt
* @param longOpt
* @return index of the option in args if found, -1 else.
* @return value of the option in args if found, null otherwise.
*/
static int getOptionArgPosition(final String[] args, final String opt, final String longOpt) {
static String getOptionArgValue(final String[] args, final String opt) {
int pos = -1;
for (int i = 0; i < args.length; i++) {
if (args[i].equals(opt) || args[i].equals(longOpt)) {
return i;
if (args[i].equals(opt)) {
pos = i;
break;
}
}
return -1;
if (pos != -1 && (pos + 1) < args.length) {
return args[pos + 1];
}
return null;
}

}
59 changes: 37 additions & 22 deletions src/main/java/fr/jmmc/oitools/OIFitsProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import fr.jmmc.oitools.fits.FitsUtils;
import fr.jmmc.oitools.model.OIFitsChecker;
import fr.jmmc.oitools.model.OIFitsCollection;
import fr.jmmc.oitools.model.OIFitsFile;
import fr.jmmc.oitools.model.OIFitsLoader;
import fr.jmmc.oitools.model.OIFitsWriter;
Expand All @@ -37,9 +38,11 @@ public class OIFitsProcessor extends OIFitsCommand {
private static final String COMMAND_HELP = "help";
private static final String COMMAND_LIST = "list";
private static final String COMMAND_CONVERT = "convert";
private static final String COMMAND_DUMP = "dump";
private static final String COMMAND_MERGE = "merge";

private static final String OPTION_OUTPUT = "-output";
private static final String OPTION_TARGET = "-target";
private static final String OPTION_INSNAME = "-insname";

/**
Expand All @@ -62,7 +65,7 @@ public static void main(final String[] args) {
// command processing
if (COMMAND_HELP.equals(command)) {
showArgumentsHelp();
} else if ("dump".equals(command)) {
} else if (COMMAND_DUMP.equals(command)) {
dump(args);
} else if (COMMAND_LIST.equals(command)) {
list(args);
Expand All @@ -88,9 +91,17 @@ public static void main(final String[] args) {
*/
private static void list(final String[] args) throws FitsException, IOException {
final List<String> fileLocations = getInputFiles(args);
final boolean check = hasOptionArg(args, "-c", "-check");

final OIFitsChecker checker = new OIFitsChecker();

final OIFitsCollection oiFitsCollection = OIFitsCollection.create(checker, fileLocations);

if (check) {
info("validation results:\n" + checker.getCheckReport());
}

// TODO: implement later a simplified output
OIFitsViewer.process(false, true, false, false, fileLocations);
OIFitsCollectionViewer.process(oiFitsCollection);
}

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

FitsUtils.setup();
FitsUtils.setup();

final StringBuilder sb = new StringBuilder(16 * 1024);

for (String fileLocation : fileLocations) {
info("Processing: " + fileLocation);
try {
FitsUtils.dumpFile(fileLocation, false, sb);

info(sb.toString());
sb.setLength(0); // reset

} catch (Exception e) {
error("Error reading file '" + fileLocation + "'", e);
}
Expand Down Expand Up @@ -153,29 +164,28 @@ private static void merge(final String[] args) throws FitsException, IOException
final String outputFilePath = getOutputFilepath(args);
final boolean check = hasOptionArg(args, "-c", "-check");

final OIFitsFile[] inputs = new OIFitsFile[fileLocations.size()];
// Optional filters:
final String targetUID = getOptionArgValue(args, OPTION_TARGET);
final String insModeUID = getOptionArgValue(args, OPTION_INSNAME);

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

Selector selector = null;
int positionOptionFilter = getOptionArgPosition(args, OPTION_INSNAME, OPTION_INSNAME);
if (positionOptionFilter > -1 && args.length > positionOptionFilter + 1) {
selector = new Selector();
selector.addPattern(Selector.INSTRUMENT_FILTER, args[positionOptionFilter + 1]);
final Selector selector = new Selector();
if (targetUID != null) {
selector.setTargetUID(targetUID);
}
if (insModeUID != null) {
selector.setInsModeUID(insModeUID);
}

// Call merge
final OIFitsFile result = Merger.process(selector, inputs);
final OIFitsFile result = Merger.process(oiFitsCollection, selector);
if (result.hasOiData()) {
// Store result
write(outputFilePath, result, check);
} else {
info("Result is empty, no file created.");
}

}

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

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

for (int i = 1; i < args.length; i++) {
// note: should be generalized to any argument having value(s):
if (OPTION_OUTPUT.substring(0, 2).equals(args[i]) || OPTION_OUTPUT.equals(args[i])
if (OPTION_OUTPUT.substring(0, 2).equals(args[i])
|| OPTION_OUTPUT.equals(args[i])
|| OPTION_TARGET.equals(args[i])
|| OPTION_INSNAME.equals(args[i])) {
i++; // skip next parameter which is the output file
} else if (args[i].startsWith("-")) {
Expand All @@ -248,13 +261,15 @@ protected static void showArgumentsHelp() {
info("|------------------------------------------------------------------------------------|");
info("| command " + COMMAND_HELP + " Show this help |");
info("| command " + COMMAND_LIST + " List content of several oifits files |");
info("| command " + COMMAND_DUMP + " Dump the given oifits files |");
info("| command " + COMMAND_CONVERT + " Convert the given input file |");
info("| command " + COMMAND_MERGE + " Merge several oifits files |");
info("| " + OPTION_OUTPUT.substring(0, 2) + " or " + OPTION_OUTPUT
+ " <file_path> Complete path, absolute or relative, for output file |");
info("| [-l] or [-log] Enable logging (quiet by default) |");
info("| [-c] or [-check] Check output file before writing |");
info("| [-insname] <insname_value> Filter result on given insname |");
info("| [-target] <target value> Filter result on given target |");
info("| [-insname] <insname value> Filter result on given insname |");
info("--------------------------------------------------------------------------------------");
}

Expand Down
Loading

0 comments on commit caf89e3

Please sign in to comment.