Thursday, 24 June 2010

my code requires the following tools. eclipse/myeclipse builder and java5 or greater version.
code contains following four files 1)FBeans, 2)FITSInterfaceAdapter, 3)FitsTest and 4)ObjectReturn
copy all these files in eclipse source(src) folder.
required jars fits.jar,ivoafits-src-0.3.jar,junit-4.7.jar.

1) FBeans.java bean class

public class FBeans {
protected int noOfAxes; //1
protected int axes1; //2
protected int axes2; //3
protected int BITPIX; //4
protected String TIME_BEG; //5
protected String TIME_END; //6
protected String DATE_OBS; //7
protected double result[][]; //8
public int getNoOfAxes() {
return noOfAxes;
}
public void setNoOfAxes(int noOfAxes) {
this.noOfAxes = noOfAxes;
}
public int getAxes1() {
return axes1;
}
public void setAxes1(int axes1) {
this.axes1 = axes1;
}
public int getAxes2() {
return axes2;
}
public void setAxes2(int axes2) {
this.axes2 = axes2;
}
public int getBITPIX() {
return BITPIX;
}
public void setBITPIX(int bitpix) {
BITPIX = bitpix;
}
public String getTIME_BEG() {
return TIME_BEG;
}
public void setTIME_BEG(String time_beg) {
TIME_BEG = time_beg;
}
public String getTIME_END() {
return TIME_END;
}
public void setTIME_END(String time_end) {
TIME_END = time_end;
}
public String getDATE_OBS() {
return DATE_OBS;
}
public void setDATE_OBS(String date_obs) {
DATE_OBS = date_obs;
}
public double[][] getResult() {
return result;
}
public void setResult(double[][] result) {
this.result = result;
}


}

2) FITSInterfaceAdapter.java



import java.io.File;
import java.io.FilenameFilter;
import java.util.HashMap;
import java.util.Map;



import nom.tam.fits.BasicHDU;
import nom.tam.fits.Data;
import nom.tam.fits.Fits;
import nom.tam.fits.FitsException;
import nom.tam.fits.Header;
import nom.tam.fits.ImageHDU;

public class FITSInterfaceAdapter
{

public short[][] getPlotDataFromFITS(String f)
{
try
{
System.out.println("Request received");
Fits fitsFile = new Fits("D://"+f);
BasicHDU[] bhdus = fitsFile.read();
if(bhdus!=null)
{
for(int index=0;index {
BasicHDU hdu = bhdus[index];
System.out.println(index+" "+hdu.getClass());
if(hdu.getClass() == nom.tam.fits.ImageHDU.class)
{
ImageHDU imhdu = (ImageHDU)hdu;
imhdu.info();
Data data = imhdu.getData();
int leng=(int)data.getSize();
short obj[][] = (short[][])data.getData();
System.out.println("Returning response");
return obj;
/**
for(int len=0;len<512;len++)
{
for(int IND=0;IND<512;IND++)
{
System.out.print(obj[len][IND]+" ");
}
System.out.println("\n");
}
*/
}
/**
Header header = hdu.getHeader();
int noCards = header.getNumberOfCards();
for(int cards=0;cards {
System.out.println(header.getCard(cards));
}
System.out.println("= = == == = == = == ");
*/
}
}
} catch (FitsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

public String[] doFileFilterListing(String nctest, String NTL_)
{

System.out.println("Filter file listing...");
System.out.println("----------------------");

// final String fileFilter = NTL_;

String dirname = "D:/FitsFiles";
File dir = new File(dirname);
FilenameFilter filter = null;

filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
int dd = 13;
String mm = "08";
int yyyy = 2009;
System.out.println("rosat");
return name.startsWith("rosat_");
}
};
System.out.println("end of the filter accept fun");
String[] children = dir.list(filter);
return children;
//printFiles(children, dirName);

}
}

3) FitsTest.java

import java.util.StringTokenizer;

import nom.tam.fits.BasicHDU;
import nom.tam.fits.Data;
import nom.tam.fits.Fits;
import nom.tam.fits.Header;
import nom.tam.fits.ImageData;
import nom.tam.fits.ImageHDU;
import nom.tam.image.ImageTiler;
import nom.tam.util.Cursor;


public class FitsTest
{
public FBeans readFits() throws Exception //public static void main(String[] args) throws Exception
{
Fits fitsFile = new Fits("sp10.fit"); //rosat_pspc_rdf2_3_bk1.fits // Enter fits file name here
BasicHDU[] bhdus = fitsFile.read();
FBeans fb=new FBeans();

if(bhdus!=null)
{


for(int index=0;index {
BasicHDU hdu = bhdus[index];
//System.out.println(index+" "+hdu.getClass());
if(hdu.getClass() == nom.tam.fits.ImageHDU.class)
{
ImageHDU imhdu = (ImageHDU)hdu;
//imhdu.info();


Data data = imhdu.getData();
int leng=(int)data.getSize();
System.out.println("Length="+leng);
System.out.println("Data="+data);
int[] axes = imhdu.getAxes();
double result[][]=new double[axes[0]][axes[1]];

switch(imhdu.getBitPix())
{
case 8: short obj[][] = (short[][])data.getData();
//double obj1[][]=(double[][])data.getData();
for(int len=0;len {
for(int IND=0;IND {
result[len][IND]=((double)((short[][])obj)[len][IND]);
System.out.print(" "+((double)((short[][])obj)[len][IND]));
//System.out.print(obj[len][IND]+" ");
}
System.out.println("\n");
}
break;
case 16: short obj1[][] = (short[][])data.getData();
//double obj1[][]=(double[][])data.getData();
for(int len=0;len {
for(int IND=0;IND {

//System.out.print(" "+((double)((short[][])obj1)[len][IND]));
result[len][IND]=((double)((short[][])obj1)[len][IND]);
fb.setResult( result);
//System.out.print(" "+result[len][IND]);
//System.out.print(obj[len][IND]+" ");

}
System.out.println("\n");
}


break;
case 32: int obj2[][] = (int[][])data.getData();
//double obj1[][]=(double[][])data.getData();
for(int len=0;len {
for(int IND=0;IND {
result[len][IND]=((double)((int[][])obj2)[len][IND]);
System.out.print(" "+((double)((int[][])obj2)[len][IND]));
//System.out.print(obj[len][IND]+" ");
}
System.out.println("\n");
}
break;
case -32: float obj3[][] = (float[][])data.getData();
//double obj1[][]=(double[][])data.getData();
for(int len=0;len {
for(int IND=0;IND {
result[len][IND]=((double)((float[][])obj3)[len][IND]);
System.out.print(" "+((double)((float[][])obj3)[len][IND]));
//System.out.print(obj[len][IND]+" ");
}
System.out.println("\n");
}
break;
case -64: double obj4[][] = (double[][])data.getData();
//double obj1[][]=(double[][])data.getData();
for(int len=0;len {
for(int IND=0;IND {
result[len][IND]=((double)((double[][])obj4)[len][IND]);
System.out.print(" "+((double)((double[][])obj4)[len][IND]));
//System.out.print(obj[len][IND]+" ");
}
System.out.println("\n");
}
break;
}//end switch
//imhdu.info();
//System.out.println("========== Details =========");
//System.out.println("imhdu.getBitPix()="+imhdu.getBitPix());
int bpix=(int)imhdu.getBitPix();
// System.out.println("====================////////=============="+bpix);

fb.setBITPIX(bpix); //4
// System.out.println("=======@@@@@@@@@@@@@--------="+fb.getBITPIX());
// System.out.println("imhdu.getOrigin()="+imhdu.getOrigin());

// System.out.println("Card 1="+imhdu.getHeader().getCard(5));
// System.out.println("Length="+leng);
// System.out.println("Data="+data);
// System.out.println("Creation Date="+imhdu.getCreationDate());
// System.out.println("get key="+imhdu.getHeader().getKey(1));
// System.out.println("get String value="+imhdu.getHeader().getStringValue(imhdu.getHeader().getKey(1)));
// System.out.println("get NAXIS int value="+imhdu.getHeader().getIntValue("NAXIS")); //imhdu.getHeader().getKey(2))

fb.setNoOfAxes(imhdu.getHeader().getIntValue("NAXIS")); //1

// System.out.println("axes[1]="+axes[1]);

fb.setAxes1(axes[1]); //2
// System.out.println("axes[0]="+axes[0]);

fb.setAxes2(axes[0]); //3

// System.out.println("get BAXIS1 int value="+imhdu.getHeader().getIntValue("BAXIS1"));
// System.out.println("get TIME-BEG int value="+imhdu.getHeader().getStringValue("TIME-BEG"));

// System.out.println("get DATE-OBS int value="+imhdu.getHeader().getStringValue("DATE-OBS"));

fb.setDATE_OBS(imhdu.getHeader().getStringValue("DATE-OBS")); //7
// System.out.println("======= End =======");
}
String s[]=new String[512];
int k=0;
Header header = hdu.getHeader();
int noCards = header.getNumberOfCards();
for(int cards=0;cards {
//System.out.println(header.getCard(cards));
s[k]=(String)header.getCard(cards);
}
for(k=0;k {
//System.out.println(header.getCard(cards));
//System.out.println(s[k]);
StringTokenizer st = new StringTokenizer(s[k], "="); //TIME-BEG= 01:43:43.359

while(st.hasMoreTokens()){
String s1=st.nextToken();
if(s1.equalsIgnoreCase("TIME-BEG"))
{ s1=st.nextToken();
fb.setTIME_BEG(s1); //5
// System.out.println("TIME-BEG="+s1);
}
if(s1.equalsIgnoreCase("TIME-END"))
{ s1=st.nextToken();
fb.setTIME_END(s1); //6
// System.out.println("TIME-END="+s1);
}
}

}
// System.out.println("= = == == = == = == ");

//return result;
}

}
return fb;
}

}

4) ObjectReturn.java

import java.io.*;
class ObjectReturn extends Object{


public static void main(String[] args) throws Exception
{



FitsTest ft=new FitsTest();

FBeans fbs=ft.readFits();
double rs1[][]=fbs.getResult();

for(int len=0;len {
for(int IND=0;IND {

//System.out.print(" "+((double)((short[][])obj1)[len][IND]));

System.out.print(" "+rs1[len][IND]);
//System.out.print(obj[len][IND]+" ");

}
System.out.println("\n");
}

System.out.println("Bean Class Data Printing");
System.out.println("BITPIX="+fbs.getBITPIX());
System.out.println("No Of Axes="+fbs.getNoOfAxes());
System.out.println("Axes1="+fbs.getAxes1());
System.out.println("Axes2="+fbs.getAxes2());
System.out.println("DATE-OBS="+fbs.getDATE_OBS());
System.out.println("TIME-BEG="+fbs.getTIME_BEG());
System.out.println("TIME-END="+fbs.getTIME_END());

}

}

Copy All these files into eclipse src folder. then run ObjectReturn.java.

*Before you run this Application you need sapmle *.fits files. so download from net.
Ex: like sp1.fit or rosat_pspc_rdf2_3_bk1.fits

2 comments:

  1. i am not able to understand what is this yaar

    ReplyDelete
  2. Thanks for the code. There are some conflicts with the html syntax when you write for loops in the code.

    ReplyDelete