001    /*
002     * @(#)ReadFits.java   $Revision: 1.2 $  $Date: 2001/04/17 15:15:34 $
003     *
004     * Copyright (C) 1999 European Southern Observatory
005     * License:  GNU General Public License version 2 or later
006     */
007    package org.eso.fits;
008    
009    import java.lang.*;
010    import java.util.*;
011    import java.io.*;
012    
013    /** TestFits class provides a static main method for testing the
014     *  FITS class library.  It also shows a typical usage of the
015     *  classes.
016     *
017     *  @version $Id: TestFits.java,v 1.2 2001/04/17 15:15:34 pgrosbol Exp $
018     *  @author  P.Grosbol, DMD/ESO, <pgrosbol@eso.org>
019     */
020    public class TestFits{
021        /** Static method for testing the FITS class library.
022         *
023         *  @param argv   array of arguments i.e.  FITS files
024         */
025        public static void main(String[] argv) {
026            if (argv.length < 1) {
027                System.out.println("Error: must have at least one argument");
028                System.exit(1);
029            }
030    
031            System.out.println("-- Test FITS files --------");
032            FitsFile file = null;
033            for (int na=0; na<argv.length; na++) {
034                try {
035                    file = new FitsFile(argv[na]);
036                } catch (FitsException e) {
037                    System.out.println("Error: is not a FITS file >"
038                                       + argv[na] + "<");
039                    continue;
040                } catch (IOException e) {
041                    System.out.println("Error: cannot open file >"
042                                       + argv[na] + "<");
043                    continue;
044                }
045    
046                int noHDU = file.getNoHDUnits();
047                System.out.println("FITS file has " + noHDU + " HDUnits");
048    
049                for (int i=0; i<noHDU; i++) {
050                    FitsHDUnit hdu = file.getHDUnit(i);
051                    FitsHeader hdr = hdu.getHeader();
052                    int noKw = hdr.getNoKeywords();
053                    int type = hdr.getType();
054                    int size = (int) hdr.getDataSize();
055                    System.out.println("  " + i + ": >" + hdr.getName()
056                                       + "< of type >" + Fits.getType(type)
057                                       + "< with " + noKw + " keywords"
058                                       + " and " + size + " bytes of data");
059                    System.out.println("   Keywords:");
060                    Enumeration enumeration = hdr.getKeywords();
061                    while (enumeration.hasMoreElements()) {
062                        FitsKeyword kw = (FitsKeyword) enumeration.nextElement();
063                        System.out.print("     " + kw.getName());
064                        switch (kw.getType()) {
065                        case FitsKeyword.COMMENT:
066                            System.out.print("(C) " + kw.getComment());
067                            break;
068                        case FitsKeyword.STRING:
069                            System.out.print("(S)= '" + kw.getString() + "'");
070                            break;
071                        case FitsKeyword.BOOLEAN:
072                            System.out.print("(B)= " + kw.getBool());
073                            break;
074                        case FitsKeyword.INTEGER:
075                            System.out.print("(I)= " + kw.getInt());
076                            break;
077                        case FitsKeyword.REAL:
078                            System.out.print("(R)= " + kw.getReal());
079                            break;
080                        case FitsKeyword.DATE:
081                            System.out.print("(D)= " + kw.getString());
082                            break;
083                        default:
084                        }
085                        if (0<kw.getComment().length()
086                            && (kw.getType()!=FitsKeyword.COMMENT)) {
087                            System.out.print(" / " + kw.getComment());
088                        }
089                        System.out.println();
090                    }
091    
092                    if (type == Fits.IMAGE) {
093                        System.out.println("\n  Check data matrix "
094                                           + "- compute mean and rms");
095                        FitsMatrix dm = (FitsMatrix) hdu.getData();
096                        int naxis[] = dm.getNaxis();
097                        double crval[] = dm.getCrval();
098                        double crpix[] = dm.getCrpix();
099                        double cdelt[] = dm.getCdelt();
100    
101                        System.out.println("  Dimension of matrix: "
102                                           + naxis.length);
103                        for (int n=0; n<naxis.length; n++)
104                            System.out.println("   Axis " + n + ": " + naxis[n]
105                                               + ",  " + crpix[n] + ",  "
106                                               + crval[n] + ",  " + cdelt[n]);
107                        System.out.println("\n");
108    
109                        int nv, off, npix;
110                        int nval = dm.getNoValues();
111                        if (0<nval) {
112                            int ncol = naxis[0];
113                            int nrow = nval/ncol;
114                            System.out.println(" Npixel,row,col: " + nval
115                                               + ", " + nrow + ", " + ncol);
116                            float data[] = new float[ncol];
117                            double mean, rms, val;
118    
119                            off = nv = npix = 0 ;
120                            mean = rms = 0.0;
121                            long time = System.currentTimeMillis();
122                            for (int nr=0; nr<nrow; nr++) {
123                                try {
124                                    dm.getFloatValues(off, ncol, data);
125                                    double wavelength = crval[0];              // JT:
126                                    double deltaWavelength = cdelt[0];         // JT:
127                                    for (int n = 0; n<ncol; n++) {
128                                        val = data[n];
129                                        System.out.println(wavelength + " " + val);  //JT:
130                                        wavelength += deltaWavelength;               //JT:
131                                        npix++;
132                                        mean += val;
133                                        rms  += val*val;
134                                    }
135                                } catch (FitsException e) {
136                                }
137    
138                                off += ncol;
139                            }
140                            mean = mean/npix;
141                            rms  = rms/npix - mean*mean;
142                            rms = ((0.0<rms) ? Math.sqrt(rms) : 0.0);
143                            float dtime =
144                                (float) (1000.0*(System.currentTimeMillis()-time)/
145                                         ((double) nval));
146                            System.out.println("  Mean: " + (float)mean +
147                                               ", rms: " + (float)rms +
148                                               ", Time: " + dtime
149                                               + " S/Mp, Pixels: " + npix);
150                        }
151                    } else if (type==Fits.BTABLE || type==Fits.ATABLE) {
152                        System.out.println("\n  Check table data - list columns");
153                        FitsTable dm = (FitsTable) hdu.getData();
154                        int nrow = dm.getNoRows();
155                        int ncol = dm.getNoColumns();
156                        FitsColumn col[] = new FitsColumn[ncol];
157                        System.out.println("  Columns: " + ncol
158                                           + ", Rows: " + nrow);
159                        for (int n=0; n<ncol; n++) {
160                            col[n] = dm.getColumn(n);
161                            System.out.print("  " + n + " >"
162                                             + col[n].getLabel() + "<, ");
163                            System.out.print(col[n].getRepeat() + " ");
164                            System.out.print(col[n].getDataType() + ", >");
165                            System.out.print(col[n].getDisplay() + "<, >");
166                            System.out.println(col[n].getUnit() + "<");
167    
168                            if (col[n].getDataType() == 'F'
169                                || col[n].getDataType() == 'E'
170                                || col[n].getDataType() == 'D') {
171                                int npix = 0;
172                                double mean, rms, val;
173                                mean = rms = 0.0;
174                                long time = System.currentTimeMillis();
175                                for (int nr=0; nr<nrow; nr++) {
176                                    val = col[n].getReal(nr);
177                                    if (Double.isNaN(val)) continue;
178                                    npix++;
179                                    mean += val;
180                                    rms  += val*val;
181                                }
182                                float dtime =
183                                    (float) (1000.0*(System.currentTimeMillis()
184                                                     -time)/((double) nrow));
185                                mean = mean/npix;
186                                rms  = rms/npix - mean*mean;
187                                rms = ((0.0<rms) ? Math.sqrt(rms) : 0.0);
188                                System.out.println("      no,mean,rms: " + npix
189                                                   + ", " + (float)mean + ", "
190                                                   + (float)rms + "; "
191                                                   + dtime + " S/Mp");
192                            } else if (col[n].getDataType() == 'I'
193                                       || col[n].getDataType() == 'J'
194                                       || col[n].getDataType() == 'B') {
195                                int npix = 0;
196                                double mean, rms, val;
197                                mean = rms = 0.0;
198                                long time = System.currentTimeMillis();
199                                for (int nr=0; nr<nrow; nr++) {
200                                    val = col[n].getInt(nr);
201                                    if (val == Long.MIN_VALUE) continue;
202                                    npix++;
203                                    mean += val;
204                                    rms  += val*val;
205                                }
206                                float dtime =
207                                    (float) (1000.0*(System.currentTimeMillis()
208                                                     -time)/((double) nrow));
209                                mean = mean/npix;
210                                rms  = rms/npix - mean*mean;
211                                rms = ((0.0<rms) ? Math.sqrt(rms) : 0.0);
212                                System.out.println("      no,mean,rms: " + npix
213                                                   + ", " + (float)mean + ", "
214                                                   + (float)rms + "; "
215                                                   + dtime + " S/Mp");
216                            }
217                        }
218                    }
219                }
220                System.out.println("-- Test finished -----------------");
221            }
222    
223            System.exit(0);
224        }
225    }