Skip to content

Commit

Permalink
Update filtering on insname + update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentJB committed Jun 4, 2018
1 parent 9b94613 commit 0f5ec3e
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 33 deletions.
16 changes: 16 additions & 0 deletions src/main/java/fr/jmmc/oitools/OIFitsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,20 @@ static boolean hasOptionArg(final String[] args, final String opt, final String
return false;
}

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

}
31 changes: 24 additions & 7 deletions src/main/java/fr/jmmc/oitools/OIFitsProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import fr.jmmc.oitools.model.OIFitsLoader;
import fr.jmmc.oitools.model.OIFitsWriter;
import fr.jmmc.oitools.processing.Merger;
import fr.jmmc.oitools.processing.Selector;
import fr.nom.tam.fits.FitsException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

/**
*
Expand All @@ -39,6 +39,7 @@ public class OIFitsProcessor extends OIFitsCommand {
private static final String COMMAND_MERGE = "merge";

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

/**
* Main entry point.
Expand Down Expand Up @@ -130,10 +131,22 @@ private static void merge(final String[] args) throws FitsException, IOException
inputs[i] = OIFitsLoader.loadOIFits(fileLocations.get(i));
}

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]);
}

// Call merge
final OIFitsFile result = Merger.process(inputs);
// Store result
write(outputFilePath, result, check);
final OIFitsFile result = Merger.process(selector, inputs);
if (result != null && result.getOiDatas().length > 0) {
// 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 @@ -151,7 +164,7 @@ private static void write(final String outputFilePath, final OIFitsFile result,
* Get output file path from command arguments
*
* @param args
* @return output file path
* @return output file path
*/
private static String getOutputFilepath(String[] args) {
String outputFilePath = null;
Expand Down Expand Up @@ -179,7 +192,8 @@ 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_INSNAME.equals(args[i])) {
i++; // skip next parameter which is the output file
} else if (args[i].startsWith("-")) {
// ignore short options
Expand All @@ -194,7 +208,9 @@ private static List<String> getInputFiles(String[] args) {
return fileLocations;
}

/** Show command arguments help */
/**
* Show command arguments help
*/
protected static void showArgumentsHelp() {
info("--------------------------------------------------------------------------------------");
info("Usage: " + OIFitsProcessor.class.getName() + " command -o <path_output_file> <file locations>");
Expand All @@ -209,6 +225,7 @@ protected static void showArgumentsHelp() {
+ " <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("--------------------------------------------------------------------------------------");
}

Expand Down
45 changes: 34 additions & 11 deletions src/main/java/fr/jmmc/oitools/processing/Merger.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@

/**
* Processing class to merge given OIFitsFile instances into a single OIFitsFile instance
*
* @author jammetv
*/
public final class Merger {

private static final Logger logger = Logger.getLogger(Merger.class.getName());

/** Utility class */
/**
* Utility class
*/
private Merger() {
super();
}
Expand Down Expand Up @@ -70,8 +73,7 @@ public static OIFitsFile process(final Selector selector, final OIFitsFile... oi
}

/**
* Merge of OIFitsFile structure.
* Note: only one target by input files are accepted and must be the the same (name)
* Merge of OIFitsFile structure. Note: only one target by input files are accepted and must be the the same (name)
*
* @param selector optional Selector instance to filter OIFits content
* @param std OIFits standard for the output OIFitsFile
Expand All @@ -80,7 +82,7 @@ public static OIFitsFile process(final Selector selector, final OIFitsFile... oi
* @throws IllegalArgumentException
*/
public static OIFitsFile process(final Selector selector, final OIFitsStandard std,
final OIFitsFile... oiFitsToMerge) throws IllegalArgumentException {
final OIFitsFile... oiFitsToMerge) throws IllegalArgumentException {

if (oiFitsToMerge == null || oiFitsToMerge.length < 1) {
throw new IllegalArgumentException("Merge: Missing OIFits inputs");
Expand Down Expand Up @@ -122,7 +124,7 @@ public static OIFitsFile process(final Selector selector, final OIFitsStandard s
processOIData(ctx);
}

return resultFile;
return (resultFile.getOiDatas() != null || resultFile.getOiDatas().length > 0) ? resultFile : null;
}

private static OIFitsFile createOIFits(final OIFitsStandard std, final OIFitsFile[] oiFitsToMerge) {
Expand Down Expand Up @@ -312,7 +314,7 @@ private static void processOICorr(final Context ctx) {
}

/**
*
* @param ctx merge context
*/
private static void processOIData(final Context ctx) {
Expand Down Expand Up @@ -375,7 +377,7 @@ private static void filterOfData(final Context ctx) {

final List<OIData> dataToKeep = ctx.dataToKeep;
// reset
dataToKeep.clear();
ctx.reset();

if (fileToMerge.hasOiData()) {
final Selector selector = ctx.selector;
Expand All @@ -400,13 +402,19 @@ private static void filterOfData(final Context ctx) {
*/
static final class Context {

/** Optional Selector */
/**
* Optional Selector
*/
final Selector selector;

/** output OIFits */
/**
* output OIFits
*/
final OIFitsFile resultFile;

/** Currently processed file */
/**
* Currently processed file
*/
OIFitsFile fileToMerge = null;

/**
Expand All @@ -426,13 +434,28 @@ static final class Context {
final Set<String> arraysUsedByData = new HashSet<String>();
final Set<String> corrUsedByData = new HashSet<String>();

/** All OIData to keep after filter */
/**
* All OIData to keep after filter
*/
final List<OIData> dataToKeep = new ArrayList<OIData>();

private Context(final Selector selector, final OIFitsFile resultFile) {
this.selector = selector;
this.resultFile = resultFile;
}

public void reset() {
dataToKeep.clear();
// Reset mapping
mapInsNames.clear();
mapArrayNames.clear();
mapCorrNames.clear();
// reset set of object to keep
insUsedByData.clear();
arraysUsedByData.clear();
corrUsedByData.clear();
}

}

}
52 changes: 37 additions & 15 deletions src/test/java/fr/jmmc/oitools/util/test/TestMergeFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import fr.jmmc.oitools.JUnitBaseTest;
import static fr.jmmc.oitools.JUnitBaseTest.TEST_DIR_OIFITS;
import fr.jmmc.oitools.OIFitsViewer;
import fr.jmmc.oitools.model.OIArray;
import fr.jmmc.oitools.model.OIData;
import fr.jmmc.oitools.model.OIFitsChecker;
Expand All @@ -45,10 +44,14 @@
*/
public class TestMergeFilter extends JUnitBaseTest {

private static final String INPUT_FILE_NAME = "GRAVITY.2016-01-09T05-37-06_singlesci_calibrated.fits";
private static final String INSNAME_FILTER_VALUE = "SPECTRO_SC"; // SPECTRO_FT

// Common data used by several tests
private static OIFitsFile input = null;
private static OIFitsFile mergeFilterBlock = null;
private static OIFitsFile mergeFilterPass = null;
private static OIFitsFile mergeFilterBlockAll = null;
private static OIFitsFile mergeFilterPassSome = null;
private static OIFitsFile mergeFilterPassAll = null;

/**
* Do the merge of 2 OIFitsFiles
Expand All @@ -60,19 +63,24 @@ public class TestMergeFilter extends JUnitBaseTest {
@BeforeClass
public static void init() throws IOException, MalformedURLException, FitsException {
input = OIFitsLoader.loadOIFits(
TEST_DIR_OIFITS + "GRAVITY.2016-01-09T05-37-06_singlesci_calibrated.fits");
TEST_DIR_OIFITS + INPUT_FILE_NAME);

Selector selector = new Selector();

// Filter block data of the files
selector.addPattern(Selector.INSTRUMENT_FILTER, "GRAV");
mergeFilterBlock = Merger.process(selector, input);
Assert.assertNotNull("Merge return a null value", mergeFilterBlock);
mergeFilterBlockAll = Merger.process(selector, input);
Assert.assertNotNull("Merge return a null value", mergeFilterBlockAll);

// Filter let pass data of the files
selector.addPattern(Selector.INSTRUMENT_FILTER, "SPECTRO_SC"); // SPECTRO_FT
mergeFilterPass = merge(selector, input);
Assert.assertNotNull("Merge return a null value", mergeFilterPass);
selector.addPattern(Selector.INSTRUMENT_FILTER, INSNAME_FILTER_VALUE);
mergeFilterPassSome = merge(selector, input);
Assert.assertNotNull("Merge return a null value", mergeFilterPassSome);

// Filter don't block any
mergeFilterPassAll = Merger.process(input);
Assert.assertNotNull("Merge return a null value", mergeFilterPassAll);

}

/**
Expand All @@ -85,14 +93,18 @@ public static void init() throws IOException, MalformedURLException, FitsExcepti
@Test // @Ignore
public void testData() throws IOException, MalformedURLException, FitsException {

List<OIData> dataList = mergeFilterBlock.getOiDataList();
List<OIData> dataList = mergeFilterBlockAll.getOiDataList();
Assert.assertEquals("No data should be in merge result of blocking filter",
0, dataList != null ? dataList.size() : 0);

dataList = mergeFilterPass.getOiDataList();
dataList = mergeFilterPassSome.getOiDataList();
Assert.assertEquals("4 Data tables should be in merge result of blocking filter",
4, dataList != null ? dataList.size() : 0);

dataList = mergeFilterPassAll.getOiDataList();
Assert.assertEquals("Bad number of Data in merge result of blocking filter",
input.getOiDatas().length, dataList != null ? dataList.size() : 0);

}

/**
Expand All @@ -105,13 +117,19 @@ public void testData() throws IOException, MalformedURLException, FitsException
@Test // @Ignore
public void testIns() throws IOException, MalformedURLException, FitsException {

OIWavelength[] insList = mergeFilterBlock.getOiWavelengths();
OIWavelength[] insList = mergeFilterBlockAll.getOiWavelengths();
Assert.assertEquals("No ins should be in merge result of blocking filter",
0, insList != null ? insList.length : 0);

insList = mergeFilterPass.getOiWavelengths();
insList = mergeFilterPassSome.getOiWavelengths();
Assert.assertEquals("1 Ins should be in merge result of blocking filter",
1, insList != null ? insList.length : 0);
Assert.assertEquals("1 Ins should be in merge result of blocking filter",
INSNAME_FILTER_VALUE, insList[0].getInsName());

insList = mergeFilterPassAll.getOiWavelengths();
Assert.assertEquals("Bad number of Ins in merge result of passing filter",
input.getOiWavelengths().length, insList != null ? insList.length : 0);

}

Expand All @@ -125,14 +143,18 @@ public void testIns() throws IOException, MalformedURLException, FitsException {
@Test // @Ignore
public void testArraysIns() throws IOException, MalformedURLException, FitsException {

OIArray[] arrayList = mergeFilterBlock.getOiArrays();
OIArray[] arrayList = mergeFilterBlockAll.getOiArrays();
Assert.assertEquals("No array should be in merge result of blocking filter",
0, arrayList != null ? arrayList.length : 0);

arrayList = mergeFilterPass.getOiArrays();
arrayList = mergeFilterPassSome.getOiArrays();
Assert.assertEquals("1 array should be in merge result of blocking filter",
1, arrayList != null ? arrayList.length : 0);

arrayList = mergeFilterPassAll.getOiArrays();
Assert.assertEquals("Bad Insname in result",
input.getOiArrays().length, arrayList != null ? arrayList.length : 0);

}

private static OIFitsFile merge(final Selector selector, final OIFitsFile... oiFitsToMerge) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
import fr.jmmc.oitools.JUnitBaseTest;
import static fr.jmmc.oitools.JUnitBaseTest.TEST_DIR_OIFITS;
import fr.jmmc.oitools.OIFitsProcessor;
import fr.jmmc.oitools.model.OIFitsFile;
import fr.jmmc.oitools.model.OIFitsLoader;
import fr.nom.tam.fits.FitsException;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.MalformedURLException;
import junit.framework.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -81,6 +86,32 @@ public void testConvert() {
Assert.assertTrue("No output file present.", output.exists());
}

@Test
public void testMergeFilterInsname() throws IOException, MalformedURLException, FitsException {
File output = new File(JUnitBaseTest.TEST_DIR_TEST + "merge_filter_result.oifits");

// Filter block
String[] result = callProcessor(new String[]{"merge",
"-o", output.getAbsolutePath(), "-insname", "SPECTRO_SC",
TEST_DIR_OIFITS + "NGC5128_2005.oifits"
});
Assert.assertTrue("Bad return message: " + result[OUT_INDEX],
getOut(result) != null ? getOut(result).startsWith("Result is empty, no file created.") : false);
Assert.assertTrue("No result file should be created", !output.exists());
output.delete();

// Filter pass
result = callProcessor(new String[]{"merge",
"-o", output.getAbsolutePath(), "-insname", "MIDI/PRISM",
TEST_DIR_OIFITS + "NGC5128_2005.oifits"
});
Assert.assertTrue("Bad return message: " + result[OUT_INDEX], getOut(result).length() == 0);
OIFitsFile mergedFile = OIFitsLoader.loadOIFits(output.getAbsolutePath());
Assert.assertEquals("Bad number of Ins in merged file", 1, mergedFile.getOiWavelengths().length);
output.delete();

}

private static String getOut(String[] result) {
return result[OUT_INDEX] != null ? result[OUT_INDEX] : "";
}
Expand Down

0 comments on commit 0f5ec3e

Please sign in to comment.