Search code examples
javafits

About an exception when i try to read a .FIT file


i have a folder with .FIT files and i have this code to read them:

private static void readFitFile() {

    try {
        List<File> filesdebuglder = Files.walk(Paths.get(LOCAL_EXPANDED_DATA_PATH))
                                         .filter(Files::isRegularFile)
                                         .map(Path::toFile)
                                         .collect(Collectors.toList());
        System.out.println("There are " + filesdebuglder.size() + " .FIT files in folder " + LOCAL_EXPANDED_DATA_PATH);

        for (File afile : filesdebuglder) {
            System.out.println("Doing something cool with file " + afile.getName() + " ...");
            Fits fitsFile = new Fits(afile);
            ImageHDU imageHDU = (ImageHDU) fitsFile.readHDU();
            StandardImageTiler tiler = imageHDU.getTiler();
            // The exception happens with getCompleteImage() method
            float[][][][] tmp = (float[][][][]) tiler.getCompleteImage();
            System.out.println("tmp is " + tmp);
            float[][] imgData = tmp[0][0];
            System.out.println("imgData is " + imgData );


        }

    } catch (FitsException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch(ClassCastException e) {
        e.printStackTrace();
    }

}

And i am getting this output :

There are 409 .FIT files in folder resources/wetransfer-39ab61
Doing something cool with file RN20130622_1x1_SIN FILTRO_000102206.REDUCED.FIT ...
java.lang.ClassCastException: [[S cannot be cast to [[[[F
at com.aironman.deeplearning4j.TrainFITSImageNetVG16.readFitFile(TrainFITSImageNetVG16.java:86)
at com.aironman.deeplearning4j.TrainFITSImageNetVG16.main(TrainFITSImageNetVG16.java:68)

I am using the latest library dependency and i can open the FIT file using GIMP.

    <dependency>
        <groupId>gov.nasa.gsfc.heasarc</groupId>
        <artifactId>nom-tam-fits</artifactId>
        <version>${nom-tam-fits.version}</version>
    </dependency>

<properties>
    <nom-tam-fits.version>1.15.2</nom-tam-fits.version>
</properties>

I am trying to read the folder with .FIT files and train to use deeplearning4j to train a model to recognize the content of these files, but i can not read any file because of the exception. What do i do wrong?

EDIT. This is the correct code:

private static void readFitFile() {

    try {
        List<File> filesdebuglder = Files.walk(Paths.get(LOCAL_EXPANDED_DATA_PATH))
                                         .filter(Files::isRegularFile)
                                         // .filter(line -> line.getName(0).toString().contains(".FIT"))
                                         .map(Path::toFile)
                                         .collect(Collectors.toList());
        System.out.println("There are " + filesdebuglder.size() + " .FIT files in folder " + LOCAL_EXPANDED_DATA_PATH);
        int count = 1;
        for (File afile : filesdebuglder) {
            System.out.println("Doing something cool with file " + afile.getName() + " ...");
            Fits fitsFile = new Fits(afile);
            ImageHDU imageHDU = (ImageHDU) fitsFile.readHDU();
            StandardImageTiler tiler = imageHDU.getTiler();
            short[][] tmp = (short[][] ) tiler.getCompleteImage();
            System.out.println("tmp is " + tmp);
            short imgData = tmp[0][0];
            System.out.println("imgData is " + imgData );

            count ++;
            System.out.println("Done with the file " + afile.getName() + " ... " + count);
            fitsFile.close();

        }

    } catch (FitsException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch(ClassCastException e) {
        e.printStackTrace();
    }

}

Solution

  • The tiler.getCompleteImage() returns an 2-dimensional short array , and you try to cast it to a 4-dimensional float array. This is not possible in java.

    See below:

    public static void main(String[] args) {
        Object s = new short[][]{};
        float[][][][] f = new float[][][][]{};
        f=(float[][][][])s;
    }
    

    java.lang.ClassCastException: [[S cannot be cast to [[[[F