int GetGrpInfo6 (StisInfo6 *sts, Hdr *hdr) { /* arguments: StisInfo6 *sts io: calibration switches and info Hdr *hdr i: header of current IMSET */ int status; int sdqflags; /* serious data quality flags */ char *buf; /* scratch for keyword value */ int use_def = 1; /* use default if missing keyword */ int no_default = 0; /* missing keyword is fatal error */ int GetDetTemp (Hdr *, int, double *); /* Get the dispersion axis. */ if ((status = Get_KeyI (hdr, "DISPAXIS", use_def, 1, &sts->dispaxis))) return (status); if (sts->dispaxis < 1 || sts->dispaxis > 2) { printf ("ERROR Dispaxis = %d is invalid\n", sts->dispaxis); return (INVALID); } /* This should be SCIENCE or WAVECAL. */ if ((buf = (char *) calloc (STIS_FNAME+1, sizeof(char))) == NULL) return (OUT_OF_MEMORY); if ((status = Get_KeyS (hdr, "ASN_MTYP", use_def, "unknown", buf, STIS_FNAME))) return (status); sts->wavecal = (strcmp (buf, "WAVECAL") == 0); free (buf); /* Exposure info. */ if ((status = Get_KeyD (hdr, "EXPTIME", no_default, 0., &sts->exptime))) return (status); if (sts->exptime < 0.) { printf ("ERROR Exposure time is invalid: %14.6g\n", sts->exptime); return (INVALID); } if ((status = Get_KeyD (hdr, "EXPSTART", no_default, 0., &sts->expstart))) return (status); if ((status = Get_KeyD (hdr, "EXPEND", no_default, 0., &sts->expend))) return (status); if ((status = Get_KeyD (hdr, "DOPPMAG", use_def, 0., &sts->doppmag))) return (status); if (sts->doppmag > 0. && strcmp (sts->obsmode, "TIME-TAG") != 0) { /* doppon could be False in timetag mode; otherwise, reset doppzero to zero if on-board Doppler correction was not done. */ int doppon; if ((status = Get_KeyI (hdr, "DOPPON", use_def, 0, &doppon))) return (status); if (!doppon) sts->doppmag = 0.; } if ((status = Get_KeyD (hdr, "DOPPZERO", use_def, 0., &sts->doppzero))) return (status); if ((status = Get_KeyD (hdr, "ORBITPER", use_def, 1., &sts->orbitper))) return (status); /* Find out which data quality bits are considered serious; default value means all bits are serious. */ if ((status = Get_KeyI (hdr, "SDQFLAGS", use_def, ALL_BITS, &sdqflags))) return (status); sdqflags &= ~HOTPIX; /* hot (but probably just warm) pixel */ sdqflags &= ~SMALLBLEM; /* too small to be concerned about */ sts->sdqflags_orig = (short) sdqflags; sts->cc_sdqflags = (short) sdqflags; /* This turns off the data quality flag checking everywhere except in crosscorrelation module. */ sts->sdqflags = 0; /* Get the "plate scale" coefficients. */ if ((status = Get_KeyD (hdr, "CD1_1", no_default, 0., &sts->cd[0]))) return (status); if ((status = Get_KeyD (hdr, "CD2_2", no_default, 0., &sts->cd[1]))) return (status); if ((status = Get_KeyD (hdr, "CRPIX1", no_default, 1., &sts->crpix[0]))) return (status); if ((status = Get_KeyD (hdr, "CRVAL1", no_default, 0., &sts->crval[0]))) return (status); /* Get the linear transformation (zero indexed) from the reference coordinate system to current image pixel coordinates. */ if ((status = GetLT0 (hdr, sts->ltm, sts->ltv))) return (status); /* Get the detector temperature (or housing temperature, if CCD). */ if (sts->fluxcorr == PERFORM) { if ((status = GetDetTemp (hdr, sts->detector, &sts->detector_temp))) { return (status); } } else { sts->detector_temp = -1.; } /* Get the MSM slop. */ if (sts->wavecal) { sts->msm_offset[0] = 0.0; sts->msm_offset[1] = 0.0; } else { if ((status = Get_KeyD (hdr, "SHIFTA1", use_def, 0., &sts->msm_offset[0]))) return (status); if ((status = Get_KeyD (hdr, "SHIFTA2", use_def, 0., &sts->msm_offset[1]))) return (status); } /* Get the mean dark count rate and the number of combined images for CTI correction. */ if ((status = Get_KeyD(hdr, "MEANDARK", no_default, 0., &sts->meandark))) return status; if ((status = Get_KeyI(hdr, "NCOMBINE", no_default, 0., &sts->ncombine))) return status; return (0); }
int GetGrpInfo1 (StisInfo1 *sts, Hdr *hdr) { /* arguments: StisInfo1 *sts io: calibration switches and info Hdr *hdr i: header of current extension */ int status; char *buf; /* scratch for keyword value */ int sdqflags; /* serious data quality flags */ int rsize; /* 1 for CCD, 2 for MAMA */ int corner[2]; /* subarray start points, detector coords */ double cd11, cd12, cd21, cd22; /* CD matrix */ int doppon; /* Doppler correction done on-board? */ int use_def = 1; /* use default if missing keyword */ int no_default = 0; /* missing keyword is fatal error */ int GetDetTemp (Hdr *, int, double *); int GetEPCTab (StisInfo1 *, float); /* Get generic parameters. */ /* Check whether we're processing a science file or wavecal, based on ASN_MTYP. Note that we won't reset the wavecal flag to false (it may have been set in GetKeyInfo1) if ASN_MTYP doesn't indicate that the observation is a wavecal, because this file might not be part of an association. */ if ((buf = calloc (STIS_FNAME+1, sizeof(char))) == NULL) return (OUT_OF_MEMORY); if ((status = Get_KeyS (hdr, "ASN_MTYP", use_def, "unknown", buf, STIS_FNAME))) return (status); /* Possible values for wavecals are "AUTO-WAVECAL" and "GO-WAVECAL" */ if (strstr (buf, "WAVECAL") != NULL) sts->wavecal = 1; free (buf); if ((status = Get_KeyD (hdr, "EXPTIME", no_default, 0., &sts->exptime))) return (status); if (sts->exptime < 0.) { printf ("ERROR Exposure time is invalid: %14.6g.\n", sts->exptime); return (GENERIC_ERROR_CODE); } if ((status = Get_KeyD (hdr, "EXPSTART", no_default, 0., &sts->expstart))) return (status); if ((status = Get_KeyD (hdr, "EXPEND", no_default, 0., &sts->expend))) return (status); /* Find out which data quality bits are considered serious; default value means all bits are serious. */ if ((status = Get_KeyI (hdr, "SDQFLAGS", use_def, 32767, &sdqflags))) return (status); sts->sdqflags = (short) sdqflags; /* Get the pixel size (ignore corner location) from ltm & ltv. */ rsize = (sts->detector == CCD_DETECTOR) ? 1 : 2; if ((status = GetCorner (hdr, rsize, sts->bin, corner))) return (status); /* For spectroscopic data, we want the dispersion axis and the sign of the dispersion. We'll get the latter from one element of the CD matrix. We also want the pixel size, which we compute from the CD matrix. */ sts->dispaxis = 1; /* initial values */ sts->dispsign = 1; if (strcmp (sts->obstype, "IMAGING") == 0) { if ((status = Get_KeyD (hdr, "CD1_1", use_def, 1., &cd11))) return (status); if ((status = Get_KeyD (hdr, "CD1_2", use_def, 0., &cd12))) return (status); if ((status = Get_KeyD (hdr, "CD2_1", use_def, 0., &cd21))) return (status); if ((status = Get_KeyD (hdr, "CD2_2", use_def, 1., &cd22))) return (status); sts->cdelt[0] = sqrt (cd11 * cd11 + cd21 * cd21); sts->cdelt[1] = sqrt (cd12 * cd12 + cd22 * cd22); } else if (strcmp (sts->obstype, "SPECTROSCOPIC") == 0) { if ((status = Get_KeyI (hdr, "DISPAXIS", use_def, 1, &sts->dispaxis))) return (status); if ((status = Get_KeyD (hdr, "CD1_1", use_def, 1., &cd11))) return (status); if ((status = Get_KeyD (hdr, "CD2_2", use_def, 1., &cd22))) return (status); if (sts->dispaxis == 1) { if (cd11 >= 0.) sts->dispsign = 1; else sts->dispsign = -1; sts->cdelt[0] = cd22; /* assume square pixels */ sts->cdelt[1] = cd22; } else if (sts->dispaxis == 2) { if (cd22 >= 0.) sts->dispsign = 1; else sts->dispsign = -1; sts->cdelt[0] = cd11; sts->cdelt[1] = cd11; } } sts->detector_temp = -1.; /* initial value (not defined) */ /* Get MAMA-specific parameters. */ if (sts->detector == NUV_MAMA_DETECTOR || sts->detector == FUV_MAMA_DETECTOR) { if ((status = Get_KeyD (hdr, "GLOBRATE", no_default, 0., &sts->globrate))) return (status); /* Get info if we need to do Doppler convolution of ref files. */ if (sts->doppcorr == PERFORM) { /* Was Doppler correction done on-board? */ if ((status = Get_KeyI (hdr, "DOPPON", use_def, 0, &doppon))) return (status); /* doppon could be False in timetag mode. */ if (!doppon) { if (strcmp (sts->obsmode, "TIME-TAG") == 0) { if ((status = Get_KeyD (hdr, "DOPPMAG", use_def, -1., &sts->doppmag))) return (status); doppon = (sts->doppmag > 0.); } } if (doppon) { if ((status = Get_KeyD (hdr, "DOPPZERO", no_default, 0., &sts->doppzero))) return (status); if ((status = Get_KeyD (hdr, "DOPPMAG", no_default, 0., &sts->doppmag))) return (status); if ((status = Get_KeyD (hdr, "ORBITPER", no_default, 0., &sts->orbitper))) return (status); } else { /* Silently reset the switch. */ sts->doppcorr = OMIT; } } } /* Get CCD-specific parameters. */ if (sts->detector == CCD_DETECTOR) { float occdhtav = -1.; /* if OCCDHTAV is present and > 0, then data is from Side B, * so look for EPC file. Otherwise, data is from Side A and * EPC file and temperature can be ignored. */ if (((status = Get_KeyF(hdr, "OCCDHTAV", use_def, -1., &occdhtav)) == 0) && (occdhtav >= 0.)) { char expname[81], epcname[81]; expname[0] = epcname[0] = '\0'; if ((status = Get_KeyS(hdr, "EXPNAME", no_default, "", expname, 80))) return (status); if (sts->crcorr != COMPLETE) { strncpy(epcname, expname, 8); epcname[8] = '\0'; sprintf(sts->epctab.name, "%sj_epc.fits", epcname); PrFileName("epcfile", sts->epctab.name); status = GetEPCTab(sts, 0.40); if (status && status != OPEN_FAILED) return (status); } if ((status = UpdateCCDTemperature(sts, hdr))) return (status); } } /* Get the detector temperature (or housing temperature, if CCD). Note that for side-2 CCD data this function must be called after calling UpdateCCDTemperature. */ if ((status = GetDetTemp (hdr, sts->detector, &sts->detector_temp))) { return (status); } /* If images have been combined (e.g. by cosmic-ray rejection), then determine the number of images that were combined together; we need this for bias image subtraction. (This isn't really a CCD-specific keyword, but it does only affect CCD data in the context of calstis1.) */ if ((status = Get_KeyI (hdr, "NCOMBINE", use_def, 1, &sts->ncombine))) return (status); if (sts->ncombine < 1) { printf ("Warning NCOMBINE = %d, reset to one.\n", sts->ncombine); sts->ncombine = 1; } return (0); }
int doPhot (StisInfo1 *sts, SingleGroup *x) { /* arguments: StisInfo1 *sts i: calibration switches, etc SingleGroup *x io: image to be calibrated; primary header is modified */ int status; PhotPar obs; char photmode[STIS_LINE+1]; /* the photmode string */ char tempphot[STIS_LINE+1]; /* photmode without A2D */ char obsmode[STIS_LINE+1]; /* based on the photmode string */ int use_default = 1; /* use default value if keyword is missing */ int found_a2d = 0; /* true if there's a CCD A2D component */ TdsInfo tds; /* time-dep, but use only temperature corr */ int GetTds1 (char *, char *, TdsInfo *); void FreeTds1 (TdsInfo *); /* Get PHOTMODE from the primary header. */ if ((status = Get_KeyS (x->globalhdr, "PHOTMODE", use_default, "", photmode, STIS_LINE)) != 0) return (status); found_a2d = removeA2D (photmode, tempphot); Phot2Obs (tempphot, obsmode); /* Initialize PhotPar struct. */ InitPhotPar (&obs, sts->phot.name, sts->phot.pedigree); /* Get the photometry values. */ if ((status = GetPhotTab (&obs, obsmode)) != 0) { printf ("Warning photmode '%s' not found.\n", obsmode); FreePhotPar (&obs); sts->photcorr = IGNORED; return 0; } /* The flag value that indicates that the time of observation is outside the range of times in the imphttab is actually -9999., but test on -9990. to avoid roundoff problems. Extrapolation is not supported, so the values were not computed. Set photcorr to IGNORED (which results in SKIPPED), but set the keywords to -9999. anyway. */ if (obs.photflam < -9990. && obs.photplam < -9990. && obs.photbw < -9990.) { sts->photcorr = IGNORED; } /* Make further changes to photflam. */ if (found_a2d && sts->photcorr == PERFORM) obs.photflam *= sts->atodgain; /* Get values from the TDS table */ if (sts->tdscorr == PERFORM && sts->photcorr == PERFORM) { if ((status = GetTds1(sts->tdstab.name, sts->opt_elem, &tds))) return (status); obs.photflam /= tempFactor(&tds, obs.photplam, sts->detector_temp); FreeTds1 (&tds); } /* Update the photometry keyword values in the primary header. */ if ((status = Put_KeyF (x->globalhdr, "PHOTFLAM", obs.photflam, "inverse sensitivity")) != 0) return (status); if ((status = Put_KeyF (x->globalhdr, "PHOTZPT", obs.photzpt, "zero point")) != 0) return (status); if ((status = Put_KeyF (x->globalhdr, "PHOTPLAM", obs.photplam, "pivot wavelength")) != 0) return (status); if ((status = Put_KeyF (x->globalhdr, "PHOTBW", obs.photbw, "RMS bandwidth")) != 0) return (status); FreePhotPar (&obs); return (0); }
int GetKeyInfo1 (StisInfo1 *sts, Hdr *phdr) { /* arguments: StisInfo1 *sts io: calibration switches and info Hdr *phdr i: primary header */ int status; int nextend; /* number of FITS extensions */ char targname[STIS_CBUF+1]; /* target name, check if BIAS or DARK */ char crcorr[STIS_CBUF+1]; /* cosmic-ray rejection keyword */ int use_def = 1; /* use default if missing keyword */ int no_default = 0; /* missing keyword is fatal error */ /* Get generic parameters. */ if ((status = Get_KeyS (phdr, "ROOTNAME", no_default, "", sts->rootname, STIS_CBUF))) return (status); if ((status = Get_KeyS (phdr, "OBSMODE", use_def, "unknown", sts->obsmode, STIS_CBUF))) return (status); if ((status = Get_KeyS (phdr, "APERTURE", use_def, "", sts->aperture, STIS_CBUF))) return (status); if ((status = Get_KeyS (phdr, "DETECTOR", no_default, "", sts->det, STIS_CBUF))) return (status); if (strcmp (sts->det, "NUV-MAMA") == 0) { sts->detector = NUV_MAMA_DETECTOR; } else if (strcmp (sts->det, "FUV-MAMA") == 0) { sts->detector = FUV_MAMA_DETECTOR; } else if (strcmp (sts->det, "CCD") == 0) { sts->detector = CCD_DETECTOR; } else { printf ("ERROR DETECTOR = %s is invalid\n", sts->det); return (HEADER_PROBLEM); } /* Grating or mirror name. */ if ((status = Get_KeyS (phdr, "OPT_ELEM", use_def, "", sts->opt_elem, STIS_CBUF))) return (status); /* If TARGNAME is BIAS or DARK, set the flag to indicate this. */ if ((status = Get_KeyS (phdr, "TARGNAME", use_def, "", targname, STIS_CBUF))) return (status); if (strcmp (targname, "BIAS") == 0) sts->bias_exposure = 1; else sts->bias_exposure = 0; if (strcmp (targname, "BIAS") == 0 || strcmp (targname, "DARK") == 0) sts->bias_or_dark = 1; else sts->bias_or_dark = 0; /* If TARGNAME indicates that this is a wavecal observation, set the wavecal flag. */ if (strcmp (targname, "WAVELINE") == 0 || strcmp (targname, "WAVEHITM") == 0) { sts->wavecal = 1; } if ((status = Get_KeyD (phdr, "RA_TARG", no_default, 0., &sts->ra_targ))) return (status); if ((status = Get_KeyD (phdr, "DEC_TARG", no_default, 0., &sts->dec_targ))) return (status); /* Check if CRCORR is complete */ if ((status = Get_KeyS (phdr, "CRCORR", use_def, "", crcorr, STIS_CBUF))) return (status); if (strcmp(crcorr, "COMPLETE") == 0) { sts->crcorr = COMPLETE; } else { sts->crcorr = PERFORM; } /* Find out how many extensions there are in this file. */ if ((status = Get_KeyI (phdr, "NEXTEND", use_def, EXT_PER_GROUP, &nextend))) return (status); /* Convert number of extensions to number of SingleGroups. */ sts->nimages = nextend / EXT_PER_GROUP; if (sts->nimages < 1) { printf ("ERROR NEXTEND = %d; must be at least three.\n", sts->nimages); return (GENERIC_ERROR_CODE); } /* If obstype is spectroscopic and photcorr is set to perform, we will reset photcorr. */ if ((status = Get_KeyS (phdr, "OBSTYPE", use_def, "", sts->obstype, STIS_CBUF))) return (status); if (strcmp (sts->obstype, "IMAGING") != 0 && strcmp (sts->obstype, "SPECTROSCOPIC") != 0) { printf ( "Warning OBSTYPE = %s; should be either IMAGING or SPECTROSCOPIC\n", sts->obstype); } /* For CCD observations, we need the APER_FOV in order to flag regions beyond the aperture for such apertures as F28X50LP. */ if ((status = Get_KeyS (phdr, "APER_FOV", use_def, "", sts->aper_fov, STIS_CBUF))) return (status); /* Get MAMA-specific parameters. */ /* there are none, at present */ /* Get CCD-specific parameters. */ if (sts->detector == CCD_DETECTOR) { if ((status = Get_KeyS (phdr, "CCDAMP", no_default, "", sts->ccdamp, STIS_CBUF-1))) return (status); if (sts->ccdamp[0] != '\0' && sts->ccdamp[1] != '\0') { printf ( "Warning Multiple amp readout `%s' is not supported.\n", sts->ccdamp); } if (islower (sts->ccdamp[0])) sts->ccdamp[0] = toupper (sts->ccdamp[0]); if (strchr ("ABCD", sts->ccdamp[0]) == NULL) { printf ("ERROR CCDAMP = `%s' is invalid.\n", sts->ccdamp); return (GENERIC_ERROR_CODE); } if ((status = Get_KeyI (phdr, "CCDGAIN", use_def, 1, &sts->ccdgain))) return (status); if ((status = Get_KeyI (phdr, "CCDOFFST", use_def, 1, &sts->ccdoffset))) return (status); if ((status = Get_KeyI (phdr, "BINAXIS1", use_def, 1, &sts->binaxis[0]))) return (status); if ((status = Get_KeyI (phdr, "BINAXIS2", use_def, 1, &sts->binaxis[1]))) return (status); } return (0); }
static int FluxToNet (StisInfo6 *sts, IntensArray *inta, int sporder) { /* This is used to store information from the fflux file in a form suitable for the reference file input routines. */ StisInfo6 fsts; ApInfo slit; PhotInfo phot; IODescPtr im; Hdr phdr; double photfactor, throughput, response, dispersion; double atodgain, readnoise; float correction; int i, dispc, helc, status; int abs_starti, thr_starti; int dummy; void FreePhot6 (PhotInfo *); void FreeThroughput6 (ApInfo *); int GetAbsPhot6 (StisInfo6 *, int, PhotInfo *, int, int *); int GetApDes6 (StisInfo6 *, ApInfo *); int GetApThr6 (StisInfo6 *, ApInfo *); int Get_KeyD (Hdr *, char *, int, double, double *); int Get_KeyS (Hdr *, char *, int, char *, char *, int); int GetSwitch (Hdr *, char *, int *); double interp1d (double, double *, double *, int, int *); void StisInit6 (StisInfo6 *); photfactor = H_PLANCK * C_LIGHT / HST_AREA; /* Initialize local data structures. */ StisInit6 (&fsts); InitRefTab (&fsts.phottab); InitRefTab (&fsts.apertab); InitRefTab (&fsts.apdestab); slit.allocated = 0; slit.gac_allocated = 0; phot.allocated = 0; phot.pcorr = NULL; /* Handling the primary header here is not efficient. But keeps this new code manageable since everything new is added at a single point. In the future we may move this to outside the main loop and pass the necessary values as part of the sts structure. */ initHdr (&phdr); im = openInputImage (sts->pxtab.name, "", 0); if (hstio_err()) return (OPEN_FAILED); getHeader (im, &phdr); if (hstio_err()) return (OPEN_FAILED); closeImage (im); /* Abort if both helcorr and dispcorr weren't performed. The criterion is: if a keyword is set to either COMPLETE or PERFORM, we assume that the operation was performed. This is because UpdHdrSwitch in Do1Dx only updates the keywords to COMPLETE if they are set to PERFORM in the input file. (note: UpdHdrSwitch is no longer used) */ if ((status = GetSwitch (&phdr, "DISPCORR", &dispc))) return (status); if ((status = GetSwitch (&phdr, "HELCORR", &helc))) return (status); if (!((dispc == PERFORM || dispc == COMPLETE) && (helc == PERFORM || helc == COMPLETE))) { printf ("ERROR No DISPCORR/HELCORR in fflux file.\n"); return (ERROR_RETURN); } /* Read header keywords. */ if ((status = Get_KeyD (&phdr, "READNSE", 1, 0., &readnoise))) return (status); if ((status = Get_KeyD (&phdr, "ATODGAIN", 1, 1., &atodgain))) return (status); if ((status = Get_KeyS (&phdr, "PHOTTAB", FATAL, "", fsts.phottab.name, STIS_LINE))) return (status); if ((status = Get_KeyS (&phdr, "APDESTAB", FATAL, "", fsts.apdestab.name, STIS_LINE))) return (status); if ((status = Get_KeyS (&phdr, "APERTAB", FATAL, "", fsts.apertab.name, STIS_LINE))) return (status); /* Copy stuff from primary data structure into local one. */ fsts.x1d_o = sts->x1d_o; fsts.dispcorr = sts->dispcorr; fsts.fluxcorr = sts->fluxcorr; fsts.pctcorr = sts->pctcorr; fsts.cenwave = sts->cenwave; strcpy (fsts.opt_elem, sts->opt_elem); strcpy (fsts.aperture, sts->aperture); /* Read the required reference info. */ dummy = 0; if ((status = GetAbsPhot6 (&fsts, sporder, &phot, 0, &dummy))) return (status); if ((status = GetApDes6 (&fsts, &slit))) return (status); if ((status = GetApThr6 (&fsts, &slit))) return (status); abs_starti = 1; /* initial values */ thr_starti = 1; /* Loop over flux array. */ for (i = 0; i < inta->nelem; i++) { response = interp1d (inta->wave[i], phot.wl, phot.thru, phot.nelem, &abs_starti); throughput = interp1d (inta->wave[i], slit.wl, slit.thr, slit.nelem, &thr_starti); if (i > 0) dispersion = inta->wave[i] - inta->wave[i-1]; else dispersion = inta->wave[1] - inta->wave[0]; /* This check is provisional; final version awaits IS's words. */ if (response <= 0.0 || dispersion <= 0.0 || throughput <= 0.0) { printf ("ERROR Error in fflux file contents.\n"); return (ERROR_RETURN); } correction = (float) (photfactor / (response * throughput * inta->wave[i] * dispersion * atodgain * CM_PER_ANGSTROM)); inta->intens[i] = inta->intens[i] / correction; } FreeThroughput6 (&slit); FreePhot6 (&phot); freeHdr (&phdr); return STIS_OK; }
int GetGrpInfo7 (StisInfo7 *sts, Hdr *hdr) { /* arguments: StisInfo7 *sts io: calibration switches and info Hdr *hdr i: header of current imset */ int status; int sdqflags; /* serious data quality flags */ char *buf; /* scratch for keyword value */ int use_def = 1; /* use default if missing keyword */ int no_default = 0; /* missing keyword is fatal error */ int GetDetTemp (Hdr *, int, double *); /* Get generic parameters. */ /* This should be SCIENCE or WAVECAL. */ if ((buf = calloc (STIS_FNAME+1, sizeof(char))) == NULL) return (OUT_OF_MEMORY); if ((status = Get_KeyS (hdr, "ASN_MTYP", use_def, "unknown", buf, STIS_FNAME))) return (status); /* Possible values for wavecals are "AUTO-WAVECAL" and "GO-WAVECAL" */ sts->wavecal = (strstr (buf, "WAVECAL") != NULL); free (buf); if ((status = Get_KeyD (hdr, "EXPTIME", no_default, 0., &sts->exptime))) return (status); if (sts->exptime < 0.) { printf ("ERROR Exposure time %.6g is invalid.\n", sts->exptime); return (GENERIC_ERROR_CODE); } if ((status = Get_KeyD (hdr, "EXPSTART", no_default, 0., &sts->expstart))) return (status); if ((status = Get_KeyD (hdr, "EXPEND", no_default, 0., &sts->expend))) return (status); /* Find out which data quality bits are considered serious; default value means all bits are serious. */ if ((status = Get_KeyI (hdr, "SDQFLAGS", use_def, 32767, &sdqflags))) return (status); sts->sdqflags = (short) sdqflags; /* Get the linear transformation (zero indexed) from the reference coordinate system to current image pixel coordinates. */ if ((status = GetLT0 (hdr, sts->ltm, sts->ltv))) return (status); if ((status = Get_KeyD (hdr, "CRPIX1", use_def, 0., &sts->crpix[0]))) return (status); if ((status = Get_KeyD (hdr, "CRPIX2", use_def, 0., &sts->crpix[1]))) return (status); sts->crpix[0]--; /* convert to zero-indexed */ sts->crpix[1]--; /* Get the detector temperature (or housing temperature, if CCD). */ if (sts->fluxcorr == PERFORM) { if ((status = GetDetTemp (hdr, sts->detector, &sts->detector_temp))) { return (status); } } else { sts->detector_temp = -1.; } /* Get the MSM offset found during wavecal processing. */ if (sts->wavecal) { sts->msm_slop[0] = 0.; sts->msm_slop[1] = 0.; } else { if ((status = Get_KeyD (hdr, "SHIFTA1", use_def, 0., &sts->msm_slop[0]))) return (status); if (sts->wx2dcorr == COMPLETE) { /* shifta2 has already been accounted for by wx2d */ sts->msm_slop[1] = 0.; } else { if ((status = Get_KeyD (hdr, "SHIFTA2", use_def, 0., &sts->msm_slop[1]))) return (status); } } if (sts->obstype == SPECTROSCOPIC_TYPE) { /* Get the dispersion axis. */ if ((status = Get_KeyI (hdr, "DISPAXIS", use_def, 1, &sts->dispaxis))) return (status); if (sts->dispaxis < 1 || sts->dispaxis > 2) { printf ("ERROR Dispaxis = %d is invalid.\n", sts->dispaxis); return (GENERIC_ERROR_CODE); } } else { sts->dispaxis = 0; } return (0); }
static int GetPSF (char *name, Hdr *phdr, double xsize, double ysize, ScatterFunctions *scf, Image *psf1, Image *psf2, Image *psf3) { /* char *name; i: name of TELTAB reference file Hdr *phdr i: primary header double xsize, ysize; i: aperture size in arcsec ScatterFunctions *scf; o: data structure with scattering functions Image *psf1,2,3; o: PSF images, previously initialized */ IRAFPointer tp; IRAFPointer cp_refwave, cp_pscale, cp_psfdim, cp_telepsf; int row, nrows, psfdim; double rw, psfscale; Image ospsf; Image imtemp, psf; char keyword[STIS_CBUF]; double xplate, yplate; double sbox, lbox; double rstart, rstop; double frac1, frac2; float fhold; double sum; int i, j, k, kk, ms, ml, ns, nl, s, l; int ns2, nl2, s1, s2, l1, l2; int istart, istop; int status; int Alloc1DImage (Image *, int); int Alloc2DImage (Image *, int, int); void InitImage (Image *); void FreeImage (Image *); /* Define plate scale. */ if (streq_ic (scf->opt_elem, "E140M")) xplate = 0.036; else if (streq_ic (scf->opt_elem, "E140H")) xplate = 0.0466; else if (streq_ic (scf->opt_elem, "E230M")) xplate = 0.033; else if (streq_ic (scf->opt_elem, "E230H")) xplate = 0.0466; else { printf ("Non supported grating.\n"); return (ERROR_RETURN); } yplate = 0.029; /* Truncate the aperture size if it extends beyond the borders of an image. */ /* See if uniformly illuminated aperture was used. */ if ((status = Get_KeyS (phdr, "SCLAMP", 0, "", keyword, STIS_CBUF))) return (status); if (!streq_ic (keyword, "NONE")) { ml = (int)floor (ysize / yplate); if (ml % 2) { status = Alloc1DImage (psf1, ml+1); status |= Alloc1DImage (psf2, ml+1); status |= Alloc1DImage (psf3, ml+1); if (status) return (status); for (i = 1; i < ml; i++) { psf1->pix[i] = 1.0 / ml; psf2->pix[i] = 1.0 / ml; psf3->pix[i] = 1.0 / ml; } psf1->pix[0] = 0.5 / ml; psf1->pix[ml] = 0.5 / ml; psf2->pix[0] = 0.5 / ml; psf2->pix[ml] = 0.5 / ml; psf3->pix[0] = 0.5 / ml; psf3->pix[ml] = 0.5 / ml; } else { status = Alloc1DImage (psf1, ml); status |= Alloc1DImage (psf2, ml); status |= Alloc1DImage (psf3, ml); if (status) return (status); for (i = 0; i < ml; i++) { psf1->pix[i] = 1.0 / ml; psf2->pix[i] = 1.0 / ml; psf3->pix[i] = 1.0 / ml; } } return (STIS_OK); } /* Scan reference file and process each PSF. */ tp = c_tbtopn (name, IRAF_READ_ONLY, 0); if (c_iraferr()) { printf ("ERROR TELTAB `%s' not found\n", name); return (OPEN_FAILED); } nrows = c_tbpsta (tp, TBL_NROWS); c_tbcfnd1 (tp, "PSFWAVE", &cp_refwave); c_tbcfnd1 (tp, "PSCALE", &cp_pscale); c_tbcfnd1 (tp, "PSFDIM", &cp_psfdim); c_tbcfnd1 (tp, "TELEPSF", &cp_telepsf); if (cp_refwave == 0 || cp_psfdim == 0 || cp_pscale == 0 || cp_telepsf == 0) { printf( "ERROR Column not found in TELTAB\n"); c_tbtclo (tp); return (COLUMN_NOT_FOUND); } kk = 0; for (row = 1; row <= nrows ; row++) { c_tbegtd (tp, cp_refwave, row, &rw); if (c_iraferr()) return (TABLE_ERROR); /* Matching reference wavelenghts. */ if (rw == scf->psfw[kk]) { /* Read PSF image and associated data. */ c_tbegtd (tp, cp_pscale, row, &psfscale); c_tbegti (tp, cp_psfdim, row, &psfdim); InitImage (&imtemp); if ((status = Alloc2DImage (&imtemp, psfdim, psfdim))) return (status); i = c_tbagtr (tp, cp_telepsf, row, imtemp.pix, 1, imtemp.npix); if (c_iraferr()) return (TABLE_ERROR); /* Find peak. */ ns = imtemp.nx; nl = imtemp.ny; fhold = -FLT_MAX; for (i = 0; i < ns; i++) { for (j = 0; j < nl; j++) { if (PIX (imtemp, i, j) > fhold) { s = i; l = j; fhold = PIX (imtemp, i, j); } } } /* Determine portion of PSF that made it through aperture. */ ns2 = ((int)NINT (xsize / psfscale)) / 2; nl2 = ((int)NINT (ysize / psfscale)) / 2; s1 = ((s - ns2) >= 0) ? (s - ns2) : 0; s2 = ((s + ns2) <= (ns - 1)) ? (s + ns2) : (ns - 1); l1 = ((l - nl2) >= 0) ? (l - nl2) : 0; l2 = ((l + nl2) <= (nl - 1)) ? (l + nl2) : (nl - 1); /* Extract sub-image. */ InitImage (&ospsf); if ((status = Alloc2DImage (&ospsf, s2 - s1 + 1, l2 - l1 + 1))) return (status); k = 0; /* location of braces looks like a typo, but harmless */ for (j = l1; j <= l2; j++) for (i = s1; i <= s2; i++) { ospsf.pix[k++] = PIX (imtemp, i, j); } FreeImage (&imtemp); ns = ospsf.nx; nl = ospsf.ny; /* # of STIS pixels (ms*ml) illuminated by aperture. */ /* modified on 2013 Nov 13 by PEH xplate / psfscale and yplate / psfscale are the factors by which the PSF in the _tel.fits file are oversampled. */ ms = (NINT (ns / (xplate / psfscale))) / 2; ml = (NINT (nl / (yplate / psfscale))) / 2; ms = 2 * ms + 1; ml = 2 * ml + 1; /* Bin oversampled PSF to STIS pixel scale. */ /* Bin columns. */ if ((status = Alloc2DImage (&imtemp, ms, nl))) return (status); sbox = ns / (double)ms; for (i = 0; i < ms; i++) { rstart = i * sbox; rstop = (i+1) * sbox; istart = (int) floor (rstart); istop = (int) floor (rstop); istop = (istop < ns) ? istop : ns-1; frac1 = rstart - istart; frac2 = 1.0 - (rstop - istop); for (j = 0; j < nl; j++) { for (k = istart+1; k < istop; PIX (imtemp, i, j) += PIX (ospsf, k++, j)); PIX (imtemp, i, j) += PIX (ospsf, istart, j) * (1.0 - frac1); PIX (imtemp, i, j) += PIX (ospsf, istop, j) * (1.0 - frac2); } } FreeImage (&ospsf); /* Bin rows. */ InitImage (&psf); if ((status = Alloc2DImage (&psf, ms, ml))) return (status); lbox = nl / (double)ml; for (j = 0; j < ml; j++) { rstart = j * lbox; rstop = (j+1) * lbox; istart = (int) floor (rstart); istop = (int) floor (rstop); istop = (istop < nl) ? istop : nl-1; frac1 = rstart - istart; frac2 = 1.0 - (rstop - istop); for (i = 0; i < ms; i++) { for (k = istart+1; k < istop; PIX (psf, i, j) += PIX (imtemp, i, k++)); PIX (psf, i, j) += PIX (imtemp, i, istart) * (1.0 - frac1); PIX (psf, i, j) += PIX (imtemp, i, istop) * (1.0 - frac2); } } FreeImage (&imtemp); /* Normalize PSF to unit area. */ sum = 0.0; for (i = 0; i < psf.npix; sum += psf.pix[i++]); for (i = 0; i < psf.npix; psf.pix[i++] /= sum); /* This awful code is a consequence of the change in reference file format that took place after the original code was completed. */ switch (kk) { case 0: if ((status = Alloc2DImage (psf1, psf.nx, psf.ny))) return (status); for (i= 0 ; i < psf1->npix; i++) psf1->pix[i] = psf.pix[i]; break; case 1: if ((status = Alloc2DImage (psf2, psf.nx, psf.ny))) return (status); for (i= 0 ; i < psf2->npix; i++) psf2->pix[i] = psf.pix[i]; break; case 2: if ((status = Alloc2DImage (psf3, psf.nx, psf.ny))) return (status); for (i= 0 ; i < psf3->npix; i++) psf3->pix[i] = psf.pix[i]; break; default: break; } FreeImage (&psf); kk++; } } if (kk == 0) { printf ("ERROR No matching rows in TELTAB `%s'\n", name); return (TABLE_ERROR); } c_tbtclo (tp); return (STIS_OK); }
int EchScatRead (Hdr *phdr, double xsize, double ysize, ScatterFunctions *scf, int verbose) { /* arguments Hdr *phdr i: primary header double xsize, ysize; i: aperture size in arcsec ScatterFunctions *scf; o: data structure with scattering functions int verbose; i: verbosity flag */ char fname[STIS_LINE]; /* file names */ Image halo1; /* halo images */ Image halo2; Image halo3; Image psf1; /* psf images */ Image psf2; Image psf3; int no_default = 0; /* missing keyword is fatal error */ int status; void InitImage (Image *); void FreeImage (Image *); InitImage (&halo1); InitImage (&halo2); InitImage (&halo3); InitImage (&psf1); InitImage (&psf2); InitImage (&psf3); /* Get scattering functions. */ if ((status = Get_KeyS (phdr, "ECHSCTAB", no_default, "", fname, STIS_LINE))) { printf ( "WARNING The input file *might* not have the required keywords\n"); printf ( " to run the scattered light correction algorithm.\n"); fflush (stdout); return (status); } if (verbose) { printf ("Reading ECHSCTAB: %s\n", fname); fflush (stdout); } if ((status = GetScatter (fname, scf))) return (status); /* Get echelle x-disp spread function. */ if ((status = Get_KeyS (phdr, "EXSTAB", no_default, "", fname, STIS_LINE))) return (status); if (verbose) { printf ("Reading EXSTAB: %s\n", fname); fflush (stdout); } if ((status = GetEchelleScatter (fname, scf))) return (status); /* Get x-disp function. */ if ((status = Get_KeyS (phdr, "CDSTAB", no_default, "", fname, STIS_LINE))) return (status); if (verbose) { printf ("Reading CDSTAB: %s\n", fname); fflush (stdout); } if ((status = GetXDisp (fname, scf))) return (status); /* Get echelle ripple functions. */ if ((status = Get_KeyS (phdr, "RIPTAB", no_default, "", fname, STIS_LINE))) return (status); if (verbose) { printf ("Reading RIPTAB: %s\n", fname); fflush (stdout); } if ((status = GetRipple (phdr, fname, scf))) return (status); /* Get wavelengths of halo and telescope PSF images. */ if ((status = Get_KeyS (phdr, "SRWTAB", no_default, "", fname, STIS_LINE))) return (status); if (verbose) { printf ("Reading SRWTAB: %s\n", fname); fflush (stdout); } if ((status = GetRefWave (phdr, fname, scf))) return (status); /* Get halo and telescope psf images, and build FTs */ if ((status = Get_KeyS (phdr, "HALOTAB", no_default, "", fname, STIS_LINE))) return (status); if (verbose) { printf ("Reading HALOTAB: %s\n", fname); fflush (stdout); } if ((status = GetHalo (fname, scf, &halo1, &halo2, &halo3))) return (status); if ((status = Get_KeyS (phdr, "TELTAB", no_default, "", fname, STIS_LINE))) return (status); if (verbose) { printf ("Reading TELTAB: %s\n", fname); fflush (stdout); } if ((status = GetPSF (fname, phdr, xsize, ysize, scf, &psf1, &psf2, &psf3))) return (status); InitCmplxArray (&(scf->ft1)); InitCmplxArray (&(scf->ft2)); InitCmplxArray (&(scf->ft3)); InitCmplxArray (&(scf->fto1)); InitCmplxArray (&(scf->fto2)); InitCmplxArray (&(scf->fto3)); if (verbose) { printf ( "Computing Fourier Transforms of kernel at wavelength %g\n", scf->kernw[0]); fflush (stdout); } if ((status = MakeFT (&halo1, &psf1, scf, &(scf->ft1), &(scf->fto1)))) return (status); if (scf->nwave > 1) { if (verbose) { printf ( "Computing Fourier Transforms of kernel at wavelength %g\n", scf->kernw[1]); fflush (stdout); } if ((status = MakeFT (&halo2, &psf2, scf, &(scf->ft2), &(scf->fto2)))) return (status); } if (scf->nwave > 2) { if (verbose) { printf ( "Computing Fourier Transforms of kernel at wavelength %g\n", scf->kernw[2]); fflush (stdout); } if ((status = MakeFT (&halo3, &psf3, scf, &(scf->ft3), &(scf->fto3)))) return (status); } FreeImage (&halo1); FreeImage (&halo2); FreeImage (&halo3); FreeImage (&psf1); FreeImage (&psf2); FreeImage (&psf3); return (STIS_OK); }