001    /* @(#)CreateFits.java   $Revision: 1.1 $    $Date: 2002/05/06 12:44:05 $
002     *
003     * Copyright (C) 2002 European Southern Observatory 
004     * License:  GNU General Public License version 2 or later
005     */
006    package org.eso.fits;
007    
008    import java.lang.*;
009    import java.util.*;
010    import java.io.*;
011    
012    /** CreateFits class provides a static main method to test creation
013     *  and writing a FITS from scratch.
014     *
015     *  @version $Revision: 1.1 $ $Date: 2002/05/06 12:44:05 $
016     *  @author  P.Grosbol, DMD/ESO, <pgrosbol@eso.org>
017     */
018    public class CreateFits{
019    
020        public static final int ROWS = 473;
021    
022        /** Static method for testing the FITS class library.
023         *
024         *  @param argv   name of FITS file to create
025         */
026        public static void main(String[] argv) {
027            if (argv.length < 1) {
028                System.err.println("Usage: java org.eso.fits.CreateFits file");
029                System.exit(1);
030            }
031    
032            System.out.println("Start CreateFits");
033    
034            // First create a data matrix
035    
036            int[] naxis = new int[2];
037            naxis[0] = 251; naxis[1] = 247;
038            int nax = naxis[0] * naxis[1];
039            float[] data = new float[nax];
040            for (int n=0; n<nax; n++) {
041                data[n] = n;
042            }
043    
044            // Create a FITS data matrix
045    
046            FitsHDUnit hdu0 = null;
047            FitsHDUnit hdu1 = null;
048            FitsHDUnit hdu2 = null;
049    
050            try {
051                FitsMatrix mtx0 = new FitsMatrix(Fits.FLOAT, naxis);
052                mtx0.setFloatValues(0, data);
053                double[] cr = new double[2];
054                cr[0] = 234.02; cr[1] = -12.1;
055                mtx0.setCrpix(cr);
056                cr[0] = -331.3; cr[1] = 721.3;
057                mtx0.setCrval(cr);
058                cr[0] = 0.214; cr[1] = 0.331;
059                mtx0.setCdelt(cr);
060    
061                // Use that to make a HDU
062    
063                FitsHeader hdr0 = mtx0.getHeader();
064                hdr0.addKeyword(new FitsKeyword("", ""));
065                hdr0.addKeyword(new FitsKeyword("DATE", new Date(),
066                                                "Date of writing"));
067                hdu0 = new FitsHDUnit(hdr0, mtx0);
068    
069                // Then we try to make a small table extension in the same way
070    
071                FitsColumn col0 = new FitsColumn(Fits.INT,    "1I", "No",  ROWS);
072                FitsColumn col1 = new FitsColumn(Fits.DOUBLE, "1D", "RA",  ROWS);
073                FitsColumn col2 = new FitsColumn(Fits.DOUBLE, "1D", "Dec", ROWS);
074                FitsColumn col3 = new FitsColumn(Fits.FLOAT,  "1E", "Mag", ROWS);
075    /*
076            for (int n=0; n<ROWS; n++) {
077                col0.setInt(n, n);
078                col1.setReal(n, 0.02*n);
079                col2.setReal(n, 0.03*n);
080                col3.setReal(n, 0.5*n);
081            }
082    
083                FitsTable tab1 = new FitsTable(Fits.BYTE, null);
084                tab1.addColumn(col0);
085                tab1.addColumn(col1);
086                tab1.addColumn(col2);
087                tab1.addColumn(col3);
088    
089                FitsHeader hdr1 = tab1.getHeader();
090                hdu1 = new FitsHDUnit(hdr1, tab1);
091    */
092                // And finally a rather small image extension
093    
094                naxis = new int[3];
095                naxis[0] = 64; naxis[1] = 64; naxis[2] = 3;
096                nax = naxis[0] * naxis[1] * naxis[2];
097                short[] ndata = new short[nax];
098                for (int n=0; n<nax; n++) {
099                    ndata[n] = (short) n;
100                }
101    
102                FitsMatrix mtx2 = new FitsMatrix(Fits.SHORT, naxis);
103                mtx2.setShortValues(0, ndata);
104    
105                FitsHeader hdr2 = mtx2.getHeader();
106                hdr2.setExtension(Fits.IMAGE);
107                hdr2.addKeyword(new FitsKeyword("", ""));
108                hdr2.addKeyword(new FitsKeyword("EXTNAME", "TEST",
109                                                "Extension name"));
110                hdr2.addKeyword(new FitsKeyword("DATE", new Date(),
111                                                "Date of writing"));
112                hdu2 = new FitsHDUnit(hdr2, mtx2);
113            } catch (FitsException e) {
114                System.err.println("Error: cannot create HDU0;" + e);
115                System.exit(1);
116            }
117    
118            // All it all together in a file
119    
120            FitsFile file = new FitsFile();
121            file.addHDUnit(hdu0);
122    //      file.addHDUnit(hdu1);
123            file.addHDUnit(hdu2);
124    
125            // And write it out to a disk file
126            
127            int noHDU = file.getNoHDUnits();
128            System.out.println("FITS file has " + noHDU + " HDUnits");
129    
130            try {
131                file.writeFile(argv[0]);
132            } catch (FitsException e) {
133                System.err.println("Error: FITS problem in writing >"
134                                   + argv[0] + "<");
135                System.exit(-1);
136            } catch (IOException e) {
137                System.err.println("Error: cannot write file >" + argv[0] + "<");
138                System.exit(-1);
139            }
140            System.out.println("Finish CreateFits");
141    
142            System.exit(0);
143        }
144    }