Skip to content

Commit 9d10589

Browse files
committed
improved Merger to filter wavelengths (+redim arrays)
1 parent 29a6078 commit 9d10589

File tree

9 files changed

+693
-166
lines changed

9 files changed

+693
-166
lines changed

src/main/java/fr/jmmc/oitools/fits/FitsTable.java

Lines changed: 137 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
import fr.jmmc.oitools.meta.KeywordMeta;
2525
import fr.jmmc.oitools.meta.Types;
2626
import fr.jmmc.oitools.meta.Units;
27+
import fr.jmmc.oitools.meta.WaveColumnMeta;
2728
import fr.jmmc.oitools.model.ModelVisitor;
2829
import fr.jmmc.oitools.model.OIFitsChecker;
2930
import fr.jmmc.oitools.model.OITable;
3031
import fr.jmmc.oitools.model.Rule;
3132
import fr.jmmc.oitools.model.range.Range;
3233
import fr.nom.tam.util.ArrayFuncs;
34+
import java.lang.reflect.Array;
3335
import java.util.ArrayList;
3436
import java.util.Arrays;
3537
import java.util.BitSet;
@@ -188,13 +190,26 @@ protected final void copyTable(final FitsTable src) throws IllegalArgumentExcept
188190
* @throws IllegalArgumentException if the number of rows is less than 1
189191
*/
190192
public final void resizeTable(final int nbKeepRows, final BitSet maskRows) throws IllegalArgumentException {
193+
resizeTable(nbKeepRows, maskRows, null);
194+
}
195+
196+
/**
197+
* Resize the table ie column arrays + fix the Fits NAXIS2 keyword value
198+
*
199+
* @param nbKeepRows number of rows to keep i.e. the Fits NAXIS2 keyword value
200+
* @param maskRows bit set indicating which rows to keep (true means keep row)
201+
* @param maskWavelengths bit set indicating which wavelength to keep (true means keep index)
202+
* @throws IllegalArgumentException if the number of rows is less than 1
203+
*/
204+
public final void resizeTable(final int nbKeepRows, final BitSet maskRows, final BitSet maskWavelengths) throws IllegalArgumentException {
191205
final int nbRows = getNbRows();
192-
if (nbKeepRows == nbRows) {
206+
if ((nbKeepRows == nbRows) && (maskWavelengths == null)) {
193207
return;
194208
}
195209
if (nbKeepRows < 1) {
196210
throw new IllegalArgumentException("Invalid number of rows : the table must have at least 1 row !");
197211
}
212+
final int nbKeepWl = (maskWavelengths != null) ? maskWavelengths.cardinality() : -1;
198213

199214
// Resize column values:
200215
for (ColumnMeta column : getColumnDescCollection()) {
@@ -203,11 +218,11 @@ public final void resizeTable(final int nbKeepRows, final BitSet maskRows) throw
203218

204219
// ignore optional columns (null):
205220
if (columnValueOriginal != null) {
206-
final int[] dims = getColumnArrayDims(column, nbKeepRows);
221+
final int[] dims = getColumnArrayDims(column, nbKeepRows, nbKeepWl);
207222
final Object columnValue = createColumnArray(column, dims);
208223

209-
// copy data
210-
filterColumnArray(columnValueOriginal, maskRows, columnValue, dims);
224+
// copy data (may filter wavelengths)
225+
filterColumnArray(columnName, columnValueOriginal, columnValue, (dims.length == 1), maskRows, nbKeepWl, maskWavelengths);
211226

212227
if (logger.isLoggable(Level.FINE)) {
213228
logger.log(Level.FINE, "COLUMN {0} = ''{1}''", new Object[]{columnName, columnValue});
@@ -231,7 +246,7 @@ public final void resizeTable(final int nbKeepRows, final BitSet maskRows) throw
231246
* @return new column arrays
232247
*/
233248
public Object createColumnArray(final ColumnMeta column, final int nbRows) {
234-
return createColumnArray(column, getColumnArrayDims(column, nbRows));
249+
return createColumnArray(column, getColumnArrayDims(column, nbRows, -1));
235250
}
236251

237252
protected static Object createColumnArray(final ColumnMeta column, final int[] dims) {
@@ -250,11 +265,13 @@ protected static Object createColumnArray(final ColumnMeta column, final int[] d
250265
return value;
251266
}
252267

253-
protected static int[] getColumnArrayDims(final ColumnMeta column, final int nbRows) {
268+
protected static int[] getColumnArrayDims(final ColumnMeta column, final int nbRows, final int nbWl) {
254269
final String name = column.getName();
255-
final int repeat = column.getRepeat(); // repeat = row size
256270
final Types type = column.getDataType(); // data type
257271

272+
// fixed array size:
273+
final int repeat = ((nbWl != -1) && column instanceof WaveColumnMeta) ? nbWl : column.getRepeat();
274+
258275
if (logger.isLoggable(Level.FINE)) {
259276
logger.log(Level.FINE, "COLUMN [{0}] [{1}{2}]", new Object[]{name, repeat, column.getType()});
260277
}
@@ -320,7 +337,8 @@ protected static void fillUndefinedArrays(final Object output, final int[] dimen
320337
/* character/date data type */
321338
Arrays.fill((String[]) output, UNDEFINED_STRING);
322339
} else {
323-
logger.log(Level.INFO, "fillUndefinedArrays: Unsupported array type: {0}", output.getClass());
340+
logger.log(Level.INFO, "fillUndefinedArrays: Unsupported array type: {0}",
341+
(output != null) ? output.getClass() : null);
324342
}
325343
} else {
326344
final Object[] oo = (Object[]) output;
@@ -330,71 +348,133 @@ protected static void fillUndefinedArrays(final Object output, final int[] dimen
330348
}
331349
}
332350

333-
protected static void filterColumnArray(final Object input, final BitSet keepMask, final Object output, final int[] dims) {
351+
protected static void filterColumnArray(final String columnName, final Object input, final Object output, final boolean is1D,
352+
final BitSet keepMaskRows, final int nbKeepWl, final BitSet keepMaskWavelengths) {
334353
// no bound checks:
335-
if (1 == dims.length) {
336-
if (output instanceof double[]) {
337-
/* double data type */
338-
final double[] os = (double[]) input;
339-
final double[] oo = (double[]) output;
354+
if (is1D) {
355+
filterColumnArray1D(columnName, input, output, keepMaskRows);
356+
} else {
357+
final Object[] os = (Object[]) input;
358+
final Object[] oo = (Object[]) output;
340359

341-
for (int i = keepMask.nextSetBit(0), j = 0, len = oo.length; i >= 0 && j < len; i = keepMask.nextSetBit(i + 1), j++) {
342-
oo[j] = os[i];
343-
}
344-
} else if (output instanceof float[]) {
345-
/* real data type */
346-
final float[] os = (float[]) input;
347-
final float[] oo = (float[]) output;
360+
for (int i = keepMaskRows.nextSetBit(0), j = 0, len = oo.length; i >= 0 && j < len; i = keepMaskRows.nextSetBit(i + 1), j++) {
361+
// Array copy needed to ensure deep copy (and not mixing sub-arrays):
362+
filterColumnArray(columnName, os[i], oo[j], nbKeepWl, keepMaskWavelengths);
363+
}
364+
}
365+
}
348366

349-
for (int i = keepMask.nextSetBit(0), j = 0, len = oo.length; i >= 0 && j < len; i = keepMask.nextSetBit(i + 1), j++) {
350-
oo[j] = os[i];
351-
}
352-
} else if (output instanceof short[]) {
353-
/* integer data type */
354-
final short[] os = (short[]) input;
355-
final short[] oo = (short[]) output;
367+
private static void filterColumnArray(final String columnName, final Object input, final Object output,
368+
final int nbKeepWl, final BitSet keepMaskWavelengths) {
356369

357-
for (int i = keepMask.nextSetBit(0), j = 0, len = oo.length; i >= 0 && j < len; i = keepMask.nextSetBit(i + 1), j++) {
358-
oo[j] = os[i];
359-
}
360-
} else if (output instanceof int[]) {
361-
/* integer data type */
362-
final int[] os = (int[]) input;
363-
final int[] oo = (int[]) output;
370+
final String oname = input.getClass().getName();
371+
final String cname = output.getClass().getName();
364372

365-
for (int i = keepMask.nextSetBit(0), j = 0, len = oo.length; i >= 0 && j < len; i = keepMask.nextSetBit(i + 1), j++) {
366-
oo[j] = os[i];
367-
}
368-
} else if (output instanceof boolean[]) {
369-
/* logical data type */
370-
final boolean[] os = (boolean[]) input;
371-
final boolean[] oo = (boolean[]) output;
373+
if (!oname.equals(cname)) {
374+
return;
375+
}
372376

373-
for (int i = keepMask.nextSetBit(0), j = 0, len = oo.length; i >= 0 && j < len; i = keepMask.nextSetBit(i + 1), j++) {
374-
oo[j] = os[i];
375-
}
376-
} else if (output instanceof String[]) {
377-
/* character/date data type */
378-
final String[] os = (String[]) input;
379-
final String[] oo = (String[]) output;
377+
if (oname.charAt(0) != '[') {
378+
return;
379+
}
380+
381+
if (oname.charAt(1) == '[') {
382+
// N dimensions
383+
final Object[] os = (Object[]) input;
384+
final Object[] oo = (Object[]) output;
380385

381-
for (int i = keepMask.nextSetBit(0), j = 0, len = oo.length; i >= 0 && j < len; i = keepMask.nextSetBit(i + 1), j++) {
382-
oo[j] = os[i];
386+
if (os.length == oo.length) {
387+
// dimensions matches:
388+
for (int i = 0; i < os.length; i += 1) {
389+
filterColumnArray(columnName, os[i], oo[i], nbKeepWl, keepMaskWavelengths);
383390
}
384391
} else {
385-
logger.log(Level.INFO, "filterColumnArray: Unsupported array type: {0}", output.getClass());
392+
if (keepMaskWavelengths == null) {
393+
logger.log(Level.WARNING, "filterColumnArray[{0}] invalid dimensions: {1} != {2}", new Object[]{columnName, os.length, oo.length});
394+
} else if (nbKeepWl != oo.length) {
395+
logger.log(Level.WARNING, "filterColumnArray[{0}] invalid dimensions: {1} != {2}", new Object[]{columnName, nbKeepWl, oo.length});
396+
} else {
397+
// dimensions matches:
398+
filterColumnArray(columnName, input, output, false, keepMaskWavelengths, nbKeepWl, keepMaskWavelengths);
399+
}
386400
}
387401
} else {
388-
final Object[] os = (Object[]) input;
389-
final Object[] oo = (Object[]) output;
402+
// 1 dimension:
403+
final int oLen = Array.getLength(input);
404+
final int cLen = Array.getLength(output);
390405

391-
for (int i = keepMask.nextSetBit(0), j = 0, len = oo.length; i >= 0 && j < len; i = keepMask.nextSetBit(i + 1), j++) {
392-
// Array copy needed to ensure deep copy (and not mixing sub-arrays):
393-
ArrayFuncs.copyArray(os[i], oo[j]);
406+
if (oLen == cLen) {
407+
// dimensions matches:
408+
System.arraycopy(input, 0, output, 0, oLen);
409+
} else {
410+
if (keepMaskWavelengths == null) {
411+
logger.log(Level.WARNING, "filterColumnArray[{0}] invalid dimensions: {1} != {2}", new Object[]{columnName, oLen, cLen});
412+
} else if (nbKeepWl != cLen) {
413+
logger.log(Level.WARNING, "filterColumnArray[{0}] invalid dimensions: {1} != {2}", new Object[]{columnName, nbKeepWl, cLen});
414+
} else {
415+
// dimensions matches:
416+
filterColumnArray1D(columnName, input, output, keepMaskWavelengths);
417+
}
394418
}
395419
}
396420
}
397421

422+
protected static void filterColumnArray1D(final String columnName, final Object input, final Object output, final BitSet keepMaskRows) {
423+
// no bound checks:
424+
if (output instanceof double[]) {
425+
/* double data type */
426+
final double[] os = (double[]) input;
427+
final double[] oo = (double[]) output;
428+
429+
for (int i = keepMaskRows.nextSetBit(0), j = 0, len = oo.length; i >= 0 && j < len; i = keepMaskRows.nextSetBit(i + 1), j++) {
430+
oo[j] = os[i];
431+
}
432+
} else if (output instanceof float[]) {
433+
/* real data type */
434+
final float[] os = (float[]) input;
435+
final float[] oo = (float[]) output;
436+
437+
for (int i = keepMaskRows.nextSetBit(0), j = 0, len = oo.length; i >= 0 && j < len; i = keepMaskRows.nextSetBit(i + 1), j++) {
438+
oo[j] = os[i];
439+
}
440+
} else if (output instanceof short[]) {
441+
/* integer data type */
442+
final short[] os = (short[]) input;
443+
final short[] oo = (short[]) output;
444+
445+
for (int i = keepMaskRows.nextSetBit(0), j = 0, len = oo.length; i >= 0 && j < len; i = keepMaskRows.nextSetBit(i + 1), j++) {
446+
oo[j] = os[i];
447+
}
448+
} else if (output instanceof int[]) {
449+
/* integer data type */
450+
final int[] os = (int[]) input;
451+
final int[] oo = (int[]) output;
452+
453+
for (int i = keepMaskRows.nextSetBit(0), j = 0, len = oo.length; i >= 0 && j < len; i = keepMaskRows.nextSetBit(i + 1), j++) {
454+
oo[j] = os[i];
455+
}
456+
} else if (output instanceof boolean[]) {
457+
/* logical data type */
458+
final boolean[] os = (boolean[]) input;
459+
final boolean[] oo = (boolean[]) output;
460+
461+
for (int i = keepMaskRows.nextSetBit(0), j = 0, len = oo.length; i >= 0 && j < len; i = keepMaskRows.nextSetBit(i + 1), j++) {
462+
oo[j] = os[i];
463+
}
464+
} else if (output instanceof String[]) {
465+
/* character/date data type */
466+
final String[] os = (String[]) input;
467+
final String[] oo = (String[]) output;
468+
469+
for (int i = keepMaskRows.nextSetBit(0), j = 0, len = oo.length; i >= 0 && j < len; i = keepMaskRows.nextSetBit(i + 1), j++) {
470+
oo[j] = os[i];
471+
}
472+
} else {
473+
logger.log(Level.INFO, "filterColumnArray[{0}]: Unsupported array type: {1}",
474+
new Object[]{columnName, (output != null) ? output.getClass() : null});
475+
}
476+
}
477+
398478
/**
399479
* Indicate to clear any cached value (derived column ...)
400480
*/

src/main/java/fr/jmmc/oitools/model/Granule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public boolean match(final Granule pattern, final Granule candidate) {
7777
}
7878
}
7979
if (pattern.hasDistinctWavelengthRanges() && (candidate.getInsMode() != null)) {
80-
if (!Range.matchRange(pattern.getDistinctMjdRanges(), candidate.getInsMode().getWavelengthRange())) {
80+
if (!Range.matchRange(pattern.getDistinctWavelengthRanges(), candidate.getInsMode().getWavelengthRange())) {
8181
return false;
8282
}
8383
}

src/main/java/fr/jmmc/oitools/model/OIFitsCollection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ private List<Granule> findGranules(final Selector selector) {
502502
}
503503

504504
// Wavelength ranges criteria:
505-
if (selector.hasWavelengthRange()) {
505+
if (selector.hasWavelengthRanges()) {
506506
pattern.getDistinctWavelengthRanges().addAll(selector.getWavelengthRanges());
507507
}
508508

src/main/java/fr/jmmc/oitools/model/range/Range.java

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import fr.jmmc.jmcs.util.ObjectUtils;
2323
import java.util.ArrayList;
2424
import java.util.Collection;
25+
import java.util.HashSet;
2526
import java.util.List;
2627
import java.util.Set;
2728

@@ -153,6 +154,10 @@ public boolean overlap(final Range other) {
153154
return (getMin() <= other.getMax()) && (getMax() >= other.getMin());
154155
}
155156

157+
public boolean overlapFully(final Range other) {
158+
return (getMin() <= other.getMin()) && (getMax() >= other.getMax());
159+
}
160+
156161
public static boolean matchRange(final Collection<Range> selected, final Range candidate) {
157162
for (Range sel : selected) {
158163
if (sel.overlap(candidate)) {
@@ -173,6 +178,59 @@ public static boolean matchRanges(final Collection<Range> selected, final Collec
173178
return false;
174179
}
175180

181+
public static boolean matchFully(final Range selected, final Collection<Range> candidates) {
182+
for (Range cand : candidates) {
183+
if (selected.overlapFully(cand)) {
184+
return true;
185+
}
186+
}
187+
return false;
188+
}
189+
190+
public static boolean matchFully(final Collection<Range> selected, final Range candidate) {
191+
final Set<Range> matchSelected = new HashSet<Range>();
192+
for (Range sel : selected) {
193+
if (sel.overlapFully(candidate)) {
194+
matchSelected.add(sel);
195+
}
196+
}
197+
return (selected.size() == matchSelected.size());
198+
}
199+
200+
public static boolean matchFully(final Collection<Range> selected, final Collection<Range> candidates) {
201+
final Set<Range> matchSelected = new HashSet<Range>();
202+
for (Range cand : candidates) {
203+
for (Range sel : selected) {
204+
if (sel.overlapFully(cand)) {
205+
matchSelected.add(sel);
206+
}
207+
}
208+
}
209+
return (selected.size() == matchSelected.size());
210+
}
211+
212+
public static void getMatchingSelected(final Collection<Range> selected, final Range candidate, final Set<Range> matching) {
213+
matching.clear();
214+
215+
for (Range sel : selected) {
216+
if (sel.overlap(candidate)) {
217+
matching.add(sel);
218+
}
219+
}
220+
}
221+
222+
public static void getMatchingSelected(final Collection<Range> selected, final Collection<Range> candidates, final Set<Range> matching) {
223+
matching.clear();
224+
225+
for (Range sel : selected) {
226+
for (Range cand : candidates) {
227+
if (sel.overlap(cand)) {
228+
matching.add(sel);
229+
}
230+
}
231+
}
232+
}
233+
176234
public static void getMatchingRanges(final Collection<Range> selected, final Collection<Range> candidates, final Set<Range> matching) {
177235
matching.clear();
178236

@@ -269,14 +327,7 @@ public static boolean equals(final List<Range> ranges, final List<Range> otherRa
269327
* @param value value to test
270328
* @return true if the given value is inside given ranges
271329
*/
272-
public static boolean contains(final List<Range> ranges, final double value) {
273-
if (ranges == null) {
274-
return false;
275-
}
276-
return find(ranges, value) != null;
277-
}
278-
279-
public static boolean contains(final Set<Range> ranges, final double value) {
330+
public static boolean contains(final Collection<Range> ranges, final double value) {
280331
if (ranges == null) {
281332
return false;
282333
}

0 commit comments

Comments
 (0)