Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
FITS.java
1 package ij.plugin;
2 import java.awt.*;
3 import java.io.*;
4 import ij.*;
5 import ij.io.*;
6 import ij.process.*;
7 import ij.measure.*;
8 
14 public class FITS extends ImagePlus implements PlugIn {
15 
16  public void run(String path) {
17  File pf = new File(path);
18  String directory = pf.getName();
19  String fileName = pf.getParent();
20  IJ.showStatus("Opening: " + directory + fileName);
21  FitsDecoder fd = new FitsDecoder(directory, fileName);
22  FileInfo fi = null;
23  try {fi = fd.getInfo();}
24  catch (IOException e) {}
25  if (fi!=null && fi.width>0 && fi.height>0 && fi.offset>0) {
26  FileOpener fo = new FileOpener(fi);
27  ImagePlus imp = fo.open(false);
28  ImageProcessor ip = imp.getProcessor();
29  ip.flipVertical(); // origin is at bottom left corner
30  setProcessor(fileName, ip);
31  Calibration cal = imp.getCalibration();
32  if (fi.fileType==FileInfo.GRAY16_SIGNED && fd.bscale==1.0 && fd.bzero==32768.0)
33  cal.setFunction(Calibration.NONE, null, "Gray Value");
34  setCalibration(cal);
35  setProperty("Info", fd.getHeaderInfo());
36  setFileInfo(fi); // needed for File->Revert
37  if (path.equals("")) show();
38  } else
39  IJ.error("This does not appear to be a FITS file.");
40  IJ.showStatus("");
41  }
42 
43 }
44 
45 class FitsDecoder {
46  private String directory, fileName;
47  private DataInputStream f;
48  private StringBuffer info = new StringBuffer(512);
49  double bscale, bzero;
50 
51  public FitsDecoder(String directory, String fileName) {
52  this.directory = directory;
53  this.fileName = fileName;
54  }
55 
56  FileInfo getInfo() throws IOException {
57  FileInfo fi = new FileInfo();
58  fi.fileFormat = FileInfo.FITS;
59  fi.fileName = fileName;
60  fi.directory = directory;
61  fi.width = 0;
62  fi.height = 0;
63  fi.offset = 0;
64 
65  f = new DataInputStream(new FileInputStream(directory + fileName));
66  String line = getString(80);
67  info.append(line+"\n");
68  if (!line.startsWith("SIMPLE"))
69  {f.close(); return null;}
70  int count = 1;
71  while ( true )
72  {
73  count++;
74  line = getString(80);
75  info.append(line+"\n");
76 
77  // Cut the key/value pair
78  int index = line.indexOf ( "=" );
79 
80  // Strip out comments
81  int commentIndex = line.indexOf ( "/", index );
82  if ( commentIndex < 0 )
83  {
84  commentIndex = line.length ();
85  }
86 
87  // Split that values
88  String key;
89  String value;
90  if ( index >= 0 )
91  {
92  key = line.substring ( 0, index ).trim ();
93  value = line.substring ( index + 1, commentIndex ).trim ();
94  }
95  else
96  {
97  key = line.trim ();
98  value = "";
99  }
100 
101  // Time to stop ?
102  if (key.equals ("END") ) break;
103 
104  // Look for interesting information
105  if (key.equals("BITPIX")) {
106  int bitsPerPixel = Integer.parseInt ( value );
107  if (bitsPerPixel==8)
108  fi.fileType = FileInfo.GRAY8;
109  else if (bitsPerPixel==16)
110  fi.fileType = FileInfo.GRAY16_SIGNED;
111  else if (bitsPerPixel==32)
112  fi.fileType = FileInfo.GRAY32_INT;
113  else if (bitsPerPixel==-32)
114  fi.fileType = FileInfo.GRAY32_FLOAT;
115  else {
116  IJ.error("BITPIX must be 8, 16, 32 or -32 (float).");
117  f.close();
118  return null;
119  }
120  } else if (key.equals("NAXIS1"))
121  fi.width = Integer.parseInt ( value );
122  else if (key.equals("NAXIS2"))
123  fi.height = Integer.parseInt( value );
124  else if (key.equals("NAXIS3")) //for multi-frame fits
125  fi.nImages = Integer.parseInt ( value );
126  else if (key.equals("BSCALE"))
127  bscale = parseDouble ( value );
128  else if (key.equals("BZERO"))
129  bzero = parseDouble ( value );
130 
131  if (count>360 && fi.width==0)
132  {f.close(); return null;}
133  }
134 
135  f.close();
136  fi.offset = 2880+2880*(((count*80)-1)/2880);
137  return fi;
138  }
139 
140  String getString(int length) throws IOException {
141  byte[] b = new byte[length];
142  f.read(b);
143  return new String(b);
144  }
145 
146  int getInteger(String s) {
147  s = s.substring(10, 30);
148  s = s.trim();
149  return Integer.parseInt(s);
150  }
151 
152  double parseDouble(String s) throws NumberFormatException {
153  Double d = new Double(s);
154  return d.doubleValue();
155  }
156 
157  String getHeaderInfo() {
158  return new String(info);
159  }
160 
161 }