使用CFITSIO访问C中的大拟合网格的正确方法是什么

时间:2017-11-15 12:57:24

标签: c fits

我有两个适合网格,我正在使用CFITSIO(https://heasarc.gsfc.nasa.gov/fitsio/c)访问。现在,第一个网格是1071(行)x 3(cols)和float数据类型。我可以访问这个并打印每个条目的输出。当我为我的第二个网格(一个1071 x 262144的大网格(1.1 Gb)以及浮点数据类型重复此代码时,我得到一个分段错误。在我继续之前,这是代码:

#FILE Cspec.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "fitsio.h"
#include "load_data.h"
#include "general_functions.h"


#define MAX_ROW 230746
#define N 131072
#define WAVEMIN 450.0
#define WAVEMAX 650.0


void printerror( int status)
{
/*****************************************************/
/* Print out cfitsio error messages and exit program */
/*****************************************************/


if (status)
{
   fits_report_error(stderr, status); /* print error report */

   exit( status );   
}
return;
}



int main()
{   /*----------------------------
    Now load the table
----------------------------*/
printf("\n\n--------------------\nLoading the models..\n--------------------");
fflush(stdout);

// Imports and definitions
fitsfile *fptr;       /* pointer to the FITS file, defined in fitsio.h */
int status, anynull;
long naxes[2], fpixel, nbuffer, npixels, ii;
char filename[]  = "models.fits";     /* name of existing FITS file   */
status = 0;

// Open the file and kick off if its not there
if ( fits_open_file(&fptr, filename, READONLY, &status) )
    printerror( status );

/* read the NAXIS1 and NAXIS2 keyword to get image size */
//if ( fits_read_keys_lng(fptr, "NAXIS", 1, 2, naxes, &nfound, &status) )
//      printerror( status );

// Define the rows and columns of fits image and thus the buffer size
naxes[0] = 1017;
naxes[1] = 262144;
    size_t buffsize = ((size_t) (naxes[1] * naxes[0]) + (naxes[1] * sizeof(float)));
float nullval, buffer[buffsize];
npixels  = naxes[0] * naxes[1];         /* number of pixels in the image */
fpixel   = 1;
nullval  = 0;                /* don't check for null values in the image */

while (npixels > 0)
{
    nbuffer = npixels;
    if (npixels > buffsize)
    nbuffer = buffsize;     /* read as many pixels as will fit in buffer */

    if ( fits_read_img(fptr, TFLOAT, fpixel, nbuffer, &nullval,
      buffer, &anynull, &status) )
    printerror( status );

    for (ii = 0; ii < nbuffer; ii++)  
    {
        // Output the value
        printf("%f\t", buffer[ii]);
    }
    npixels -= nbuffer;    /* increment remaining number of pixels */
    fpixel  += nbuffer;    /* next pixel to be read in image */
}

if ( fits_close_file(fptr, &status) )
    printerror( status );

return 0;
}

我使用编译:

nvcc Cspec2.c general_functions.c load_data.c -I/iraf/iraf/vendor/cfitsio  /iraf/iraf/vendor/cfitsio/libcfitsio.a  -lm -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64

其他c文件(general_functions.c和load_data.c)不相关。最后两个标志的原因来自https://heasarc.gsfc.nasa.gov/fitsio/c/f_user/node13.html,它描述了使用fit文件时的大小限制。有一点是这样的(第4段):

“如果在支持大文件的平台上使用-D_LARGEFILE_SOURCE和-D_FILE_OFFSET_BITS = 64标志编译CFITSIO,则它可以读取和写入包含最多2 ** 31 2880字节FITS记录的FITS文件,或大约大小为6太字节。仍需要每个扩展中的NAXISn和PCOUNT关键字的值在有符号的4字节整数(最大值= 2,147,483,648)的范围内。因此,图像的每个维度(由NAXISn关键字),表的总宽度(NAXIS1关键字),表中的行数(NAXIS2关键字)以及二进制表中的可变长度数组堆的总大小(PCOUNT关键字)必须小于此限制。”

我假设添加这些标志会对此进行排序,因为1071 * 262,144 = 280,756,224,它低于有符号4字节整数的范围(最大值= 2,147,483,648)。这里还有更多信息(https://heasarc.gsfc.nasa.gov/fitsio/c/c_user/node32.html)。我哪里错了?为什么会导致seg故障?此代码适用于较小的网格(1071 * 3)。我假设我必须添加一些额外的编译器标志或更改我的代码中的一些东西?问题在于我使用IRAF发行版的cfitsio吗?

我从解决问题中发现的一件事是我无法输出“buffsize”变量 - 这就是它自己导致的一个seg错误。

我正在运行64位Ubuntu 16.04 LTS并且我正在使用nvcc编译器,因为我计划将一些后续工作卸载到GPU。我还是比较新的C(来自python)。

由于

萨姆

0 个答案:

没有答案