@Test public void testHduFromHeader() throws Exception { Header header = new Header(); header.addValue("SIMPLE", true, ""); header.addValue("BITPIX", 8, ""); header.addValue("NAXIS", 2, ""); header.addValue("NAXIS1", 4, ""); header.addValue("NAXIS3", 4, ""); ImageHDU hdu = (ImageHDU) Fits.makeHDU(header); Assert.assertNotNull(hdu); }
public void copyToHeader(Header h) throws nom.tam.fits.HeaderCardException { String[] srt = wcsKeys.keySet().toArray(new String[0]); java.util.Arrays.sort(srt); for (String key : srt) { Object o = wcsKeys.get(key); if (o instanceof Integer) { h.addValue(key, ((Integer) o).intValue(), "Copied WCS eleemnt"); } else if (o instanceof Double) { h.addValue(key, ((Double) o).doubleValue(), "Copied WCS element"); } else if (o instanceof String) { h.addValue(key, (String) o, "Copied WCS element"); } } }
@Test public void testFitsUndefinedHdu5() throws Exception { Header head = new Header(); head.setXtension("UNKNOWN"); head.setBitpix(BasicHDU.BITPIX_BYTE); head.setNaxes(1); head.addValue("NAXIS1", 1000, null); head.addValue("PCOUNT", 0, null); head.addValue("GCOUNT", 2, null); UndefinedHDU hdu = (UndefinedHDU) FitsFactory.hduFactory(head); byte[] data = (byte[]) hdu.getData().getData(); Assert.assertEquals(2000, data.length); Arrays.fill(data, (byte) 1); BufferedFile buf = new BufferedFile("target/testFitsUndefinedHdu5", "rw"); hdu.write(buf); buf.close(); Arrays.fill(data, (byte) 2); buf = new BufferedFile("target/testFitsUndefinedHdu5", "rw"); hdu.read(buf); data = (byte[]) hdu.getData().getData(); buf.close(); Assert.assertEquals((byte) 1, data[0]); }
protected void customisePrimaryHeader(Header hdr) throws HeaderCardException { hdr.addValue("VOTMETA", true, "Table metadata in VOTable format"); }
/** * Write FITS WCS keywords given key values. Only relatively simple WCSs are handled here. We * assume we are dealing with axes 1 and 2. * * @param h The header to be updated. * @param s A Scaler giving the transformation between standard projection coordinates and * pixel/device coordinates. * @param projString A three character string giving the projection used. Supported projections * are: "Tan", "Sin", "Ait", "Car", "Zea". * @param coordString A string giving the coordinate system used. The first character gives the * general frame. For most frames the remainder of the string gives the equinox of the * coordinate system. E.g., J2000, B1950, Galactic, "E2000", "H2020.10375". */ public void updateHeader( Header h, Scaler s, double[] crval, String projString, String coordString) throws Exception { if (proj.isFixedProjection()) { h.addValue("CRVAL1", toDegrees(proj.getReferencePoint()[0]), "Fixed reference center"); h.addValue("CRVAL2", toDegrees(proj.getReferencePoint()[1]), "Fixed reference center"); } else { h.addValue("CRVAL1", crval[0], "Reference longitude"); h.addValue("CRVAL2", crval[1], "Reference latitude"); } coordString = coordString.toUpperCase(); String[] prefixes = new String[2]; char c = coordString.charAt(0); if (c == 'J' || c == 'I') { h.addValue("RADESYS", "FK5", "Coordinate system"); prefixes[0] = "RA--"; prefixes[1] = "DEC-"; } else if (c == 'B') { h.addValue("RADESYS", "FK4", "Coordinate system"); prefixes[0] = "RA--"; prefixes[1] = "DEC-"; } else { prefixes[0] = c + "LON"; prefixes[1] = c + "LAT"; } if (c != 'G' && c != 'I') { try { double equinox = Double.parseDouble(coordString.substring(1)); h.addValue("EQUINOX", equinox, "Epoch of the equinox"); } catch (Exception e) { // Couldn't parse out the equinox } } if (c == 'I') { h.addValue("EQUINOX", 2000, "ICRS coordinates"); } String upProj = projString.toUpperCase(); h.addValue("CTYPE1", prefixes[0] + "-" + upProj, "Coordinates -- projection"); h.addValue("CTYPE2", prefixes[1] + "-" + upProj, "Coordinates -- projection"); // Note that the scaler transforms from the standard projection // coordinates to the pixel coordinates. // P = P0 + M X where X is the standard coordinates and P is the // pixel coordinates. So the reference pixels are just the constants // in the scaler. // Remember that FITS pixels are offset by 0.5 from 0 offset pixels. h.addValue("CRPIX1", s.x0 + 0.5, "X reference pixel"); h.addValue("CRPIX2", s.y0 + 0.5, "Y reference pixel"); // Remember that the FITS values are of the form // X = M(P-P0) // so we'll need to invert the scaler. // // Do we need a matrix? if (abs(s.a01) < 1.e-14 && abs(s.a10) < 1.e-14) { // No cross terms, so we'll just use CDELTs h.addValue("CDELT1", toDegrees(1 / s.a00), "X scale"); h.addValue("CDELT2", toDegrees(1 / s.a11), "Y scale"); } else { // We have cross terms. It's simplest // just to use the CD matrix and not worry about // normalization. First invert the matrix to get // the transformation in the direction that FITS uses. Scaler rev = s.inverse(); h.addValue("CD1_1", toDegrees(rev.a00), "Matrix element"); h.addValue("CD1_2", toDegrees(rev.a01), "Matrix element"); h.addValue("CD2_1", toDegrees(rev.a10), "Matrix element"); h.addValue("CD2_2", toDegrees(rev.a11), "Matrix element"); } }
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; }