static int ReadAtoDArray (TblInfo *tabinfo, int row, TblArray *tabarray) { extern int status; int nret; /* number of elements actually read */ /* Find out how many elements there are in the ATOD array, and allocate space for the array to be read from the table. */ c_tbegti (tabinfo->tp, tabinfo->cp_nelem, row, &tabarray->nelem); if (c_iraferr()) return (status = TABLE_ERROR); tabarray->atod = (float *) malloc (tabarray->nelem * sizeof(float)); if (tabarray->atod == NULL) return (status = OUT_OF_MEMORY); nret = c_tbagtr (tabinfo->tp, tabinfo->cp_atod, row, tabarray->atod, 1, tabarray->nelem); if ( (status = c_iraferr()) ) return (status = TABLE_ERROR); if (nret < tabarray->nelem) { sprintf (MsgText, "CORRECTION array in row %d of ATODTAB is too short.", row); trlerror (MsgText); free (tabarray->atod); return (status = TABLE_ERROR); } return (status); }
static int GetXDisp (char *name, ScatterFunctions *scf) { /* arguments char *name; i: name of CDSTAB reference file ScatterFunctions *scf; o: data structure with scattering functions */ IRAFPointer tp; IRAFPointer cp_optelem, cp_nelem, cp_cdscat; char opt_elem[STIS_CBUF]; int row, nrows, i; tp = c_tbtopn (name, IRAF_READ_ONLY, 0); if (c_iraferr()) { printf ("ERROR CDSTAB `%s' not found\n", name); return (OPEN_FAILED); } nrows = c_tbpsta (tp, TBL_NROWS); c_tbcfnd1 (tp, "OPT_ELEM", &cp_optelem); c_tbcfnd1 (tp, "NELEM", &cp_nelem); c_tbcfnd1 (tp, "CDSCAT", &cp_cdscat); if (cp_optelem == 0 || cp_nelem == 0 || cp_cdscat == 0) { printf( "ERROR Column not found in CDSTAB\n"); c_tbtclo (tp); return (COLUMN_NOT_FOUND); } for (row = 1; row <= nrows ; row++) { c_tbegtt (tp, cp_optelem, row, opt_elem, STIS_CBUF-1); if (streq_ic (opt_elem, scf->opt_elem)) { c_tbegti (tp, cp_nelem, row, &(scf->nxdisp)); scf->xdisp = (float *) calloc (scf->nxdisp, sizeof (float)); if (scf->xdisp == NULL ) return (OUT_OF_MEMORY); i = c_tbagtr (tp, cp_cdscat, row, scf->xdisp, 1, scf->nxdisp); if (c_iraferr()) return (TABLE_ERROR); c_tbtclo (tp); return (STIS_OK); } } printf( "ERROR Row with %s optical element not found in CDSTAB\n", opt_elem); c_tbtclo (tp); return (ROW_NOT_FOUND); }
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); }
static int GetHalo (char *name, ScatterFunctions *scf, Image *halo1, Image *halo2, Image *halo3) { /* char *name; i: name of HALOTAB reference file ScatterFunctions *scf; o: data structure with scattering functions Image *halo1,2,3; o: halo images, previously initialized */ IRAFPointer tp; IRAFPointer cp_optelem, cp_refwave, cp_haldim, cp_halo; char opt_elem[STIS_CBUF]; int row, nrows, i, status, k, haldim; double rw; int Alloc2DImage (Image *, int, int); tp = c_tbtopn (name, IRAF_READ_ONLY, 0); if (c_iraferr()) { printf ("ERROR HALOTAB `%s' not found\n", name); return (OPEN_FAILED); } nrows = c_tbpsta (tp, TBL_NROWS); c_tbcfnd1 (tp, "OPT_ELEM", &cp_optelem); c_tbcfnd1 (tp, "HALOWAVE", &cp_refwave); c_tbcfnd1 (tp, "HALDIM", &cp_haldim); c_tbcfnd1 (tp, "HALO", &cp_halo); if (cp_optelem == 0 || cp_refwave == 0 || cp_haldim == 0 || cp_halo == 0) { printf( "ERROR Column not found in HALOTAB\n"); c_tbtclo (tp); return (COLUMN_NOT_FOUND); } k = 0; for (row = 1; row <= nrows ; row++) { c_tbegtt (tp, cp_optelem, row, opt_elem, STIS_CBUF-1); c_tbegtd (tp, cp_refwave, row, &rw); if (c_iraferr()) return (TABLE_ERROR); if ((rw == scf->kernw[k]) && (streq_ic (opt_elem, scf->opt_elem))) { c_tbegti (tp, cp_haldim, row, &haldim); /* This awful code is a consequence of the change in reference file format that took place after the original code was completed. */ switch (k) { case 0: if ((status = Alloc2DImage (halo1, haldim, haldim))) return (status); i = c_tbagtr (tp, cp_halo, row, halo1->pix, 1, halo1->npix); if (c_iraferr()) return (TABLE_ERROR); break; case 1: if ((status = Alloc2DImage (halo2, haldim, haldim))) return (status); i = c_tbagtr (tp, cp_halo, row, halo2->pix, 1, halo2->npix); if (c_iraferr()) return (TABLE_ERROR); break; case 2: if ((status = Alloc2DImage (halo3, haldim, haldim))) return (status); i = c_tbagtr (tp, cp_halo, row, halo3->pix, 1, halo3->npix); if (c_iraferr()) return (TABLE_ERROR); break; default: break; } k++; } } if (k == 0) { printf ("ERROR No matching rows in HALOTAB `%s'\n", name); return (TABLE_ERROR); } c_tbtclo (tp); return (STIS_OK); }