I'm trying to open a FITS file with healpix using a C code:
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "chealpix.h"
#include "fitsio.h"
#include <string.h>
#include "fitsio2.h"
int main(int argc, char **argv){
char *coordsys="G";
char *order="RING";
long nside;
long *p_nside=&nside;
nside=512.;
float *map;
map=read_healpix_map("file.fits",p_nside,coordsys,order);
return(0);
}
This code returns a segmentation fault (core dumped)
when using gdb
, I get
Program received signal SIGSEGV, Segmentation fault.
ffgkys (status=0x7fffffffdaf4, comm=<optimized out>, value=0x55555555a004 "G", keyname=<optimized out>,
fptr=<optimized out>) at getkey.c:808
808 value[0] = '\0';
This is the first time I am using healpix and I have no idea what is causing the problem. Has anyone any idea what could be wrong?
Per the documentation of read_healpix_map()
:
ordering
and coordsys
are output arguments - they are pointers to space into which the function will write results. You have passed pointers to string constants in memory that will normally be marked read-only and may not be sufficiently large to receive the result in any case.
nside
is also an output argument, initialising it to 512 serves no purpose. You do not need p_nside
; you can simply pass &nside
It is not the most clear of interfaces; specifically it is not clear whether coordsys
is a string or a single char
. The documentation uses single quotes so you might assume char
but then it does that for ordering
too and that is clearly a string. It is safest to assume coordsys
is a string - it will do no harm.
char coordsys[] = "G"; // This will be overwritten
char order[] = "NESTED" ; // This will be overwritten,
initialise to largest expected string
to ensure sufficient space is allocated
long nside = 0 ;
float* map = read_healpix_map( "file.fits", &nside, coordsys, order ) ;
printf( "nside:%ld, coordsys:%s, order:%s", nside, coordsys, order ) ;
Post Script: Looking at the source code both coordsys
and ordering
are passed to fits_read_key()
, so coordsys
is treated as a string.