24
24
import fr .jmmc .oitools .meta .KeywordMeta ;
25
25
import fr .jmmc .oitools .meta .Types ;
26
26
import fr .jmmc .oitools .meta .Units ;
27
+ import fr .jmmc .oitools .meta .WaveColumnMeta ;
27
28
import fr .jmmc .oitools .model .ModelVisitor ;
28
29
import fr .jmmc .oitools .model .OIFitsChecker ;
29
30
import fr .jmmc .oitools .model .OITable ;
30
31
import fr .jmmc .oitools .model .Rule ;
31
32
import fr .jmmc .oitools .model .range .Range ;
32
33
import fr .nom .tam .util .ArrayFuncs ;
34
+ import java .lang .reflect .Array ;
33
35
import java .util .ArrayList ;
34
36
import java .util .Arrays ;
35
37
import java .util .BitSet ;
@@ -188,13 +190,26 @@ protected final void copyTable(final FitsTable src) throws IllegalArgumentExcept
188
190
* @throws IllegalArgumentException if the number of rows is less than 1
189
191
*/
190
192
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 {
191
205
final int nbRows = getNbRows ();
192
- if (nbKeepRows == nbRows ) {
206
+ if (( nbKeepRows == nbRows ) && ( maskWavelengths == null ) ) {
193
207
return ;
194
208
}
195
209
if (nbKeepRows < 1 ) {
196
210
throw new IllegalArgumentException ("Invalid number of rows : the table must have at least 1 row !" );
197
211
}
212
+ final int nbKeepWl = (maskWavelengths != null ) ? maskWavelengths .cardinality () : -1 ;
198
213
199
214
// Resize column values:
200
215
for (ColumnMeta column : getColumnDescCollection ()) {
@@ -203,11 +218,11 @@ public final void resizeTable(final int nbKeepRows, final BitSet maskRows) throw
203
218
204
219
// ignore optional columns (null):
205
220
if (columnValueOriginal != null ) {
206
- final int [] dims = getColumnArrayDims (column , nbKeepRows );
221
+ final int [] dims = getColumnArrayDims (column , nbKeepRows , nbKeepWl );
207
222
final Object columnValue = createColumnArray (column , dims );
208
223
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 );
211
226
212
227
if (logger .isLoggable (Level .FINE )) {
213
228
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
231
246
* @return new column arrays
232
247
*/
233
248
public Object createColumnArray (final ColumnMeta column , final int nbRows ) {
234
- return createColumnArray (column , getColumnArrayDims (column , nbRows ));
249
+ return createColumnArray (column , getColumnArrayDims (column , nbRows , - 1 ));
235
250
}
236
251
237
252
protected static Object createColumnArray (final ColumnMeta column , final int [] dims ) {
@@ -250,11 +265,13 @@ protected static Object createColumnArray(final ColumnMeta column, final int[] d
250
265
return value ;
251
266
}
252
267
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 ) {
254
269
final String name = column .getName ();
255
- final int repeat = column .getRepeat (); // repeat = row size
256
270
final Types type = column .getDataType (); // data type
257
271
272
+ // fixed array size:
273
+ final int repeat = ((nbWl != -1 ) && column instanceof WaveColumnMeta ) ? nbWl : column .getRepeat ();
274
+
258
275
if (logger .isLoggable (Level .FINE )) {
259
276
logger .log (Level .FINE , "COLUMN [{0}] [{1}{2}]" , new Object []{name , repeat , column .getType ()});
260
277
}
@@ -320,7 +337,8 @@ protected static void fillUndefinedArrays(final Object output, final int[] dimen
320
337
/* character/date data type */
321
338
Arrays .fill ((String []) output , UNDEFINED_STRING );
322
339
} 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 );
324
342
}
325
343
} else {
326
344
final Object [] oo = (Object []) output ;
@@ -330,71 +348,133 @@ protected static void fillUndefinedArrays(final Object output, final int[] dimen
330
348
}
331
349
}
332
350
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 ) {
334
353
// 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 ;
340
359
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
+ }
348
366
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 ) {
356
369
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 ();
364
372
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
+ }
372
376
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 ;
380
385
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 );
383
390
}
384
391
} 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
+ }
386
400
}
387
401
} 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 );
390
405
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
+ }
394
418
}
395
419
}
396
420
}
397
421
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
+
398
478
/**
399
479
* Indicate to clear any cached value (derived column ...)
400
480
*/
0 commit comments