关于我尝试读取.FIT文件时的异常

时间:2018-07-17 11:10:40

标签: java fits

我有一个包含.FIT文件的文件夹,并且我有以下代码可以读取它们:

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();
    }

}

我正在得到这个输出:

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)

我正在使用最新的库依赖关系,可以使用GIMP打开FIT文件。

    <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>

我正在尝试读取包含.FIT文件的文件夹,并训练使用deeplearning4j训练模型来识别这些文件的内容,但是由于异常,我无法读取任何文件。我做错了什么?

编辑。这是正确的代码:

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();
    }

}

1 个答案:

答案 0 :(得分:2)

tiler.getCompleteImage()返回一个二维short数组,您尝试将其强制转换为一个二维float数组。 在Java中是不可能的。

参见下文:

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

java.lang.ClassCastException:[[S不能转换为[[[[F