Beispiel #1
0
std::string write_image(std::string pathname, float *array, std::string name, int naxis, long *naxes) {
  // this function writes a float array to a FITS image file
  TRACE_ENTER();
  // create new file
  fitsfile *fptr;
  int status = 0;
  fits_create_file(&fptr, pathname.c_str(), &status);
  if (status != 0) {
    // cannot create the file
    // likely, the file already exists
    // try to open it, delete it and create it anew
    // try to open the file
    status = 0;
    fits_open_file(&fptr, pathname.c_str(), READWRITE, &status);
    if (status != 0) {
      // cannot open the file, for some reason
      return FORMAT_STATUS(status);
    }
    // try to delete the file
    status = 0;
    fits_delete_file(fptr, &status);
    if (status != 0) {
      // cannot delete the file, for some reason
      return FORMAT_STATUS(status);
    }
    // try to create the file
    status = 0;
    fits_create_file(&fptr, pathname.c_str(), &status);
    if (status != 0) {
      // cannot create the file, for some reason
      return FORMAT_STATUS(status);
    }
  }
  // create the primary array image
  fits_create_img(fptr, FLOAT_IMG, naxis, naxes, &status);
  if (status != 0) {
    return FORMAT_STATUS(status);
  }
  // write a keyword
  fits_update_key(fptr, TFLOAT, name.c_str(), array, name.c_str(), &status);
  if (status != 0) {
    return FORMAT_STATUS(status);
  }
  // write the array of floats to the image
  long fpixel = 1;
  long nelements = 1;
  for (int i = 0; i < naxis; i++) {
    nelements = nelements * naxes[i];
  }
  fits_write_img(fptr, TFLOAT, fpixel, nelements, (void*)array, &status);
  if (status != 0) {
    return FORMAT_STATUS(status);
  }
  // close the file
  fits_close_file(fptr, &status);
  if (status != 0) {
    return FORMAT_STATUS(status);
  }
  return "WRITE_OK";
}
void save_2d_image_potential(SimulationData &sim_data, double *potential, const char * fits_file_name) {

	double *save_data;
	save_data = (double*)mkl_malloc(sim_data.get_num_x() * sim_data.get_num_y() * sizeof(double), 64);
	fitsfile *fptr;
	int status = 0;
	long fpixel = 1, naxis = 2, nelements;
	long naxes[2] = {sim_data.get_num_y(), sim_data.get_num_x()};

	for (int i = 0; i < sim_data.get_num_x(); ++i) {
		for (int j = 0; j < sim_data.get_num_y(); ++j) {
			save_data[i * sim_data.get_num_y() + j] = 0;
			for (int k = 0; k < sim_data.get_num_z(); ++k) {
				save_data[i * sim_data.get_num_y() + j] += potential[i * sim_data.get_num_y() * sim_data.get_num_z() + j * sim_data.get_num_z() + k];
			}
		}
	}

	fits_create_file(&fptr, fits_file_name, &status);
	fits_create_img(fptr, DOUBLE_IMG, naxis, naxes, &status);
	nelements = naxes[0] * naxes[1];
	fits_write_img(fptr, TDOUBLE, fpixel, nelements, save_data, &status);
	fits_close_file(fptr, &status);
	fits_report_error(stderr, status);	

	mkl_free(save_data);


}
Beispiel #3
0
int CDfits_open_file( char *filename )
{
  int status;
  int bitpix   =  8;
  int naxis   =  0;
  long *naxes;
  
  status = 0;
  /* Open file */
  fits_create_file( &cfitsptr, filename, &status );
  /* Write primary header */
  bitpix = 8;
  naxis = 0;
  fits_create_img( cfitsptr, bitpix, naxis, naxes, &status );

  /* Add first keywords */
  fits_update_key( cfitsptr, TSTRING, "HDRVER", "1.19", "Header version", &status);
  fits_write_date( cfitsptr, &status);

  new_file = 1;
  
  
  return( status );
  
}
int fitscopy ( char *infile, char *outfile) 
{
	fitsfile *infptr, *outfptr;   /* FITS file pointers defined in fitsio.h */
	int status = 0, ii = 1;       /* status must always be initialized = 0  */

	/* Open the input file */
	if ( !fits_open_file(&infptr, infile, READONLY, &status) )
	{
		/* Create the output file */
		if ( !fits_create_file(&outfptr, outfile, &status) )
		{
			/* Copy every HDU until we get an error */
			while( !fits_movabs_hdu(infptr, ii++, NULL, &status) )
				fits_copy_hdu(infptr, outfptr, 0, &status);
 
			/* Reset status after normal error */
			if (status == END_OF_FILE) status = 0;

			fits_close_file(outfptr,  &status);
		}
		fits_close_file(infptr, &status);
	}

	/* if error occured, print out error message */
	if (status) fits_report_error(stderr, status);
	return(status);
}
Beispiel #5
0
void he_write_healpix_map(float **tmap,int nfields,long nside,char *fname)
{
  fitsfile *fptr;
  int ii,status=0;
  char *ttype[]={"T","Q","U"};
  char *tform[]={"1E","1E","1E"};
  char *tunit[]={"mK","mK","mK"};

  if((nfields!=1)&&(nfields!=3)) {
    fprintf(stderr,"CRIME: nfields must be 1 or 3\n");
    exit(1);
  }

  fits_create_file(&fptr,fname,&status);
  fits_create_tbl(fptr,BINARY_TBL,0,nfields,ttype,tform,
		  tunit,"BINTABLE",&status);
  fits_write_key(fptr,TSTRING,"PIXTYPE","HEALPIX","HEALPIX Pixelisation",
		 &status);

  fits_write_key(fptr,TSTRING,"ORDERING","RING",
		 "Pixel ordering scheme, either RING or NESTED",&status);
  fits_write_key(fptr,TLONG,"NSIDE",&nside,
		 "Resolution parameter for HEALPIX",&status);
  fits_write_key(fptr,TSTRING,"COORDSYS","G",
		 "Pixelisation coordinate system",&status);
  fits_write_comment(fptr,
		     "G = Galactic, E = ecliptic, C = celestial = equatorial",
		     &status);
  for(ii=0;ii<nfields;ii++) {
    fits_write_col(fptr,TFLOAT,ii+1,1,1,nside2npix(nside),tmap[ii],&status);
  }
  fits_close_file(fptr, &status);
}
/** Opens a FITS file.
  * \param[in] fileName The name of the file to open.
  * \param[in] openMode Mask defining how the file should be opened; bits are 
  *            NDFileModeRead, NDFileModeWrite, NDFileModeAppend, NDFileModeMultiple
  * \param[in] pArray A pointer to an NDArray; this is used to determine the array and attribute properties.
  */
asynStatus 
NDFileFITS::openFile(const char *fileName, NDFileOpenMode_t openMode, NDArray *pArray) {
  static const char *functionName = "openFile";
  
  // We don't support reading yet
  if (openMode & NDFileModeRead) return(asynError);

  // We don't support opening an existing file for appending yet
  if (openMode & NDFileModeAppend) return(asynError);
  
  // At present only 16 bits unsigned images are valid.
  if (pArray->dataType != NDUInt16) return(asynError);

  //>>>>>>>>>>>>>>if (FITSType != 0) return(asynError);

  fitsStatus = 0;
  if(fits_create_file(&fitsFilePtr, fileName, &fitsStatus)) {// create new FITS file
    // get the error description
    fits_get_errstatus(fitsStatus, fits_status_str);
    asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
      "%s::%s error opening file %s error=%s\n",
      driverName, functionName, fileName, fits_status_str);
    fits_clear_errmsg();
    return (asynError);
  }

  return(asynSuccess);
}
void save_data_in_fits_file(const char* file_name, double* data,
                            size_t num_of_elements)
{
    fitsfile* file;
    int status = 0;
    char* ttype[] = { "SAMPLE" };
    char* tform[] = { "1D" };
    char* tunit[] = { "" };

    if (fits_create_file(&file, file_name, &status) != 0)
        goto error;

    if (fits_create_tbl(file, BINARY_TBL, 0, 1, ttype, tform, tunit,
                        "DECOMPR", &status) != 0)
        goto error;

    if (fits_write_col(file, TDOUBLE, 1, 1, 1, num_of_elements, data,
                       &status) != 0)
        goto error;

    fits_close_file(file, &status);
    return;

error:
    fits_report_error(stderr, status);
    if (file != NULL)
        fits_close_file(file, &status);
    exit(1);
}
Beispiel #8
0
int main(int argc, char *argv[])
{
    fitsfile *infptr, *outfptr;   /* FITS file pointers defined in fitsio.h */
    int status = 0;       /* status must always be initialized = 0  */

    if (argc != 3)
    {
 printf("Usage:  fitscopy inputfile outputfile\n");
 printf("\n");
 printf("Copy an input file to an output file, optionally filtering\n");
 printf("the file in the process.  This seemingly simple program can\n");
 printf("apply powerful filters which transform the input file as\n");
 printf("it is being copied.  Filters may be used to extract a\n");
 printf("subimage from a larger image, select rows from a table,\n");
 printf("filter a table with a GTI time extension or a SAO region file,\n");
 printf("create or delete columns in a table, create an image by\n");
 printf("binning (histogramming) 2 table columns, and convert IRAF\n");
 printf("format *.imh or raw binary data files into FITS images.\n");
 printf("See the CFITSIO User's Guide for a complete description of\n");
 printf("the Extended File Name filtering syntax.\n");
 printf("\n");
 printf("Examples:\n");
 printf("\n");
 printf("fitscopy in.fit out.fit                   (simple file copy)\n");
 printf("fitscopy - -                              (stdin to stdout)\n");
 printf("fitscopy in.fit[11:50,21:60] out.fit      (copy a subimage)\n");
 printf("fitscopy iniraf.imh out.fit               (IRAF image to FITS)\n");
 printf("fitscopy in.dat[i512,512] out.fit         (raw array to FITS)\n");
 printf("fitscopy in.fit[events][pi>35] out.fit    (copy rows with pi>35)\n");
 printf("fitscopy in.fit[events][bin X,Y] out.fit  (bin an image) \n");
 printf("fitscopy in.fit[events][col x=.9*y] out.fit        (new x column)\n");
 printf("fitscopy in.fit[events][gtifilter()] out.fit       (time filter)\n");
 printf("fitscopy in.fit[2][regfilter(\"pow.reg\")] out.fit (spatial filter)\n");
 printf("\n");
 printf("Note that it may be necessary to enclose the input file name\n");
 printf("in single quote characters on the Unix command line.\n");
      return(0);
    }
    /* Open the input file */
    if ( !fits_open_file(&infptr, argv[1], READONLY, &status) )
    {
      /* Create the output file */
      if ( !fits_create_file(&outfptr, argv[2], &status) )
      {
 
        /* copy the previous, current, and following HDUs */
        fits_copy_file(infptr, outfptr, 1, 1, 1, &status);

        fits_close_file(outfptr,  &status);
      }
      fits_close_file(infptr, &status);
    }

    /* if error occured, print out error message */
    if (status) fits_report_error(stderr, status);
    return(status);
}
//----------------------------------------------------------------------------------
void sfits_to_lfits(char *source_fits,double *posx1, double *posx2, double *alpha1, double *alpha2,double ysc1,double ysc2, double dsi, int nlx, int nly, char *lensed_fits) {

    fitsfile *fptr_in;
	int status,  nfound, anynull;
    long naxes[2],fpixel,npixels, i, j, index;

    float nullval;
    status = 0;

    fits_open_file(&fptr_in, source_fits, READONLY, &status);
    fits_read_keys_lng(fptr_in, "NAXIS", 1, 2, naxes, &nfound, &status);

	long nsx = naxes[0];
	long nsy = naxes[1];


    npixels  = nsx*nsy;
    fpixel   = 1;
    nullval  = 0;

	double *source_map = calloc(npixels,sizeof(double));
    fits_read_img(fptr_in, TDOUBLE, fpixel, npixels, &nullval,
                  source_map, &anynull, &status);

    fits_close_file(fptr_in, &status);

	double *posy1 = calloc(nlx*nly,sizeof(double));
	double *posy2 = calloc(nlx*nly,sizeof(double));

	for(i=0;i<nlx;i++) for(j=0;j<nly;j++){
		index = i*nlx+j;
		posy1[index] = posx1[index]-alpha1[index];
		posy2[index] = posx2[index]-alpha2[index];
	}

	double *lensed_map = calloc(nlx*nly,sizeof(double));
	Interplation_on_source_plane(source_map,posy1,posy2,ysc1,ysc2,dsi,nsx,nsy,nlx,nly,lensed_map);
    fitsfile *fptr_out;
    int fpixel_out = fpixel;
    long bitpix_out =  DOUBLE_IMG;
    long naxis_out = 2;
    long npixels_out = nlx*nly;
    long naxes_out[naxis_out];
    naxes_out[0] = nlx;
    naxes_out[1] = nly;

    status = 0;

    remove(lensed_fits);
    fits_create_file(&fptr_out, lensed_fits, &status);
    fits_create_img(fptr_out,  bitpix_out, naxis_out, naxes_out, &status);
    fits_write_img(fptr_out, TDOUBLE, fpixel_out, npixels_out, lensed_map, &status);
    fits_close_file(fptr_out, &status);

	free(posy1);
	free(posy2);
}
Beispiel #10
0
void OutputFileFITS::create(const std::string &filename) {
	int status = 0;

	fits_create_file(&infptr, filename.c_str(), &status);

	if (status)
		throwException("Error in OutputFileFITS::create() ", status);

	opened = true;
}
static void write_pixels(oskar_Mem* data, const char* filename,
        int width, int height, int num_planes, int i_plane, int* status)
{
    long naxes[3], firstpix[3], num_pix;
    int dims_ok = 0;
    fitsfile* f = 0;
    if (*status) return;
    if (oskar_file_exists(filename))
    {
        int naxis = 0, imagetype = 0;
        fits_open_file(&f, filename, READWRITE, status);
        fits_get_img_param(f, 3, &imagetype, &naxis, naxes, status);
        if (naxis == 3 &&
                naxes[0] == width &&
                naxes[1] == height &&
                naxes[2] == num_planes)
        {
            dims_ok = 1;
        }
        else
        {
            *status = 0;
            fits_close_file(f, status);
            remove(filename);
            f = 0;
        }
    }
    if (!dims_ok)
    {
        naxes[0] = width;
        naxes[1] = height;
        naxes[2] = num_planes;
        fits_create_file(&f, filename, status);
        fits_create_img(f, oskar_mem_is_double(data) ? DOUBLE_IMG : FLOAT_IMG,
                3, naxes, status);
    }
    if (*status || !f)
    {
        if (f) fits_close_file(f, status);
        *status = OSKAR_ERR_FILE_IO;
        return;
    }
    num_pix = width * height;
    firstpix[0] = 1;
    firstpix[1] = 1;
    firstpix[2] = 1 + i_plane;
    if (i_plane < 0)
    {
        firstpix[2] = 1;
        num_pix *= num_planes;
    }
    fits_write_pix(f, oskar_mem_is_double(data) ? TDOUBLE : TFLOAT,
            firstpix, num_pix, oskar_mem_void(data), status);
    fits_close_file(f, status);
}
Beispiel #12
0
void FitsFile::Create()
{
	if(_isOpen) {
		throw FitsIOException("File was opened twice");
	} else {
		int status = 0;
		fits_create_file(&_fptr, (std::string("!") + _filename).c_str(), &status);
		CheckStatus(status);
		_isOpen = true;
	}
}
Beispiel #13
0
/*  This routine provides simple FITS writer. It uses the routines
 *  provided by the fitsTcl/cfitsio libraries
 *
 *  NOTE : It will fail if the image already exists
 */
int ApogeeAltaManager::saveimage(unsigned short *src_buffer, char *filename, int nx, int ny)
{
	fitsfile *fptr;       /* pointer to the FITS file, defined in fitsio.h */
	long  fpixel, nelements;
	unsigned short *array;
	unsigned short *simg;
	int status;
	/* initialize FITS image parameters */
	int bitpix   =  USHORT_IMG; /* 16-bit unsigned short pixel values       */
	long naxis    =   2;  /* 2-dimensional image                            */
	long naxes[2];

	naxes[0] = nx-bcols;
	naxes[1] = ny;
	array = src_buffer;
	status = 0;         /* initialize status before calling fitsio routines */
	simg = (unsigned short *)CCD_locate_buffer(const_cast<char *>("stemp"),2,nx-bcols,ny,1,1);

	if (fits_create_file(&fptr, filename, &status)) /* create new FITS file */
		printerror( status );           /* call printerror if error occurs */

	/* write the required keywords for the primary array image.     */
	/* Since bitpix = USHORT_IMG, this will cause cfitsio to create */
	/* a FITS image with BITPIX = 16 (signed short integers) with   */
	/* BSCALE = 1.0 and BZERO = 32768.  This is the convention that */
	/* FITS uses to store unsigned integers.  Note that the BSCALE  */
	/* and BZERO keywords will be automatically written by cfitsio  */
	/* in this case.                                                */

	if ( fits_create_img(fptr,  bitpix, naxis, naxes, &status) )
		printerror( status );

	fpixel = 1;                               /* first pixel to write      */
	nelements = naxes[0] * naxes[1];          /* number of pixels to write */

	if (bcols > 0)
	{
		dobiassubtract(src_buffer,simg,naxes[0],naxes[1]);

		/* write the array of unsigned integers to the FITS file */
		if ( fits_write_img(fptr, TUSHORT, fpixel, nelements, simg, &status) )
			printerror( status );
	} else
	{
		/* write the array of unsigned integers to the FITS file */
		if ( fits_write_img(fptr, TUSHORT, fpixel, nelements, src_buffer, &status) )
			printerror( status );
	}

	if ( fits_close_file(fptr, &status) )                /* close the file */
		printerror( status );

	return(status);
}
Beispiel #14
0
/**
 * Saves the image to a FITS file using the cfitsio library.
 *
 * @param output_filename string, where to save the FITS file
 * @param sx, integer, horizontal image size
 * @param sy, integer, vertical image size
 * @param data, rows of a two dimensional integer array, image data
 * @return  Zero on success, non-zero on failure.
 */
int camera_save_fits_image(char * output_filename, long sx, long sy, unsigned short * data) {
  fitsfile *fptr;
  int status = 0;
  long fpixel, nelements;
  float gain;
  int bitpix = USHORT_IMG;
  long naxis = 2;
  long naxes[2] = {sx, sy};

  /* Delete old file if it already exists */
  remove(output_filename);

  /* create new FITS file */
  if (fits_create_file(&fptr, output_filename, &status)) {
    if (status) fits_report_error(stderr, status);
    return status;
  }

  /* the setting of bitpix = USHORT_IMG implies that BITPIX = 16,
   BSCALE = 1.0 and BZERO = 32768, and the latter two keywords are
   added automatically. */
  if (fits_create_img(fptr, bitpix, naxis, naxes, &status)) {
    if (status) fits_report_error(stderr, status);
    return status;
  }

  /* first pixel to write */
  fpixel = 1;
  /* number of pixels to write */
  nelements = naxes[0] * naxes[1];

  /* write the array of unsigned integers to the FITS file */
  if (fits_write_img(fptr, TUSHORT, fpixel, nelements, data, &status)) {
    if (status) fits_report_error(stderr, status);
    return status;
  }

  /* write the gain parameter of the camera to the header */
  gain = ML16803_CAMERA_GAIN;
  if (fits_update_key(fptr, TFLOAT, "GAIN", &gain, "Camera gain of ML16803", &status)) {
    if (status) fits_report_error(stderr, status);
    return status;
  }

  /* close the file */
  if (fits_close_file(fptr, &status)) {
    if (status) fits_report_error(stderr, status);
    return status;
  }

  return 0;
}
Beispiel #15
0
void writeFITS(const cv::Mat& cvImage, const std::string& filename)
{
  if(cvImage.channels() != 1)
  {
    CustomException("writeFITS: Only able to write FITS from single channel images.");
  }
  
  fitsfile *fptr; //pointer to the FITS file defined in fitsioh
  int status;
  char err_text[100];
  cv::Mat floatImg = cv::Mat_<float>(cvImage);
  long fpixel = 1, naxis = 2, nelements;
  long naxes[2] = {cvImage.cols, cvImage.rows};
  float array[cvImage.cols][cvImage.rows];
  for(int i = 0;i < floatImg.rows ;i++)
  {
    for(int j = 0;j < floatImg.cols ;j++)
    {
      array[j][i] = floatImg.at<float>(j,i);
    }
  }

  status=0;
  fits_create_file(&fptr, filename.c_str(), &status);
  if (status)
  {
    fits_report_error(stdout, status);
    fits_get_errstatus(status,err_text);
    fptr = nullptr;
    throw  CustomException("writeFITS: Cannot create fits file.");
  }

  fits_create_img(fptr, FLOAT_IMG, naxis, naxes, &status);
  if (status)
  {
    fits_report_error(stdout, status);
    fits_get_errstatus(status,err_text);
    fptr = nullptr;
    throw  CustomException("writeFITS: Cannot create image file.");
  }
  nelements = naxes[0] * naxes[1];
  fits_write_img(fptr, TFLOAT, fpixel, nelements, array[0],  &status);
  if (status)
  {
    fits_report_error(stdout, status);
    fits_get_errstatus(status,err_text);
    fptr = nullptr;
    throw  CustomException("writeFITS: Cannot write image file.");
  }
  fits_close_file(fptr, &status);
  fits_report_error(stderr, status);
}
Beispiel #16
0
fitsfile* create_fits_file(const char* filename, int precision,
        int width, int height, int num_times, int num_channels,
        double centre_deg[2], double fov_deg[2], double start_time_mjd,
        double delta_time_sec, double start_freq_hz, double delta_freq_hz,
        int* status)
{
    long naxes[4];
    double delta;
    fitsfile* f = 0;
    FILE* t = 0;
    if (*status) return 0;

    /* Create a new FITS file and write the image headers. */
    t = fopen(filename, "rb");
    if (t)
    {
        fclose(t);
        remove(filename);
    }
    naxes[0]  = width;
    naxes[1]  = height;
    naxes[2]  = num_channels;
    naxes[3]  = num_times;
    fits_create_file(&f, filename, status);
    fits_create_img(f, (precision == OSKAR_DOUBLE ? DOUBLE_IMG : FLOAT_IMG),
            4, naxes, status);
    fits_write_date(f, status);

    /* Write axis headers. */
    delta = oskar_convert_fov_to_cellsize(fov_deg[0] * M_PI/180, width) * 180/M_PI;
    write_axis_header(f, 1, "RA---SIN", "Right Ascension",
            centre_deg[0], -delta, width / 2 + 1, 0.0, status);
    delta = oskar_convert_fov_to_cellsize(fov_deg[1] * M_PI/180, height) * 180/M_PI;
    write_axis_header(f, 2, "DEC--SIN", "Declination",
            centre_deg[1], delta, height / 2 + 1, 0.0, status);
    write_axis_header(f, 3, "FREQ", "Frequency",
            start_freq_hz, delta_freq_hz, 1.0, 0.0, status);
    write_axis_header(f, 4, "UTC", "Time",
            start_time_mjd, delta_time_sec, 1.0, 0.0, status);

    /* Write other headers. */
    fits_write_key_str(f, "BUNIT", "JY/BEAM", "Brightness units", status);
    fits_write_key_str(f, "TIMESYS", "UTC", NULL, status);
    fits_write_key_str(f, "TIMEUNIT", "s", "Time axis units", status);
    fits_write_key_dbl(f, "MJD-OBS", start_time_mjd, 10, "Start time", status);
    fits_write_key_dbl(f, "OBSRA", centre_deg[0], 10, "RA", status);
    fits_write_key_dbl(f, "OBSDEC", centre_deg[1], 10, "DEC", status);
    /*fits_flush_file(f, status);*/

    return f;
}
Beispiel #17
0
int write_fits(char* path,unsigned char* image)
{
	fitsfile *fptrout;
	unsigned int naxis = 2;
    long naxes[2];
    int status = 0;
    long nelements = width*height;
    naxes[0] = width;
	naxes[1] = height;
	fits_create_file(&fptrout,path, &status);
	fits_create_img(fptrout, BYTE_IMG, naxis, naxes, &status);
	fits_write_img(fptrout, TBYTE, 1, nelements, image, &status);
	fits_close_file(fptrout, &status);
}
Beispiel #18
0
int main(int argc, char *argv[])
{
    fitsfile *infptr, *outfptr;
    int status = 0, hdutype, ii;
    char infile[FLEN_FILENAME],outfile[FLEN_FILENAME]; 

    if (argc != 3)
    {
        printf("Usage: fitscopy inputfile outputfile\n");
        return(1);
    }

    strcpy(infile,  argv[1] );  
    strcpy(outfile, argv[2] );  

    if ( fits_open_file(&infptr, infile, READONLY, &status) )
    {
         fits_report_error(stderr, status);
         return(status);
    }

    if ( fits_create_file(&outfptr, outfile, &status) )
    {
         fits_close_file(infptr, &status);
         fits_report_error(stderr, status);
         return(status);
    }

    /* attempt to move to next HDU, until we get an EOF error */
    for (ii = 1; !(fits_movabs_hdu(infptr, ii, &hdutype, &status) ); ii++) 
    {
        if ( fits_copy_hdu(infptr, outfptr, 0, &status) )
        {
            fits_report_error(stderr, status);
            break;
        }
    }

    if (status == END_OF_FILE)   
        status = 0;              /* got the expected EOF error; reset = 0  */

    fits_close_file(infptr,  &status);
    fits_close_file(outfptr, &status);

    if (status)
        fits_report_error(stderr, status);

    return(0);
}
Beispiel #19
0
void save_3d_image(SimulationData &sim_data, WaveFunction &psi, const char * fits_file_name) {

	fitsfile *fptr;
	int status = 0;
	long fpixel = 1, naxis = 3, nelements;
	long naxes[3] = {sim_data.get_num_x(), sim_data.get_num_y(), sim_data.get_num_z()};

	fits_create_file(&fptr, fits_file_name, &status);
	fits_create_img(fptr, DOUBLE_IMG, naxis, naxes, &status);
	nelements = naxes[0] * naxes[1] * naxes[2];
	fits_write_img(fptr, TDOUBLE, fpixel, nelements, psi.abs_psi, &status);
	fits_close_file(fptr, &status);
	fits_report_error(stderr, status);	

}
Beispiel #20
0
int nemo_main(void)
{
    fitsfile *fptr;       /* pointer to the FITS file; defined in fitsio.h */
    int status, ii, jj;
    long  fpixel = 1, naxis = 2, nelements, exposure;
    long naxes[2] = { 300, 200 };   /* image is 300 pixels wide by 200 rows */
    short array[200][300];
    string fname = getparam("out");
    float time4 = PI;
    double time8 = PI;

    status = 0;         /* initialize status before calling fitsio routines */
    fits_create_file(&fptr, fname, &status);   /* create new file */

    /* Create the primary array image (16-bit short integer pixels */
    fits_create_img(fptr, SHORT_IMG, naxis, naxes, &status);

    /* Write a keyword; must pass the ADDRESS of the value */
    exposure = 1500.;
    fits_update_key(fptr, TLONG, "EXPOSURE", &exposure,
         "Total Exposure Time", &status);
    dprintf(1,"exposure %d\n",status);
    fits_update_key(fptr, TFLOAT, "TIME4", &time4,
		    "Sample REAL*4", &status);    
    dprintf(1,"time4    %d\n",status);
    fits_update_key(fptr, TDOUBLE, "TIME8", &time8,
		    "Sample REAL*8", &status);
    dprintf(1,"time8    %d\n",status);
    fits_update_key(fptr, TSTRING, "DATEOBS", "2001/09/30",
		    "Sample STRING", &status);
    dprintf(1,"string   %d\n",status);

    /* Initialize the values in the image with a linear ramp function */
    for (jj = 0; jj < naxes[1]; jj++)
        for (ii = 0; ii < naxes[0]; ii++)
            array[jj][ii] = ii + jj;

    nelements = naxes[0] * naxes[1];          /* number of pixels to write */

    /* Write the array of integers to the image */
    fits_write_img(fptr, TSHORT, fpixel, nelements, array[0], &status);

    fits_close_file(fptr, &status);            /* close the file */

    fits_report_error(stderr, status);  /* print out any error messages */
    return status ;
}
Beispiel #21
0
local void print_ccd(real **ccd, char filename[], real t_exposure) { 

    fitsfile *fptr;
    int status, ii, jj;
    long  fpixel, nelements, exposure;
    //    unsigned short array[512][512];  
    unsigned short array[XCCD_MAX][YCCD_MAX];  

//    char filename[] = "ccd.fit";
    int bitpix   =  USHORT_IMG; 
    long naxis    =   2;
    //    long naxes[2] = { 512, 512 }; 
    long naxes[2] = { XCCD_MAX, YCCD_MAX }; 

    remove(filename); 
    status = 0;


    if (fits_create_file(&fptr, filename, &status)) 
         printf("%s\n", status);       

    if ( fits_create_img(fptr,  bitpix, naxis, naxes, &status) )
         printf("%s\n", status);     

    float val;
    for (jj = 0; jj < naxes[1]; jj++) {   
      for (ii = 0; ii < naxes[0]; ii++) {
//          cin >> val;
            array[jj][ii] = (unsigned short)ccd[jj][ii];
        }
    }

    fpixel = 1;
    nelements = naxes[0] * naxes[1]; 

    if ( fits_write_img(fptr, TUSHORT, fpixel, nelements, array[0], &status) )
         printf("%s\n", status);

    exposure = long(t_exposure);
    if ( fits_update_key(fptr, TLONG, "EXPOSURE", &exposure,
         "Exposure time in seconds", &status) )
         printf("%s\n", status);  

    if ( fits_close_file(fptr, &status) )  
         printf("%s\n", status);        
}
Beispiel #22
0
int write_map(const char *filename, double *intensity, double *q, double *u, long nrows)
{
    
    fitsfile *fptr;
    int status = 0;
    int hdutype;
    int tfields=3;
    char extname[] = "BINTABLE";   /* extension name */
    char *ttype[] = { "I","Q","U" };
    char *tform[] = { "1E","1E","1E" };
    char *tunit[] = { " "," ", " " };
    if (fits_create_file(&fptr, filename, &status)) 
    fprintf(stderr, "%s (%d): Could not create new fits file.\n", 
        __FILE__, __LINE__);

    /* move to 1st HDU  */
    fits_movabs_hdu(fptr, 1, &hdutype, &status);
    //long nrows = 12L*nside*nside*2;
    //long nside = sqrtl(nrows/12L);
    long nside = 1024; //FIXME compute it

    /* append a new empty binary table onto the FITS file */
    fits_create_tbl( fptr, BINARY_TBL, nrows, tfields, ttype, tform,
            tunit, extname, &status);

    fits_write_key(fptr, TSTRING, "ORDERING", "NESTED", 
             "Pixel ordering scheme, either RING or NESTED", &status);

    fits_write_key(fptr, TLONG, "NSIDE", &nside, "Resolution parameter for HEALPIX", &status);
    long firstrow  = 1;  /* first row in table to write   */
    long firstelem = 1;  /* first element in row  (ignored in ASCII tables)  */

    if (fits_write_col(fptr, TDOUBLE, 1, firstrow, firstelem, nrows, intensity,
             &status))
    fprintf(stderr, "%s (%d): Could not write signal.\n", __FILE__, __LINE__);
    if (fits_write_col(fptr, TDOUBLE, 2, firstrow, firstelem, nrows, q,
             &status))
    fprintf(stderr, "%s (%d): Could not write signal.\n", __FILE__, __LINE__);
    if (fits_write_col(fptr, TDOUBLE, 3, firstrow, firstelem, nrows, u,
             &status))
    fprintf(stderr, "%s (%d): Could not write signal.\n", __FILE__, __LINE__);

    fits_close_file(fptr, &status);

    return true;
}
Beispiel #23
0
static VipsFits *
vips_fits_new_write( VipsImage *in, const char *filename )
{
	VipsFits *fits;
	int status;

	status = 0;

	if( !(fits = VIPS_NEW( in, VipsFits )) )
		return( NULL );
	fits->filename = vips_strdup( VIPS_OBJECT( in ), filename );
	fits->image = in;
	fits->fptr = NULL;
	fits->lock = NULL;
	fits->band_select = -1;
	fits->buffer = NULL;
	g_signal_connect( in, "close", 
		G_CALLBACK( vips_fits_close_cb ), fits );

	if( !(fits->filename = vips_strdup( NULL, filename )) )
		return( NULL );

	/* We need to be able to hold one scanline of one band.
	 */
	if( !(fits->buffer = VIPS_ARRAY( NULL, 
		VIPS_IMAGE_SIZEOF_ELEMENT( in ) * in->Xsize, VipsPel )) )
		return( NULL );

	/* fits_create_file() will fail if there's a file of thet name, unless
	 * we put a "!" in front ofthe filename. This breaks conventions with
	 * the rest of vips, so just unlink explicitly.
	 */
	g_unlink( filename );

	if( fits_create_file( &fits->fptr, filename, &status ) ) {
		vips_error( "fits", 
			_( "unable to write to \"%s\"" ), filename );
		vips_fits_error( status );
		return( NULL );
	}

	fits->lock = vips_g_mutex_new();

	return( fits );
}
Beispiel #24
0
int write2Dfits(char *fname, double *f, int pwidth, int pheight)
{
  /*  write out image as a fits file */

  fitsfile *afptr;
  int status = 0;
  int anaxis;
  long anaxes[2], fpixel[2]={1,1};
  int bitpix;
  int size;

  bitpix = -32;
  anaxis = 2;
  anaxes[0] = pwidth;
  anaxes[1] = pheight;

  size = anaxes[0]*anaxes[1];

  fits_create_file(&afptr, fname, &status);
  fits_create_img(afptr, bitpix, anaxis, anaxes, &status);

  if (status) {
    fits_report_error(stderr, status); /* print error message */
    return(status);
  }

  /* write all data into image array */
  if (fits_write_pix(afptr, TDOUBLE, fpixel, size, f, &status) )
    {
      printf(" error reading pixel data \n");
      exit (2);
    }

  /* close the fits file */

  fits_close_file(afptr, &status);

  if (status) {
    fits_report_error(stderr, status); /* print error message */
    exit(2);
  }

  return 0;

}
//----------------------------------------------------------------------------------
void save_to_fits(double *lensed_map, int nlx, int nly, char *lensed_fits) {

    fitsfile *fptr_out;
    int fpixel_out = 1;
    long bitpix_out =  DOUBLE_IMG;
    long naxis_out = 2;
    long npixels_out = nlx*nly;
    long naxes_out[naxis_out];
    naxes_out[0] = nlx;
    naxes_out[1] = nly;

    int status = 0;

    remove(lensed_fits);
    fits_create_file(&fptr_out, lensed_fits, &status);
    fits_create_img(fptr_out,  bitpix_out, naxis_out, naxes_out, &status);
    fits_write_img(fptr_out, TDOUBLE, fpixel_out, npixels_out, lensed_map, &status);
    fits_close_file(fptr_out, &status);
}
Beispiel #26
0
void	writeimage(const image_t *image, const char *filename) {
	if (debug) {
		fprintf(stderr, "%s:%d: writing image %s\n", __FILE__, __LINE__,
			filename);
	}
	char	errmsg[80];
	int	status = 0;
	fitsfile	*fits = NULL;
	if (fits_create_file(&fits, filename, &status)) {
		fits_get_errstatus(status, errmsg);
		fprintf(stderr, "cannot create fits file %s: %s\n",
			filename, errmsg);
		return;
	}

	// find dimensions of pixel array
	int	naxis = 2;
	long	naxes[2] = { image->width, image->height };
	if (fits_create_img(fits, DOUBLE_IMG, naxis, naxes, &status)) {
		fits_get_errstatus(status, errmsg);
		fprintf(stderr, "cannot create image: %s\n", errmsg);
		goto bad;
	}

	// read the pixel data
	long	npixels = image->width * image->height;
	long	firstpixel[3] = { 1, 1, 1 };
	if (fits_write_pix(fits, TDOUBLE, firstpixel, npixels,
			image->data, &status)) {
		fits_get_errstatus(status, errmsg);
		fprintf(stderr, "cannot write pixel data: %s\n", errmsg);
	}

bad:
	// close the file
	if (fits_close_file(fits, &status)) {
		fits_get_errstatus(status, errmsg);
		fprintf(stderr, "cannot close fits file %s: %s\n",
			filename, errmsg);
		goto bad;
	}
}
Beispiel #27
0
void write_output(const char* filename, size_t width, size_t height, size_t noutput, cl_float* output[], const char* names[])
{
    int status = 0;
    
    // the FITS file
    fitsfile* fptr;
    
    // create FITS file
    fits_create_file(&fptr, filename, &status);
    
    // write FITS file
    write_fits(fptr, TFLOAT, width, height, noutput, (void**)output, names, &status);
               
    // close FITS file
    fits_close_file(fptr, &status);
    
    // report FITS errors
    if(status)
        fits_error(filename, status);
}
Beispiel #28
0
int	WriteFITS(unsigned short *buffer, int cols, int rows, char *filename) {
#ifdef HAVE_FITSIO_H
	int	status = 0;
	fitsfile	*fits;
	unlink(filename);
	fits_create_file(&fits, filename, &status);
	if (status) {
		std::cerr << "cannot create file " << filename << std::endl;
		return -1;
	}
	long	naxes[2] = { cols, rows };
	fits_create_img(fits, SHORT_IMG, 2, naxes, &status);
	long	fpixel[2] = { 1, 1 };
	fits_write_pix(fits, TUSHORT, fpixel, cols * rows, buffer, &status);
	fits_close_file(fits, &status);
#else
	std::cerr << "FITS not supported" << std::endl;
	return -1;
#endif
}
void save_file( const std::string& filename, const image_type& img ) {
	fitsfile* f;
	int status = 0;

    std::string fname = "!" + filename;
	fits_create_file( &f, fname.c_str(), &status );
	
	long dims[2];
	dims[0] = img.extents.first;	dims[1] = img.extents.second;

	typedef CFitsIOTypeMap<	image_type::element > map;
	fits_create_img( f, map::image_type, 2, dims, &status );
	fits_write_img( f, map::type, 1, img.num_elements(), 
		(void*)img.ptr(), &status );
        
    fits_close_file( f, &status );

	if( status ) {
		fits_report_error( stderr, status );
	}
}
void makebasisfile( int kk, int datax, int slice, void *address )
{
  long int bitpix = -32, nElements, naxis = 2, naxes[2], fpixel = 1;
  fitsfile *fptr;             /* pointer to output FITS files; defined in fitsio.h */
  int status = 0;
  char tname[128];

  /* Initialize FITS image parameters */
  /* First build the file name. */
  (void)strcpy( tname, "basis" );
  (void)sprintf( strBuf, "%d", kk+1 );
  (void)strcat( tname, strBuf );
  (void)strcat( tname, ".fits" );
  (void)printf( "tname = %s\n", tname );
  naxes[0] = datax;  /* datax pixels wide */
  naxes[1] = slice;  /* by slice rows */

  if ( fits_create_file( &fptr, tname, &status ) ) {  /* Create new FITS file */
    printerror( status, __LINE__ );
  }

  /* Write the required keywords for the primary array image */
  if ( fits_create_img( fptr,  bitpix, naxis, naxes, &status ) ) {
    printerror( status, __LINE__ );
  }

  nElements = naxes[0] * naxes[1];       /* number of pixels to write */

  /* Write the array of floats to the file. */
  if ( fits_write_img(fptr, TFLOAT, fpixel, nElements, address, &status) ) {
    printerror( status, __LINE__ );
  }

  if ( fits_close_file(fptr, &status) ) {  /* close the file */
    printerror( status, __LINE__ );
  }

  return;
}