예제 #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";
}
예제 #2
0
/**
 * Read data from a user-specified text file and write out in OIFITS format.
 */
void demo_write(void)
{
  oi_array array;
  oi_target targets;
  oi_wavelength wave;
  oi_vis vis;
  oi_vis2 vis2;
  oi_t3 t3;
  char filename[FLEN_FILENAME];
  FILE *fp;
  int i, irec, iwave, itarg, status;
  fitsfile *fptr;

  /* get input filename */
  printf("Enter input TEXT filename: ");
  fgets(filename, FLEN_FILENAME, stdin);
  filename[strlen(filename)-1] = '\0'; /* zap newline */

  fp = fopen(filename, "r");

  /* Read info for OI_ARRAY table */
  fscanf(fp, "OI_ARRAY arrname %s ", array.arrname);
  fscanf(fp, "frame %s ", array.frame);
  fscanf(fp, "arrayx %lf arrayy %lf arrayz %lf ", &array.arrayx,
	 &array.arrayy, &array.arrayz);
  fscanf(fp, "nelement %d ", &array.nelement);
  array.elem = malloc(array.nelement*sizeof(element));
  for (i=0; i<array.nelement; i++) {
    
    fscanf(fp, "tel_name %s sta_name %s ", array.elem[i].tel_name,
	   array.elem[i].sta_name);
    fscanf(fp, "staxyz %lf %lf %lf diameter %f ", &array.elem[i].staxyz[0],
	   &array.elem[i].staxyz[1], &array.elem[i].staxyz[2],
	   &array.elem[i].diameter);
    array.elem[i].sta_index = i+1;
  }
  array.revision = 1;

  /* Read info for OI_TARGET table */
  fscanf(fp, "OI_TARGET ntarget %d ", &targets.ntarget);
  targets.targ = malloc(targets.ntarget*sizeof(target));
  for (itarg=0; itarg<targets.ntarget; itarg++) {
    fscanf(fp, "target_id %d ", &targets.targ[itarg].target_id);
    fscanf(fp, "target %s ", targets.targ[itarg].target);
    fscanf(fp, "raep0 %lf ", &targets.targ[itarg].raep0);
    fscanf(fp, "decep0 %lf ", &targets.targ[itarg].decep0);
    fscanf(fp, "equinox %f ", &targets.targ[itarg].equinox);
    fscanf(fp, "ra_err %lf ", &targets.targ[itarg].ra_err);
    fscanf(fp, "dec_err %lf ", &targets.targ[itarg].dec_err);
    fscanf(fp, "sysvel %lf ", &targets.targ[itarg].sysvel);
    fscanf(fp, "veltyp %s ", targets.targ[itarg].veltyp);
    fscanf(fp, "veldef %s ", targets.targ[itarg].veldef);
    fscanf(fp, "pmra %lf ", &targets.targ[itarg].pmra);
    fscanf(fp, "pmdec %lf ", &targets.targ[itarg].pmdec);
    fscanf(fp, "pmra_err %lf ", &targets.targ[itarg].pmra_err);
    fscanf(fp, "pmdec_err %lf ", &targets.targ[itarg].pmdec_err);
    fscanf(fp, "parallax %f ", &targets.targ[itarg].parallax);
    fscanf(fp, "para_err %f ", &targets.targ[itarg].para_err);
    fscanf(fp, "spectyp %s ", targets.targ[itarg].spectyp);
  }
  targets.revision = 1;

  /* Read info for OI_WAVELENGTH table */
  fscanf(fp, "OI_WAVELENGTH insname %s ", wave.insname);
  fscanf(fp, "nwave %d ", &wave.nwave);
  wave.eff_wave = malloc(wave.nwave*sizeof(float));
  wave.eff_band = malloc(wave.nwave*sizeof(float));
  fscanf(fp, "eff_wave ");
  for(i=0; i<wave.nwave; i++) {
    fscanf(fp, "%f ", &wave.eff_wave[i]);
  }
  fscanf(fp, "eff_band ");
  for(i=0; i<wave.nwave; i++) {
    fscanf(fp, "%f ", &wave.eff_band[i]);
  }
  wave.revision = 1;

  /* Read info for OI_VIS table */
  fscanf(fp, "OI_VIS date-obs %s ", vis.date_obs);
  fscanf(fp, "arrname %s insname %s ", vis.arrname, vis.insname);
  fscanf(fp, "numrec %ld ", &vis.numrec);
  vis.record = malloc(vis.numrec*sizeof(oi_vis_record));
  printf("Reading %ld vis records...\n", vis.numrec);
  /* loop over records */
  for(irec=0; irec<vis.numrec; irec++) {
    fscanf(fp, "target_id %d time %lf mjd %lf ", &vis.record[irec].target_id,
	   &vis.record[irec].time, &vis.record[irec].mjd);
    fscanf(fp, "int_time %lf visamp ", &vis.record[irec].int_time);
    vis.record[irec].visamp = malloc(wave.nwave*sizeof(DATA));
    for(iwave=0; iwave<wave.nwave; iwave++) {
      fscanf(fp, "%lf ", &vis.record[irec].visamp[iwave]);
    }
    fscanf(fp, "visamperr ");
    vis.record[irec].visamperr = malloc(wave.nwave*sizeof(DATA));
    for(iwave=0; iwave<wave.nwave; iwave++) {
      fscanf(fp, "%lf ", &vis.record[irec].visamperr[iwave]);
    }
    fscanf(fp, "visphi ");
    vis.record[irec].visphi = malloc(wave.nwave*sizeof(DATA));
    for(iwave=0; iwave<wave.nwave; iwave++) {
      fscanf(fp, "%lf ", &vis.record[irec].visphi[iwave]);
    }
    fscanf(fp, "visphierr ");
    vis.record[irec].visphierr = malloc(wave.nwave*sizeof(DATA));
    for(iwave=0; iwave<wave.nwave; iwave++) {
      fscanf(fp, "%lf ", &vis.record[irec].visphierr[iwave]);
    }
    fscanf(fp, "ucoord %lf vcoord %lf ", &vis.record[irec].ucoord,
	   &vis.record[irec].vcoord);
    fscanf(fp, "sta_index %d %d ", &vis.record[irec].sta_index[0],
	   &vis.record[irec].sta_index[1]);
    vis.record[irec].flag = malloc(wave.nwave*sizeof(char));
    for(iwave=0; iwave<wave.nwave; iwave++) {
      vis.record[irec].flag[iwave] = FALSE;
    }
  }
  vis.revision = 1;
  vis.nwave = wave.nwave;

  /* Read info for OI_VIS2 table */
  fscanf(fp, "OI_VIS2 date-obs %s ", vis2.date_obs);
  fscanf(fp, "arrname %s insname %s ", vis2.arrname, vis2.insname);
  fscanf(fp, "numrec %ld ", &vis2.numrec);
  vis2.record = malloc(vis2.numrec*sizeof(oi_vis2_record));
  printf("Reading %ld vis2 records...\n", vis2.numrec);
  /* loop over records */
  for(irec=0; irec<vis2.numrec; irec++) {
    fscanf(fp, "target_id %d time %lf mjd %lf ", &vis2.record[irec].target_id,
	   &vis2.record[irec].time, &vis2.record[irec].mjd);
    fscanf(fp, "int_time %lf vis2data ", &vis2.record[irec].int_time);
    vis2.record[irec].vis2data = malloc(wave.nwave*sizeof(DATA));
    for(iwave=0; iwave<wave.nwave; iwave++) {
      fscanf(fp, "%lf ", &vis2.record[irec].vis2data[iwave]);
    }
    fscanf(fp, "vis2err ");
    vis2.record[irec].vis2err = malloc(wave.nwave*sizeof(DATA));
    for(iwave=0; iwave<wave.nwave; iwave++) {
      fscanf(fp, "%lf ", &vis2.record[irec].vis2err[iwave]);
    }
    fscanf(fp, "ucoord %lf vcoord %lf ", &vis2.record[irec].ucoord,
	   &vis2.record[irec].vcoord);
    fscanf(fp, "sta_index %d %d ", &vis2.record[irec].sta_index[0],
	   &vis2.record[irec].sta_index[1]);
    vis2.record[irec].flag = malloc(wave.nwave*sizeof(char));
    for(iwave=0; iwave<wave.nwave; iwave++) {
      vis2.record[irec].flag[iwave] = FALSE;
    }
  }
  vis2.revision = 1;
  vis2.nwave = wave.nwave;

  /* Read info for OI_T3 table */
  fscanf(fp, "OI_T3 date-obs %s ", t3.date_obs);
  fscanf(fp, "arrname %s insname %s ", t3.arrname, t3.insname);
  fscanf(fp, "numrec %ld ", &t3.numrec);
  t3.record = malloc(t3.numrec*sizeof(oi_t3_record));
  printf("Reading %ld t3 records...\n", t3.numrec);
  /* loop over records */
  for(irec=0; irec<t3.numrec; irec++) {
    fscanf(fp, "target_id %d time %lf mjd %lf ", &t3.record[irec].target_id,
	   &t3.record[irec].time, &t3.record[irec].mjd);
    fscanf(fp, "int_time %lf t3amp ", &t3.record[irec].int_time);
    t3.record[irec].t3amp = malloc(wave.nwave*sizeof(DATA));
    for(iwave=0; iwave<wave.nwave; iwave++) {
      fscanf(fp, "%lf ", &t3.record[irec].t3amp[iwave]);
    }
    fscanf(fp, "t3amperr ");
    t3.record[irec].t3amperr = malloc(wave.nwave*sizeof(DATA));
    for(iwave=0; iwave<wave.nwave; iwave++) {
      fscanf(fp, "%lf ", &t3.record[irec].t3amperr[iwave]);
    }
    fscanf(fp, "t3phi ");
    t3.record[irec].t3phi = malloc(wave.nwave*sizeof(DATA));
    for(iwave=0; iwave<wave.nwave; iwave++) {
      fscanf(fp, "%lf ", &t3.record[irec].t3phi[iwave]);
    }
    fscanf(fp, "t3phierr ");
    t3.record[irec].t3phierr = malloc(wave.nwave*sizeof(DATA));
    for(iwave=0; iwave<wave.nwave; iwave++) {
      fscanf(fp, "%lf ", &t3.record[irec].t3phierr[iwave]);
    }
    fscanf(fp, "u1coord %lf v1coord %lf ", &t3.record[irec].u1coord,
	   &t3.record[irec].v1coord);
    fscanf(fp, "u2coord %lf v2coord %lf ", &t3.record[irec].u2coord,
	   &t3.record[irec].v2coord);
    fscanf(fp, "sta_index %d %d %d ", &t3.record[irec].sta_index[0],
	   &t3.record[irec].sta_index[1],
	   &t3.record[irec].sta_index[2]);
    t3.record[irec].flag = malloc(wave.nwave*sizeof(char));
    for(iwave=0; iwave<wave.nwave; iwave++) {
      t3.record[irec].flag[iwave] = FALSE;
    }
  }
  t3.revision = 1;
  t3.nwave = wave.nwave;

  fclose(fp);

  /* Write out FITS file */
  printf("Enter output OIFITS filename: ");
  fgets(filename, FLEN_FILENAME, stdin);
  filename[strlen(filename)-1] = '\0'; /* zap newline */
  printf("Writing FITS file %s...\n", filename);
  status = 0;
  fits_create_file(&fptr, filename, &status);
  if (status) {
    fits_report_error(stderr, status);
    exit(EXIT_FAILURE);
  }
  write_oi_target(fptr, targets, &status);
  write_oi_vis(fptr, vis, 1, &status);
  write_oi_vis2(fptr, vis2, 1, &status);
  write_oi_t3(fptr, t3, 1, &status);
  write_oi_array(fptr, array, 1, &status);
  write_oi_wavelength(fptr, wave, 1, &status);

  if (status) {
    /* Error occurred - delete partially-created file and exit */
    fits_delete_file(fptr, &status);
    exit(EXIT_FAILURE);
  } else {
    fits_close_file(fptr, &status);
  }

  /* Free storage */
  free_oi_target(&targets);
  free_oi_vis(&vis);
  free_oi_vis2(&vis2);
  free_oi_t3(&t3);
  free_oi_array(&array);
  free_oi_wavelength(&wave);
}
예제 #3
0
std::string write_image_3D(std::string pathname_3D, float *array_2D_real, float *array_2D_imag, std::string name_3D, int naxis_2D, long *naxes_2D) {
  // 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_3D.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_3D.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_3D.c_str(), &status);
    if (status != 0) {
      // cannot create the file, for some reason
      return FORMAT_STATUS(status);
    }
  }
  // create the primary array image
  int naxis_3D = naxis_2D+1;
  long *naxes_3D = new long [naxis_3D];
  for (int i = 0; i < naxis_2D; i++) {
    naxes_3D[i] = naxes_2D[i];
  }
  naxes_3D[naxis_3D-1] = 2;
  fits_create_img(fptr, FLOAT_IMG, naxis_3D, naxes_3D, &status);
  if (status != 0) {
    return FORMAT_STATUS(status);
  }
  // write a keyword
  fits_update_key(fptr, TFLOAT, name_3D.c_str(), array_2D_real, name_3D.c_str(), &status);
  if (status != 0) {
    return FORMAT_STATUS(status);
  }
  // write the array of floats to the image
  long *fpixel = new long [naxis_3D];
  fpixel[0] = 1;
  fpixel[1] = 1;
  fpixel[2] = 1;
  long *lpixel = new long [naxis_3D];
  lpixel[0] = naxes_2D[0];
  lpixel[1] = naxes_2D[1];
  lpixel[2] = 1;
  fits_write_subset(fptr, TFLOAT, fpixel, lpixel, (void*)array_2D_real, &status);
  if (status != 0) {
    return FORMAT_STATUS(status);
  }
  // write a keyword
  fits_update_key(fptr, TFLOAT, name_3D.c_str(), array_2D_imag, name_3D.c_str(), &status);
  if (status != 0) {
    return FORMAT_STATUS(status);
  }
  // write the array of floats to the image
  fpixel[0] = 1;
  fpixel[1] = 1;
  fpixel[2] = 2;
  lpixel[0] = naxes_2D[0];
  lpixel[1] = naxes_2D[1];
  lpixel[2] = 2;
  fits_write_subset(fptr, TFLOAT, fpixel, lpixel, (void*)array_2D_imag, &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";
}
예제 #4
0
int countsmalibur(AG_params & params) {

	int numcol = 0;
	long nrows = 0; 
	int status = 0;
	double l = 0, b = 0;	
	double x = 0, y = 0;	
	int i = 0, ii = 0;	
	double the = 0;	
	long mxdim= params.mxdim; // dimension (in pixels) of the map
	unsigned short A[mxdim][mxdim];

	for (i = 0; i < mxdim; i++)
	{   for (ii = 0; ii < mxdim; ii++)
		{
			A[i][ii] = 0;
		}
	}	

	double baa = params.ba * D2R;
	double laa = params.la * D2R;
	
	int bitpix   =  USHORT_IMG; /* 16-bit unsigned short pixel values       */
	long naxis    =   2;  /* 2-dimensional image                            */    
	long naxes[2] = { mxdim, mxdim };   /* image is 300 pixels wide by 200 rows */		
	
	fitsfile * evtFits;
	char tempname[FLEN_FILENAME];
	strcpy(tempname, tmpnam(NULL));
	if ( fits_create_file(&evtFits, tempname, &status) != 0 ) {
		printf("Errore in apertura file %s\n",tempname);
		return status;
	}	
	
	char expr[1024];
	strcpy(expr,params.evtexpr().c_str());
	
	std::cout << std::endl << "AG_ctsmapgen....................................adding events files"<< std::endl;
	status = addfile(evtFits, params.evtfile, expr, params.tmin, params.tmax);
	std::cout << "AG_ctsmapgen....................................addfile exiting STATUS : "<< status<< std::endl << std::endl ;	
	

	fitsfile * mapFits;
	if ( fits_create_file(&mapFits, params.outfile, &status) != 0 ) {
		printf("Errore in apertura file '%s'\n",params.outfile);
		return status;
	}	

	
	fits_movabs_hdu(evtFits, 2, NULL, &status);	
	fits_get_num_rows(evtFits, &nrows, &status);
	cout << nrows << endl;

	double ra, dec;
	double dummy;
	switch (params.projection) {
	    case AG_params::ARC:
		for (long k = 0; k<nrows; ++k) {
			fits_get_colnum(evtFits, 1, "RA", &numcol, &status);
			fits_read_col(evtFits, TDOUBLE, numcol, k+1, 1, 1, NULL, &ra, NULL, &status);
			fits_get_colnum(evtFits, 1, "DEC", &numcol, &status);
			fits_read_col(evtFits, TDOUBLE, numcol, k+1, 1, 1, NULL, &dec, NULL, &status);

		    eulerold(ra, dec, &l, &b, 1);
		    l*=D2R;
		    b*=D2R;
		    the = sin(b)*sin(baa)+cos(b)*cos(baa)*cos(l-laa);
		    if (the < -1.0) {
			    the = PI;
		    } else if (the > 1.0) {
			    the = 0.0;
		    } else {
			    the = acos(the);
		    }
		    x = R2D/Alikesinaa(the) * cos(b)*sin(l-laa);
		    y = R2D/Alikesinaa(the) * (sin(b)*cos(baa) - cos(b)*sin(baa)*cos(l-laa));

		    i=(int)floor(((-x+(params.mdim/2.))/params.mres));
		    ii=(int)floor(((y+(params.mdim/2.))/params.mres));

		    if (params.inmap(i,ii)) {
			    A[ii][i]+=1;
			}
		}
		break;
		
	    case AG_params::AIT:
		for (long k = 0; k<nrows; ++k) {
			fits_get_colnum(evtFits, 1, "RA", &numcol, &status);
			fits_read_col(evtFits, TDOUBLE, numcol, k+1, 1, 1, NULL, &ra, NULL, &status);
			fits_get_colnum(evtFits, 1, "DEC", &numcol, &status);
			fits_read_col(evtFits, TDOUBLE, numcol, k+1, 1, 1, NULL, &dec, NULL, &status);
		    eulerold(ra, dec, &l, &b, 1);
		    l*=D2R;
		    b*=D2R;
		    the = sin(b)*sin(baa)+cos(b)*cos(baa)*cos(l-laa);
		    if (the < -1.0) {
			    the = PI;
		    } else if (the > 1.0) {
			    the = 0.0;
		    } else {
			    the = acos(the);
		    }
		    l=l-laa;

		    if ( l < PI  ) { 
		      l=-l; 
		    }
		    else { 
		      l=2*PI -l; 
		    }

		    x=R2D*(sqrt(2.0)*2.0*cos(b)*sin(l/2.0))/sqrt(1.0 + cos(b)*cos(l/2.0) ) ;         
		    y=R2D*(sqrt(2.0)*sin(b))/sqrt(1.0 + cos(b)*cos(l/2.0) );

		    i=(int)floor(((x+(params.mdim/2.))/params.mres));
		    ii=(int)floor(((y+(params.mdim/2.))/params.mres));

		    if (params.inmap(i,ii)) {
			    A[ii][i]+=1;
			}
		}
		break;
	}
	
		

	long nelement =  naxes[0] * naxes[1];
	std::cout<< "creating Counts Map...................................." << std::endl;	
	fits_create_img(mapFits, bitpix, naxis, naxes, &status);
	std::cout<< "writinig Counts Map...................................." << std::endl;		
	fits_write_img(mapFits, bitpix, 1, nelement, A, &status);	
	std::cout<< "writing header........................................" << std::endl<< std::endl;	

		
	
	params.write_fits_header(mapFits, status);
	
	
	fits_delete_file(evtFits, &status);
	fits_close_file(mapFits, &status);	
	return status;
}