Пример #1
0
int findPreScanBias(SingleGroup *raz, float *mean, float *sigma){
    /** this calls resistmean, which does a better job clipping outlying pixels
        that just a standard stddev clip single pass*/
        
    extern int status;
    
    int i,j,k;              /*Looping variables */
    float plist[55377];    /*bias pixels to measure*/
    float min=0.0;
    float max=0.0;
    float rmean;
    float rsigma;
    float sigreg =7.5; /*sigma clip*/
    
    int subcol = RAZ_COLS/4;
    int npix=0; /*track array size for resistant mean*/
    rmean=0.;
    rsigma=0.;
    
    /*init plist*/
    for (i=0;i<55377;i++){
        plist[i]=0.;
    }
    
    for (k=1;k<=4;k++){  /*for each quadrance cdab*/
        npix=0;
        for (i=5;i<25; i++){ /*specific area fr post scan bias pixels*/
            for (j=0; j<2051; j++){
                if (npix < 55377 ){
                    plist[npix] = Pix(raz->sci.data,i+(k-1)*subcol,j);
                    npix+=1;
                }
            }
        }
        
        resistmean(plist, npix, sigreg, &rmean, &rsigma, &min, &max);
        mean[k]= rmean;
        sigma[k] = rsigma;        
    }
    return status;
}
Пример #2
0
int findPostScanBias(SingleGroup *raz, float *mean, float *sigma){

    extern int status;
    
    int i,j,k;              /*Looping variables */
    float plist[55377];  /*bias bpixels to measure*/
    float min=0.0;
    float max=0.0;
    float rmean=0.0;
    float rsigma=0.0;
    float sigreg =7.5; /*sigma clip*/
    

    int subcol = RAZ_COLS/4;
    int npix=0; /*track array size for resistant mean*/

    /*init plist*/
    for (i=0;i<55377;i++){
        plist[i]=0.;
    }
        
    for (k=1;k<=4;k++){  /*for each quadrant cdab = 0123*/
        npix=0;
        for (i=RAZ_ROWS+5;i<= subcol-1; i++){ /*specific area for post scan bias pixels*/
            for (j=0; j<2051; j++){
                if (npix < 55377){
                    plist[npix] = Pix(raz->sci.data,i+(k-1)*subcol,j);
                    npix+=1;
                }
            }
        }
                
        resistmean(plist, npix, sigreg, &rmean, &rsigma, &min, &max);
        mean[k]= rmean;
        sigma[k] = rsigma;
        
    }
    return status;
}
Пример #3
0
int blevcorr (WF3Info *wf3, SingleNicmosGroup *input) {

/* Arguments:
**	wf3	 i: WFC3 info structure
**	input	io: image to be bias corrected
*/

	/* Local variables */
	int i, j, q;		/* loop limits */
	int i1, i2, j1, j2;	/* pixel indexes */
	float mean;		/* ref pixel stats */
	float stdv, min, max;	/* more stats */
	float *refpix;		/* array to hold all the reference pixels */
	int arrsize;		/* size of ref pixel array */        
	float sigrej;		/* rejection limit for statistics */
	int pixcount;		/* counter for reference pixels */
           
	/* Function definitions */
	int resistmean(float *, int, float, float *,float *,float *,float *);

	/* Set defaults */
	sigrej = 3.0; /*sigma rejection limit for mean calculation*/
    
	/* Allocate memory for the temporary reference pixel array:
	   there are 5 pixels on each end of each row, but 1 is ignored
	   on each side for a total of 8 being used per row */
	arrsize= (input->sci.data.ny) * 8;
	refpix = (float *) calloc(arrsize, sizeof(float));
	if (refpix == NULL) {
	    sprintf (MsgText, "Memory allocation failure in blevcorr");
	    trlerror (MsgText);
	    return (status = 1);
	}
    
	/* zero out the memory here just to be sure */
	for (i=0; i<arrsize; i++)
	     refpix[i]=0.0;
        
	/* Loop over the 4 quads of the image to gather the reference 
	   pixels into a new array that can be passed to the resistant 
	   mean stats routine */
	pixcount = 0;
	for (q = 1; q <= 4; q++) {      
        
	     /* Set the bounds of the ref pixels in each quadrant;
	     ** note that these are zero-indexed. */
	     switch (q) {
		case 1: i1 = wf3->biassecta[0];
			i2 = wf3->biassecta[1];
			j1 = (input->sci.data.ny / 2);
			j2 = input->sci.data.ny - wf3->trimy[1] - 1;
			for (j=j1; j<=j2; j++) {
			     for (i=i1; i<=i2; i++) {
				  refpix[pixcount]=Pix(input->sci.data,i,j);
				  pixcount++;
			     }
			}
			break;

		case 2: i1 = wf3->biassecta[0];
			i2 = wf3->biassecta[1];
			j1 = wf3->trimy[0];
			j2 = (input->sci.data.ny / 2) - 1;
			for (j=j1; j<=j2; j++) {
			     for (i=i1; i<=i2; i++) {
				  refpix[pixcount]=Pix(input->sci.data,i,j);
				  pixcount++;
			     }
			}
			break;

		case 3: i1 = wf3->biassectb[0];
			i2 = wf3->biassectb[1];
			j1 = wf3->trimy[0];
			j2 = (input->sci.data.ny / 2) - 1;
			for (j=j1; j<=j2; j++) {
			     for (i=i1; i<=i2; i++) {
				  refpix[pixcount]=Pix(input->sci.data,i,j);
				  pixcount++;
			     }
			}
			break;

		case 4: i1 = wf3->biassectb[0];
			i2 = wf3->biassectb[1];
			j1 = (input->sci.data.ny / 2);
			j2 = input->sci.data.ny - wf3->trimy[1] - 1;
			for (j=j1; j<=j2; j++) {
			     for (i=i1; i<=i2; i++) {
				  refpix[pixcount]=Pix(input->sci.data,i,j);
				  pixcount++;
			     }
			}
	     }
	                  
	}
     
	/* Compute stats of the ref pixels */
	if (resistmean(refpix, pixcount, sigrej, &mean, &stdv, &min, &max))
	    return (status = 1);
         
	/* Record the computed mean in the header for the science extension*/
	if (putKeyF (&input->sci.hdr, "MEANBLEV", mean, ""))
	    return (status = 1);
     
	/* Subtract the mean value from entire image */
	for (j=0; j < input->sci.data.ny; j++) {
	     for (i=0; i < input->sci.data.nx; i++) {
		  Pix(input->sci.data,i,j) -= mean;
	     }
	}

	/* Free local memory */
	free (refpix);

	/* Successful return */
	return (status = 0);
}
Пример #4
0
void rej_sky (char *sky, IODescPtr ipsci[], IODescPtr ipdq[], int nimgs,
	      short badinpdq, float efac[], DataUnits bunit[], float skyval[]) {

/* Revision history:
**
** H. Bushouse	18-Jun-2002	Made bin width checking more robust. Also
**				allocate and free histogram for each image
**				(following CALACS changes).
** H. Bushouse	06-Dec-2007	Added calls to getHeader before each call to
**				getShortLine to prevent getShortLine from
**				crashing on null DQ arrays.
** H. Bushouse	22-May-2008	Avoid arithmetic overflow in binning
**				calculations.
** H. Bushouse	08-Oct-2008	Added capabilities for "mean" sky calculation
**				mode, using resistmean function.
** H. Bushouse	14-Dec-2011	Upgraded to rescale input data that are in
**				units of count rates. (PR 69969; Trac #814)
*/

    extern int status;

    int         *histgrm;   /* pointer to the histogram */
    int         nbins;      /* number of bins */
    int         min, max;
    float       data_min;
    float       hwidth;     /* histogram resolution */
    float       hmin;       /* minimum histogram value */
    int         i, k, h;
    float       *a;
    short       *b;
    int         line, npt;
    int         dimx, dimy;
    float       sum, mean;
    Hdr		dqhdr;

    Bool   mode, rmean;	    /* sky calculation mode flags */
    float *skyarr;	    /* pointer to sky values array */
    float  ssig, smin, smax; /* sky resistantmean values */

    float cr_mode (int *, int, float, float);
    int	  resistmean (float *, int, float, float *, float *, float *, float *);

    /* -------------------------------- begin ------------------------------- */

    nbins=0;
    min=0;
    max=0;
    hwidth=0.0f;
    hmin=0.0f;
    npt=0;
    histgrm=NULL;
    skyarr=NULL;

    /* decide what to do according to sky */
    if (strcmp (sky, "none") == 0) {
        for (k = 0; k < nimgs; ++k) {
            skyval[k] = 0.;
        }
        return;
    } else if (strcmp (sky, "mode") == 0) {
	mode = True;
	rmean = False;
    } else if (strcmp (sky, "mean") == 0) {
	rmean = True;
	mode = False;
    } else {
        trlerror ("illegal sky value");
        status = INVALID_VALUE;
        return;
    }

    dimx = getNaxis1(ipsci[0]);
    dimy = getNaxis2(ipsci[0]);

    a = (float *) calloc (dimx, sizeof(float));
    b = (short *) calloc (dimx, sizeof(short));
    if (a == NULL || b == NULL) {
        trlerror ("Couldn't allocate memory for sky arrays");
        status = OUT_OF_MEMORY;
        return; 
    }

    if (mode) {

	/* compute MIN and MAX values for data */
	/* use the minimum and twice of the mean to determine the data range */
	data_min = INT_MAX;
	sum = 0.;
	npt = 0;

	initHdr (&dqhdr);
	getHeader(ipdq[0], &dqhdr);

	for (line = 0; line < dimy; line++) {

	     /* read the data in */
	     getFloatLine (ipsci[0], line, a);
	     getShortLine (ipdq[0], line, b);

	     /* Rescale data to counts, if necessary */
	     if (bunit[0] == COUNTRATE) {
		 for (i = 0; i < dimx; ++i) {
		      a[i] *= efac[0];
		 }
	     }

	     for (i = 0; i < dimx; ++i) {
		  if ( (b[i] & badinpdq) == WF3_OK) {
			data_min = (a[i] < data_min) ? a[i] : data_min;
			sum += a[i];
			npt++;
		  }
	     }
	} /* End of loop over lines */

	freeHdr(&dqhdr);

	/* Compute min and max for histogram.
	MIN is min of good data or MINVAL, which ever is greater
	DELTA is difference between mean of data and MIN
	MAX is mean plus DELTA
	This insures that the mean falls in the center of the range
	between min and max. */
	if (npt == 0) npt = 1;
	min = (data_min < MINVAL) ? MINVAL : data_min;
	mean = (sum > 0.) ? (int) ( (sum / (float)npt) + 1) : 1;
	max = 2 * mean - min;
    
	/* use the mode as sky value, use the bin size of 1 (DN) */
	nbins = max - min + 1; 

	/* Insure that there are at least MIN_BINS in the histogram
	and reset the width accordingly. */ 
	if (nbins < MIN_BINS) {
	    nbins = MIN_BINS;
	    hwidth = (float) nbins / (float)MIN_BINS;
	} else if (nbins > MAX_BINS) {
	    hwidth = (float) nbins / (float)MAX_BINS;
	    nbins = MAX_BINS;
	} else {
	    hwidth = 1.;
	}
	hmin = (float) min;

	/*
	sprintf (MsgText, 
	"sky computed using min %d, max %d, mean %g, bins %d, and width %g",
	min, max, mean, nbins, hwidth);
	trlmessage (MsgText);
	*/
    }

    /* Now loop over the input images, computing the sky value for each
    ** image, using either the mode or the resistant mean */
    for (k = 0; k < nimgs; ++k) {

	if (mode) {
	    /*  set up a new histogram array for each image */
	    histgrm = (int *) calloc (nbins, sizeof(int));
	    if (histgrm == NULL){
		    trlerror ("Couldn't allocate memory for sky histogram array");
		    status = OUT_OF_MEMORY;
		return;   
	    }
	} else if (rmean) {
	    skyarr = (float *) calloc (dimx*dimy, sizeof(float));
	    if (skyarr == NULL){
		trlerror ("Couldn't allocate memory for sky array");
		status = OUT_OF_MEMORY;
		return;   
	    }
	    npt = 0;
	}

        initHdr (&dqhdr);
        getHeader (ipdq[k], &dqhdr);
        for (line = 0; line < dimy; line++) {

            /* read the data in */
            getFloatLine (ipsci[k], line, a);
            getShortLine (ipdq[k], line, b);

	    /* Rescale data to counts, if necessary */
	    if (bunit[k] == COUNTRATE) {
		for (i = 0; i < dimx; ++i) {
		     a[i] *= efac[k];
		}
	    }

            /* construct the histogram for the mode calculation */
	    if (mode) {
		for (i = 0; i < dimx; ++i) {
		     if (b[i] == WF3_OK) {

			 /* Adjust the bin position by the width of each bin */
			 if (fabs((a[i]-min)/hwidth) < INT_MAX) {
			     h = (int)((a[i] - min) / hwidth);
			     if (h >= 0 && h < nbins)
				 histgrm[h]++;
			 }
		     }
		}

	    /* load the sky array for calculating the mean */
	    } else if (rmean) {
		for (i = 0; i < dimx; ++i) {
		     if (b[i] == WF3_OK) {
			 skyarr[npt] = a[i];
			 npt++;
		     }
		}
	    }
        } /* End of loop over lines */
        freeHdr(&dqhdr);

        /* calculate the mode from the histogram */
	if (mode) {
            skyval[k] = cr_mode (histgrm, nbins, hwidth, hmin);
            free (histgrm);

	/* calculate the resistant mean */
	} else if (rmean) {
	    resistmean (skyarr, npt, SIGREJ, &skyval[k], &ssig, &smin, &smax);
	    free (skyarr);
	}
    }

    free(a);
    free(b);
}