Skip to content

Commit e4d3182

Browse files
authored
Merge pull request #1787 from Caltech-IPAC/FIREFLY-1765-multispectrum-extract
FIREFLY-1765: Allow non-array type columns
2 parents 0384e4f + 1d69953 commit e4d3182

File tree

3 files changed

+108
-15
lines changed

3 files changed

+108
-15
lines changed

src/firefly/java/edu/caltech/ipac/firefly/server/persistence/MultiSpectrumProcessor.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import edu.caltech.ipac.table.GroupInfo;
2525
import edu.caltech.ipac.table.JsonTableUtil;
2626
import edu.caltech.ipac.table.ParamInfo;
27+
import edu.caltech.ipac.table.ResourceInfo;
2728
import edu.caltech.ipac.table.TableMeta;
2829
import edu.caltech.ipac.table.TableUtil;
2930

@@ -33,6 +34,7 @@
3334
import java.util.List;
3435
import java.util.stream.Collectors;
3536

37+
import static edu.caltech.ipac.firefly.data.sofia.VOSpectraModel.SPECTRADM_UTYPE;
3638
import static edu.caltech.ipac.util.StringUtils.applyIfNotEmpty;
3739
import static edu.caltech.ipac.util.StringUtils.isEmpty;
3840

@@ -48,8 +50,11 @@ public class MultiSpectrumProcessor extends EmbeddedDbProcessor {
4850
public static final String MODE = "mode";
4951
public static final String SEL_ROW_IDX = "sel_row_idx";
5052
public static final String SPECTR_IDX = "spectr_idx";
53+
public static final String MULTI_SPECTRUM_UTYPE = "ipac:MultiSpectrum"; // utype for the MultiSpectrum table
54+
public static final String MULTI_SPECTRUM_SERVICE_UTYPE = MULTI_SPECTRUM_UTYPE + "-service";
55+
public static final String SPECTRUM_DATA_UTYPE = SPECTRADM_UTYPE + ".Data"; // utype for the spectrum data table
5156

52-
private enum Mode { fetch, // retrieve the MultiSpectrum table, return data product table
57+
public enum Mode { fetch, // retrieve the MultiSpectrum table, return data product table
5358
links, // return links table to the spectrums
5459
extract // extract the spectrum from array data, then return a SpectralDM table.
5560
};
@@ -90,21 +95,24 @@ protected DataGroupPart getResultSet(TableServerRequest treq, DbAdapter dbAdapte
9095
//====================================================================
9196

9297

93-
record MultiSpecInfo(DataGroup table, List<DataType> metaCols, List<GroupInfo> spectrums){}
98+
record MultiSpecInfo(DataGroup table, List<DataType> metaCols, List<GroupInfo> spectrums, List<ResourceInfo> services){}
9499

95100

96101
private static MultiSpecInfo findSpectrums(TableServerRequest treq, DbAdapter dbAdapter) throws DataAccessException {
97102
DataGroup table = dbAdapter.getHeaders(dbAdapter.getDataTable());
98103

99-
if (!table.getAttribute(TableMeta.UTYPE, "").equalsIgnoreCase("ipac:MultiSpectrum")) return null;
104+
if (!table.getAttribute(TableMeta.UTYPE, "").equalsIgnoreCase(MULTI_SPECTRUM_UTYPE)) return null;
100105

101106
List<GroupInfo> spectrums = table.getGroupInfos().stream()
102107
.filter(g -> String.valueOf(g.getUtype()).toLowerCase().matches("(?i)ipac:(Spectrum\\.)?ArrayData"))
103108
.collect(Collectors.toList());
104109
List<DataType> metaCols = Arrays.stream(table.getDataDefinitions())
105110
.filter(dt -> !dt.isArrayType()).collect(Collectors.toList());
111+
List<ResourceInfo> services = table.getResourceInfos().stream()
112+
.filter(ri -> MULTI_SPECTRUM_SERVICE_UTYPE.equalsIgnoreCase(String.valueOf(ri.getUtype())))
113+
.collect(Collectors.toList());
106114

107-
return new MultiSpecInfo(table, metaCols, spectrums);
115+
return new MultiSpecInfo(table, metaCols, spectrums, services);
108116
}
109117

110118
private DataGroupPart createSpectrumTable(TableServerRequest treq, DbAdapter dbAdapter, MultiSpecInfo specs) throws DataAccessException {
@@ -135,9 +143,19 @@ private DataGroupPart createSpectrumTable(TableServerRequest treq, DbAdapter dbA
135143
}
136144

137145
// add SpectralDM meta
138-
table.addAttribute(TableMeta.UTYPE, "spec:Spectrum");
139-
selSpec.setUtype("spec:Spectrum.Data");
140-
table.setGroupInfos(Arrays.asList(selSpec));
146+
table.getTableMeta().setAttribute(TableMeta.UTYPE, SPECTRADM_UTYPE);
147+
selSpec.setUtype(SPECTRUM_DATA_UTYPE);
148+
table.setGroupInfos(List.of(selSpec));
149+
150+
// copy service descriptors if any
151+
if (!specs.services.isEmpty()) {
152+
table.setResourceInfos(
153+
specs.services.stream()
154+
.filter(ri -> ri.getUtype().equalsIgnoreCase(MULTI_SPECTRUM_SERVICE_UTYPE))
155+
.map(ResourceInfo::copyOf)
156+
.peek(ri -> ri.setUtype("adhoc:service"))
157+
.toList());
158+
}
141159

142160
// therefore, convert all paramRefs to columnRefs
143161
table.getGroupInfos().forEach(gInfo -> {
@@ -147,8 +165,6 @@ private DataGroupPart createSpectrumTable(TableServerRequest treq, DbAdapter dbA
147165
gInfo.setParamRefs(null);
148166
});
149167

150-
151-
152168
return new DataGroupPart(table, 0, table.size());
153169
}
154170

@@ -200,7 +216,7 @@ private DataGroupPart createDataProductTable(TableServerRequest treq, DbAdapter
200216
.filter(gi -> !"ipac:Spectrum.ArrayData".equalsIgnoreCase(String.valueOf(gi.getUtype())))
201217
.collect(Collectors.toList())); // remove all Spectrum.ArrayData groups;
202218
table.setResourceInfos(table.getResourceInfos().stream()
203-
.filter(ri -> !"ipac:MultiSpectrum".equalsIgnoreCase(String.valueOf(ri.getUtype())))
219+
.filter(ri -> !MULTI_SPECTRUM_UTYPE.equalsIgnoreCase(String.valueOf(ri.getUtype())))
204220
.collect(Collectors.toList())); // remove all ipac:MultiSpectrum resources;
205221

206222
for (int i = 0; i < table.size(); i++) {
@@ -281,16 +297,16 @@ private DataGroup transformArrayToRows(DataGroup table, List<GroupInfo.RefInfo>
281297
Logger.getLogger().warn("MultiSpectrumProcessor:transformArrayToRows is expecting only 1 row but received: " + table.size());
282298
}
283299
Object[] cary = table.get(0).getData();
284-
if (!cary[0].getClass().isArray()) {
285-
Logger.getLogger().error("MultiSpectrumProcessor:transformArrayToRows: table cell is not an array");
286-
return null;
287-
}
288300
int nrows = Array.getLength(cary[cary.length-1]); // assuming the added data has the right values. need to find a better way to handle this.
289301
for (int i = 0; i < nrows; i++) {
290302
Object[] rowData = new Object[cols.size()];
291303
int csize = (cols.size() - params.size());
292304
for (int c = 0; c < csize; c++) {
293-
rowData[c] = getAryVal(cary, c, i);;
305+
if (cary[c].getClass().isArray()) {
306+
rowData[c] = getAryVal(cary, c, i);;
307+
} else {
308+
rowData[c] = cary[c]; // not an array, use same value for all rows.
309+
}
294310
}
295311
for (int c = 0; c < params.size(); c++) {
296312
ParamInfo pinfo = table.getParam(toCname(params.get(c).getRef(), table));
@@ -300,6 +316,7 @@ private DataGroup transformArrayToRows(DataGroup table, List<GroupInfo.RefInfo>
300316
}
301317
return ntable;
302318
}
319+
303320
private Object getAryVal(Object[] aryOfAry, int oIdx, int idx) {
304321
try {
305322
return getAryVal(aryOfAry[oIdx], idx);

src/firefly/java/edu/caltech/ipac/table/ResourceInfo.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ public ResourceInfo(String ID, String name, String type, String utype, String de
5353

5454
public Map<String, String> getInfos() { return infos; }
5555
public void setInfos(Map<String, String> infos) { this.infos = infos; }
56+
57+
public ResourceInfo copyOf() {
58+
ResourceInfo copy = new ResourceInfo();
59+
copy.setID(this.ID);
60+
copy.setName(this.name);
61+
copy.setType(this.type);
62+
copy.setUtype(this.utype);
63+
copy.setDesc(this.desc);
64+
copy.setGroups(new ArrayList<>(this.groups));
65+
copy.setParams(new ArrayList<>(this.params));
66+
copy.setInfos(new HashMap<>(this.infos));
67+
return copy;
68+
}
5669
}
5770

5871

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* License information at https://github.com/Caltech-IPAC/firefly/blob/master/License.txt
3+
*/
4+
5+
package edu.caltech.ipac.firefly.server.persistence;
6+
7+
import edu.caltech.ipac.firefly.ConfigTest;
8+
import edu.caltech.ipac.firefly.data.TableServerRequest;
9+
import edu.caltech.ipac.firefly.server.util.Logger;
10+
import edu.caltech.ipac.firefly.util.FileLoader;
11+
import edu.caltech.ipac.table.DataGroup;
12+
import edu.caltech.ipac.table.VotableTest;
13+
import org.apache.logging.log4j.Level;
14+
import org.junit.BeforeClass;
15+
import org.junit.Test;
16+
17+
import java.io.File;
18+
19+
import static edu.caltech.ipac.firefly.data.sofia.VOSpectraModel.SPECTRADM_UTYPE;
20+
import static edu.caltech.ipac.firefly.server.persistence.MultiSpectrumProcessor.*;
21+
import static edu.caltech.ipac.table.TableMeta.UTYPE;
22+
import static org.junit.Assert.*;
23+
24+
/**
25+
* Date: 6/3/25
26+
*
27+
* @author loi
28+
* @version : $
29+
*/
30+
public class MultiSpectrumProcessorTest extends ConfigTest {
31+
32+
private static File testFile
33+
;
34+
35+
@BeforeClass
36+
public static void setUp() {
37+
setupServerContext(null);
38+
testFile = FileLoader.resolveFile(VotableTest.class, "/multispectrum-array.vot"); // uses same ./table test data directory
39+
if (false) Logger.setLogLevel(Level.DEBUG);
40+
}
41+
42+
@Test
43+
public void testExtract() {
44+
MultiSpectrumProcessor processor = new MultiSpectrumProcessor();
45+
TableServerRequest treq = new TableServerRequest(MultiSpectrumProcessor.PROC_ID);
46+
treq.setParam(SOURCE, testFile.getAbsolutePath());
47+
treq.setParam(MODE, Mode.extract.name());
48+
treq.setParam(SEL_ROW_IDX, "0");
49+
try {
50+
DataGroup table = processor.getData(treq).getData();
51+
assertNotNull("Should not be null", table);
52+
assertEquals("Should have 694 row", 694, table.size());
53+
assertEquals("Should have 18 columns", 18, table.getDataDefinitions().length);
54+
assertEquals("Is a Spectrum", SPECTRADM_UTYPE, table.getAttribute(UTYPE));
55+
assertEquals("Has defined spectrum data", SPECTRUM_DATA_UTYPE, table.getGroupInfos().getFirst().getUtype());
56+
assertEquals("Is photometry spectrum", "photometry", table.getGroupInfos().getFirst().getID());
57+
assertTrue("Has Service Descriptor", table.getResourceInfos().getFirst().getUtype().equalsIgnoreCase("adhoc:service"));
58+
} catch (Exception e) {
59+
Logger.getLogger().error(e);
60+
throw new RuntimeException("Failed to process multi-spectrum data", e);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)