Esempio n. 1
0
static void add_columns(fitstable_t* tab, anbool write) {
    tfits_type any = fitscolumn_any_type();
    tfits_type d = fitscolumn_double_type();
    tfits_type f = fitscolumn_float_type();
    tfits_type u8 = fitscolumn_u8_type();
    tfits_type i16 = fitscolumn_i16_type();
    tfits_type i32 = fitscolumn_i32_type();
    tfits_type i64 = fitscolumn_i64_type();
    tfits_type i = fitscolumn_int_type();
    tfits_type logical = fitscolumn_boolean_type();
    tfits_type b = fitscolumn_bool_type();
    tfits_type c = fitscolumn_char_type();
    char* nil = " ";
    MatchObj mo;

    ADDCOL(i,  i32, "QUAD",            nil, quadno);
    ADDCOL(u8, u8,  "DIMQUADS",        nil, dimquads);
    ADDARR(i,  i32, "STARS",           nil, star, DQMAX);
    ADDARR(i,  i32, "FIELDOBJS",       nil, field, DQMAX);
    ADDARR(i64,i64, "IDS",             nil, ids, DQMAX);
    ADDCOL(f,  f,   "CODEERR",         nil, code_err);
    ADDARR(d,  d,   "QUADPIX",         nil, quadpix, 2*DQMAX);
    ADDARR(d,  d,   "QUADXYZ",         nil, quadxyz, 3*DQMAX);
    ADDARR(d,  d,   "CENTERXYZ",       nil, center, 3);
    ADDCOL(d,  d,   "RADIUS",          "DEG", radius_deg);
    ADDCOL(i,  i32,   "NMATCH",          nil, nmatch);
    ADDCOL(i  ,i32,   "NDISTRACT",       nil, ndistractor);
    ADDCOL(i  ,i32,   "NCONFLICT",       nil, nconflict);
    ADDCOL(i  ,i32,   "NFIELD",          nil, nfield);
    ADDCOL(i  ,i32,   "NINDEX",          nil, nindex);
    ADDCOL(i  ,i32,   "NAGREE",          nil, nagree);
    //ADDCOL(i16,i16, "BESTI",           nil, besti);
    ADDARR(d,  d,   "CRVAL",           nil, wcstan.crval, 2);
    ADDARR(d,  d,   "CRPIX",           nil, wcstan.crpix, 2);
    ADDARR(d,  d,   "CD",              nil, wcstan.cd, 4);
    ADDCOL(b, logical, "WCS_VALID",    nil, wcs_valid);
    ADDCOL(i,i32,   "FIELDNUM",        nil, fieldnum);
    ADDCOL(i,i32,   "FIELDID",         nil, fieldfile);
    ADDCOL(i16,i16, "INDEXID",         nil, indexid);
    ADDCOL(i16,i16, "HEALPIX",         nil, healpix);
    ADDCOL(i16,i16, "HPNSIDE",         nil, hpnside);
    ADDARR(c,  c,   "FIELDNAME",       nil, fieldname, sizeof(mo.fieldname)-1);
    ADDCOL(b, logical, "PARITY",       nil, parity);
    ADDCOL(i,i32,   "QTRIED",          nil, quads_tried);
    ADDCOL(i,i32,   "QMATCHED",        nil, quads_matched);
    ADDCOL(i,i32,   "QSCALEOK",        nil, quads_scaleok);
    ADDCOL(i16,i16, "QPEERS",          nil, quad_npeers);
    ADDCOL(i,i32,   "NVERIFIED",       nil, nverified);
    ADDCOL(f,  f,   "TIMEUSED",        "s", timeused);
    ADDCOL(f,  f,   "LOGODDS",         nil, logodds);
    ADDCOL(f,  f,   "WORSTLOGODDS",    nil, worstlogodds);
}
Esempio n. 2
0
static void add_columns(fitstable_t* tab, anbool write) {
    tfits_type any = fitscolumn_any_type();
    tfits_type d = fitscolumn_double_type();
    tfits_type f = fitscolumn_float_type();
    tfits_type u8 = fitscolumn_u8_type();
    tfits_type i32 = fitscolumn_i32_type();
    tfits_type J = TFITS_BIN_TYPE_J;
    tfits_type bitfield = fitscolumn_bitfield_type();
    char* nil = " ";

	ADDCOL(d,  d,   "RA",                "deg",  ra);
	ADDCOL(d,  d,   "DEC",               "deg",  dec);
	ADDCOL(f,  f,   "SIGMA_RACOSDEC",    "deg",  sigma_racosdec);
	ADDCOL(f,  f,   "SIGMA_DEC",         "deg",  sigma_dec);
	ADDCOL(f,  f,   "PM_RACOSDEC",       "arcsec/yr", pm_racosdec);
	ADDCOL(f,  f,   "PM_DEC",            "arcsyc/yr", pm_dec);
	ADDCOL(f,  f,   "SIGMA_PM_RACOSDEC", "arcsec/yr", sigma_pm_racosdec);
	ADDCOL(f,  f,   "SIGMA_PM_DEC",      "arcsyc/yr", sigma_pm_dec);
	ADDCOL(f,  f,   "EPOCH_RA",          "yr", epoch_ra);
	ADDCOL(f,  f,   "EPOCH_DEC",         "yr", epoch_dec);
    ADDCOL(f,  f,   "MAG_B",             "mag", mag_B);
    ADDCOL(f,  f,   "MAG_V",             "mag", mag_V);
    ADDCOL(f,  f,   "MAG_R",             "mag", mag_R);
    ADDCOL(f,  f,   "MAG_J",             "mag", mag_J);
    ADDCOL(f,  f,   "MAG_H",             "mag", mag_H);
    ADDCOL(f,  f,   "MAG_K",             "mag", mag_K);
	ADDCOL(i32,J,   "USNOB_ID",           nil, usnob_id);
	ADDCOL(i32,J,   "TWOMASS_ID",         nil, twomass_id);
	ADDCOL(i32,J,   "YB6_ID",             nil, yb6_id);
	ADDCOL(i32,J,   "UCAC2_ID",           nil, ucac2_id);
	ADDCOL(i32,J,   "TYCHO2_ID",          nil, tycho2_id);
    ADDCOL(u8, u8,  "ASTROMETRY_SRC",     nil, astrometry_src);
    ADDCOL(u8, u8,  "BLUE_SRC",           nil, blue_src);
    ADDCOL(u8, u8,  "VISUAL_SRC",         nil, visual_src);
    ADDCOL(u8, u8,  "RED_SRC",            nil, red_src);
	ADDCOL(i32, J,  "NOMAD_ID",           nil, nomad_id);
    ADDARR(bitfield, bitfield, "FLAGS",   nil, flags, 2);
}
int main(int argc, char** args) {
    int argchar;
    kdtree_t* kd;
    int Nleaf = 25;
    char* infn = NULL;
    char* outfn = NULL;
    char* tychofn = NULL;
    char* crossfn = NULL;
	char* progname = args[0];
    FILE* f;

    tycstar_t* tycstars = NULL;
    int Ntyc = 0;

	int exttype  = KDT_EXT_DOUBLE;
	int datatype = KDT_DATA_U32;
	int treetype = KDT_TREE_U32;
	int tt;
	int buildopts = 0;
	int i, N, D;

    dl* ras;
    dl* decs;
    dl* hds;

    fl* mag1s;
    fl* mag2s;
    fl* mag3s;

    int nbad = 0;
    int nox = 0;

    int* hd;
    double* xyz;

    qfits_header* hdr;

    while ((argchar = getopt (argc, args, OPTIONS)) != -1)
        switch (argchar) {
        case 'T':
            tychofn = optarg;
            break;
        case 'X':
            crossfn = optarg;
            break;
        case 'R':
            Nleaf = (int)strtoul(optarg, NULL, 0);
            break;
		case 't':
			treetype = kdtree_kdtype_parse_tree_string(optarg);
			break;
		case 'd':
			datatype = kdtree_kdtype_parse_data_string(optarg);
			break;
		case 'b':
			buildopts |= KD_BUILD_BBOX;
			break;
		case 's':
			buildopts |= KD_BUILD_SPLIT;
			break;
		case 'S':
			buildopts |= KD_BUILD_SPLITDIM;
			break;
        case '?':
            fprintf(stderr, "Unknown option `-%c'.\n", optopt);
        case 'h':
			printHelp(progname);
            return 0;
        default:
            return -1;
        }

    if (optind != argc - 2) {
        printHelp(progname);
        exit(-1);
    }

    infn = args[optind];
    outfn = args[optind+1];

	if (!(buildopts & (KD_BUILD_BBOX | KD_BUILD_SPLIT))) {
		printf("You need bounding-boxes or splitting planes!\n");
		printHelp(progname);
		exit(-1);
	}

    if (tychofn || crossfn) {
        if (!(tychofn && crossfn)) {
            printf("You need both -T <Tycho2> and -X <Crossref> to do cross-referencing.\n");
            exit(-1);
        }
    }

    if (tychofn) {
        int i, N;
        tycho2_fits* tyc;
        FILE* f;
        int nx, nox;
		int lastgrass = 0;

        tyc = tycho2_fits_open(tychofn);
        if (!tyc) {
            ERROR("Failed to open Tycho-2 catalog.");
            exit(-1);
        }
        printf("Reading Tycho-2 catalog...\n");

        N = tycho2_fits_count_entries(tyc);
        tycstars = calloc(N, sizeof(tycstar_t));

        for (i=0; i<N; i++) {
            tycho2_entry* te;
			int grass = (i*80 / N);
			if (grass != lastgrass) {
				printf(".");
				fflush(stdout);
				lastgrass = grass;
			}
			te = tycho2_fits_read_entry(tyc);
            tycstars[i].tyc1 = te->tyc1;
            tycstars[i].tyc2 = te->tyc2;
            tycstars[i].tyc3 = te->tyc3;
            tycstars[i].ra   = te->ra;
            tycstars[i].dec  = te->dec;
            tycstars[i].mag_BT = te->mag_BT;
            tycstars[i].mag_VT = te->mag_VT;
            tycstars[i].mag_HP = te->mag_HP;
        }
        tycho2_fits_close(tyc);

        printf("Sorting...\n");
        qsort(tycstars, N, sizeof(tycstar_t), compare_tycs);
        Ntyc = N;

        f = fopen(crossfn, "rb");
        if (!f) {
            SYSERROR("Failed to open cross-reference file %s", crossfn);
            exit(-1);
        }

        nx = 0;
        nox = 0;
        while (TRUE) {
            char buf[1024];
            int tyc1, tyc2, tyc3, hd, nhd, ntyc;
            char ftyc, sptype0, sptype1, sptype2;
            tycstar_t* s;

            if (!fgets(buf, sizeof(buf), f)) {
                if (ferror(f)) {
                    SYSERROR("Failed to read a line of text from the cross-reference file");
                    exit(-1);
                }
                break;
            }

            if (sscanf(buf, " %d %d %d%c %d %c%c%c %d %d",
                       &tyc1, &tyc2, &tyc3, &ftyc, &hd,
                       &sptype0, &sptype1, &sptype2, &nhd, &ntyc) != 10) {
                ERROR("Failed to parse line: \"%s\"", buf);
            }

            //printf("%i %i %i %i %i %i\n", tyc1, tyc2, tyc3, hd, nhd, ntyc);
            s = find_tycho(tycstars, Ntyc, tyc1, tyc2, tyc3);
            if (!s) {
                ERROR("Failed to find Tycho-2 star %i-%i-%i", tyc1, tyc2, tyc3);
                nox++;
            } else {
                s->hd = hd;
                s->ntyc = ntyc;
            }
            nx++;
        }
        fclose(f);

        printf("Read %i cross-references.\n", nx);
        printf("Failed to find %i cross-referenced Tycho-2 stars.\n", nox);

        printf("Sorting...\n");
        qsort(tycstars, N, sizeof(tycstar_t), compare_hds);
    }

    f = fopen(infn, "rb");
    if (!f) {
        SYSERROR("Failed to open input file %s", infn);
        exit(-1);
    }

    ras = dl_new(1024);
    decs = dl_new(1024);
    hds = il_new(1024);

    mag1s = fl_new(1024);
    mag2s = fl_new(1024);
    mag3s = fl_new(1024);

    printf("Reading HD catalog...\n");
    for (;;) {
        char buf[1024];
        double ra, dec;
        int hd;
        float mag1, mag2, mag3;

        mag1 = mag2 = mag3 = 0.0;

        if (!fgets(buf, sizeof(buf), f)) {
            if (ferror(f)) {
                SYSERROR("Failed to read a line of text from the input file");
                exit(-1);
            }
            break;
        }

        if (buf[0] == '#')
            continue;
        if (buf[0] == '\n')
            continue;

        if (sscanf(buf, " %lf| %lf| %d", &ra, &dec, &hd) < 3) {
            // ignore three invalid lines
            if (nbad > 3) {
                ERROR("Failed to parse line: \"%s\"", buf);
            }
            nbad++;
        } else {

            if (tycstars) {
                tycstar_t* s = find_hd(tycstars, Ntyc, hd);
                if (!s) {
                    //printf("Failed to find cross-ref for HD %i\n", hd);
                    nox++;
                } else {
                    ra = s->ra;
                    dec = s->dec;

                    mag1 = s->mag_VT;
                    mag2 = s->mag_BT;
                    mag3 = s->mag_HP;
                }
            }

            dl_append(ras, ra);
            dl_append(decs, dec);
            il_append(hds, hd);
            fl_append(mag1s, mag1);
            fl_append(mag2s, mag2);
            fl_append(mag3s, mag3);
        }
    }
    fclose(f);

    N = dl_size(ras);
    printf("Read %i entries and %i bad lines.\n", N, nbad);

    if (dl_size(ras) != HD_NENTRIES) {
        printf("WARNING: expected %i Henry Draper catalog entries.\n", HD_NENTRIES);
    }

    if (nox) {
        printf("Found %i HD entries with no cross-reference (expect this to be about 1%%)\n", nox);
    }

    hd = malloc(sizeof(int) * N);
    il_copy(hds, 0, N, hd);
    il_free(hds);
    for (i=0; i<N; i++)
        if (hd[i] != i+1) {
            printf("Line %i is HD %i\n", i+1, hd[i]);
            break;
        }
    // HACK  - don't allocate 'em in the first place...
    free(hd);

    xyz = malloc(sizeof(double) * 3 * N);
    for (i=0; i<N; i++) {
        radecdeg2xyzarr(dl_get(ras, i), dl_get(decs, i), xyz + 3*i);
    }

    dl_free(ras);
    dl_free(decs);

	tt = kdtree_kdtypes_to_treetype(exttype, treetype, datatype);
	D = 3;
	{
		// limits of the kdtree...
        double lo[] = {-1.0, -1.0, -1.0};
        double hi[] = { 1.0,  1.0,  1.0};
        kd = kdtree_new(N, D, Nleaf);
        kdtree_set_limits(kd, lo, hi);
	}
	printf("Building tree...\n");
	kd = kdtree_build(kd, xyz, N, D, Nleaf, tt, buildopts);

    hdr = qfits_header_default();
    qfits_header_add(hdr, "AN_FILE", "HDTREE", "Henry Draper catalog kdtree", NULL);
    BOILERPLATE_ADD_FITS_HEADERS(hdr);
    fits_add_long_history(hdr, "This file was created by the following command-line:");
    fits_add_args(hdr, args, argc);

    if (kdtree_fits_write(kd, outfn, hdr)) {
        ERROR("Failed to write kdtree");
        exit(-1);
    }

    // Write mags as tag-along table.
    {
        fitstable_t* tag;
        tag = fitstable_open_for_appending(outfn);
        if (!tag) {
            ERROR("Failed to open kd-tree file for appending");
            exit(-1);
        }

        fitstable_add_write_column(tag, fitscolumn_float_type(), "MAG_VT", "");
        fitstable_add_write_column(tag, fitscolumn_float_type(), "MAG_BT", "");
        fitstable_add_write_column(tag, fitscolumn_float_type(), "MAG_HP", "");

        if (fitstable_write_header(tag)) {
            ERROR("Failed to write tag-along header");
            exit(-1);
        }

        for (i=0; i<N; i++) {
            fitstable_write_row(tag, fl_get(mag1s, i), fl_get(mag2s, i),
                                fl_get(mag3s, i));
        }
        if (fitstable_fix_header(tag)) {
            ERROR("Failed to fix tag-along header");
            exit(-1);
        }
        if (fitstable_close(tag)) {
            ERROR("Failed to close tag-along data");
            exit(-1);
        }
    }
    fl_free(mag1s);
    fl_free(mag2s);
    fl_free(mag3s);

    printf("Done.\n");

	qfits_header_destroy(hdr);
    free(xyz);
    kdtree_free(kd);
    free(tycstars);

    return 0;
}
Esempio n. 4
0
int loadltcube(char *filename, LTCUBE_DATA_STRUCT *ltcubeds) {
  fitstable_t* tab;
  qfits_header* hdr;
  tfits_type flt = fitscolumn_float_type();
  
  if ( (tab = fitstable_open(filename))==NULL) {
    printf("# Cannot open %s in loadltcube() %s:%d\n",filename,__FILE__,__LINE__);
    return -1;
  }
  fitstable_open_next_extension(tab);
  hdr = fitstable_get_header(tab);
  strncpy(ltcubeds->ordering,qfits_header_getstr(hdr, "ORDERING"),sizeof(ltcubeds->ordering));
  strncpy(ltcubeds->coordsys,qfits_header_getstr(hdr, "COORDSYS"),sizeof(ltcubeds->coordsys));
  strncpy(ltcubeds->thetabin,qfits_header_getstr(hdr, "THETABIN"),sizeof(ltcubeds->thetabin));

  ltcubeds->nside=qfits_header_getint(hdr,"NSIDE",-1);
  ltcubeds->firstpix=qfits_header_getint(hdr,"FIRSTPIX",-1);
  ltcubeds->lastpix=qfits_header_getint(hdr,"LASTPIX",-1);
  if (debug) { printf("ORDERING= %s; COORDSYS= %s; THETABIN= %s; NSIDE= %d FIRSTPIX= %d LASTPIX= %d\n",
		      ltcubeds->ordering,
		      ltcubeds->coordsys,
		      ltcubeds->thetabin,
		      ltcubeds->nside,
		      ltcubeds->firstpix,
		      ltcubeds->lastpix);
  }


  /* load in livetime cube data */
  ltcubeds->nMu = fitstable_get_array_size(tab, "COSBINS");
  ltcubeds->ncosbins = fitstable_nrows(tab);
  printf("# nrows= %d\n",ltcubeds->ncosbins);
  if ( (ltcubeds->cosbins= (float *) fitstable_read_column_array(tab, "COSBINS", flt))==NULL) {
    printf("# cannot load vector COSBINS from %s in loadltcude() %s:%d\n",filename,__FILE__,__LINE__);
    fitstable_close(tab);
    return -1;
  }

  fitstable_open_next_extension(tab);

  if ( (ltcubeds->cthetamin= (float *) fitstable_read_column(tab, "CTHETA_MIN", flt))==NULL) {
    printf("# cannot load vector CTHETA_MIN from %s in loadltcude() %s:%d\n",filename,__FILE__,__LINE__);
    fitstable_close(tab);
    return -1;
  }

  if ( (ltcubeds->cthetamax= (float *) fitstable_read_column(tab, "CTHETA_MAX", flt))==NULL) {
    printf("# cannot load vector CTHETA_MAX from %s in loadltcude() %s:%d\n",filename,__FILE__,__LINE__);
    fitstable_close(tab);
    return -1;
  }

  if ( (ltcubeds->aeffmubin = (int *) calloc(ltcubeds->nMu,sizeof(int)))==NULL) {
    printf("# cannot allocate vector AEFFMUBIN in loadltcude() %s:%d\n",__FILE__,__LINE__);
    fitstable_close(tab);
    return -1;
  }

  ltcubeds->aeffmubin[0]=-99;

  if (debug>2) {
    int i, j;
    printf("# nMu= %d\n",ltcubeds->nMu);
    for (i=0;i<ltcubeds->nMu;i++) {
      printf("%3d %10g %10g\n",i,ltcubeds->cthetamin[i],ltcubeds->cthetamax[i]);
    }
    for (j=0;j<10;j++) {
      printf("j= %d",j);
      for (i=0;i<ltcubeds->nMu;i++) {
	printf(" %10g",ltcubeds->cosbins[j*ltcubeds->nMu+i]);
      }
      printf("\n");
    }
  }

  printf("# loaded %s\n",filename);
  return 0;
}
int
loadphotondata(char *filename, char *passfile) {
    fitstable_t* tab;
    qfits_header* hdr;
    tfits_type flt = fitscolumn_float_type(), chtype = fitscolumn_char_type(), dbltype=fitscolumn_double_type();
    float *localphotondata[NPHOTON_LOADDATA], *galdisrsp=NULL, *isodisrsp=NULL;
    double *localphotontime;
    char *conversion_type=NULL;
    char buffer[255];
    int i, j, ncurr, ndiff, nresp=0, conv_type, psf_cnt, psf_ind;
    double rmax2, ehold, rad, r2hold;

    if ( (tab = fitstable_open(filename))==NULL) {
        printf("# Cannot open %s in loadphotondata() %s:%d\n",filename,__FILE__,__LINE__);
        return -1;
    }
    hdr = fitstable_get_primary_header(tab);
    hdr = fitstable_get_header(tab);
    ncurr = fitstable_nrows(tab);
    if (debug) printf("# filename= %s ncurr= %d\n",filename,ncurr);
    /* load in the energy, theta, ra and dec */
    for (i=0; i<NPHOTON_LOADDATA; i++) {
        if ( (localphotondata[i]=
                    (float *) fitstable_read_column_array(tab,
                            photon_colname[i], flt))==NULL) {
            printf("# vector for %s is NULL in loadphotondata() %s:%d\n",photon_colname[i],__FILE__,__LINE__);
            for (j=0; j<i; j++) {
                SAFEFREE( localphotondata[i]);
            }
            fitstable_close(tab);
            return -1;
        }
    }

    /* load in conversion type */
    if ( (conversion_type= (char *) fitstable_read_column(tab, "CONVERSION_TYPE", chtype))==NULL) {
        printf("# array for conversion_type is NULL in loadphotondata() %s:%d\n",__FILE__,__LINE__);
        for (i=0; i<NPHOTON_LOADDATA; i++) {
            SAFEFREE( localphotondata[i]);
        }
        fitstable_close(tab);
        return -1;
    }

    /* load in photon arrival time */
    if ( (localphotontime= (double *) fitstable_read_column(tab, "TIME", dbltype))==NULL) {
        printf("# array for localphotontime is NULL in loadphotondata() %s:%d\n",__FILE__,__LINE__);
        for (i=0; i<NPHOTON_LOADDATA; i++) {
            SAFEFREE( localphotondata[i]);
        }
        SAFEFREE( conversion_type);
        fitstable_close(tab);
        return -1;
    }

    /* load in the diffuse response functions that match the passfile */
    ndiff = qfits_header_getint(hdr, "NDIFRSP",-1);
    for (i=0; i<ndiff; i++) {
        sprintf(buffer,"DIFRSP%d",i);
        char *respname = qfits_header_getstr(hdr, buffer);
        if (debug) printf("# %d %s = %s\n",i,buffer,respname);
        if (strcasestr(respname,passfile)) {
            if (strcasestr(respname,"gll")) {
                if (debug) printf("# galactic is %s\n",buffer);
                if ( (galdisrsp=
                            (float *) fitstable_read_column_array(tab,
                                    buffer, flt))==NULL) {
                    printf("# array for %s is NULL in loadphotondata() %s:%d\n",buffer,__FILE__,__LINE__);
                    for (j=0; j<i; j++) {
                        SAFEFREE( localphotondata[i]);
                    }
                    SAFEFREE( conversion_type);
                    SAFEFREE(localphotontime);
                    SAFEFREE( galdisrsp);
                    SAFEFREE( isodisrsp);
                    fitstable_close(tab);
                    return -1;
                }
                nresp++;
            }
            if (strcasestr(respname,"iso")) {
                if (debug) printf("# iso is %s\n",buffer);
                if ( (isodisrsp=
                            (float *) fitstable_read_column_array(tab,
                                    buffer, flt))==NULL) {
                    printf("# array for %s is NULL in loadphotondata() %s:%d\n",buffer,__FILE__,__LINE__);
                    for (j=0; j<i; j++) {
                        SAFEFREE( localphotondata[i]);
                    }
                    SAFEFREE( conversion_type);
                    SAFEFREE(localphotontime);
                    SAFEFREE( galdisrsp);
                    SAFEFREE( isodisrsp);
                    fitstable_close(tab);
                    return -1;
                }
                nresp++;
            }
        }
    }

    if (nresp<2) {
        printf("Could not find the matching response functions in loadphotondata() %s:%d.\n",__FILE__,__LINE__);
        return -1;
    }

    if (alloc_globals(ncurr+ntot)) {
        SAFEFREE(localphotontime);
        SAFEFREE(conversion_type);
        SAFEFREE(data);
        for (i=0; i<NPHOTON_LOADDATA; i++) {
            SAFEFREE(localphotondata[i]);
        }
        return -1;
    }
    fitstable_close(tab);

    j=0;
    for (i=0; i<ncurr; i++) {
        ehold=localphotondata[ENERGY][i];
        if (ehold>=e0 && ehold<=e1) {
            double ra, dec;
            __sincospi((ra=localphotondata[RA][i])/180.0,data+d*(j+ntot)+1,data+d*(j+ntot));
            __sincospi((dec=localphotondata[DEC][i])/180.0,data+d*(j+ntot)+2,&rad);
            data[d*(ntot+j)]*=rad;
            data[d*(ntot+j)+1]*=rad;
            if (ehold<energy_min) energy_min=ehold;
            /* calculate RMAX2 for this photon */
            conv_type=conversion_type[i];
            rmax2=photondata[COSTHETA][j+ntot]=cos(localphotondata[THETA][i]*M_PI/180.0);
            if (debug>3) printf("# costheta= %g\n",rmax2);
#if 1
            /* fast way --- evenly spaced in log energy and costheta -- no interpolation */
            //      rmax2=psfds.psfdata[conv_type][RMAX2][(int) ((rmax2-psfds.mumin)/psfds.mustep)*psfds.nE + (int) ((log10(ehold)-psfds.lemin)/psfds.lestep)];
            rmax2=rmax2_funk(psfds,conv_type,rmax2,ehold);
#else
            /* slow way --- find the right bin -- no interpolation */
            for (psf_cnt=0; psf_cnt<psfds.nE; psf_cnt++) {
                if (ehold>psfds.psfdata[conv_type][ENERG_LO][psf_cnt] && ehold<=psfds.psfdata[conv_type][ENERG_HI][psf_cnt]) {
                    psf_ind=psf_cnt;
                    break;
                }
            }
            for (psf_cnt=0; psf_cnt<psfds.nMu; psf_cnt++) {
                if (rmax2>psfds.psfdata[conv_type][CTHETA_LO][psf_cnt] && rmax2<=psfds.psfdata[conv_type][CTHETA_HI][psf_cnt]) {
                    psf_ind+=psf_cnt*psfds.nE;
                    break;
                }
            }
            rmax2=psfds.psfdata[conv_type][RMAX2][psf_ind];
#endif
            if (debug>3) printf("# rmax2= %g\n",rmax2);
            if (conv_type) {
                /* back conversion */
                r2hold=SPE_squared_back(ehold)*rmax2;
                data[d*(ntot+j)+3]=-(r2hold>4 ? 4 : r2hold);
            } else {
                /* front conversion */
                r2hold=SPE_squared_front(ehold)*rmax2;
                data[d*(ntot+j)+3]=(r2hold>4 ? 4 : r2hold);
            }
            if (debug>3) printf("# %g rmax2= %g %g %g\n",ehold,data[d*(ntot+j)+3],SPE_squared_front(ehold),localphotondata[THETA][i]);

            photondata[ENERGY][j+ntot]=ehold;
            photondata[DIFRSP_GAL][j+ntot]=galdisrsp[i];
            photondata[DIFRSP_ISO][j+ntot]=isodisrsp[i];
            r2hold=aeffds.aeffdata[conv_type][AEFF_EFF_AREA]
                   [(int) ((photondata[COSTHETA][j+ntot]-aeffds.mumin)/aeffds.mustep)*aeffds.nE + (int) ((log10(ehold)-aeffds.lemin)/aeffds.lestep)];
            if (r2hold<EFFAREAMIN) r2hold=EFFAREAMIN;
            photondata[EFFAREA][j+ntot]=r2hold;

            /* calculate the effective area integral */
            r2hold=calcefft(ra,dec,ehold,&aeffds,&ltcubeds);
            if (r2hold<EFFAREATMIN) r2hold=EFFAREATMIN;
            photondata[EFFAREAT][j+ntot]=r2hold;

            photontime[j+ntot]=localphotontime[i];
            j++;
        }
    }

    for (i=0; i<NPHOTON_LOADDATA; i++) {
        SAFEFREE(localphotondata[i]);
    }
    SAFEFREE( galdisrsp);
    SAFEFREE( isodisrsp);
    SAFEFREE(conversion_type);
    SAFEFREE(localphotontime);

    ntot+=j;
    if (debug) printf("# ntot= %d j= %d\n",ntot,j);
    if (j!=ncurr) {
        if (alloc_globals(ncurr+ntot)) {
            SAFEFREE(data);
            for (i=0; i<NPHOTON_LOADDATA; i++) {
                SAFEFREE(localphotondata[i]);
            }
            return -1;
        }
    }

    return 0;
}
Esempio n. 6
0
int
loadpsffile(char *passfile, PSF_DATA_STRUCT *psfds, float f_cutoff) {
  char filename[2][255]={"front.fits","back.fits"};
  char buffer[255];
  int i, j, f, ncurr;
  float *psfscale;
  fitstable_t* tab;
  qfits_header* hdr;
  tfits_type flt = fitscolumn_float_type();
  float ***psfdata;
  int nE, nMu;

  if ( !(psfdata=(float ***) calloc(2,sizeof(float **))) ) {
    printf("# Cannot allocate psfdata in loadpsffile() %s:%d\n",__FILE__,__LINE__);
    return -1;
  }

  if ( !(psfdata[0]=(float **) calloc(NPSF_DATA,sizeof(float *))) ) {
    printf("# Cannot allocate psfdata[0] in loadpsffile() %s:%d\n",__FILE__,__LINE__);
    return -1;
  }

  if ( !(psfdata[1]=(float **) calloc(NPSF_DATA,sizeof(float *))) ) {
    printf("# Cannot allocate psfdata[1] in loadpsffile() %s:%d\n",__FILE__,__LINE__);
    return -1;
  }


  psfds->psfdata=psfdata;
  psfds->f_cutoff=f_cutoff;

  if (debug>2) {
    printf("      ");
    for (i=0;i<NPSF_DATA;i++) {
      printf(" %10s",psf_colname[i]);
    }
    printf("\n");
  }

  for (f=0;f<2;f++) {
    sprintf(buffer,"psf_%s_%s",passfile,filename[f]);
    printf("# loading PSF file: %s\n",buffer);
    if ( (tab = fitstable_open(buffer))==NULL) {
      printf("# Cannot open %s in loadpsffile() %s:%d\n",buffer,__FILE__,__LINE__);
      return -1;
    }
    hdr = fitstable_get_primary_header(tab);
    ncurr = fitstable_nrows(tab);
    for (i=ENERG_LO;i<=GTAIL;i++) {
      if ( (psfdata[f][i]= (float *) fitstable_read_column_array(tab, psf_colname[i], flt))==NULL) {
	printf("# Cannot file column %s (file=%s) in loadpsffile() %s:%d\n",psf_colname[i],buffer,__FILE__,__LINE__);
	fitstable_close(tab);
	return -1;
      }
    }
    int D = fitstable_get_array_size(tab, psf_colname[NTAIL]);
    psfds->nE = nE = fitstable_get_array_size(tab, psf_colname[ENERG_LO]);
    psfds->nMu = nMu = fitstable_get_array_size(tab, psf_colname[CTHETA_LO]);
    for (i=FCORE;i<=RMAX2;i++) {
      if (!(psfdata[f][i]=(float *) calloc(D,sizeof(float)))) {
	printf("# Cannot allocate psfdata[%d][%d] in loadpsffile() %s:%d\n",f,i,__FILE__,__LINE__);
	return -1;
      }
    }

    for (j=0;j<D;j++) {
      if (debug>2) printf("[%2d] %2d",f,j);
      score_g=psfdata[f][SCORE][j];
      gcore_g=psfdata[f][GCORE][j];
      stail_g=psfdata[f][STAIL][j];
      gtail_g=psfdata[f][GTAIL][j];
      psfdata[f][FACTORTAIL][j]=factortail_g=gtail_g*stail_g*stail_g*2;
      psfdata[f][FACTORCORE][j]=factorcore_g=gcore_g*score_g*score_g*2;
      fcore_g=stail_g/score_g;
      psfdata[f][FCORE][j]=fcore_g=1/(1+psfdata[f][NTAIL][j]*fcore_g*fcore_g);
      psfdata[f][NORMCORE][j]=fcore_g/(2*M_PI*score_g*score_g)*(1-1/gcore_g);
      psfdata[f][NORMTAIL][j]=(1-fcore_g)/(2*M_PI*stail_g*stail_g)*(1-1/gtail_g); ;
      psfdata[f][RMAX2][j]=root(f_cutoff);
      if (debug>2) {
	for (i=0;i<2;i++) {
	  printf(" %10g",log10(psfdata[f][i][j%nE]));
	}
	for (i=2;i<4;i++) {
	  printf(" %10g",psfdata[f][i][(j/nE)%nMu]);
	}
	for (i=4;i<NPSF_DATA;i++) {
	  printf(" %10g",psfdata[f][i][j]);
	}
	printf(" %10g\n",ickingfunk(psfdata[f][RMAX2][j]));
      }
    }
    if (f==0) {
      psfds->lemin=log10(psfdata[f][ENERG_LO][0]);
      psfds->lestep=log10(psfdata[f][ENERG_HI][0])-log10(psfdata[f][ENERG_LO][0]);
      psfds->mumin=psfdata[f][CTHETA_LO][0];
      psfds->mustep=psfdata[f][CTHETA_HI][0]-psfdata[f][CTHETA_LO][0];
      fitstable_open_next_extension(tab);
      hdr = fitstable_get_primary_header(tab);
      ncurr = fitstable_nrows(tab);
      if ( (psfscale= (float *) fitstable_read_column_array(tab, "PSFSCALE", flt))==NULL) {
	printf("# Cannot read PSFSCALE (file=%s) in loadpsffile() %s:%d\n",buffer,__FILE__,__LINE__);
	fitstable_close(tab);
	return -1;
      }
      c0front2=psfscale[0]*psfscale[0];
      c1front2=psfscale[1]*psfscale[1];
      c0back2=psfscale[2]*psfscale[2];
      c1back2=psfscale[3]*psfscale[3];
      minus2beta=2*psfscale[4];
      if (debug>1) { printf("# %g %g %g %g %g\n",c0front2,c1front2,c0back2,c1back2,minus2beta); }
    }
    fitstable_close(tab);
  }
  free((void *) psfscale);
  return 0;
}