void c_tbegtr (const IRAFPointer tp, const IRAFPointer cp, int row, float *buffer) { /* Read a value of type float from a table column. arguments: IRAFPointer tp i: table descriptor IRAFPointer cp i: column descriptor int row i: row number (one indexed) float *buffer o: value read from table */ TableDescr *tbl_descr; ColumnDescr *col_descr; int anynul=0; long firstelem=1, nelem=1; float nulval=IRAF_INDEFR; int status = 0; tbl_descr = (TableDescr *)tp; col_descr = (ColumnDescr *)cp; if (col_descr->datatype < 0) { char *value; int maxch; maxch = col_descr->width + 5; value = (char *)calloc (maxch, sizeof(char)); c_tbegtt (tp, cp, row, value, maxch); if (strcmp (value, "INDEF") == 0) *buffer = IRAF_INDEFR; else *buffer = atof (value); free (value); } else if (col_descr->datatype == IRAF_INT) { int i_value; c_tbegti (tp, cp, row, &i_value); if (i_value == IRAF_INDEFI) *buffer = IRAF_INDEFR; else *buffer = i_value; } else if (col_descr->datatype == IRAF_SHORT) { short si_value; c_tbegts (tp, cp, row, &si_value); if (si_value == IRAF_INDEFS) *buffer = IRAF_INDEFR; else *buffer = si_value; } else { /* fits_read_col_flt = ffgcve */ fits_read_col_flt (tbl_descr->fptr, col_descr->colnum, (long)row, firstelem, nelem, nulval, buffer, &anynul, &status); if (status != 0) setError (status, "c_tbegtr: error reading element"); } }
int extractFoldData(fitsfile *fp,dSet *data,float dm,float *fx,float *fy,float *freq_y,float *time_y,float *bpass, int sub0) { int n=0; int status=0; int colnum; int i,j,k,l; int initflag=0; float nval=0; float ty[data->phead.nbin]; float **offs; // [data->phead.nsub]; float **dat_scl; // [data->phead.nsub]; double f0,chanbw,tdelay; int bn,cdelay; double bintime; int addDelay = 0; //500; // float bpass[data->phead.nchan*2]; float bpass_offs[2]; float bpass_scl[2]; float meanVal,rmsVal; // get mean/RMS of off-pulse for scaling. no longer need OFFS/DAT_SCL printf("sub0 = %d\n",sub0); if (dm < 0) dm = data->phead.dm; // Need to remove a baseline from each polarisation channel before summing // Get first frequency channel // Central frequency f0 = data->phead.freq; //-data->phead.chanbw*data->phead.nchan/2; // chanbw = data->phead.bw/data->phead.nchan; chanbw = data->phead.chanbw; bintime = (double)data->phead.period/(double)data->phead.nbin; fits_movnam_hdu(fp,BINARY_TBL,"BANDPASS",1,&status); if (status) { printf("Unable to move to bandpass table in FITS file\n"); exit(1); } fits_get_colnum(fp,CASEINSEN,"DAT_OFFS",&colnum,&status); fits_read_col_flt(fp,colnum,1,1,2,nval,bpass_offs,&initflag,&status); fits_get_colnum(fp,CASEINSEN,"DAT_SCL",&colnum,&status); fits_read_col_flt(fp,colnum,1,1,2,nval,bpass_scl,&initflag,&status); // Now read the bandpass fits_get_colnum(fp,CASEINSEN,"DATA",&colnum,&status); // NOTE: Starting at element 2 as element 1 is junk fits_read_col_flt(fp,colnum,1,2,data->phead.nchan,nval,bpass,&initflag,&status); fits_read_col_flt(fp,colnum,1,3+data->phead.nchan,data->phead.nchan,nval,bpass+data->phead.nchan,&initflag,&status); for (i=0;i<data->phead.nchan*2;i++) { if (i<data->phead.nchan) bpass[i] = bpass[i]*bpass_scl[0] + bpass_offs[0]; else bpass[i] = bpass[i]*bpass_scl[1] + bpass_offs[1]; // printf("bpass: %d %g %g %g %g %g \n",i,bpass[i],bpass_scl[0],bpass_scl[1],bpass_offs[0],bpass_offs[1]); } // exit(1); fits_movnam_hdu(fp,BINARY_TBL,"SUBINT",1,&status); if (status) { printf("Unable to move to subint table in FITS file\n"); exit(1); } // REMOVED: No longer need dat_scl or offs. OFFS/DAT_SCL // offs = (float **)malloc(sizeof(float *)*data->phead.nsub); // dat_scl = (float **)malloc(sizeof(float *)*data->phead.nsub); // for (i=0;i<data->phead.nsub;i++) // { // offs[i] = (float *)malloc(sizeof(float)*data->phead.nchan*data->phead.npol); // dat_scl[i] = (float *)malloc(sizeof(float)*data->phead.nchan*data->phead.npol); // } // fits_get_colnum(fp,CASEINSEN,"DAT_OFFS",&colnum,&status); // if (status) { // printf("Unable to find DAT_OFFS in the subint table in FITS file\n"); // exit(1); // } // for (i=0;i<data->phead.nsub;i++) // { // fits_read_col_flt(fp,colnum,i+1,1,data->phead.nchan*data->phead.npol,nval,offs[i],&initflag,&status); // // printf("offs = %g\n",offs[i][5]); // // offs[i] =0; // } // // fits_get_colnum(fp,CASEINSEN,"DAT_SCL",&colnum,&status); // if (status) { // printf("Unable to find DAT_SCL in the subint table in FITS file\n"); // exit(1); // } // for (i=0;i<data->phead.nsub;i++) // { // fits_read_col_flt(fp,colnum,i+1,1,data->phead.nchan*data->phead.npol,nval,dat_scl[i],&initflag,&status); // // printf("dat_scl = %g\n",dat_scl[i][5]); // // dat_scl[i]=1.0; // } fits_get_colnum(fp,CASEINSEN,"DATA",&colnum,&status); if (status) { printf("Unable to find data in the subint table in FITS file\n"); exit(1); } if (sub0==0) { for (i=0;i<data->phead.nbin;i++) { fx[i] = i; fy[i] = 0; } // printf("Loading %d subintegrations\n",data->phead.nsub); printf("Number of frequency channels = %d\n",data->phead.nchan); printf("Number of polarisations = %d\n",data->phead.npol); for (i=0;i<data->phead.nchan*data->phead.nbin;i++) freq_y[i] = 0; } for (i=sub0;i<data->phead.nsub;i++) // *data->phead.nbin;i++) { for (j=0;j<data->phead.nbin;j++) time_y[i*data->phead.nbin+j] = 0; } for (l=sub0;l<data->phead.nsub;l++) { for (j=0;j<data->phead.npol && j < 2;j++) // Do not add cross terms! { for (i=0;i<data->phead.nchan;i++) { // Must calculate the frequency of this channel // ... calculate the delay caused by the DM // ... dedisperse the subintegration // tdelay = 4.15e-3*dm*(pow(f0/1000.0,-2)-pow((f0+chanbw*i)/1000.0,-2)); tdelay = 4.15e-3*dm*(pow(f0/1000.0,-2)-pow((f0+(chanbw*i-chanbw*data->phead.nchan/2.0))/1000.0,-2)); cdelay = nint(-tdelay/bintime); // if (l==0 && j==0) // printf("Have %g %g %g %d %d\n",dm,f0,f0+chanbw*i,i,cdelay); fits_read_col_flt(fp,colnum,l+1,j*(data->phead.nchan*data->phead.nbin)+i*data->phead.nbin+1,data->phead.nbin,nval,ty,&initflag,&status); meanVal=0; //for (k=0;k<data->phead.nbin;k++) //{ // ty[k] = ((ty[k]+offs[l][j*data->phead.nchan+i])*dat_scl[l][j*data->phead.nchan+i]); //+offs[l][j*data->phead.nchan+i]); /* if (j==0) ty[k] -= bpass[i]; else if (j==1) ty[k] -= bpass[data->phead.nchan+i]; else { ty[k] -= sqrt(bpass[data->phead.nchan+i]*bpass[i]); } */ //meanVal+=ty[k]; // } getbaseline(ty,data->phead.nbin,0.35,&meanVal,&rmsVal); if (rmsVal == 0.0 ) rmsVal=1.0; // printf("Val = %g\n",ty[10]); for (k=0;k<data->phead.nbin;k++) { // if (i==10 && l==10) // printf("Orig value = %g\n",ty[k]); //ty[k] = ((ty[k]+offs[l][j*data->phead.nchan+i])*dat_scl[l][j*data->phead.nchan+i]); //+offs[l][j*data->phead.nchan+i]); ty[k] -= meanVal; ty[k] /= rmsVal; // Subtract bandpass /* if (j==0) ty[k] -= bpass[i]; else if (j==1) ty[k] -= bpass[data->phead.nchan+i]; else { ty[k] -= sqrt(bpass[data->phead.nchan+i]*bpass[i]); } */ // if (l==10) // printf("New value = %g\n",ty[k]); bn = k-cdelay + addDelay; // bn = nint(fmod(k-tdelay/bintime,data->phead.nbin)); while (bn >= data->phead.nbin) bn -= data->phead.nbin; while (bn < 0) bn += data->phead.nbin; freq_y[i*data->phead.nbin+k]+=(ty[k]); ///(float)(data->phead.npol*data->phead.nsub)); time_y[l*data->phead.nbin+bn]+=(ty[k]);///(float)(data->phead.npol*data->phead.nchan)); // printf("timey = %g\n",time_y[l*data->phead.nbin+bn]); fy[bn]+=(ty[k]/(float)(data->phead.nchan*data->phead.npol*data->phead.nsub)); } } } } // REMOVED: OFFS/DAT_SCL table no longer needed. //for (i=0;i<data->phead.nsub;i++) // { // free(offs[i]); // free(dat_scl[i]); // } //free(offs); //free(dat_scl);*/ printf("Status = %d\n",status); }
int HEALPixIn(struct healpix *hpxdat) { char crdsys[32], ordering[32]; int anynul, hdutype, iaxis, nfound, status; long firstpix, ipix, lastpix, naxis, *naxes = 0x0, nside = 0, repeat; float *datap, nulval; LONGLONG firstelem, irow, nelem, npix = 0, nrow = 0; fitsfile *fptr; status = 0; hpxdat->data = 0x0; /* Open the FITS file and move to the first HDU with NAXIS != 0. */ if (fits_open_data(&fptr, hpxdat->infile, READONLY, &status)) goto fitserr; /* Is this the primary HDU or an extension? */ if (fits_get_hdu_type(fptr, &hdutype, &status)) goto fitserr; if (!(hdutype == IMAGE_HDU || hdutype == BINARY_TBL)) { fprintf(stderr, "ERROR: %s does not contain HEALPix data.\n", hpxdat->infile); return 1; } /* Get the image size. */ if (fits_read_key_lng(fptr, "NAXIS", &naxis, 0x0, &status)) goto fitserr; naxes = malloc(naxis*sizeof(long)); if (fits_read_keys_lng(fptr, "NAXIS", 1, (int)naxis, naxes, &nfound, &status)) goto fitserr; if (hdutype == IMAGE_HDU) { /* Look for the first non-degenerate image axis. */ for (iaxis = 0; iaxis < nfound; iaxis++) { if (naxes[iaxis] > 1) { /* Assume for now that it is the total number of pixels. */ npix = naxes[iaxis]; break; } } } else if (hdutype == BINARY_TBL) { /* Binary tables are simpler. */ if (nfound > 1) nrow = naxes[1]; /* (Note that fits_get_coltypell() is not available in cfitsio 2.x.) */ if (fits_get_coltype(fptr, hpxdat->col, 0x0, &repeat, 0x0, &status)) { goto fitserr; } nelem = (LONGLONG)repeat; } if (!npix && !nrow) { fprintf(stderr, "ERROR: Could not determine image size.\n"); goto cleanup; } /* Number of pixels per side of each base-resolution pixel. */ if (fits_read_key_lng(fptr, "NSIDE", &nside, 0x0, &status)) { /* Some HEALPix files, e.g. SFD dust maps, don't record NSIDE. */ if (status != KEY_NO_EXIST) goto fitserr; status = 0; } /* FIRSTPIX and LASTPIX, if present, record the 0-relative pixel numbers of * the first and last pixels stored in the data. */ firstpix = -1; if (fits_read_key_lng(fptr, "FIRSTPIX", &firstpix, 0x0, &status)) { if (status != KEY_NO_EXIST) goto fitserr; status = 0; } lastpix = -1; if (fits_read_key_lng(fptr, "LASTPIX", &lastpix, 0x0, &status)) { if (status != KEY_NO_EXIST) goto fitserr; status = 0; } if (!nside) { /* Deduce NSIDE. */ if (lastpix >= 0) { /* If LASTPIX is present without NSIDE we can only assume it's npix. */ nside = (int)(sqrt((double)((lastpix+1) / 12)) + 0.5); } else if (npix) { nside = (int)(sqrt((double)(npix / 12)) + 0.5); } else if (nrow) { nside = (int)(sqrt((double)((nrow * nelem) / 12)) + 0.5); } } hpxdat->nside = (int)nside; hpxdat->npix = 12*nside*nside; /* Ensure that FIRSTPIX and LASTPIX are set. */ if (firstpix < 0) firstpix = 0; if (lastpix < 0) lastpix = hpxdat->npix - 1; /* Any sign of a coordinate system identifier? */ if (fits_read_key_str(fptr, "COORDSYS", crdsys, 0x0, &status)) { if (status != KEY_NO_EXIST) goto fitserr; status = 0; } else if (crdsys[0] == 'G') { hpxdat->crdsys = 'G'; } else if (crdsys[0] == 'E') { hpxdat->crdsys = 'E'; } else if (crdsys[0] == 'C') { /* ("celestial") */ hpxdat->crdsys = 'Q'; } /* Nested or ring ordering? */ if (fits_read_key_str(fptr, "ORDERING", ordering, 0x0, &status)) { /* Some HEALPix files, e.g. SFD dust maps, don't record ORDERING. */ if (status != KEY_NO_EXIST) goto fitserr; status = 0; } else if (strcmp(ordering, "NESTED") == 0) { hpxdat->ordering = 'N'; } else if (strcmp(ordering, "RING") == 0) { hpxdat->ordering = 'R'; } else { fprintf(stderr, "WARNING: Invalid ORDERING keyword: %s.\n", ordering); } /* Allocate memory and read the data. */ if ((hpxdat->data = malloc((hpxdat->npix)*sizeof(float))) == NULL) { perror("HPXcvt"); goto cleanup; } nulval = HEALPIX_NULLVAL; datap = hpxdat->data; for (ipix = 0; ipix < firstpix; ipix++) { *(datap++) = nulval; } firstelem = (LONGLONG)1; if (hdutype == IMAGE_HDU) { if (fits_read_img_flt(fptr, 0l, firstelem, npix, nulval, datap, &anynul, &status)) goto fitserr; } else if (hdutype == BINARY_TBL) { for (irow = 0; irow < nrow; irow++) { if (fits_read_col_flt(fptr, hpxdat->col, irow+1, firstelem, nelem, nulval, datap, &anynul, &status)) goto fitserr; datap += nelem; } } datap = hpxdat->data + (lastpix + 1); for (ipix = (lastpix+1); ipix < hpxdat->npix; ipix++) { *(datap++) = nulval; } /* Clean up. */ fits_close_file(fptr, &status); status = 0; return 0; fitserr: fits_report_error(stderr, status); cleanup: if (naxes) free(naxes); if (hpxdat->data) free(hpxdat->data); hpxdat->data = 0x0; return 1; }