CSharpFits: Writing FITS-file results in wrong image

592 views Asked by At

I am currently working on a C#-project to reduce echelle spectra, which are basically 2D 16bit images in FITS format. The end result, for now, should be another FITS-file, but one-dimensional.

For this task I am using the CSharpFits-library (https://github.com/SidWatch/CSharpFITS), a port from a Java-Implementation (https://github.com/nom-tam-fits/nom-tam-fits), where all the documentation is located.

I am still in the learning phase for this library. So far I successfully can read a FITS-file, convert it to an array of array of ints and work with the data. But trying to write a FITS-file gives me one particular problem which I cannot get my head around. The following code, as a toy example, produces the result as seen in the picture.

private void SaveFits()
{
    int[][] array = new int[10][];
    int delta = 65536 / 100;
    for (int i = 0; i < array.Length; i++)
    {
        array[i] = new int[10];
        for (int j = 0; j < array[i].Length; j++)
        {
            array[i][j] = (i + 1) * (j + 1) * delta;
         }
    }
    nom.tam.fits.Fits fits = new nom.tam.fits.Fits();
    nom.tam.fits.BasicHDU hdu = nom.tam.fits.Fits.MakeHDU(array);
    hdu.AddValue("BITPIX", 16, null);  // set bit depth of 16 bit
    hdu.AddValue("NAXIS", 2, null);    // 2D-image
    fits.AddHDU(hdu);                  // Debugging here shows correct HDU-data, i.e. gradient from top left to bottom right
    nom.tam.util.BufferedFile file = new nom.tam.util.BufferedFile(@"D:\test.fits", System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
    fits.Write(file);  
    file.Flush();
    file.Close();
}

pixel-representation of 10-by-10-pixel FITS-file

As one can see, the code should produce a 16 bit image with a greyscale gradient from top left to bottom right. After creating the HDU, console output of its data array reads

655   1310  1965  2620  3275  3930  4585  5240  5895  6550
1310  2620  3930  5240  6550  7860  9170 10480 11790 13100
1965  3930  5895  7860  9825 11790 13755 15720 17685 19650
2620  5240  7860 10480 13100 15720 18340 20960 23580 26200
3275  6550  9825 13100 16375 19650 22925 26200 29475 32750
3930  7860 11790 15720 19650 23580 27510 31440 35370 39300
4585  9170 13755 18340 22925 27510 32095 36680 41265 45850
5240 10480 15720 20960 26200 31440 36680 41920 47160 52400
5895 11790 17685 23580 29475 35370 41265 47160 53055 58950
6550 13100 19650 26200 32750 39300 45850 52400 58950 65500

Yet in the final image (viewed in PixInsight, but any other program that can display FITS-images shows the same) every other column are only pixels with value 0, and every other row the first row is repeated.

So far I have tried to actually create the HDU as ImageHDU, to no effect. I also checked the HDU-header-fields of a correctly-read FITS file, and tried to set them manually in mine (with hdu.AddValue), but nothing has worked so far. Several examples of writing FITS-files with CSharpFITS can be found here (https://csharp.hotexamples.com/examples/nom.tam.fits/Fits/Write/php-fits-write-method-examples.html), but there nothing else than what I have done is used.

Suggestions are greatly appreciated. Thank you!

1

There are 1 answers

0
kinarf On BEST ANSWER

According to Iguananauts comment, changing the int array to a short array gives the expected result.