Skip to content

Commit

Permalink
ACSREJ algorithm modified for ERR array (#561)
Browse files Browse the repository at this point in the history
* Update to the cosmic ray rejection algorithm as to the way
the output ERR extension is computed for the CRJ file. The
output ERR is now propagated from the usable input ERR extensions
versus being computed by a model.

* Modified code to ensure the ERR extension of the CRC/CRJ file is
always a two-dimensional image, even if the entire image could be
represented by a single value.  This change was trigged by INS testing,
and to avoid possible confusion by users.
  • Loading branch information
mdlpstsci authored Feb 8, 2022
1 parent 9b4d921 commit 1a22b1f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
4 changes: 4 additions & 0 deletions pkg/acs/calacs/Dates
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
08-Feb-2022 CALACS 10.3.5 Update to the cosmic ray rejection algorithm as to the way
the output ERR extension is computed for the CRJ file. The
output ERR is now propagated from the usable input ERR extensions
versus being computed by a model.
11-Jan-2022 CALACS 10.3.4 Fixed a bug which was triggered ONLY when executing the condaforge
distribution on MacOS where failure was due to a missing return statement.
Only call computeDarktime() once to compute the updated
Expand Down
6 changes: 6 additions & 0 deletions pkg/acs/calacs/History
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 08-Feb-2022 - MDD - Version 10.3.5
Update to the cosmic ray rejection algorithm as to the way
the output ERR extension is computed for the CRJ file. The
output ERR is now propagated from the usable input ERR extensions
versus being computed by a model.

### 11-Jan-2022 - MDD - Version 10.3.4
Fixed a bug which was triggered ONLY when executing the condaforge
distribution on MacOS where failure was due to a missing return
Expand Down
8 changes: 8 additions & 0 deletions pkg/acs/calacs/Updates
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Update for version 10.3.5 - 08-Feb-2022 (MDD)
pkg/acs/calacs/Dates
pkg/acs/calacs/History
pkg/acs/calacs/Updates
pkg/acs/calacs/include/acsversion.h
pkg/acs/calacs/acsrej/acsrej_do.c
pkg/acs/calacs/acsrej/acsrej_loop.c

Update for version 10.3.4 - 11-Jan-2022 (MDD)
pkg/acs/calacs/Dates
pkg/acs/calacs/History
Expand Down
7 changes: 5 additions & 2 deletions pkg/acs/calacs/acsrej/acsrej_do.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,13 @@ int acsrej_do (IRAFPointer tpin, char *outfile, char *mtype, clpar *par,
/* SCI: Convert e/s to electrons, add sky back in */
PPix(&sg.sci.data, i, j) = (PPix(&sg.sci.data, i, j) *
texpt) + skysum;
/* ERR: Convert e/s to electrons */
PPix(&sg.err.data, i, j) *= texpt;
}
}
/* Set at least one pixel to a different value to insure
that an image array actually gets produced. This was done
so the result of a test where the input ERR are all zero will
produce an actual output ERR array. */
PPix(&sg.err.data, 0, 0) = -1.;
} else {
/* DQ values here do not make sense, but I guess it is not
a real issue because the output is dummy anyway. */
Expand Down
47 changes: 41 additions & 6 deletions pkg/acs/calacs/acsrej/acsrej_loop.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# include <stdio.h>
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
Expand Down Expand Up @@ -142,6 +142,9 @@ Code Outline:
for BIAS and DARK images. Bad pixels (BPIXTAB flag of 4)
are ignored during the combination process for the
SCI and ERR arrays (i.e. treat bad pixels as normal pixels).
18-Jan-2022 M.D. DeLaPena Modified the computation of the output ERR image so
the output in the CRJ file is propagated from the input
ERR images, using only non-CR pixels (Git Issue#371).
*/
int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],
char imgname[][CHAR_FNAME_LENGTH], int grp [], int nimgs,
Expand Down Expand Up @@ -182,7 +185,7 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],
comparison during CR rejection.
As output, it is the average image also in e/s,
recalculated from good pixels only.
avevar o: ERR array in e/s, recalculated from good pixels only.
avevar o: ERR array in e, recalculated from good pixels only.
efacsum o: Array of total exposure times, in seconds, for each of
the pixels. This is calculated from the number of good
pixels left for that location after CR rejection.
Expand All @@ -209,6 +212,7 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],
short maskdq;

float efacn, skyvaln;
float exptot; /* Total exposure time of all images from TEXPTIME and EXPTIME */
int readnoise_only;

ShortTwoDArray dq2; /* local array for storing output blv DQ vals */
Expand All @@ -218,10 +222,13 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],
float ***thresh, ***spthresh;
short ***mask;

float ***scroll_buferr;

float *sum;
float *sumvar;
float *buf, *buferr;
short *bufdq;
float *err2;

Byte ***crmask; /* Compressed CR HIT mask for all images */

Expand Down Expand Up @@ -310,12 +317,18 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],
trlerror ("Couldn't allocate memory for scratch array in ACSREJ_LOOP.");
return (status = OUT_OF_MEMORY);
}

scroll_buferr= (float ***) calloc (nimgs, sizeof(float **));
if (scroll_buferr== NULL) {
trlerror ("Couldn't allocate memory for scratch array in ACSREJ_LOOP.");
return (status = OUT_OF_MEMORY);
}
for (k = 0; k < nimgs; k++) {
mask[k] = allocShortBuff (buffheight, dim_x);
pic[k] = allocFloatBuff(buffheight, dim_x);
thresh[k] = allocFloatBuff (buffheight, dim_x);
spthresh[k] = allocFloatBuff (buffheight, dim_x);

scroll_buferr[k] = allocFloatBuff (buffheight, dim_x);
}

zerofbuf = calloc (dim_x, sizeof(float));
Expand All @@ -328,6 +341,8 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],
buferr = calloc (dim_x, sizeof(float));
bufdq = calloc (dim_x, sizeof(short));

err2 = calloc(dim_x, sizeof(float));

/*
If we want to perform SHADCORR here, ...
*/
Expand Down Expand Up @@ -413,8 +428,10 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],
rog2[k] = SQ(noise.val[k]); /* e^2, copied to noise2 below */
}

exptot = 0.0;
for (n = 0; n < nimgs; n++) {
exp2[n] = SQ(efac[n]); /* s^2 */
exptot += efac[n];
}

/* Set up gain and values used for each image */
Expand Down Expand Up @@ -448,6 +465,8 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],
memcpy (pic[k][j], zerofbuf, dim_x * sizeof(float));
memcpy (thresh[k][j], zerofbuf, dim_x * sizeof(float));
memcpy (spthresh[k][j], zerofbuf, dim_x * sizeof(float));

memcpy (scroll_buferr[k][j], zerofbuf, dim_x * sizeof(float));
}
}
} /* End initialization section */
Expand All @@ -458,6 +477,7 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],
/* Zero out this buffer for the next line */
memcpy (sum, zerofbuf, dim_x * sizeof(float));
memcpy (sumvar, zerofbuf, dim_x * sizeof(float));
memcpy (err2, zerofbuf, dim_x * sizeof(float));

/* Set up the gain and noise values used for this line in
ALL images */
Expand Down Expand Up @@ -528,6 +548,9 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],
scrollFloatBuff (buferr, line, dim_y, buffheight, dim_x,
spthresh[k], zerofbuf);

scrollFloatBuff (buferr, line, dim_y, buffheight, dim_x,
scroll_buferr[k], zerofbuf);

/* Recalculate thresholds properly. Only the last inserted
row of the scrolling buffer needs to be recalculated.
Expand Down Expand Up @@ -555,6 +578,8 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],
InitFloatSect (pic[k], buf, ipsci[k], line, width, dim_x);
InitShortSect (mask[k], bufdq, ipdq[k], line, width, dim_x);

InitFloatSect (scroll_buferr[k], buferr, iperr[k], line, width, dim_x);

/* No data when (ii < width), just zeroes */
for (ii = width; ii < buffheight; ii++) {
/* Scale the pic value by the sky value and exposure
Expand Down Expand Up @@ -737,6 +762,10 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],
/* add the sky-subtracted but UN-scaled counts */
sum[i] += pic[n][width][i] * efacn; /* e */
PIX(efacsum, i, line, dim_x) += efacn; /* s */

/* add the error contributions from each image for a pixel which is not rejected
* err2 is in e^2 */
err2[i] += SQ(scroll_buferr[n][width][i]);
}
}

Expand Down Expand Up @@ -769,6 +798,7 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],
sumvar[i] += nse[0];
}
}

} /* End of loop over first amp used for line */

/* SECOND AMP */
Expand All @@ -795,6 +825,8 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],
}
} /* End of loop over second amp used for line */

/* If pixels are not marked as rejected, then use the
exposure and error data for the output error computation */
for (i = 0; i < dim_x; i++) {
/* output DQF is just the logical OR of all
(original) input DQF */
Expand Down Expand Up @@ -858,7 +890,7 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],

/* BOTH AMPS */
for (i = 0; i < dim_x; i++) {
/* Total EXPTIME (in seconds) of from images where this pixel
/* Total EXPTIME (in seconds) from images where this pixel
is not flagged as CR. */
pixexp = PIX(efacsum, i, line, dim_x);

Expand All @@ -872,10 +904,10 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],

/* For final iter, convert variance to error.
sumvar is e^2
avevar is e/s; goes to ERR
avevar is e; goes to ERR
*/
if (iter == (niter - 1)) {
PPix(avevar, i, line) = sqrt(sumvar[i]) / pixexp;
PPix(avevar, i, line) = (exptot / pixexp) * sqrt(err2[i]);
}
} else {
if (iter == (niter - 1)) {
Expand Down Expand Up @@ -942,18 +974,21 @@ int acsrej_loop (IODescPtr ipsci[], IODescPtr iperr[], IODescPtr ipdq[],
free (sumvar);
free (zerofbuf);
free (zerosbuf);
free (err2);

for (k = 0; k < nimgs; k++) {
freeFloatBuff (pic[k], buffheight);
freeFloatBuff (thresh[k], buffheight);
freeFloatBuff (spthresh[k], buffheight);
freeShortBuff (mask[k], buffheight);
freeFloatBuff (scroll_buferr[k], buffheight);
}

free (mask);
free (pic);
free (thresh);
free (spthresh);
free (scroll_buferr);
free (buf);
free (buferr);
free (bufdq);
Expand Down
4 changes: 2 additions & 2 deletions pkg/acs/calacs/include/acsversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#define INCL_ACSVERSION_H

/* This string is written to the output primary header as CAL_VER. */
#define ACS_CAL_VER "10.3.4 (11-Jan-2022)"
#define ACS_CAL_VER_NUM "10.3.4"
#define ACS_CAL_VER "10.3.5 (08-Feb-2022)"
#define ACS_CAL_VER_NUM "10.3.5"

/* name and version number of the CTE correction algorithm */
#define ACS_GEN1_CTE_NAME "PixelCTE 2012"
Expand Down

0 comments on commit 1a22b1f

Please sign in to comment.