diff -u -r nomv1.04.0/tam/fits/Fits.java nom/tam/fits/Fits.java --- nomv1.04.0/tam/fits/Fits.java 2009-12-24 09:10:30.000000000 +0100 +++ nom/tam/fits/Fits.java 2010-06-22 16:54:43.742940361 +0200 @@ -1044,4 +1044,125 @@ String resul = new String(asc,15,1) ; return resul.concat(new String(asc,0,15)) ; } + + /** Create a copy of the current FITS data and return it. + * @since 2010-01-31 + * @author Richard J. Mathar + */ + public Fits clone() throws CloneNotSupportedException + { + Fits s = new Fits() ; + for( int h = 0 ; h < getNumberOfHDUs() ; h++) + { + try + { + /* try to preserve the order. Alternative: s.addHDU(getHDU(h)) ; + */ + s.insertHDU(getHDU(h),h) ; + } + catch ( Exception e) + { + throw new CloneNotSupportedException(e.toString() ) ; + } + + /* Note that this is not a faithful copy, since the primary header + * will for example be commented with the current date of creation, and + * the comment within header cards may be modified relative to the original + * In that sense, it is mandatory to re-calculate the DATASUM and CHECKSUM keywords + * in the copy if they exist. + */ + try + { + BasicHDU hdu = s.getHDU(h) ; + Header hdr = hdu.getHeader() ; + if ( hdr.findCard("DATASUM") != null) + s.setDatasum(hdu) ; + if ( hdr.findCard("CHECKSUM") != null) + s.setChecksum(hdu) ; + } + catch( Exception e) + { + System.err.println(e.toString() ) ; + } + } + + return s ; + } + + /** Update the DATASUM and CHECKSUM keywords if present. + * The difference to the setDataSum() and setCheckSum() functions is + * that the card files are only updated if they exist. + * @param hdu The HDU to be updated. + * @see FITS checksum convention + * @since 2010-02-01 + * @author Richard J. Mathar + */ + public void updateCDsum(BasicHDU hdu) + { + Header hdr = hdu.getHeader() ; + try + { + if ( hdr.findCard("DATASUM") != null) + setDatasum(hdu) ; + if ( hdr.findCard("CHECKSUM") != null) + setChecksum(hdu) ; + } + catch (Exception e) + { + System.err.println(e.toString() ) ; + } + } + + /** Add or update the DATASUM keyword. + * The implementation parallels the CHECKSUM case, but needs less caution + * because the header card is not part of the data sum to be calculated. + * @param hdr the primary or other header to get the current DATE + * @throws nom.tam.fits.HeaderCardException + * @author Richard J. Mathar + * @see FITS checksum convention + * @since 2010-02-01 + * @author Richard J. Mathar + */ + public static void setDatasum(BasicHDU hdu) + throws nom.tam.fits.HeaderCardException, nom.tam.fits.FitsException, java.io.IOException + { + /* the next line with the delete is needed to avoid some unexpected + * problems with non.tam.fits.Header.checkCard() which otherwise says + * it expected PCOUNT and found DATE. + */ + Header hdr = hdu.getHeader() ; + hdr.deleteKey("DATASUM") ; + + /* Convert the sequence of 2880 byte data cards into a byte array. + */ + ByteArrayOutputStream hduByteImage = new ByteArrayOutputStream() ; + final Data dats = hdu.getData() ; + BufferedDataOutputStream b = new BufferedDataOutputStream(hduByteImage) ; + dats.write(b) ; + /* Ensure that checksum() will have all the data, even those at the end of the stream + */ + b.flush() ; + + /* Write the number to the DATASUM. + */ + final byte[] data = hduByteImage.toByteArray() ; + final long csu = checksum(data) ; + final String doneAt = "as of " + FitsDate.getFitsDateString() ; + hdr.addValue("DATASUM", ""+csu, doneAt ) ; + } + + /** Add or Modify the DATASUM keyword in all headers. + * @throws nom.tam.fits.HeaderCardException + * @throws nom.tam.fits.FitsException + * @author Richard J. Mathar + * @see FITS checksum convention + * @since 2010-02-01 + * @author Richard J. Mathar + */ + public void setDatasum() + throws nom.tam.fits.HeaderCardException, nom.tam.fits.FitsException, java.io.IOException + { + for (int i=0; i< getNumberOfHDUs(); i ++) + setDatasum(getHDU(i)) ; + } } diff -u -r nomv1.04.0/tam/fits/test/BinaryTableTester.java nom/tam/fits/test/BinaryTableTester.java --- nomv1.04.0/tam/fits/test/BinaryTableTester.java 2009-07-27 08:57:02.000000000 +0200 +++ nom/tam/fits/test/BinaryTableTester.java 2010-06-22 16:29:59.677208275 +0200 @@ -9,6 +9,7 @@ import static org.junit.Assert.assertEquals; import junit.framework.JUnit4TestAdapter; import java.lang.reflect.*; +import java.lang.annotation.*; /** This class tests the binary table classes for * the Java FITS library, notably BinaryTableHDU,