|
| 1 | +/** |
| 2 | + * License information at https://github.com/Caltech-IPAC/firefly/blob/master/License.txt |
| 3 | + */ |
| 4 | +package edu.caltech.ipac.firefly.server.query; |
| 5 | + |
| 6 | +import edu.caltech.ipac.firefly.data.TableServerRequest; |
| 7 | +import edu.caltech.ipac.firefly.server.util.QueryUtil; |
| 8 | +import edu.caltech.ipac.firefly.server.util.ipactable.DataGroupWriter; |
| 9 | +import edu.caltech.ipac.util.*; |
| 10 | + |
| 11 | +import java.io.File; |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.ArrayList; |
| 14 | + |
| 15 | + |
| 16 | +/** |
| 17 | + * @author tatianag |
| 18 | + */ |
| 19 | +@SearchProcessorImpl(id = "XYWithErrors") |
| 20 | +public class XYWithErrorsProcessor extends IpacTablePartProcessor { |
| 21 | + private static final String SEARCH_REQUEST = "searchRequest"; |
| 22 | + private static final String X_COL_EXPR = "xColOrExpr"; |
| 23 | + private static final String Y_COL_EXPR = "yColOrExpr"; |
| 24 | + private static final String XERR_COL_EXPR = "xErrColOrExpr"; |
| 25 | + private static final String XERR_LOW_COL_EXPR = "xErrLowColOrExpr"; |
| 26 | + private static final String XERR_HIGH_COL_EXPR = "xErrHighColOrExpr"; |
| 27 | + private static final String YERR_COL_EXPR = "yErrColOrExpr"; |
| 28 | + private static final String YERR_LOW_COL_EXPR = "yErrLowColOrExpr"; |
| 29 | + private static final String YERR_HIGH_COL_EXPR = "yErrHighColOrExpr"; |
| 30 | + |
| 31 | + private static final Col NO_COL = new Col(); |
| 32 | + |
| 33 | + @Override |
| 34 | + protected File loadDataFile(TableServerRequest request) throws IOException, DataAccessException { |
| 35 | + String searchRequestJson = request.getParam(SEARCH_REQUEST); |
| 36 | + DataGroup dg = SearchRequestUtils.dataGroupFromSearchRequest(searchRequestJson); |
| 37 | + String xColOrExpr = request.getParam(X_COL_EXPR); |
| 38 | + String yColOrExpr = request.getParam(Y_COL_EXPR); |
| 39 | + String xErrColOrExpr = request.getParam(XERR_COL_EXPR); |
| 40 | + String xErrLowColOrExpr = request.getParam(XERR_LOW_COL_EXPR); |
| 41 | + String xErrHighColOrExpr = request.getParam(XERR_HIGH_COL_EXPR); |
| 42 | + String yErrColOrExpr = request.getParam(YERR_COL_EXPR); |
| 43 | + String yErrLowColOrExpr = request.getParam(YERR_LOW_COL_EXPR); |
| 44 | + String yErrHighColOrExpr = request.getParam(YERR_HIGH_COL_EXPR); |
| 45 | + |
| 46 | + boolean hasXError = !StringUtils.isEmpty(xErrColOrExpr); |
| 47 | + boolean hasXLowError = !StringUtils.isEmpty(xErrLowColOrExpr); |
| 48 | + boolean hasXHighError = !StringUtils.isEmpty(xErrHighColOrExpr) ; |
| 49 | + boolean hasYError = !StringUtils.isEmpty(yErrColOrExpr); |
| 50 | + boolean hasYLowError = !StringUtils.isEmpty(yErrLowColOrExpr); |
| 51 | + boolean hasYHighError = !StringUtils.isEmpty(yErrHighColOrExpr); |
| 52 | + if ((hasXError || hasXLowError || hasXHighError || hasYError || hasYLowError || hasYHighError) && dg.size()>=QueryUtil.DECI_ENABLE_SIZE) { |
| 53 | + throw new DataAccessException("Errors for more than "+QueryUtil.DECI_ENABLE_SIZE+" are not supported"); |
| 54 | + } |
| 55 | + |
| 56 | + // the output table columns: rowIdx, x, y, [[left, right], [low, high]] |
| 57 | + DataType[] dataTypes = dg.getDataDefinitions(); |
| 58 | + |
| 59 | + // create the array of getters, which know how to get double values |
| 60 | + ArrayList<Col> colsLst = new ArrayList<>(); |
| 61 | + colsLst.add(getCol(dataTypes, xColOrExpr,"x", false)); |
| 62 | + colsLst.add(getCol(dataTypes, yColOrExpr, "y", false)); |
| 63 | + |
| 64 | + if (hasXError) { |
| 65 | + colsLst.add(getCol(dataTypes, xErrColOrExpr, "xErr", true)); |
| 66 | + } |
| 67 | + if (hasXLowError) { |
| 68 | + colsLst.add(getCol(dataTypes, xErrLowColOrExpr, "xErrLow", true)); |
| 69 | + } |
| 70 | + if (hasXHighError) { |
| 71 | + colsLst.add(getCol(dataTypes, xErrHighColOrExpr, "xErrHigh", true)); |
| 72 | + } |
| 73 | + if (hasYError) { |
| 74 | + colsLst.add(getCol(dataTypes, yErrColOrExpr, "yErr", true)); |
| 75 | + } |
| 76 | + if (hasYLowError) { |
| 77 | + colsLst.add(getCol(dataTypes, yErrLowColOrExpr, "yErrLow", true)); |
| 78 | + } |
| 79 | + if (hasYHighError) { |
| 80 | + colsLst.add(getCol(dataTypes, yErrHighColOrExpr, "yErrHigh", true)); |
| 81 | + } |
| 82 | + |
| 83 | + Col[] cols = colsLst.toArray(new Col[colsLst.size()]); |
| 84 | + |
| 85 | + // create the array of output columns |
| 86 | + ArrayList<DataType> columnList = new ArrayList<>(); |
| 87 | + columnList.add(new DataType("rowIdx", Integer.class)); |
| 88 | + ArrayList<DataGroup.Attribute> colMeta = new ArrayList<>(); |
| 89 | + for (Col col : cols) { |
| 90 | + if (col.getter.isExpression()) { |
| 91 | + columnList.add(new DataType(col.colname, col.colname, Double.class, DataType.Importance.HIGH, "", false)); |
| 92 | + } else { |
| 93 | + columnList.add(dg.getDataDefintion(col.colname).copyWithNoColumnIdx(columnList.size())); |
| 94 | + colMeta.addAll(IpacTableUtil.getAllColMeta(dg.getAttributes().values(), col.colname)); |
| 95 | + } |
| 96 | + } |
| 97 | + DataType columns [] = columnList.toArray(new DataType[columnList.size()]); |
| 98 | + |
| 99 | + // create the return data group |
| 100 | + DataGroup retval = new DataGroup("XY with Errors", columns); |
| 101 | + |
| 102 | + DataObject retrow; |
| 103 | + int ncols = cols.length; |
| 104 | + Col col; |
| 105 | + DataType dt; |
| 106 | + double val; |
| 107 | + String formatted; |
| 108 | + DataType dtRowIdx = columns[0]; |
| 109 | + |
| 110 | + for (int rIdx = 0; rIdx < dg.size(); rIdx++) { |
| 111 | + DataObject row = dg.get(rIdx); |
| 112 | + retrow = new DataObject(retval); |
| 113 | + for (int c = 0; c < ncols ; c++) { |
| 114 | + col = cols[c]; |
| 115 | + val = col.getter.getValue(row); |
| 116 | + if (Double.isNaN(val) && !col.canBeNaN) { |
| 117 | + retrow = null; |
| 118 | + break; |
| 119 | + } else { |
| 120 | + dt = columns[c+1]; |
| 121 | + formatted = col.getter.getFormattedValue(row); |
| 122 | + if (formatted == null) { |
| 123 | + retrow.setDataElement(dt, QueryUtil.convertData(dt.getDataType(), val)); |
| 124 | + } else { |
| 125 | + retrow.setFormattedData(dt, formatted); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + if (retrow != null) { |
| 130 | + retrow.setDataElement(dtRowIdx, rIdx); |
| 131 | + retval.add(retrow); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + for (Col c : cols) { |
| 136 | + colMeta.add(new DataGroup.Attribute(c.exprColName, c.colOrExpr)); |
| 137 | + } |
| 138 | + retval.setAttributes(colMeta); |
| 139 | + |
| 140 | + retval.shrinkToFitData(); |
| 141 | + File outFile = createFile(request); |
| 142 | + DataGroupWriter.write(outFile, retval); |
| 143 | + return outFile; |
| 144 | + } |
| 145 | + |
| 146 | + private Col getCol(DataType[] dataTypes, String colOrExpr, String exprColName, boolean canBeNaN) throws DataAccessException { |
| 147 | + Col col = new Col(dataTypes, colOrExpr, exprColName, canBeNaN); |
| 148 | + if (!col.getter.isValid()) { |
| 149 | + throw new DataAccessException("Invalid column or expression: "+colOrExpr); |
| 150 | + } |
| 151 | + return col; |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + private static class Col { |
| 156 | + DataObjectUtil.DoubleValueGetter getter; |
| 157 | + String colname; |
| 158 | + String exprColName; |
| 159 | + String colOrExpr; |
| 160 | + boolean canBeNaN; |
| 161 | + |
| 162 | + Col() { |
| 163 | + this.canBeNaN = true; |
| 164 | + } |
| 165 | + |
| 166 | + Col(DataType[] dataTypes, String colOrExpr, String exprColName, boolean canBeNaN) { |
| 167 | + this.colOrExpr = colOrExpr; |
| 168 | + this.exprColName = exprColName; |
| 169 | + this.getter = new DataObjectUtil.DoubleValueGetter(dataTypes, colOrExpr); |
| 170 | + if (getter.isExpression()) { |
| 171 | + this.colname = exprColName; |
| 172 | + } else { |
| 173 | + this.colname = colOrExpr; |
| 174 | + } |
| 175 | + this.canBeNaN = canBeNaN; |
| 176 | + } |
| 177 | + } |
| 178 | +} |
| 179 | + |
0 commit comments