示例#1
0
 /**
  * Sets the table value of this parameter directly.
  *
  * @param table
  */
 public void setValueFromTable(StarTable table) {
   table_ = table;
   String name = table.getName();
   if (name == null) {
     name = "unnamed table";
   }
   setStringValue(name);
   setGotValue(true);
 }
 /**
  * Loads a StarTable into TOPCAT. Must be called from the event dispatch thread.
  *
  * @param table table to load
  * @param sender sender ID
  * @param key identifier for the loaded table
  */
 private void loadTable(StarTable table, URI sender, String key) {
   String name = table.getName();
   if (name == null || name.trim().length() == 0) {
     name = sender.toString();
   }
   TopcatModel tcModel = controlWindow_.addTable(table, name, true);
   if (key != null && key.trim().length() > 0) {
     idMap_.put(key, new TableWithRows(tcModel, null));
   }
 }
示例#3
0
  /**
   * Turns a TopcatModel into a StarTable, ready for serialization.
   *
   * @param tcModel model
   * @return table
   */
  public StarTable encode(TopcatModel tcModel) {

    /* Prepare table data and metadata for use and adjustment. */
    final StarTable dataModel = tcModel.getDataModel();
    List<DescribedValue> paramList = new ArrayList<DescribedValue>();
    long nrow = dataModel.getRowCount();
    ColumnStarTable extraTable = ColumnStarTable.makeTableWithRows(nrow);

    /* Mark as serialized TopcatModel. */
    paramList.add(new DescribedValue(IS_TCMODEL_INFO, Boolean.TRUE));
    paramList.add(new DescribedValue(VERSION_INFO, TopcatUtils.getVersion()));

    /* Record label. */
    paramList.add(new DescribedValue(LABEL_INFO, tcModel.getLabel()));

    /* Record column sequences. */
    ColumnList colList = tcModel.getColumnList();
    int nCol = colList.size();
    int[] icols = new int[nCol];
    boolean[] activs = new boolean[nCol];
    for (int jc = 0; jc < nCol; jc++) {
      icols[jc] = colList.getColumn(jc).getModelIndex();
      activs[jc] = colList.isActive(jc);
    }
    paramList.add(new DescribedValue(COLS_INDEX_INFO, icols));
    paramList.add(new DescribedValue(COLS_VISIBLE_INFO, activs));

    /* Record whether to broadcast rows. */
    paramList.add(new DescribedValue(SEND_ROWS_INFO, tcModel.getRowSendModel().isSelected()));

    /* Record sort order. */
    SortOrder sortOrder = tcModel.getSelectedSort();
    TableColumn sortCol = sortOrder == null ? null : sortOrder.getColumn();
    if (sortCol != null) {
      int icolSort = tcModel.getColumnList().indexOf(sortCol);
      if (icolSort >= 0) {
        boolean sense = tcModel.getSortSenseModel().isSelected();
        paramList.add(new DescribedValue(SORT_COLUMN_INFO, new Integer(icolSort)));
        paramList.add(new DescribedValue(SORT_SENSE_INFO, Boolean.valueOf(sense)));
      }
    }

    /* Store row subset flags in a new column. */
    List<RowSubset> subsetList = new ArrayList<RowSubset>(tcModel.getSubsets());
    boolean hadAll = subsetList.remove(RowSubset.ALL);
    assert hadAll;
    RowSubset[] subsets = subsetList.toArray(new RowSubset[0]);
    if (subsets.length > 0) {
      String[] subsetNames = new String[subsets.length];
      for (int is = 0; is < subsets.length; is++) {
        subsetNames[is] = subsets[is].getName();
      }
      paramList.add(new DescribedValue(SUBSET_NAMES_INFO, subsetNames));
      Object flagsArray = createFlagsArray(dataModel, subsets);
      if (flagsArray != null) {
        ColumnData flagsCol = ArrayColumn.makeColumn(SUBSET_FLAGS_INFO.getName(), flagsArray);
        ColumnInfo info = new ColumnInfo(SUBSET_FLAGS_INFO);
        info.setContentClass(flagsCol.getColumnInfo().getContentClass());
        flagsCol.setColumnInfo(info);
        extraTable.addColumn(flagsCol);
      }
    }

    /* Record current subset. */
    int iset = subsetList.indexOf(tcModel.getSelectedSubset());
    if (iset >= 0) {
      paramList.add(new DescribedValue(CURRENT_SUBSET_INFO, new Integer(iset)));
    }

    /* Copy parameters from the input table.
     * Be paranoid about possible name clashes. */
    for (Iterator it = dataModel.getParameters().iterator(); it.hasNext(); ) {
      Object item = it.next();
      if (item instanceof DescribedValue) {
        DescribedValue dval = (DescribedValue) item;
        String name = dval.getInfo().getName();
        String utype = dval.getInfo().getUtype();
        if (!isCodecUtype(utype)) {
          paramList.add(dval);
        }
      }
    }

    /* Prepare the output table object. */
    List<StarTable> joinList = new ArrayList<StarTable>();
    joinList.add(dataModel);
    if (extraTable.getColumnCount() > 0) {
      joinList.add(extraTable);
    }
    StarTable[] joins = joinList.toArray(new StarTable[0]);
    StarTable outTable =
        new MetaCopyStarTable(new JoinStarTable(joinList.toArray(new StarTable[0])));
    outTable.setName(dataModel.getName());

    /* Set the parameters. */
    outTable.getParameters().clear();
    outTable.getParameters().addAll(paramList);

    /* Return the result. */
    return outTable;
  }
  public Header getHeader() throws HeaderCardException {

    /* Work out the dimensions in columns and bytes of the table. */
    int rowLength = 0;
    int nUseCol = 0;
    int ncol = table.getColumnCount();
    for (int icol = 0; icol < ncol; icol++) {
      ColumnWriter writer = colWriters[icol];
      if (writer != null) {
        nUseCol++;
        rowLength += writer.getLength();
      }
    }

    /* Prepare a FITS header block. */
    Header hdr = new Header();

    /* Add HDU layout metadata. */
    hdr.addValue("XTENSION", "BINTABLE", "binary table extension");
    hdr.addValue("BITPIX", 8, "8-bit bytes");
    hdr.addValue("NAXIS", 2, "2-dimensional table");
    hdr.addValue("NAXIS1", rowLength, "width of table in bytes");
    hdr.addValue("NAXIS2", rowCount, "number of rows in table");
    hdr.addValue("PCOUNT", 0, "size of special data area");
    hdr.addValue("GCOUNT", 1, "one data group");
    hdr.addValue("TFIELDS", nUseCol, "number of columns");

    /* Add EXTNAME record containing table name. */
    String tname = table.getName();
    if (tname != null && tname.trim().length() > 0) {
      FitsConstants.addTrimmedValue(hdr, "EXTNAME", tname, "table name");
    }

    /* Add HDU metadata describing columns. */
    int jcol = 0;
    for (int icol = 0; icol < ncol; icol++) {
      ColumnWriter colwriter = colWriters[icol];
      if (colwriter != null) {
        jcol++;
        String forcol = " for column " + jcol;
        ColumnInfo colinfo = colInfos[icol];

        /* Name. */
        String name = colinfo.getName();
        if (name != null && name.trim().length() > 0) {
          FitsConstants.addTrimmedValue(hdr, "TTYPE" + jcol, name, "label" + forcol);
        }

        /* Format. */
        String form = colwriter.getFormat();
        hdr.addValue("TFORM" + jcol, form, "format" + forcol);

        /* Units. */
        String unit = colinfo.getUnitString();
        if (unit != null && unit.trim().length() > 0) {
          FitsConstants.addTrimmedValue(hdr, "TUNIT" + jcol, unit, "units" + forcol);
        }

        /* Blank. */
        Number bad = colwriter.getBadNumber();
        if (bad != null) {
          hdr.addValue("TNULL" + jcol, bad.longValue(), "blank value" + forcol);
        }

        /* Shape. */
        int[] dims = colwriter.getDims();
        if (dims != null && dims.length > 1) {
          StringBuffer sbuf = new StringBuffer();
          for (int i = 0; i < dims.length; i++) {
            sbuf.append(i == 0 ? '(' : ',');
            sbuf.append(dims[i]);
          }
          sbuf.append(')');
          hdr.addValue("TDIM" + jcol, sbuf.toString(), "dimensions" + forcol);
        }

        /* Scaling. */
        double zero = colwriter.getZero();
        double scale = colwriter.getScale();
        if (zero != 0.0) {
          hdr.addValue("TZERO" + jcol, zero, "base" + forcol);
        }
        if (scale != 1.0) {
          hdr.addValue("TSCALE" + jcol, scale, "factor" + forcol);
        }

        /* Comment (non-standard). */
        String comm = colinfo.getDescription();
        if (comm != null && comm.trim().length() > 0) {
          try {
            hdr.addValue("TCOMM" + jcol, comm, null);
          } catch (HeaderCardException e) {
            // never mind.
          }
        }

        /* UCD (non-standard). */
        String ucd = colinfo.getUCD();
        if (ucd != null && ucd.trim().length() > 0 && ucd.length() < 68) {
          try {
            hdr.addValue("TUCD" + jcol, ucd, null);
          } catch (HeaderCardException e) {
            // never mind.
          }
        }

        /* Utype (non-standard). */
        String utype = colinfo.getUtype();
        if (utype != null && utype.trim().length() > 0 && utype.trim().length() < 68) {
          try {
            hdr.addValue("TUTYP" + jcol, utype, null);
          } catch (HeaderCardException e) {
            // never mind.
          }
        }
      }
    }
    return hdr;
  }