001 package util; 002 003 import java.text.DecimalFormat; 004 import org.eso.fits.*; 005 import java.lang.*; 006 import java.util.*; 007 import java.io.*; 008 009 /** Convert FITS to text file 010 * @version 1.0 2003-06-27 011 * @author John Talbot 012 */ 013 public class ConvertFits { 014 /** Static method for converting FITS to text files. 015 * 016 * @param argv array of arguments i.e. FITS files 017 */ 018 public static void main(String[] argv) { 019 if (argv.length < 1) { 020 System.out.println("Error: must have at least one argument"); 021 System.exit(1); 022 } 023 024 FitsFile file = null; 025 for (int na=0; na<argv.length; na++) { 026 try { 027 file = new FitsFile(argv[na]); 028 } catch (FitsException e) { 029 System.out.println("Error: is not a FITS file >" + argv[na] + "<"); 030 continue; 031 } catch (IOException e) { 032 System.out.println("Error: cannot open file >" + argv[na] + "<"); 033 continue; 034 } 035 036 int noHDU = file.getNoHDUnits(); 037 System.out.println("number of header units = " + noHDU); 038 for (int i=0; i<noHDU; i++) { 039 FitsHDUnit hdu = file.getHDUnit(i); 040 FitsHeader hdr = hdu.getHeader(); 041 int noKw = hdr.getNoKeywords(); 042 int type = hdr.getType(); 043 int size = (int) hdr.getDataSize(); 044 045 if (true) { 046 System.out.println(hdr.getName()); 047 Enumeration enumeration = hdr.getKeywords(); 048 while (enumeration.hasMoreElements()) { 049 FitsKeyword kw = (FitsKeyword) enumeration.nextElement(); 050 System.out.print("#" + kw.getName()); 051 switch (kw.getType()) { 052 case FitsKeyword.COMMENT: 053 System.out.print("(C) " + kw.getComment()); 054 break; 055 case FitsKeyword.STRING: 056 System.out.print("(S)= '" + kw.getString() + "'"); 057 break; 058 case FitsKeyword.BOOLEAN: 059 System.out.print("(B)= " + kw.getBool()); 060 break; 061 case FitsKeyword.INTEGER: 062 System.out.print("(I)= " + kw.getInt()); 063 break; 064 case FitsKeyword.REAL: 065 System.out.print("(R)= " + kw.getReal()); 066 break; 067 case FitsKeyword.DATE: 068 System.out.print("(D)= " + kw.getString()); 069 break; 070 default: 071 } 072 if (0<kw.getComment().length() && (kw.getType()!=FitsKeyword.COMMENT)) { 073 System.out.print(" / " + kw.getComment()); 074 } 075 System.out.println(); 076 } 077 078 if (type == Fits.IMAGE) { 079 FitsMatrix dm = (FitsMatrix) hdu.getData(); 080 int naxis[] = dm.getNaxis(); 081 double crval[] = dm.getCrval(); 082 double crpix[] = dm.getCrpix(); 083 double cdelt[] = dm.getCdelt(); 084 085 086 int nv, off, npix; 087 int nval = dm.getNoValues(); 088 if (0 < nval) { 089 int ncol = naxis[0]; 090 int nrow = nval/ncol; 091 float data[] = new float[ncol]; 092 double val; 093 off = nv = npix = 0 ; 094 DecimalFormat xFormatter = new DecimalFormat("0.0"); 095 DecimalFormat yFormatter = new DecimalFormat("0.000000"); 096 for (int nr=0; nr<nrow; nr++) { 097 try { 098 dm.getFloatValues(off, ncol, data); 099 double wavelength = crval[0]; 100 double deltaWavelength = cdelt[0]; 101 for (int n = 0; n<ncol; n++) { 102 val = data[n]; 103 System.out.print(xFormatter.format(wavelength)); 104 if (val != Double.NaN) { 105 System.out.println(" " + yFormatter.format(val)); 106 } else { 107 System.out.println(" 0.000000" ); 108 } 109 wavelength += deltaWavelength; 110 npix++; 111 } 112 } catch (FitsException e) { 113 } 114 off += ncol; 115 } 116 } 117 } 118 119 } // END IF INTENSITY 120 } 121 } 122 123 System.exit(0); 124 } 125 }