|
| 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_LOW_COL_EXPR = "xErrLowColOrExpr"; |
| 25 | + private static final String XERR_HIGH_COL_EXPR = "xErrHighColOrExpr"; |
| 26 | + private static final String YERR_LOW_COL_EXPR = "yErrLowColOrExpr"; |
| 27 | + private static final String YERR_HIGH_COL_EXPR = "yErrHighColOrExpr"; |
| 28 | + |
| 29 | + private static final Col NO_COL = new Col(); |
| 30 | + |
| 31 | + @Override |
| 32 | + protected File loadDataFile(TableServerRequest request) throws IOException, DataAccessException { |
| 33 | + String searchRequestJson = request.getParam(SEARCH_REQUEST); |
| 34 | + DataGroup dg = SearchRequestUtils.dataGroupFromSearchRequest(searchRequestJson); |
| 35 | + String xColOrExpr = request.getParam(X_COL_EXPR); |
| 36 | + String yColOrExpr = request.getParam(Y_COL_EXPR); |
| 37 | + String xErrLowColOrExpr = request.getParam(XERR_LOW_COL_EXPR); |
| 38 | + String xErrHighColOrExpr = request.getParam(XERR_HIGH_COL_EXPR); |
| 39 | + String yErrLowColOrExpr = request.getParam(YERR_LOW_COL_EXPR); |
| 40 | + String yErrHighColOrExpr = request.getParam(YERR_HIGH_COL_EXPR); |
| 41 | + |
| 42 | + boolean hasXLowError = !StringUtils.isEmpty(xErrLowColOrExpr); |
| 43 | + boolean hasXHighError = !StringUtils.isEmpty(xErrHighColOrExpr) ; |
| 44 | + boolean hasYLowError = !StringUtils.isEmpty(yErrLowColOrExpr); |
| 45 | + boolean hasYHighError = !StringUtils.isEmpty(yErrHighColOrExpr); |
| 46 | + if ((hasXLowError || hasXHighError || hasYLowError || hasYHighError) && dg.size()>=QueryUtil.DECI_ENABLE_SIZE) { |
| 47 | + throw new DataAccessException("Errors for more than "+QueryUtil.DECI_ENABLE_SIZE+" are not supported"); |
| 48 | + } |
| 49 | + |
| 50 | + // the output table columns: rowIdx, x, y, [[left, right], [low, high]] |
| 51 | + DataType[] dataTypes = dg.getDataDefinitions(); |
| 52 | + Col xCol = getCol(dataTypes, xColOrExpr,"x", false); |
| 53 | + Col yCol = getCol(dataTypes, yColOrExpr, "y", false); |
| 54 | + Col xLowCol = NO_COL; |
| 55 | + Col xHighCol = NO_COL; |
| 56 | + Col yLowCol = NO_COL; |
| 57 | + Col yHighCol = NO_COL; |
| 58 | + if (hasXLowError || hasXHighError) { |
| 59 | + String xLowColOrExpr = hasXLowError ? xColOrExpr+"-"+xErrLowColOrExpr : xColOrExpr; |
| 60 | + xLowCol = getCol(dataTypes, xLowColOrExpr, "left", true); |
| 61 | + String xHighColOrExpr = hasXHighError ? xColOrExpr+"+"+xErrHighColOrExpr : xColOrExpr; |
| 62 | + xHighCol = getCol(dataTypes, xHighColOrExpr, "right", true); |
| 63 | + } |
| 64 | + if (hasYLowError || hasYHighError) { |
| 65 | + String yLowColOrExpr = hasYLowError ? yColOrExpr+"-"+yErrLowColOrExpr : yColOrExpr; |
| 66 | + yLowCol = getCol(dataTypes, yLowColOrExpr, "low", true); |
| 67 | + String yHighColOrExpr = hasYHighError ? yColOrExpr+"+"+yErrHighColOrExpr : yColOrExpr; |
| 68 | + yHighCol = getCol(dataTypes, yHighColOrExpr, "high", true); |
| 69 | + } |
| 70 | + |
| 71 | + // create the array of getters, which know how to get double values |
| 72 | + ArrayList<Col> colsLst = new ArrayList<>(); |
| 73 | + colsLst.add(xCol); |
| 74 | + colsLst.add(yCol); |
| 75 | + if (hasXLowError || hasXHighError) { |
| 76 | + colsLst.add(xLowCol); |
| 77 | + colsLst.add(xHighCol); |
| 78 | + } |
| 79 | + if (hasYLowError || hasYHighError) { |
| 80 | + colsLst.add(yLowCol); |
| 81 | + colsLst.add(yHighCol); |
| 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 | + ArrayList<DataGroup.Attribute> colMeta = new ArrayList<>(); |
| 88 | + columnList.add(new DataType("rowIdx", Integer.class)); |
| 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 | + retval.setAttributes(colMeta); |
| 102 | + |
| 103 | + DataObject retrow; |
| 104 | + int ncols = cols.length; |
| 105 | + Col col; |
| 106 | + DataType dt; |
| 107 | + double val; |
| 108 | + String formatted; |
| 109 | + DataType dtRowIdx = columns[0]; |
| 110 | + |
| 111 | + for (int rIdx = 0; rIdx < dg.size(); rIdx++) { |
| 112 | + DataObject row = dg.get(rIdx); |
| 113 | + retrow = new DataObject(retval); |
| 114 | + for (int c = 0; c < ncols ; c++) { |
| 115 | + col = cols[c]; |
| 116 | + val = col.getter.getValue(row); |
| 117 | + if (Double.isNaN(val) && !col.canBeNaN) { |
| 118 | + retrow = null; |
| 119 | + break; |
| 120 | + } else { |
| 121 | + dt = columns[c+1]; |
| 122 | + formatted = col.getter.getFormattedValue(row); |
| 123 | + if (formatted == null) { |
| 124 | + retrow.setDataElement(dt, QueryUtil.convertData(dt.getDataType(), val)); |
| 125 | + } else { |
| 126 | + retrow.setFormattedData(dt, formatted); |
| 127 | + } |
| 128 | + col.updateMinMax(val); |
| 129 | + } |
| 130 | + } |
| 131 | + if (retrow != null) { |
| 132 | + retrow.setDataElement(dtRowIdx, rIdx); |
| 133 | + retval.add(retrow); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if (retval.size() > 0) { |
| 138 | + double xMin = Math.min(xCol.minVal, xLowCol.minVal); |
| 139 | + retval.addAttribute("X-MIN", String.valueOf(xMin)); |
| 140 | + double xMax = Math.max(xCol.maxVal, xHighCol.maxVal); |
| 141 | + retval.addAttribute("X-MAX", String.valueOf(xMax)); |
| 142 | + |
| 143 | + double yMin = Math.min(yCol.minVal, yLowCol.minVal); |
| 144 | + retval.addAttribute("Y-MIN", String.valueOf(yMin)); |
| 145 | + double yMax = Math.max(yCol.maxVal, yHighCol.maxVal); |
| 146 | + retval.addAttribute("Y-MAX", String.valueOf(yMax)); |
| 147 | + } |
| 148 | + |
| 149 | + retval.shrinkToFitData(); |
| 150 | + File outFile = createFile(request); |
| 151 | + DataGroupWriter.write(outFile, retval); |
| 152 | + return outFile; |
| 153 | + } |
| 154 | + |
| 155 | + private Col getCol(DataType[] dataTypes, String colOrExpr, String exprColName, boolean canBeNaN) throws DataAccessException { |
| 156 | + Col col = new Col(dataTypes, colOrExpr, exprColName, canBeNaN); |
| 157 | + if (!col.getter.isValid()) { |
| 158 | + throw new DataAccessException("Invalid column or expression: "+colOrExpr); |
| 159 | + } |
| 160 | + return col; |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + private static class Col { |
| 165 | + DataObjectUtil.DoubleValueGetter getter; |
| 166 | + String colname; |
| 167 | + boolean canBeNaN; |
| 168 | + double minVal = Double.MAX_VALUE; |
| 169 | + double maxVal = Double.MIN_VALUE; |
| 170 | + |
| 171 | + Col() { |
| 172 | + this.canBeNaN = true; |
| 173 | + } |
| 174 | + |
| 175 | + Col(DataType[] dataTypes, String colOrExpr, String exprColName, boolean canBeNaN) { |
| 176 | + this.getter = new DataObjectUtil.DoubleValueGetter(dataTypes, colOrExpr); |
| 177 | + if (getter.isExpression()) { |
| 178 | + this.colname = exprColName; |
| 179 | + } else { |
| 180 | + this.colname = colOrExpr; |
| 181 | + } |
| 182 | + this.canBeNaN = canBeNaN; |
| 183 | + } |
| 184 | + |
| 185 | + |
| 186 | + public void updateMinMax(double val) { |
| 187 | + if (val > maxVal) { maxVal = val; } |
| 188 | + if (val < minVal) { minVal = val; } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | + |
0 commit comments