0

I am trying to write and fill the image with pixel data to new fits file using cfitsio. I am not sure where I am doing wrong but all of the data are not being written to the fits file. I used binary vi to check out the data and image data is not properly created. I used the second image HST WFPC 2 fits file from NASA website. This is the code that I wrote:

#include <string.h> 
#include <stdio.h> 
#include "fitsio.h" 

int main() { 
    fitsfile *fptr; 
    int status = 0, i;  

    fits_open_file(&fptr, "WFPC2ASSNu5780205bx.fits", READONLY, &status);  
    long naxes[2]; 
    fits_get_img_size(fptr, 3, naxes, &status); 

    fitsfile *ofptr;
    fits_create_file(&ofptr, "o_nasa.fits", &status); 
    fits_copy_header(fptr, ofptr, &status); 

    long fp[2] = {1, 1}; 
    long nelements = naxes[0];

    float arr[nelements]; 
    for (i = 0; i < nelements; i++) { 
        arr[i] = 100;
    }   

    int ii, jj, kk; 
    for (ii = 1; ii <= naxes[0]; ii++) { 
        for (jj = 1; jj <= naxes[1]; jj++) { 
            fits_write_pix(ofptr, TFLOAT, fp, nelements, arr, &status); // this is not working
        }   
    }   
    fits_close_file(ofptr, &status); 
    fits_close_file(fptr, &status); 

    return status;
} 
pseudo
  • 385
  • 2
  • 19

1 Answers1

0

I found out what is wrong with the code. I just posted the answer since there doesn't seem to be a lot of community working on CFITSIO. The code is wrong in the lines in the double for loop with variables ii, jj. I thought fp, the first pixel array to point where the image array starts writing, is used once where to start the whole iterations but instead write_pix uses it throughout the iterations to know where to write. My code is also trying to write/update the 1, 1 pixel and it seems to be NULL. The correct code is:

for (fp[1] = 1; fp[1] <= naxes[0]; fp[1]++) { 
    for (fp[0] = 1; fp[0] <= naxes[1]; fp[0]++) { 
        fits_write_pix(ofptr, TFLOAT, fp, nelements, arr, &status); 
    }
}
pseudo
  • 385
  • 2
  • 19