初学JAVA,没想到读取Fits,尤其是在MATLAB中一条命令搞定的显示数据,显得比较麻烦。这里用了两个包,分别完成读取及显示的功能。
读取Fits的头文件以及获取Table的数据: Java Library for access to FITS files
对于读取出来的波长和流量进行绘图: Plot Package,jahuwaldt.plot
在他们类的基础上,参考他们给出的Demo,我稍微封装了一点,以方便以后的调用。
对jfits的封装,因为我主要读取文件只是获取其波长和流量的值,所以自己在那个基础上做了一个类
package org.eso.fits;
import java.io.IOException;
import java.util.ListIterator;
import org.eso.fits.FitsException;
import org.eso.fits.FitsFile;
import org.eso.fits.FitsHDUnit;
import org.eso.fits.FitsHeader;
import org.eso.fits.FitsKeyword;
import org.eso.fits.FitsMatrix;
public class LamostFits {
public LamostFits() {
// TODO Auto-generated constructor stub
}
public FitsFile GetFitsFile(String filePathString) {
FitsFile file = null;
try {
System.out.println("new fits file");
file = new FitsFile(filePathString);
} catch (FitsException e) {
System.out.println("Error: is not a FITS file >" + filePathString
+ "<");
} catch (IOException e) {
System.out.println("Error: cannot open file >" + filePathString
+ "<");
}
return file;
}
public int GetDataLength(FitsFile file) {
FitsHDUnit hdu = file.getHDUnit(0);
FitsMatrix dataMatrix = (FitsMatrix) hdu.getData();
int[] dimSize = dataMatrix.getNaxis();
return dimSize[0];
}
public int GetFlux(FitsFile file, float[] flux) {
FitsHDUnit hdu = file.getHDUnit(0);
FitsMatrix dataMatrix = (FitsMatrix) hdu.getData();
int len = dataMatrix.getNaxis()[0];
try {
dataMatrix.getFloatValues(0, len, flux);
} catch (FitsException e) {
e.printStackTrace();
return -1;
}
return len;
}
public void GetLambda(FitsFile file, float[] lambda) {
FitsHDUnit hdu = file.getHDUnit(0);
FitsHeader hdr = hdu.getHeader();
ListIterator itr = hdr.getKeywords();
double lambda_start = 0;
double lambda_deta = 0;
while (itr.hasNext()) {
FitsKeyword kw = (FitsKeyword) itr.next();
if (kw.getName().equals("CRVAL1")) {
/*
* CRVAL1(R)= 3.5682 / Central wavelength (log10) of first pixel
* CD1_1(R)= 1.0E-4 / Log10 dispersion per pixel
*/
lambda_start = kw.getReal();
} else if (kw.getName().equals("CD1_1")) {
lambda_deta = kw.getReal();
}
}
for (int i = 0; i < lambda.length; i++) {
lambda[i] = (float) Math.pow(10, lambda_start + lambda_deta * i);
}
}
}
而对于数据的显示这块,同样,我想要有matlab那样,直接给出x,y的值,显示其曲线即可,重新定义了一个可以传递x,y参数的函数进行绘图。
package jahuwaldt.plot;
import java.awt.*;
import javax.swing.*;
public class PlotXY extends JFrame {
public static void PlotFits(float[] lambda, float[] flux, String title) {
double[] xArr = new double[lambda.length];
double[] yArr = new double[lambda.length];
for (int i = 0; i < lambda.length; i++) {
xArr[i] = (double) (lambda[i]);
yArr[i] = (double) flux[i];
}
Plot2D aPlot = new SimplePlotXY(xArr, yArr, title, "Wavelength",
"Flux", null, null, null);
PlotPanel panel = new PlotPanel(aPlot);
panel.setBackground(Color.white);
PlotXY window = new PlotXY("SimplePlotXY Plot Window", panel);
window.setSize(1000, 600);
window.setLocation(100, 100);
window.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
window.show();
}
}
具体的调用过程:
package org.eso.fits;
import jahuwaldt.plot.PlotXY;
import java.io.FileNotFoundException;
public class SpectraFits{
public static void main(String[] argv) throws FileNotFoundException, FitsException {
LamostFits lamFits = new LamostFits();
String fitsname = "xxx";
String filenameString = "fitFiles/" + fitsname + ".fits";
FitsFile file = lamFits.GetFitsFile(filenameString);
int len = lamFits.GetDataLength(file);
float[] lambda = new float[len];
lamFits.GetLambda(file, lambda);
float[] flux = new float[len];
lamFits.GetFlux(file, flux);
PlotXY.PlotFits(lambda, flux, fitsname);
}
}
显示结果: