Esempio n. 1
0
int GetAsnName (char *filename, char *asn_name) {

    extern int status;
    IODescPtr im;           /* descriptor for an image */
    Hdr phdr;               /* primary header */

    /* Function definitions */
    int GetKeyStr (Hdr *, char *, int, char *, char *, int);

    /* Read primary header of ASN file into hdr. */
    initHdr (&phdr);
    im = openInputImage (filename, "", 0);
    if (hstio_err())
        return (status = OPEN_FAILED);

    getHeader (im, &phdr);          /* get primary header */
    if (hstio_err())
        return (status = OPEN_FAILED);

    closeImage (im);

    asn_name[0] = '\0';
    if (GetKeyStr (&phdr, "ASN_TAB", 0, "", asn_name, SZ_FITS_REC)) {
        trlkwerr ("ASN_TAB", asn_name);
        return (status = KEYWORD_MISSING);
    }

    /* Close the file's primary header. */
    freeHdr (&phdr);

    /* Successful return */
    return (status);

}
Esempio n. 2
0
static int UpdateHdr (char *output) {

    extern int status;

    Hdr phdr;               /* primary header */
    IODescPtr im;		/* descriptor for output image */

    int PutKeyBool (Hdr *, char *, Bool, char *);

    sprintf(MsgText, "Trying to open %s...",output);
    trlmessage (MsgText);

    initHdr (&phdr);

    /* Open input image in order to read its primary header. */
    im = openUpdateImage (output, "", 0, &phdr);				
    if (hstio_err()) {
        trlopenerr (output);
        closeImage(im);
        return (status = OPEN_FAILED);
    }

    if (PutKeyBool (&phdr, "ASN_PROD", True, "") ) {
        freeHdr (&phdr);
        trlerror ("Couldn't update ASN_PROD keyword in ASN table header");
        return(status = KEYWORD_MISSING);
    }

    /* write out primary header */
    if (putHeader (im))
        status = HEADER_PROBLEM;	
    if (hstio_err() || status) {
        trlreaderr (output);
        closeImage (im);
        return (status = OPEN_FAILED);
    }

    closeImage (im);
    /* Close the ASN table's primary header. */
    freeHdr (&phdr);

    sprintf(MsgText, "Updated Global Header for %s...",output);
    trlmessage (MsgText);

    return (status);

}
Esempio n. 3
0
int CheckDetector (char *image, int detector, char *keyword, int *badtype) {

	extern int status;

	FitsKw key;		/* location of keyword in header */
	IODescPtr im;		/* descriptor for primary header unit */
	Hdr phdr;		/* primary header */

	char keyval[SZ_FITS_REC+1];

	keyval[0] = '\0';
	initHdr (&phdr);

	/* Open the primary header of the reference file. */
	im = openInputImage (image, "", 0);
	getHeader (im, &phdr);
	if (hstio_err())
	    return (status = HEADER_PROBLEM);

	/* Get the DETECTOR keyword. */
	key = findKw (&phdr, keyword);
	if (key == NotFound) {
	    trlkwerr (keyword, image);
	    return (status = KEYWORD_MISSING);
	} else {
	    getStringKw (key, keyval, SZ_FITS_REC);
	    if (hstio_err()) {
		trlkwerr (keyword, image);
		return (status = KEYWORD_MISSING);
	    }
	}

	/* Does the ref file DETECTOR value match the science image? */
	if (detector == IR_DETECTOR) {
	    if (strncmp (keyval, "IR", strlen(keyval)) != 0) {
		sprintf (MsgText, "%s %s='%s' does not match science data",
			 image, keyword, keyval);
		trlerror (MsgText);
		(*badtype)++;
	    }
	} else {
	    if (strncmp (keyval, "UVIS", strlen(keyval)) != 0) {
		sprintf (MsgText, "%s %s='%s' does not match science data",
			 image, keyword, keyval);
		trlerror (MsgText);
		(*badtype)++;
	    }
	}

	/* Close the reference file. */
	closeImage (im);
	freeHdr (&phdr);

	return (status);
}
Esempio n. 4
0
int CheckGain (char *image, float gain, char *keyword, int *badtype) {

	extern int status;

	FitsKw key;		/* location of keyword in header */
	IODescPtr im;   /* descriptor for primary header unit */
	Hdr phdr;		/* primary header */

	float keyval;

	initHdr (&phdr);

	/* Open the primary header of the reference file. */
	im = openInputImage (image, "", 0);
	getHeader (im, &phdr);
	if (hstio_err())
	    return (status = HEADER_PROBLEM);

	/* Get the CCDGAIN keyword. */
	key = findKw (&phdr, keyword);
	if (key == NotFound) {
	    trlkwerr (keyword, image);
	    return (status = KEYWORD_MISSING);
	} else {
	    keyval = getFloatKw (key);
	    if (hstio_err()) {
		trlkwerr (keyword, image);
		return (status = KEYWORD_MISSING);
	    }
	}

	/* Does the ref file CCDGAIN value match the science image? */
	/* A value of -1 is considered to be OK */
	if ((keyval != -1) && (gain != keyval)) {
	    sprintf (MsgText, "%s %s=%g does not match science data",
		     image, keyword, keyval);
	    trlerror (MsgText);
	    (*badtype)++;
	}

	/* Close the reference file. */
	closeImage (im);
	freeHdr (&phdr);

	return (status);
}
Esempio n. 5
0
int LoadHdr (char *input, Hdr *phdr) {

	extern int status;
	
	IODescPtr im;		/* descriptor for input image */
   	
	sprintf(MsgText, "Trying to open %s...",input);
	trlmessage (MsgText);

	/* Open input image in order to read its primary header. */
	im = openInputImage (input, "", 0);				

	if (hstio_err()) {
		trlopenerr (input);
	    return (status = OPEN_FAILED);
	}
		
    initHdr (phdr);	

	/* get primary header */
	if (getHeader (im, phdr) )
		status = HEADER_PROBLEM;	
	if (hstio_err() || status) {
		trlreaderr (input);
		closeImage (im);
		freeHdr (phdr);
	    return (status = OPEN_FAILED);
	}
	
	closeImage (im);
    
	sprintf(MsgText, "Read in Primary header from %s...",input);
	trlmessage (MsgText);

	return (status);
}	
Esempio n. 6
0
int GetGlobalInfo (AsnInfo *asn) {

    /* Arguments:
     **	asn	io: association info structure
     */
    extern int status;
    Hdr phdr;               /* primary header */

    char detector[SZ_FITS_REC+1];

    /* Function definitions */
    int GetKeyStr (Hdr *, char *, int, char *, char *, int);
    int LoadHdr (char *, Hdr *);
    /*	int GetSwitch (Hdr *, char *, int *); */

    if (asn->debug) {
        trlmessage ("GetGlobalInfo: Ready to open primary header... ");
    } 

    if (asn->debug) {
        sprintf(MsgText, "GetGlobalInfo: asn_table is %s",asn->asn_table);
        trlmessage (MsgText);
    }

    /* Read primary header of ASN file into phdr. */
    if (LoadHdr (asn->asn_table, &phdr)) {
        sprintf (MsgText, "Could not load header from table %s",
                asn->asn_table);
        trlerror (MsgText);
        return (status);
    }
    if (asn->debug) {
        trlmessage ("GetGlobalInfo: Read in header from Image");
    }	

    /* Get the observing mode keyword values from header */
    asn->instr[0] = '\0';
    if (GetKeyStr (&phdr, "INSTRUME", 0, "", asn->instr, SZ_FITS_REC)) {
        trlkwerr ("INSTRUME", asn->asn_table);
        return (status = KEYWORD_MISSING);
    }

    asn->detector = 0;
    detector[0]   = '\0';
    if (GetKeyStr (&phdr, "DETECTOR", 0, "", detector, SZ_FITS_REC)) {
        trlkwerr ("DETECTOR", asn->asn_table);
        return (status = KEYWORD_MISSING);
    }

    /* Convert detector string to usable value */
    if (strncmp (detector, "UVIS", 4) == 0) {
        asn->detector = CCD_DETECTOR;
    } else if (strncmp (detector, "IR", 2) == 0) {
        asn->detector = IR_DETECTOR;
    } else {
        asn->detector = UNKNOWN_DETECTOR;
        return (status = HEADER_PROBLEM);
    }

    checkGlobalInfo(asn);

    /* You can NOT create a summed image with only 1 input */
    if (asn->process == SINGLE) {
        asn->rptcorr = OMIT;
    }

    /* If we are not processing an entire association, then
     ** we will not have the inputs necessary for a DTHCORR. */
    /*	if (dthcorr == PERFORM) { */
    if (asn->process == SINGLE) {
        asn->dthcorr = DUMMY;
    }
    /*	} */

    /* Otherwise, leave asn->dthcorr as set by reading ASN table itself */

    /* Close the ASN table's primary header here. */
    freeHdr (&phdr);



    if (asn->debug) {
        trlmessage ("GetGlobalInfo: Detector and Instrument determined");
    }	

    /* Successful return */
    return (status);
}
Esempio n. 7
0
/*
Description:
------------
If using the exposure time, the scaling factors are normalized to ratios 
relative to the max exposure. 

    Date            Author      Description
    ----            ------      -----------
    24-Sep-1998     W.J. Hack   Initial Version
    18-Mar-1999     W.J. Hack   Revised to read EXPTIMEs from Primary headers
                                using image-template list directly
    20-Oct-1999     W.J. Hack   Revised to compute number of good input images
                                and insure they are less than MAX_FILES.
    14-Apr-2000     W.J. Hack   Revised to also return final EXPEND appropriate
                                for output CR-combined product
    14-Mar-2002     W.J. Hack   Added computation of cumulative DARKTIME
    4-Apr-2002      W.J. Hack   added initialization of 'totd'
   24-Apr-2002      W.J. Hack   removed darktime altogether, find initial EXPSTART
*/
int cr_scaling (char *expname, IRAFPointer tpin, float efac[], int *nimgs, double *expend, double *expstart)
{
    extern int status;

    Hdr         prihdr;
    int         nzero, k;
    char        fdata[CHAR_FNAME_LENGTH + 1];
    IODescPtr   ip;
    int         numimgs;        /* How many good input images are there? */

    double     end, keyend, keystart, start;

    int         GetKeyFlt (Hdr *, char *, int, float, float *);
    int         GetKeyDbl (Hdr *, char *, int, double, double *);
    /* -------------------------------- begin ---------------------------------- */

    /* Rewind the image template pointer */
    c_imtrew(tpin);

    *nimgs = c_imtlen(tpin);
    end = 0.0;
    keyend = 0.0;
    start = 1e+10;
    keystart = 0.0;

    
    /* Check to make sure there are not too many images to work with... */
    if (*nimgs > MAX_FILES) {
        trlerror("There are too many input images to combine. "); 
        return(status = NOTHING_TO_DO);
    }

    /* if the parameter scaling is null, all images have equal weight. 
        If no keyword name is given for the exposure time, assume equal
        weights of 1 for all images.
    */
    if (expname[0] == '\0') {
        return (status);
    }

    /* Use exposure time as scaling factor */
    nzero = 0;	
     
    /* loop all input files counting how many usable inputs there are */
    numimgs = 0;
    for (k = 0; k < *nimgs; ++k) {

        /* read the next input image name in the template list */
        c_imtgetim (tpin, fdata, CHAR_FNAME_LENGTH);

        /* open the primary header */
        ip = openInputImage (fdata, "", 0);
        if (hstio_err()) {
            sprintf (MsgText, "Cannot open data file '%s'", fdata);
            trlerror (MsgText);
            return (status = OPEN_FAILED);
        }

        initHdr (&prihdr);

        /* read in primary header from image */
        getHeader (ip, &prihdr);

        if (GetKeyFlt (&prihdr, expname, USE_DEFAULT, 0., &efac[k]) != 0) {
            sprintf (MsgText, "cannot read '%s' from the primary header of '%s'", expname, fdata);
            trlerror (MsgText);
            freeHdr (&prihdr);
            return(status = KEYWORD_MISSING);
        }
        
        if (efac[k] < 0.) {
            sprintf (MsgText, "exposure time of file '%s' is negative", fdata);
            trlerror (MsgText);
            freeHdr (&prihdr);
            return(status = INVALID_VALUE);
        }
        if (efac[k] == 0.) {
            nzero++;
        }
        
        numimgs++;
        if (GetKeyDbl (&prihdr, "EXPEND", USE_DEFAULT, 0., &keyend) != 0) {
            sprintf (MsgText, "cannot read 'EXPEND' from the primary header of '%s'", fdata);
            trlerror (MsgText);
            freeHdr (&prihdr);
            return(status = KEYWORD_MISSING);
        }
        if (GetKeyDbl (&prihdr, "EXPSTART", USE_DEFAULT, 0., &keystart) != 0) {
            sprintf (MsgText, "cannot read 'EXPSTART' from the primary header of '%s'", fdata);
            trlerror (MsgText);
            freeHdr (&prihdr);
            return(status = KEYWORD_MISSING);
        }
        
        end = (keyend > end) ? keyend: end;
        start = (keystart < start) ? keystart : start;
        closeImage (ip);
        freeHdr (&prihdr);
    }
    
    if (nzero > 0 && nzero < *nimgs) {
        trlwarn ("Some (but not all) input imsets have zero exposure time.");
        trlwarn ("Final product will be compromised!");
        
        /* This type of error will need to be handled differently in order
            to allow pipeline processing of this type of dataset. 
        return (status = INVALID_VALUE);
        */
    }
    
    /* Only return the number of valid input images,
        initial EXPSTART and final EXPEND value
    */
    *nimgs = numimgs;
    *expend = end;
    *expstart = start;
    
    return (status);
}
Esempio n. 8
0
/*  acsrej_do -- Perform the cosmic ray rejection for ACS images

  Description:
  ------------
  This is mostly a file bookkeeping routine for the cosmic ray rejection task.
  It takes care of input/output files open/close, check for dimensions, read/
  write data from/to files, allocate memory spaces etc.

    Date          Author          Description
    ----          ------          -----------
    06-May-1996   J.-C. Hsu       Adapt from the SPP code crrej_do.x
    05-Aug-1998   W. Hack         Modified to handle ACS data
    11-Feb-1999   W. Hack         EXPTIME now in Pri. Hdr.
    18-Mar-1999   W.J. Hack       Revised to read EXPTIMEs from Primary headers
                                  for cr_scaling using tpin directly
    14-Sep-1999   W.J. Hack       Cleaned up SHADCORR usage. Added check for max
                                  number of files here.
*/
int acsrej_do (IRAFPointer tpin, char *outfile, char *mtype, clpar *par, int newpar[])
{
    extern int  status;

    IODescPtr   ipsci[MAX_FILES];   /* science image descriptor */
    IODescPtr   ipdq[MAX_FILES];    /* data quality image descriptor */
    float       skyval[MAX_FILES];  /* background DN values */
    float       efac[MAX_FILES];    /* exposure factors */
    multiamp    noise;              /* readout noise */
    multiamp    gain;               /* A-to-D gain factors */
    float       exptot;
    float       texpt;
    int         nimgs;
    SingleGroup sg;
    int         niter = 0;
    float       sigma[MAX_ITER];

    Hdr         phdr;               /* primary header */
    int         extver;             /* Current extension being processed*/
    int         numext;             /* Number of extensions in each image */
    int         nextend;            /* Number of output extensions */
    char        imgname[MAX_FILES][CHAR_FNAME_LENGTH];
    char        fimage[CHAR_FNAME_LENGTH];  /* Name of first image in list */
    char        root[CHAR_FNAME_LENGTH];    /* ROOTNAME for output CRJ file */
    char        uroot[CHAR_FNAME_LENGTH];   /* Upper case version of rootname */
    char        *shadrefname;

    int         ext[MAX_FILES];
    int         dim_x, dim_y;       /* image dimensions */
    int         i, j, n;            /* loop indices */
    float       *efacsum, *work;
    int         nrej;               /* total number of rejected pixels */
    float       skysum;             /* total sky level */
    int         logit;
    RefImage    shadref;
    int         shadswitch;
    double      expend, expstart;
    int         non_zero;           /* number of input images with EXPTIME>0.*/
    int         found;
    char        imgdefault[CHAR_FNAME_LENGTH];  /* name of first input image with EXPTIME > 0. */

    int     GetSwitch (Hdr *, char *, int *);
    int     UpdateSwitch (char *, int, Hdr *, int *);

    void    InitRefImg (RefImage *);
    int     ImgHistory (const RefImage *, Hdr *);
    int     ImgPedigree (RefImage *);

    int     acsrej_check (IRAFPointer, int, int, clpar *, int [],  char [][CHAR_FNAME_LENGTH],
                int [], IODescPtr [], IODescPtr [],
                multiamp *, multiamp *, int *,
                int *, int);
    int     cr_scaling (char *, IRAFPointer, float [], int *, double *, double *);
    int     rejpar_in(clpar *, int [], int, float,   int *, float []);
    void    acsrej_sky (char *, IODescPtr [], IODescPtr [], int, short, float []);
    void    cr_history (SingleGroup *, clpar *, int);
    int     acsrej_init (IODescPtr [], IODescPtr [], clpar *, int, int, int,
                multiamp, multiamp, float [], float [],
                SingleGroup *,   float *);
    int     acsrej_loop (IODescPtr [], IODescPtr [], char [][CHAR_FNAME_LENGTH],
                int [], int, clpar *, int, int, int, float [],
                multiamp, multiamp, float [], float [],
                FloatTwoDArray *, FloatTwoDArray *, float *,
                ShortTwoDArray *, int *, char *);
    int     PutKeyFlt (Hdr *, char *, float, char *);
    int     PutKeyDbl (Hdr *, char *, double, char *);
    int     PutKeyStr (Hdr *, char *, char *, char *);
    int     GetKeyStr (Hdr *, char *, int, char *, char *, int);
    int     PutKeyInt (Hdr *, char *, int, char *);
    int     GetKeyInt (Hdr *, char *, int, int, int *);
    void    UFilename (char *, Hdr *);
    void    UMemType (char *, Hdr *);
    void    UExpname (char *, Hdr *);
    int     LoadHdr (char *, Hdr *);
    void    UpperAll (char *, char *, int);
    void    TimeStamp (char *, char *);
    void    WhichError (int);
    void    PrSwitch (char *, int);
    void    FindAsnRoot (char *, char *);
    void    initmulti (multiamp *);
    /* -------------------------------- begin ---------------------------------- */
    /* Initialize necessary structures */
    InitRefImg (&shadref);
    root[0] = '\0';
    uroot[0] = '\0';
    initmulti (&noise);
    initmulti (&gain);

    numext = 0;
    nextend = 0;


    /* Since CR-SPLIT images are in separate files, we need to
        combine the same chip's exposure from each file.  Therefore
        we will loop over each extension in the first image, determine
        what chip that corresponds to, and get the same chip from the
        rest of the images (which could be in any arbitrary
        extension in each of the images).
    */

    /* First, let's determine how many extensions/chips in each file */
    c_imtgetim (tpin, fimage, CHAR_FNAME_LENGTH);

    if (LoadHdr (fimage, &phdr) )
        return (status = ERROR_RETURN);

    if (GetKeyInt (&phdr, "NEXTEND", NO_DEFAULT, 0, &nextend) == 0)
        numext = nextend / EXT_PER_GROUP;
    else
        numext = 1;

    shadswitch = 0;
    /* Check to see if SHADCORR was set to PERFORM in image header */
    if (GetSwitch (&phdr, "SHADCORR", &shadswitch) )
        return(status);

    /* If shadcorr was set either by the user on the command line
        or in the image header, initialize shadcorr processing.
    */
    if (par->shadcorr == PERFORM || shadswitch == PERFORM) {

        /* Use par->shadcorr as switch for performing shading correction */
        par->shadcorr = PERFORM;
        shadrefname = calloc(CHAR_FNAME_LENGTH, sizeof(char));

        if (GetKeyStr (&phdr, "SHADFILE", NO_DEFAULT, "", shadrefname, CHAR_FNAME_LENGTH) )
            return(status);
        strcpy (shadref.name, shadrefname);
        /* Read in PEDIGREE and DESCRIPTION for SHADFILE */
        if (ImgPedigree (&shadref) )
            return (status);
        /* If a DUMMY shadfile was specified, turn off shadcorr */
        if (shadref.goodPedigree == DUMMY)
            par->shadcorr = OMIT;
        free (shadrefname);
    }

    freeHdr (&phdr);

    /* Initialize efac */
    for (n = 0; n < MAX_FILES; n++) efac[n] = 1.0;

    /* calculate the scaling factors due to different exposure time */
    strcpy (par->expname, "EXPTIME");
    if (cr_scaling (par->expname, tpin, efac, &nimgs, &expend, &expstart) ){
        WhichError (status);
        return (status);
    }

    /* make sure there is more than one image to process */
    if (nimgs < 2) {
        trlmessage ("Needs more than one input image.");
        return (status = NOTHING_TO_DO);
    }

    /* calculate the total exposure time */
    exptot = 0.;
    non_zero = 0;
    for (n = 0; n < nimgs; ++n) {
        exptot += efac[n];
        /* Count how many inputs have non-zero(valid) EXPTIME */
        if (efac[n] > 0.) non_zero++;
    }
    /* for the case of all images have zero exposure time, use equal
        exposure time of 1. */
    if (exptot == 0.) {
        for (n = 0; n < nimgs; ++n) {
            efac[n] = 1.;
        }
        texpt = (float) nimgs;
        non_zero = nimgs;
    } else {
        texpt = exptot;
    }

    /* Now, start the loop. */
    for (extver = 1; extver <= numext; extver++) {

        if (par->printtime) {
            TimeStamp ("Start cosmic ray rejection","");
        }

        /* open input files and temporary files, check the parameters */
        if (acsrej_check (tpin, extver, numext, par, newpar, imgname, ext,
            ipsci, ipdq, &noise, &gain, &dim_x, &dim_y, nimgs)) {
            WhichError (status);
            return(status);
        }

        /* Now that we have read in SHADCORR, report if it will be performed */
        PrSwitch ("shadcorr", par->shadcorr);

        /* read in the parameters */
        if (rejpar_in (par, newpar, nimgs, exptot,   &niter, sigma) )
            return(status);

        /* allocate array space */
        efacsum = calloc (dim_x*dim_y, sizeof(float));
        work    = calloc (nimgs*dim_x, sizeof(float));

        /* calculate the sky levels */
        acsrej_sky (par->sky, ipsci, ipdq, nimgs, par->badinpdq, skyval);
        if (status != ACS_OK) {
            WhichError (status);
            return (status);
        }
        if (par->verbose) {
            for (n = 0; n < nimgs; n++) {
                sprintf (MsgText, "sky of '%s[sci,%d]' is %0.3f DN", imgname[n], ext[n], skyval[n]);
                trlmessage (MsgText);
            }
        }

        /* use the first input image to set up the data structure */
        initSingleGroup (&sg);

        /* Find the first image in the input list which has an
            EXPTIME > 0. to use for initializing the output SingleGroup.
        */
        found = 0;
        n = 0;
        /* By default, simply use the first one, so initialize accordingly.*/
        strcpy (imgdefault, imgname[0]);
        do {
            if (efac[n] > 0.) {
                strcpy(imgdefault,imgname[n]);
                found = 1;
            }
            n++;
        } while (found == 0);

        getSingleGroup (imgdefault, extver, &sg);

        if (non_zero > 1){
            /* compute the initial pixel values to be used to compare against all
               images. */
            if (non_zero < nimgs){
                trlwarn ("Some input exposures had EXPTIME = 0.");
            }

            if (acsrej_init (ipsci, ipdq, par, nimgs, dim_x, dim_y,
                    noise, gain, efac, skyval, &sg, work) ) {
                WhichError(status);
                closeSciDq(nimgs, ipsci, ipdq, par);
                return (status);
            }

            if (par->printtime)
                TimeStamp ("Calculated initial guess for extension", "");

            /* do the iterative cosmic ray rejection calculations */
            if (acsrej_loop (ipsci, ipdq, imgname, ext, nimgs, par, niter, dim_x, dim_y,
                    sigma, noise, gain, efac, skyval,
                    &sg.sci.data, &sg.err.data, efacsum, &sg.dq.data,
                    &nrej, shadref.name) ) {
                WhichError(status);
                closeSciDq(nimgs, ipsci, ipdq, par);
                return (status);
            }
        } else {
            trlwarn ("Cosmic-ray rejection NOT performed!");
            if (non_zero > 0) {
                trlwarn ("Some input exposures had EXPTIME = 0.");
                trlwarn ("Output product will not be cosmic-ray cleaned!");
            } /*else {
                trlwarn ("ALL input exposures had EXPTIME = 0.");
                trlwarn ("Output product will be BLANK!");
            } */
        } /* End if(non_zero) block */


        /* must close all images, now that we are done reading them */
        closeSciDq(nimgs, ipsci, ipdq, par);

        /* calculate the total sky ... */
        skysum = 0.;
        for (n = 0; n < nimgs; ++n) {
            skysum += skyval[n];
        }
        /* ... and force it to be non-negative */
        if (skysum < 0.) skysum = 0.;

        if (par->printtime){
            if (non_zero > 1){
                TimeStamp ("Finished detecting cosmic rays on extension", "");
            } else {
                TimeStamp ("Done checking this extension","");
            }
        }

        /* write to the output image */
        if (non_zero > 0){
            for (j = 0; j < dim_y; ++j) {
                for (i = 0; i < dim_x; ++i) {
                    PPix(&sg.sci.data,i,j) = PPix(&sg.sci.data,i,j)*texpt + skysum;
                    PPix(&sg.err.data,i,j) *= texpt;
                }
            }
        } else {
            for (j = 0; j < dim_y; ++j) {
                for (i = 0; i < dim_x; ++i) {
                    PPix(&sg.sci.data,i,j) = par->fillval;
                    PPix(&sg.err.data,i,j) = 0.;
                    /* Set DQ value to one which will always be considered BAD */
                    PPix(&sg.dq.data,i,j) = 1;
                }
            }
                    /* Set at least one pixel to a different value to insure
                      that an image array actually gets produced. */
                    PPix(&sg.err.data,0,0) = -1.;
                    PPix(&sg.dq.data,0,0) = 8;
        }

        /* update the exposure time of the output images */
        PutKeyFlt (sg.globalhdr, "TEXPTIME", exptot, "");
        PutKeyFlt (sg.globalhdr, "SKYSUM", skysum, "Total sky level (DN)");
        PutKeyDbl (sg.globalhdr, "EXPSTART", expstart, "computed exposure start time (Modified Julian Date)");
        PutKeyDbl (sg.globalhdr, "EXPEND", expend, "exposure end time (Modified Julian Date)");
        /*
            Updated REJ_RATE to use 'texpt' as a safe value when
            EXPTIME=0 for all members. WJH, 24 Feb 2003
        */
        PutKeyFlt (sg.globalhdr, "REJ_RATE", (float)nrej/texpt,
                "Cosmic ray impact rate (pixels/sec)");
        PutKeyFlt (sg.globalhdr, "EXPTIME", exptot, "");
        if (par->shadcorr) {
            logit = 0;
            if (UpdateSwitch ("SHADCORR", par->shadcorr, sg.globalhdr, &logit) )
                return (status);

                PrSwitch ("shadcorr", COMPLETE);

            if (logit) {
                /*Records SHADFILE information in header comments... */
                if (ImgHistory (&shadref, sg.globalhdr))
                    return (status);

            }
        }

        /* record parameters to the output file */
        cr_history (&sg, par, nextend);
        PutKeyInt (&sg.sci.hdr, "NCOMBINE", nimgs, "");
        UFilename (outfile, sg.globalhdr);
        UMemType (mtype, sg.globalhdr);
        FindAsnRoot (outfile, root);
        UpperAll (root, uroot, strlen(root)+1 );
        /*  EXPNAME values modified for all extensions in a SingleGroup.
                   WJH 7 July 1999
        */
        UExpname (root, &sg.sci.hdr);
        UExpname (root, &sg.err.hdr);
        UExpname (root, &sg.dq.hdr);

        PutKeyStr (sg.globalhdr, "ROOTNAME", uroot,"Rootname of the observation set");

        /* Output CHIP to the same EXTVER as the CHIP ID */
        putSingleGroup (outfile, extver, &sg, 0);
        freeSingleGroup (&sg);

        if (par->printtime)
            TimeStamp ("Finished writing out extension", "");

        /* deallocate memories */
        free (efacsum);
        free (work);

    }
    /* Set status to a value which will be understood by
        CALACS to turn off subsequent processing. */
    if (non_zero == 0) status = NO_GOOD_DATA;

    return (status);
}
Esempio n. 9
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;
}
Esempio n. 10
0
int ImgPedigree (RefImage *ref) {

	FitsKw key;		/* location of keyword in header */
	IODescPtr im;		/* descriptor for primary header unit */
	Hdr phdr;		/* primary header */

	initHdr (&phdr);
	ref->goodPedigree = GOOD_PEDIGREE;	/* initial value */

	if (!GotFileName (ref->name)) {
	    ref->exists = EXISTS_NO;
	    return (0);
	}

	/* Open the primary header of the reference file. */
	im = openInputImage (ref->name, "", 0);
	if (hstio_err()) {
	    ref->exists = EXISTS_NO;
	    clear_hstioerr();
	    return (0);
	}
	ref->exists = EXISTS_YES;
	getHeader (im, &phdr);
	if (hstio_err())
	    return (HEADER_PROBLEM);

	/* Get pedigree and descrip.  If either or both are missing,
	   that's not an error in this case.
	*/
	key = findKw (&phdr, "PEDIGREE");
	if (key == NotFound) {
	    ref->pedigree[0] = '\0';
	} else {
	    getStringKw (key, ref->pedigree, STIS_FITS_REC);
	    if (hstio_err()) {
		printf ("ERROR    Trying to get PEDIGREE.\n");
		return (HEADER_PROBLEM);
	    }
	}

	key = findKw (&phdr, "DESCRIP");
	if (key == NotFound) {
	    ref->descrip[0] = '\0';
	} else {
	    getStringKw (key, ref->descrip, STIS_FITS_REC);
	    if (hstio_err()) {
		printf ("ERROR    Trying to get DESCRIP.\n");
		return (HEADER_PROBLEM);
	    }
	}

	/* Is this a dummy reference file? */
	if (strncmp (ref->pedigree, "DUMMY", 5) == 0)
	    ref->goodPedigree = DUMMY_PEDIGREE;	/* dummy, so pedigree is bad */
	else
	    ref->goodPedigree = GOOD_PEDIGREE;		/* pedigree is good */

	/* Done with this image for the time being. */
	closeImage (im);
	freeHdr (&phdr);

	return (0);
}
Esempio n. 11
0
/* acsrej_init -- get the initial average pixel values

  Description:
  ------------
  Get the initial average according to the specified scheme

  Date          Author          Description
  ----          ------          -----------
  22-Sep-1998   W.J. Hack       initial version, uses multiamp noise,gain
  20-Mar-2000   W.J. Hack       corrected problem with rog2, needed to be array
  15-Apr-2002   W.J. Hack       correctly zero'd out npts array, added
                                DQ checking for BOTH median and minimum images,
                                also added ERR array for median image.
  26-Aug-2002   J. Blakeslee    Modified threshhold for minimum to use SCALENSE
                                only with sky-subtracted pixels.
  01-Dec-2015   P.L. Lim        Calculations now entirely in electrons.
  13-Jan-2016   P.L. Lim        Removed variance init and cleaned up function.
*/
int acsrej_init (IODescPtr ipsci[], IODescPtr ipdq[], clpar *par, int nimgs,
                 int dim_x, int dim_y, float efac[], float skyval[],
                 SingleGroup *sg, float *work) {
    /*
      Parameters:

      ipsci   i: Array of pointers to SCI extension of the given EXTVER,
                 each pointer is an input image. Unit now in electrons.
      ipdq    i: Array of pointers to DQ extension of the given EXTVER,
                 each pointer is an input image.
      par     i: User specified parameters.
      nimgs   i: Number of input images.
      dim_x, dim_y  i: Image dimension taken from the first input image.
                       All images must have the same dimension.
      efac    i: Array of EXPTIME for each image. If all inputs have
                 EXPTIME=0 (all biases), then the values are all set to 1.
      skyval  i: Array of sky values for each input image.
                 Unit now in electrons.
      sg      o: Its "sci" component is the average image used for
                 comparison during CR rejection. Unit is e/s.
                 This is really the median or minimum depending on
                 "initgues" provided by the user.
      work    o: Intermediate result used to calculate sg but not used
                 outside this function.
    */
    extern int status;

    float     val, raw, dumf;
    int       i, j, n, p, dum;
    float     *buf;
    short     *bufdq;
    int       *npts, *ipts;
    short     dqpat;
    Hdr       dqhdr;

    void      ipiksrt (float [], int, int[]);

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

    dqpat = par->badinpdq;

    ipts = calloc (nimgs, sizeof(int));
    npts = calloc (dim_x, sizeof(int));
    buf = calloc (dim_x, sizeof(float));
    bufdq = calloc (dim_x, sizeof(short));

    /* Use the stack median to construct the initial average. */
    if (strncmp(par->initgues, "median", 3) == 0) {
        for (j = 0; j < dim_y; j++) {
            memset (npts, 0, dim_x*sizeof(int));

            for (n = 0; n < nimgs; n++) {
                initHdr(&dqhdr);
                getHeader(ipdq[n], &dqhdr);
                getFloatLine (ipsci[n], j, buf);  /* electrons */
                getShortLine (ipdq[n], j, bufdq);
                freeHdr(&dqhdr);

                /* Only use GOOD pixels to build initial image.
                   work array is already initialized to zeroes in acsrej_do.c */
                if (efac[n] > 0.) {
                    for (i = 0; i < dim_x; i++) {
                        if ((bufdq[i] & dqpat) == OK) {
                            PIX(work, npts[i], i, nimgs) =
                                (buf[i] - skyval[n]) / efac[n];  /* e/s */
                            npts[i] += 1;
                        }
                    }
                }
            }  /* End of nimgs loop */

            /* ALL AMPS */
            for (i = 0; i < dim_x; i++) {
                dum = npts[i];  /* Number of good data points */
                if (dum == 0)
                    Pix(sg->sci.data, i, j) = 0.0F;
                else {
                    /* Setup index array for sorting... */
                    for (p=0; p < nimgs; p++) ipts[p] = p;
                    /* Sort pixel stack and corresponding index array. */
                    ipiksrt (&PIX(work, 0, i, nimgs), dum, ipts);
                     /* Even number of input images for this pixel */
                    if ((dum / 2) * 2 == dum) {
                        Pix(sg->sci.data, i, j) =
                            (PIX(work, dum / 2 - 1, i, nimgs) +
                             PIX(work, dum / 2, i, nimgs)) / 2.;
                    } else {
                        Pix(sg->sci.data, i, j) = PIX(work, dum / 2, i, nimgs);
                    }
                }
            } /* End loop over ALL AMPS used on pixels in the line */
        } /* End loop over lines */

    /* use the minimum to construct the initial average */
    } else {
        if (strncmp(par->initgues, "minimum", 3) != 0) {
            sprintf (MsgText,
                     "Invalid INITGUES value %s, reset it to 'minimum'",
                     par->initgues);
            trlwarn (MsgText);
            strcpy (par->initgues, "minimum");
        }

        for (n = 0; n < nimgs; n++) {
            initHdr(&dqhdr);
            getHeader(ipdq[n], &dqhdr);

            for (j = 0; j < dim_y; j++) {
                getFloatLine (ipsci[n], j, buf);  /* electrons */
                getShortLine (ipdq[n], j, bufdq);

                /* ALL AMPS */
                for (i = 0; i < dim_x; i++) {
                    raw = buf[i];  /* e */
                    dumf = raw - skyval[n];  /* e */

                    if (efac[n] > 0.) {
                        /* Can be negative */
                        val = dumf / efac[n];  /* e/s */
                    } else {
                        val = 0.;
                    }

                    if ( (n == 0) || (val < Pix(sg->sci.data, i, j)) ) {
                        /* If this pixel is bad in the first image,
                           then the min is automatically set to 0. As a
                           result, only negative val is going to be stored and
                           valid positive min is ignored.
                           SLIGHTLY BUGGY HERE??? */
                        if ((bufdq[i] & dqpat) == OK && (efac[n] > 0.)) {
                            Pix(sg->sci.data, i, j) = val;  /* e/s */
                        } else {
                            Pix(sg->sci.data, i, j) = 0.;
                        }
                    }
                } /* End of loop over ALL AMPS for this line in each image */
            } /* End of loop over lines in image (y) */

            freeHdr(&dqhdr);
        } /* End of loop over images in set */
    }

    /* free the memory */
    free (ipts);
    free (npts);
    free (buf);
    free (bufdq);

    return (status);
}
Esempio n. 12
0
int TargPos (StisInfo12 *sci, int extver, double shift1, double shift2) {

/* arguments:
StisInfo12 *sci        i: info about science data file
int extver             i: EXTVER number of extensions to update
double shift1, shift2  i: shift to be assigned to SHIFTAi keywords
*/

	int status;

	IODescPtr im;		/* descriptor for an image */
	Hdr hdr;		/* header for an extension */

	/* Update SCI extension. */

	initHdr (&hdr);
	im = openUpdateImage (sci->input, "SCI", extver, &hdr);
	if (hstio_err())
	    return (OPEN_FAILED);

	/* Update SHIFTAi. */
	if ((status = UpdateShift (&hdr, shift1, shift2)))
	    return (status);

	putHeader (im);
	if (hstio_err())
	    return (OPEN_FAILED);
	closeImage (im);
	freeHdr (&hdr);

	/* Update ERR extension. */

	im = openUpdateImage (sci->input, "ERR", extver, &hdr);
	if (hstio_err())
	    return (OPEN_FAILED);

	if ((status = UpdateShift (&hdr, shift1, shift2)))
	    return (status);

	putHeader (im);
	if (hstio_err())
	    return (OPEN_FAILED);
	closeImage (im);
	freeHdr (&hdr);

	/* Update DQ extension. */

	im = openUpdateImage (sci->input, "DQ", extver, &hdr);
	if (hstio_err())
	    return (OPEN_FAILED);

	if ((status = UpdateShift (&hdr, shift1, shift2)))
	    return (status);

	putHeader (im);
	if (hstio_err())
	    return (OPEN_FAILED);
	closeImage (im);
	freeHdr (&hdr);

	/* write to trailer */
	if (fabs (shift1) < MUCH_TOO_BIG)	/* shift is OK */
	    printf ("         SHIFTA1 set to %.6g\n", shift1);
	if (fabs (shift2) < MUCH_TOO_BIG)
	    printf ("         SHIFTA2 set to %.6g\n", shift2);

	return (0);
}
Esempio n. 13
0
int loadPCTETAB (char *filename, CTEParamsFast *pars) {
    /* Read the cte parameters from the reference table PCTETAB

       These are taken from the PCTETAB global header:
       CTE_NAME - name of cte algorithm
       CTE_VER - version number of cte algorithm
       CTEDATE0 - date of instrument installation in HST, in fractional years
       CTEDATE1 - reference date of CTE model pinning, in fractional years

       PCTETLEN - max length of CTE trail
       PCTERNOI - readnoise amplitude and clipping level
       PCTESMIT - number of iterations used in CTE forward modeling
       PCTESHFT - number of iterations used in the parallel transfer
       PCTENSMD - readnoise mitigation algorithm
       PCTETRSH - over-subtraction threshold

       The table has 4 extensions:

Filename: wfc3_cte.fits
No.    Name         Type      Cards   Dimensions   Format
0    PRIMARY     PrimaryHDU      21   ()
1    QPROF       BinTableHDU     16   <pars->nTraps>R x 3C    ['i', 'i', 'i']
2    SCLBYCOL    BinTableHDU     20   <pars->nScaleTableColumns> x 5C   ['i', 'e', 'e', 'e', 'e']
3    RPROF       ImageHDU        12   (<pars->nTraps>, 100)   float32
4    CPROF       ImageHDU        12   (<pars->nTraps>, 100)   float32

     */

    extern int status; /* variable for return status */

    /* VARIABLE FOR FILENAME + EXTENSION NUMBER. */
    char filename_wext[strlen(filename) + 4];


    /* NAMES OF DATA COLUMNS WE WANT FROM THE FILE, DATA WILL BE STORED IN THE PARS STRUCTURE */
    const char wcol[] = "W";
    const char qlevq[] = "QLEV_Q";
    const char dpdew[] = "DPDE_W";
    const char iz[]    = "IZ";
    const char sens512[]= "SENS_0512";
    const char sens1024[] = "SENS_1024";
    const char sens1536[] = "SENS_1536";
    const char sens2048[] = "SENS_2048";

    /* HSTIO VARIABLES */
    Hdr hdr_ptr;
    initHdr(&hdr_ptr);
    /* LOAD PRIMARY HEADER */
    if (LoadHdr(filename, &hdr_ptr)) {
        sprintf(MsgText,"(pctecorr) Error loading header from %s",filename);
        cteerror(MsgText);
        status = OPEN_FAILED;
        return status;
    }

    /* GET CTE_NAME KEYWORD */
    if (GetKeyStr (&hdr_ptr, "CTE_NAME", NO_DEFAULT, "", pars->cte_name, SZ_CBUF)) {
        cteerror("(pctecorr) Error reading CTE_NAME keyword from PCTETAB");
        status = KEYWORD_MISSING;
        return status;
    }
    sprintf(MsgText,"\nCTE_NAME: %s",pars->cte_name);
    trlmessage(MsgText);

    /* GET VERSION NUMBER  */
    if (GetKeyStr(&hdr_ptr, "CTE_VER", NO_DEFAULT, "", pars->cte_ver, SZ_CBUF)) {
        cteerror("(pctecorr) Error reading CTE_VER keyword from PCTETAB");
        status = KEYWORD_MISSING;
        return status;
    }
    sprintf(MsgText,"CTE_VER: %s",pars->cte_ver);
    trlmessage(MsgText);

    /* GET DATE OF INSTRUMENT INSTALLATION IN HST */
    if (GetKeyDbl(&hdr_ptr, "CTEDATE0", NO_DEFAULT, -999, &pars->cte_date0)) {
        cteerror("(pctecorr) Error reading CTEDATE0 keyword from PCTETAB");
        status = KEYWORD_MISSING;
        return status;
    }

    sprintf(MsgText,"CTEDATE0: %g",pars->cte_date0);
    trlmessage(MsgText);

    /* GET REFRENCE DATE OF CTE MODEL PINNING */
    if (GetKeyDbl(&hdr_ptr, "CTEDATE1", NO_DEFAULT, -999, &pars->cte_date1)) {
        cteerror("(pctecorr) Error reading CTEDATE1 keyword from PCTETAB");
        status = KEYWORD_MISSING;
        return status;
    }

    sprintf(MsgText,"CTEDATE1: %g",pars->cte_date1);
    trlmessage(MsgText);

    /* READ MAX LENGTH OF CTE TRAIL */
    if (GetKeyInt(&hdr_ptr, "PCTETLEN", NO_DEFAULT, -999, &pars->cte_len)) {
        cteerror("(pctecorr) Error reading PCTETLEN keyword from PCTETAB");
        status = KEYWORD_MISSING;
        return status;
    }

    sprintf(MsgText,"PCTETLEN: %d",pars->cte_len);
    trlmessage(MsgText);

    /* GET READ NOISE CLIPPING LEVEL */
    if (GetKeyDbl(&hdr_ptr, "PCTERNOI", NO_DEFAULT, -999, &pars->rn_amp)) {
        cteerror("(pctecorr) Error reading PCTERNOI keyword from PCTETAB");
        status = KEYWORD_MISSING;
        return status;
    }

    sprintf(MsgText,"PCTERNOI: %f",pars->rn_amp);
    trlmessage(MsgText);

    /* GET NUMBER OF ITERATIONS USED IN FORWARD MODEL */
    if (GetKeyInt(&hdr_ptr, "PCTENFOR", NO_DEFAULT, -999, &pars->n_forward)) {
        cteerror("(pctecorr) Error reading PCTENFOR keyword from PCTETAB");
        status = KEYWORD_MISSING;
        return status;
    }
    sprintf(MsgText,"PCTERNFOR: %d",pars->n_forward);
    trlmessage(MsgText);

    /* GET NUMBER OF ITERATIONS USED IN PARALLEL TRANSFER*/
    if (GetKeyInt(&hdr_ptr, "PCTENPAR", NO_DEFAULT, -999, &pars->n_par)) {
        cteerror("(pctecorr) Error reading PCTENPAR keyword from PCTETAB");
        status = KEYWORD_MISSING;
        return status;
    }

    sprintf(MsgText,"PCTERNPAR: %d",pars->n_par);
    trlmessage(MsgText);

    /* GET READ NOISE MITIGATION ALGORITHM*/
    if (GetKeyInt(&hdr_ptr, "PCTENSMD", NO_DEFAULT, -999, &pars->noise_mit)) {
        cteerror("(pctecorr) Error reading PCTENSMD keyword from PCTETAB");
        status = KEYWORD_MISSING;
        return status;
    }
    sprintf(MsgText,"PCTENSMD: %d",pars->noise_mit);
    trlmessage(MsgText);

    /* GET OVER SUBTRACTION THRESHOLD */
    if (GetKeyDbl(&hdr_ptr, "PCTETRSH", NO_DEFAULT, -999, &pars->thresh)) {
        cteerror("(pctecorr) Error reading PCTETRSH keyword from PCTETAB");
        status = KEYWORD_MISSING;
        return status;
    }
    sprintf(MsgText,"PCTETRSH: %g",pars->thresh);
    trlmessage(MsgText);

    /*FIX THE READOUT CR'S? */
    if (GetKeyInt(&hdr_ptr, "FIXROCR", NO_DEFAULT, -999, &pars->fix_rocr)){
        cteerror("(pctecorr) Error reading FIXROCR keyword from PCTETAB");
        status = KEYWORD_MISSING;
        return status;
    }
    sprintf(MsgText,"FIXROCR: %d",pars->fix_rocr);
    trlmessage(MsgText);


    /* DONE READING STUFF FROM THE PRIMARY HEADER */
    freeHdr(&hdr_ptr);
    /****************************************************************************/
    /* READ  DATA FROM FIRST TABLE EXTENSIONS */
    sprintf(filename_wext, "%s[%i]", filename, 1);

    /* OPEN  PARAMETERS FILE TO EXTENSION NUMBER 1 */
    IRAFPointer tbl_ptr = c_tbtopn(filename_wext, IRAF_READ_ONLY, 0); // xtables table pointer
    if (c_iraferr()) {
        sprintf(MsgText,"(pctecorr) Error opening %s with xtables",filename_wext);
        cteerror(MsgText);
        status = OPEN_FAILED;
        c_tbtclo(tbl_ptr);
        return status;
    }

    /* READ DATA FROM TABLE */
    /* get column pointer for w */
    IRAFPointer w_ptr = c_tbcfnd1_retPtr(tbl_ptr, wcol);
    if (c_iraferr() || !w_ptr) {
        sprintf(MsgText,"(pctecorr) Error getting column %s of PCTETAB",wcol);
        cteerror(MsgText);
        status = COLUMN_NOT_FOUND;
        return status;
    }

    /* GET COLUMN POINTER FOR QLEVQ */
    IRAFPointer qlevq_ptr = c_tbcfnd1_retPtr(tbl_ptr, qlevq);
    if (c_iraferr() || !qlevq_ptr) {
        sprintf(MsgText,"(pctecorr) Error getting column %s of PCTETAB",qlevq);
        cteerror(MsgText);
        status = COLUMN_NOT_FOUND;
        return status;
    }

    /* GET COLUMN POINTER FOR DPDEW */
    IRAFPointer dpdew_ptr = c_tbcfnd1_retPtr(tbl_ptr, dpdew);
    if (c_iraferr() || !dpdew_ptr) {
        sprintf(MsgText,"(pctecorr) Error getting column %s of PCTETAB",dpdew);
        cteerror(MsgText);
        status = COLUMN_NOT_FOUND;
        return status;
    }


    // LOOP OVER TABLE ROWS UP TO SIZE TRAPS
    int ctraps = 0; // actual usable traps, i.e. see if more traps were added to reference file
    {unsigned j;
    for (j = 0; j < pars->nTraps; ++j) {

        /* GET W FROM THIS ROW */
    	pars->wcol_data[j] = c_tbeGetInt(tbl_ptr, w_ptr, j+1);
        if (c_iraferr()) {
            sprintf(MsgText,"(pctecorr) Error reading row %d of column %s in PCTETAB",j+1, wcol);
            cteerror(MsgText);
            status = TABLE_ERROR;
            return status;
        }

        /* GET QLEVQ FROM THIS ROW */
        pars->qlevq_data[j] = c_tbeGetDouble(tbl_ptr, qlevq_ptr, j+1);
        if (c_iraferr()) {
            sprintf(MsgText,"(pctecorr) Error reading row %d of column %s in PCTETAB",j+1, qlevq);
            cteerror(MsgText);
            status = TABLE_ERROR;
            return status;
        }

        if (pars->qlevq_data[j] < 999999.)
            ctraps+=1;

        /* GET DPDEW FROM THIS ROW */
        pars->dpdew_data[j] = c_tbeGetDouble(tbl_ptr, dpdew_ptr, j+1);
        if (c_iraferr()) {
            sprintf(MsgText,"(pctecorr) Error reading row %d of column %s in PCTETAB",j+1, dpdew);
            cteerror(MsgText);
            status = TABLE_ERROR;
            return status;
        }
        if (ctraps > pars->nTraps){
            sprintf(MsgText,"More TRAPS in reference file than available, update TRAPS: %i -> %i",pars->nTraps,(int)ctraps);
            trlmessage(MsgText);
        }
    }}

    /*IF CTRAPS EVER OVERFLOWS INT THIS NEEDS TO BE CHANGED*/
    pars->cte_traps = ctraps;

    /*
    sprintf(MsgText,"(pctecorr) data check for PCTETAB QPROF, row %i, %i\t%g\t%g\ttraps=%i\n",20,
            pars->wcol_data[19],pars->qlevq_data[19], pars->dpdew_data[19], pars->cte_traps);
    trlmessage(MsgText);
    */

    /* CLOSE CTE PARAMETERS FILE FOR EXTENSION 1*/
    c_tbtClose((void*)&tbl_ptr);
    assert(!tbl_ptr);
    /****************************************************************************/
    /****************************************************************************/
    /* READ CTE SCALING DATA FROM SECOND TABLE EXTENSION */
    sprintf(filename_wext, "%s[%i]", filename, 2);

    tbl_ptr = c_tbtopn(filename_wext, IRAF_READ_ONLY, 0);
    if (c_iraferr()) {
        sprintf(MsgText,"(pctecorr) Error opening %s with xtables",filename_wext);
        cteerror(MsgText);
        status = OPEN_FAILED;
        c_tbtclo(tbl_ptr);
        return status;
    }

    /*get column pointer for iz column*/
    IRAFPointer iz_ptr = c_tbcfnd1_retPtr(tbl_ptr, iz);
    if (c_iraferr() || iz_ptr == 0) {
        sprintf(MsgText,"(pctecorr) Error getting column %s of PCTETAB",iz);
        cteerror(MsgText);
        status = COLUMN_NOT_FOUND;
        return status;
    }

    /* get column pointer for sens512 */
    IRAFPointer sens512_ptr = c_tbcfnd1_retPtr(tbl_ptr, sens512);
    if (c_iraferr() || w_ptr == 0) {
        sprintf(MsgText,"(pctecorr) Error getting column %s of PCTETAB",sens512);
        cteerror(MsgText);
        status = COLUMN_NOT_FOUND;
        return status;
    }

    /* get column pointer for sens1024 */
    IRAFPointer sens1024_ptr = c_tbcfnd1_retPtr(tbl_ptr, sens1024);
    if (c_iraferr() || w_ptr == 0) {
        sprintf(MsgText,"(pctecorr) Error getting column %s of PCTETAB",sens1024);
        cteerror(MsgText);
        status = COLUMN_NOT_FOUND;
        return status;
    }
    /* get column pointer for sens1536 */
    IRAFPointer sens1536_ptr = c_tbcfnd1_retPtr(tbl_ptr, sens1536);
    if (c_iraferr() || w_ptr == 0) {
        sprintf(MsgText,"(pctecorr) Error getting column %s of PCTETAB",sens1536);
        cteerror(MsgText);
        status = COLUMN_NOT_FOUND;
        return status;
    }
    /* get column pointer for sens2048 */
    IRAFPointer sens2048_ptr = c_tbcfnd1_retPtr(tbl_ptr, sens2048);
    if (c_iraferr() || w_ptr == 0) {
        sprintf(MsgText,"(pctecorr) Error getting column %s of PCTETAB",sens2048);
        cteerror(MsgText);
        status = COLUMN_NOT_FOUND;
        return status;
    }


    /* read data from table */
    /* loop over table rows */
    {unsigned j;
    for (j = 0; j < pars->nScaleTableColumns; ++j)
    {
        /* get trap from this row */
        pars->iz_data[j] = c_tbeGetInt(tbl_ptr, iz_ptr, j+1);
        if (c_iraferr()) {
            sprintf(MsgText,"(pctecorr) Error reading row %d of column %s in PCTETAB",j+1, iz);
            cteerror(MsgText);
            return (status = TABLE_ERROR);
        }
        pars->scale512[j] = c_tbeGetDouble(tbl_ptr, sens512_ptr, j+1);
        if (c_iraferr()) {
            sprintf(MsgText,"(pctecorr) Error reading row %d of column %s in PCTETAB",j+1, sens512);
            cteerror(MsgText);
            return (status = TABLE_ERROR);
        }

        pars->scale1024[j] = c_tbeGetDouble(tbl_ptr, sens1024_ptr, j+1);
        if (c_iraferr()) {
            sprintf(MsgText,"(pctecorr) Error reading row %d of column %s in PCTETAB",j+1, sens1024);
            cteerror(MsgText);
            return (status = TABLE_ERROR);
        }
        pars->scale1536[j] = c_tbeGetDouble(tbl_ptr, sens1536_ptr, j+1);
        if (c_iraferr()) {
            sprintf(MsgText,"(pctecorr) Error reading row %d of column %s in PCTETAB",j+1, sens1536);
            cteerror(MsgText);
            return (status = TABLE_ERROR);
        }
        pars->scale2048[j] = c_tbeGetDouble(tbl_ptr, sens2048_ptr, j+1);
        if (c_iraferr()) {
            sprintf(MsgText,"(pctecorr) Error reading row %d of column %s in PCTETAB",j+1, sens2048);
            cteerror(MsgText);
            return (status = TABLE_ERROR);
        }
    }}
    // for testing
    /*{
    unsigned j = pars->nColumns;
    sprintf(MsgText,"(pctecorr) data check for PCTETAB SCLBYCOL row %d, %d %g\t%g\t%g\t%g\ntotal traps = %i",
            j,pars->iz_data[j-1],pars->scale512[j-1],pars->scale1024[j-1],pars->scale1536[j-1],pars->scale2048[j-1],pars->cte_traps);
    trlmessage(MsgText);
    }
    */

    /* close CTE parameters file for extension 2*/
    c_tbtClose((void*)&tbl_ptr);
    assert(!tbl_ptr);

    /****************************************************************************/
    /*  extension 3: differential trail profile as image */
    ctemessage("Reading in image from extension 3");

    /* Get the coefficient images from the PCTETAB */
    pars->rprof = malloc(sizeof(*pars->rprof));
    if (pars->rprof == NULL){
        sprintf (MsgText, "Can't allocate memory for RPROF ref data");
        trlerror (MsgText);
        return (status = 1);
    }
    initFloatHdrData(pars->rprof);
    pars->rprof->data.storageOrder = COLUMNMAJOR;
    if (getFloatHD (filename, "RPROF", 1, pars->rprof)){
        return (status=1);
    }


    /****************************************************************************/
    /* ext number 4 : cummulative trail profile as image */
    ctemessage("Reading in image from extension 4");

    pars->cprof  = malloc(sizeof(*pars->cprof));
    if (pars->cprof == NULL){
        sprintf (MsgText, "Can't allocate memory for CPROF ref data");
        trlerror (MsgText);
        return (status = 1);
    }

    /* Get the coefficient images from the PCTETAB */
    initFloatHdrData (pars->cprof);
    pars->cprof->data.storageOrder = COLUMNMAJOR;
    if (getFloatHD (filename, "CPROF", 1, pars->cprof)){
        return (status=1);
    }

    return(status);
}
Esempio n. 14
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);
}
Esempio n. 15
0
static void
handleReadRequest(INKCont pCont, INKHttpTxn pTxn)
{
  LOG_SET_FUNCTION_NAME("handleReadRequest");

  INKMBuffer reqHdrBuf = NULL, newHttpHdrBuf = NULL;
  INKMLoc reqHdrLoc = NULL, newHttpHdrLoc = NULL;

  INKHttpType httpType;

  int iOldHttpVersion, iHttpMethodLength, iHttpVersion;
  const char *sHttpMethod = NULL;
  char *outputString = NULL, *sOldHttpMethod = NULL;

  HdrInfo_T *pReqHdrInfo = NULL, *pNewReqHdrInfo = NULL;

#if 0
  const char *constant_request_header_str =
    "GET http://www.joes-hardware.com/ HTTP/1.0\r\nDate: Wed, 05 Jul 2000 22:12:26 GMT\r\nConnection: Keep-Alive\r\nUser-Agent: Mozilla/4.51 [en] (X11; U; IRIX 6.2 IP22)\r\nHost: www.joes-hardware.com\r\nCache-Control: no-cache\r\nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*\r\nAccept-Charset: iso-8859-1,*,utf-8\r\nAccept-Encoding: gzip\r\nAccept-Language: en\r\nX-Number-Header: 12345\r\nX-Silly-Header: frobnichek grobbledegook\r\nAccept-Charset: windows-1250, koi8-r\r\nX-Silly-Header: wawaaa\r\n\r\n";

#endif


  pReqHdrInfo = initHdr();
  pNewReqHdrInfo = initHdr();

  INKDebug(REQ, "\n>>>>>> handleReadRequest <<<<<<<\n");

  /* Get Request Marshall Buffer */
  if (!INKHttpTxnClientReqGet(pTxn, &reqHdrBuf, &reqHdrLoc)) {
    LOG_API_ERROR_COMMENT("INKHttpTxnClientReqGet", "ERROR: Can't retrieve client req hdr");
    goto done;
  }


    /******* (1): Get every specifics from the HTTP header *********/

  INKDebug(REQ, "--------------------------------");

  getHdrInfo(pReqHdrInfo, reqHdrBuf, reqHdrLoc);
  printHttpHeader(reqHdrBuf, reqHdrLoc, REQ, 1);

#ifdef DEBUG
  negTesting(reqHdrBuf, reqHdrLoc);
#endif

    /*********** (2): Create/Copy/Destroy **********/
  /* For every request, create, copy and destroy a new HTTP header and 
   * print the details */

  INKDebug(REQ, "--------------------------------");
  if ((newHttpHdrBuf = INKMBufferCreate()) == INK_ERROR_PTR) {
    LOG_API_ERROR_COMMENT("INKMBufferCreate", "skipping to section 3");
    goto section_3;             /* Skip to section (3) down the line directly; I hate GOTOs */
  }

    /*** INKHttpHdrCreate ***/
  if ((newHttpHdrLoc = INKHttpHdrCreate(newHttpHdrBuf)) == INK_ERROR_PTR) {
    LOG_API_ERROR_COMMENT("INKHttpHdrCreate", "skipping to section 3");
    goto section_3;             /* Skip to section (3) down the line directly; I hate GOTOs */
  }

  /* Make sure the newly created HTTP header has INKHttpType value of INK_HTTP_TYPE_UNKNOWN */
  if ((httpType = INKHttpHdrTypeGet(newHttpHdrBuf, newHttpHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR_COMMENT("INKHttpHdrTypeGet", "but still continuing...");
  } else if (httpType != INK_HTTP_TYPE_UNKNOWN) {
    LOG_AUTO_ERROR("INKHttpHdrCreate", "Newly created hdr not of type INK_HTTP_TYPE_UNKNOWN");
  }

  /* set the HTTP header type: a new buffer has a type INK_HTTP_TYPE_UNKNOWN by default */
  if (INKHttpHdrTypeSet(newHttpHdrBuf, newHttpHdrLoc, INK_HTTP_TYPE_REQUEST) == INK_ERROR) {
    LOG_API_ERROR_COMMENT("INKHttpHdrTypeSet", "unable to set it to INK_HTTP_TYPE_REQUEST");
  } else if ((httpType = INKHttpHdrTypeGet(newHttpHdrBuf, newHttpHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR_COMMENT("INKHttpHdrTypeGet", "still continuing");
  } else if (httpType != INK_HTTP_TYPE_REQUEST) {
    LOG_AUTO_ERROR("INKHttpHdrTypeSet", "Type not set to INK_HTTP_TYPE_REQUEST");
  }

    /*** INKHttpHdrCopy ***/
  if (INKHttpHdrCopy(newHttpHdrBuf, newHttpHdrLoc, reqHdrBuf, reqHdrLoc) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrCopy");
  }
  getHdrInfo(pNewReqHdrInfo, newHttpHdrBuf, newHttpHdrLoc);

  if (!identical_hdr(pNewReqHdrInfo, pReqHdrInfo)) {
    LOG_AUTO_ERROR("INKHttpHdrCopy", "New req buffer not identical to the original");
  }

  printHttpHeader(newHttpHdrBuf, newHttpHdrLoc, REQ, 2);

  FREE(pNewReqHdrInfo->httpMethod);
  FREE(pNewReqHdrInfo->hostName);

section_3:
    /********* (3): Excercise all the INK__Set on ReqBuf *********/
  INKDebug(REQ, "--------------------------------");

    /*** INKHttpHdrMethodSet ***/
  /* save the original method */
  if ((sHttpMethod = INKHttpHdrMethodGet(reqHdrBuf, reqHdrLoc, &iHttpMethodLength)) == INK_ERROR_PTR) {
    LOG_API_ERROR("INKHttpHdrMethodGet");
  } else {
    sOldHttpMethod = INKstrndup(sHttpMethod, iHttpMethodLength);
  }
  /* change it to some unknown method */
  if (INKHttpHdrMethodSet(reqHdrBuf, reqHdrLoc, "FOOBAR", strlen("FOOBAR")) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrMethodSet");
  } else {
    if ((sHttpMethod = INKHttpHdrMethodGet(reqHdrBuf, reqHdrLoc, &iHttpMethodLength)) == INK_ERROR_PTR) {
      LOG_API_ERROR("INKHttpHdrMethodGet");
    } else if (strncmp(sHttpMethod, "FOOBAR", iHttpMethodLength)) {
      LOG_AUTO_ERROR("INKHttpHdrMethodSet/Get", "GET method different from SET method");
    }
  }

  outputString = INKstrndup(sHttpMethod, iHttpMethodLength);
  INKDebug(REQ, "(3): new HTTP Header Method = %s", outputString);
  FREE(outputString);
  STR_RELEASE(reqHdrBuf, reqHdrLoc, sHttpMethod);

  printHttpHeader(reqHdrBuf, reqHdrLoc, REQ, 3);

  /* set it back to the original method */
  /*INKHttpHdrMethodSet (reqHdrBuf, reqHdrLoc, sOldHttpMethod, iHttpMethodLength); */
  if (INKHttpHdrMethodSet(reqHdrBuf, reqHdrLoc, sOldHttpMethod, strlen(sOldHttpMethod)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrMethodSet");
  } else if ((sHttpMethod = INKHttpHdrMethodGet(reqHdrBuf, reqHdrLoc, &iHttpMethodLength)) == INK_ERROR_PTR) {
    LOG_API_ERROR("INKHttpHdrMethodGet");
  } else if (strncmp(sHttpMethod, sOldHttpMethod, iHttpMethodLength)) {
    LOG_AUTO_ERROR("INKHttpHdrMethodSet/Get", "GET method different from SET method");
  }

  outputString = INKstrndup(sHttpMethod, iHttpMethodLength);
  INKDebug(REQ, "(3): original HTTP Header Method = %s", outputString);
  FREE(outputString);
  STR_RELEASE(reqHdrBuf, reqHdrLoc, sHttpMethod);


    /*** INKHttpHdrVersionSet ***/
  /* get the original version */
  if ((iOldHttpVersion = INKHttpHdrVersionGet(reqHdrBuf, reqHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionGet");
  }

  /* change it to some unknown version */
  if (INKHttpHdrVersionSet(reqHdrBuf, reqHdrLoc, INK_HTTP_VERSION(10, 10)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionSet");
  } else if ((iHttpVersion = INKHttpHdrVersionGet(reqHdrBuf, reqHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionGet");
  } else if ((INK_HTTP_MAJOR(iHttpVersion) != 10) || (INK_HTTP_MINOR(iHttpVersion) != 10)) {
    LOG_AUTO_ERROR("INKHttpHdrVersionSet/Get", "SET HTTP version different from GET version");
  }

  INKDebug(REQ, "(3): new HTTP version; Major = %d   Minor = %d",
           INK_HTTP_MAJOR(iHttpVersion), INK_HTTP_MINOR(iHttpVersion));

  /* change it back to the original version */
  if (INKHttpHdrVersionSet(reqHdrBuf, reqHdrLoc, iOldHttpVersion) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionSet");
  } else if ((iHttpVersion = INKHttpHdrVersionGet(reqHdrBuf, reqHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionGet");
  } else if (iHttpVersion != iOldHttpVersion) {
    LOG_AUTO_ERROR("INKHttpHdrVersionSet/Get", "SET HTTP version different from GET version");
  }

  getHdrInfo(pNewReqHdrInfo, reqHdrBuf, reqHdrLoc);
  if (!identical_hdr(pNewReqHdrInfo, pReqHdrInfo)) {
    LOG_AUTO_ERROR("INK..Set", "ReqBuf: Values not restored properly");
  }

  /* (3): clean-up */
  FREE(sOldHttpMethod);

done:
    /*************** Clean-up ***********************/
  /*
     FREE(pReqHdrInfo->httpMethod);
     FREE(pReqHdrInfo->hostName);

     FREE(pNewReqHdrInfo->httpMethod);
     FREE(pNewReqHdrInfo->hostName);

     FREE(pReqHdrInfo);
     FREE(pNewReqHdrInfo);
   */
  freeHdr(pReqHdrInfo);
  freeHdr(pNewReqHdrInfo);

  /* release hdrLoc */
  HANDLE_RELEASE(reqHdrBuf, INK_NULL_MLOC, reqHdrLoc);
  HANDLE_RELEASE(newHttpHdrBuf, INK_NULL_MLOC, newHttpHdrLoc);

  /* destroy hdr */
  HDR_DESTROY(newHttpHdrBuf, newHttpHdrLoc);

  /* destroy mbuffer */
  BUFFER_DESTROY(newHttpHdrBuf);


  if (INKHttpTxnReenable(pTxn, INK_EVENT_HTTP_CONTINUE) == INK_ERROR) {
    LOG_API_ERROR("INKHttpTxnReenable");
  }
  INKDebug(REQ, "..... exiting handleReadRequest ......\n");

}                               /* handleReadReadRequest */
Esempio n. 16
0
static void
handleSendResponse(INKCont pCont, INKHttpTxn pTxn)
{
  LOG_SET_FUNCTION_NAME("handleSendResponse");

  INKMBuffer respHdrBuf = NULL, newHttpHdrBuf = NULL, parseBuffer = NULL;
  INKMLoc respHttpHdrLoc = NULL, newHttpHdrLoc = NULL, parseHttpHdrLoc = NULL;

  INKHttpStatus oldHttpStatus, tmpHttpStatus;
  INKHttpType httpType;
  INKHttpParser httpRespParser = NULL;

  HdrInfo_T *pRespHdrInfo = NULL, *pNewRespHdrInfo = NULL;

  int iHttpHdrReasonLength, iOldHttpVersion, iTmpHttpVersion, iTmpHttpHdrReasonLength;
  const char *sHttpHdrReason = NULL, *sTmpHttpHdrReason = NULL, *pHttpParseStart = NULL, *pHttpParseEnd = NULL;
  char *sOldHttpReason = NULL;

  const char *sRespHdrStr1 =
    "HTTP/1.1 200 OK\r\nServer: Netscape-Enterprise/4.1\r\nDate: Tue, 31 Oct 2000 03:38:19 GMT\r\nContent-type: text/html\r\nAge: 3476\r\nContent-Length: 12440\r\nVia: HTTP/1.1 ts-sun14 (Traffic-Server/4.0.0 [cHs f ])\r\n\r\n";

  const char *sRespHdrStr2 =
    "HTTP/1.1 404 Not Found \r\nServer: Netscape-Enterprise/4.1\r\nDate: Tue, 31 Oct 2000 03:38:19 GMT\r\nContent-type: text/html\r\nAge: 3476\r\nContent-Length: 12440\r\nVia: HTTP/1.1 ts-sun24 (Traffic-Server/4.0.0 [cHs f ])\r\n\r\n";

  const char *sRespHdrStr3 =
    "HTTP/1.1 505 HTTP Version Not Supported \r\nServer: Netscape-Enterprise/4.1\r\nDate: Tue, 31 Oct 2000 03:38:19 GMT\r\nContent-type: text/html\r\nAge: 3476\r\nContent-Length: 12440\r\nVia: HTTP/1.1 ts-sun34 (Traffic-Server/4.0.0 [cHs f ])\r\n\r\n";



  pRespHdrInfo = initHdr();
  pNewRespHdrInfo = initHdr();

  INKDebug(RESP, ">>> handleSendResponse <<<<\n");

  /* Get Response Marshall Buffer */
  if (!INKHttpTxnClientRespGet(pTxn, &respHdrBuf, &respHttpHdrLoc)) {
    LOG_API_ERROR_COMMENT("INKHttpTxnClientReqGet", "ERROR: Can't retrieve client req hdr");
    goto done;
  }

#ifdef DEBUG
  negTesting(respHdrBuf, respHttpHdrLoc);
#endif

    /******* (1): Exercise all possible INK*GET and print the values **********/

  INKDebug(RESP, "--------------------------------");
  getHdrInfo(pRespHdrInfo, respHdrBuf, respHttpHdrLoc);
  printHttpHeader(respHdrBuf, respHttpHdrLoc, RESP, 1);

    /******* (2): Create a new header and check everything is copied correctly *********/

  INKDebug(RESP, "--------------------------------");

  if ((newHttpHdrBuf = INKMBufferCreate()) == INK_ERROR_PTR) {
    LOG_API_ERROR_COMMENT("INKMBufferCreate", "skipping to section(4)");
    goto resp_4;
  }

    /*** INKHttpHdrCreate ***/
  if ((newHttpHdrLoc = INKHttpHdrCreate(newHttpHdrBuf)) == INK_ERROR_PTR) {
    LOG_API_ERROR_COMMENT("INKMHTTPHdrCreate", "skipping to section(4)");
    goto resp_4;
  }

  /* Make sure the newly created HTTP header has INKHttpType value of INK_HTTP_TYPE_UNKNOWN */
  if ((httpType = INKHttpHdrTypeGet(newHttpHdrBuf, newHttpHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR_COMMENT("INKMHTTPHdrCreate", "continuing");
  } else if (httpType != INK_HTTP_TYPE_UNKNOWN) {
    LOG_AUTO_ERROR("INKHttpHdrCreate", "Newly created hdr not of type INK_HTTP_TYPE_UNKNOWN");
  }


    /*** INKHttpHdrCopy ***/
  if (INKHttpHdrCopy(newHttpHdrBuf, newHttpHdrLoc, respHdrBuf, respHttpHdrLoc) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrCopy");
  }

  getHdrInfo(pNewRespHdrInfo, newHttpHdrBuf, newHttpHdrLoc);
  printHttpHeader(newHttpHdrBuf, newHttpHdrLoc, RESP, 2);

  if (!identical_hdr(pRespHdrInfo, pNewRespHdrInfo)) {
    LOG_AUTO_ERROR("INKHttpHdrCopy", "copy of the resp header not identical to the original");
  }

  /* Reuse:
   * newHttpHdrBuf, newHttHdrLoc */

    /******* (3): Now excercise some INK..SETs on the new header ********/
  INKDebug(RESP, "--------------------------------");

    /*** INKHttpHdrTypeSet ***/
  /* ERROR: 
   * 1. Setting type other than INK_HTTP_TYPE_UNKNOWN, INK_HTTP_TYPE_REQUEST, 
   * INK_HTTP_TYPE_RESPONSE, and,
   * 2. Setting the type twice.  The hdr type has been already set during INKHttpHdrCopy 
   * above, so setting it again is incorrect */
  if (INKHttpHdrTypeSet(newHttpHdrBuf, newHttpHdrLoc, INK_HTTP_TYPE_RESPONSE) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrTypeSet");
  }

    /*** INKHttpHdrReasonSet ***/
  /* save the original reason */
  if ((sHttpHdrReason = INKHttpHdrReasonGet(newHttpHdrBuf, newHttpHdrLoc, &iHttpHdrReasonLength))
      == INK_ERROR_PTR) {
    LOG_API_ERROR("INKHttpHdrReasonGet");
  } else {
    sOldHttpReason = INKstrndup(sHttpHdrReason, iHttpHdrReasonLength);
  }

  /* Note: 
   * INKHttpHdrReasonGet may return a NULL reason string (for e.g. I tried www.eyesong.8m.com).
   * Do NOT assume that INKstrndup always returns a null terminated string.  INKstrndup does 
   * not returns a NULL terminated string for a NULL ptr as i/p parameter.  It simply returns 
   * it backs. So functions like strlen() on the return value might cause TS to crash */


  if (INKHttpHdrReasonSet(newHttpHdrBuf, newHttpHdrLoc, "dummy reason", strlen("dummy reason")) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrReasonGet");
  } else {
    if ((sTmpHttpHdrReason = INKHttpHdrReasonGet(newHttpHdrBuf, newHttpHdrLoc, &iTmpHttpHdrReasonLength))
        == INK_ERROR_PTR) {
      LOG_API_ERROR("INKHttpHdrReasonGet");
    } else if (sTmpHttpHdrReason && strncmp(sTmpHttpHdrReason, "dummy reason", iTmpHttpHdrReasonLength)) {
      LOG_AUTO_ERROR("INKHttpHdrReasonSet/Get", "GET reason different from the SET reason");
    }
    STR_RELEASE(newHttpHdrBuf, newHttpHdrLoc, sTmpHttpHdrReason);
  }

    /*** INKHttpStatusSet ***/
  /* save the original value */
  if ((oldHttpStatus = INKHttpHdrStatusGet(newHttpHdrBuf, newHttpHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrStatusGet");
  }

  /* change it to some unknown value */
  if (INKHttpHdrStatusSet(newHttpHdrBuf, newHttpHdrLoc, INK_HTTP_STATUS_NONE) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrStatusSet");
  } else if ((tmpHttpStatus = INKHttpHdrStatusGet(newHttpHdrBuf, newHttpHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrStatusGet");
  } else if (tmpHttpStatus != INK_HTTP_STATUS_NONE) {
    LOG_AUTO_ERROR("INKHttpHdrStatusGet/Set", "GET status different from the SET status");
  }


    /*** INKHttpHdrVersionSet ***/
  /* get the original version */
  if ((iOldHttpVersion = INKHttpHdrVersionGet(newHttpHdrBuf, newHttpHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionGet");
  }

  /* change it to some unknown version */
  if (INKHttpHdrVersionSet(newHttpHdrBuf, newHttpHdrLoc, INK_HTTP_VERSION(10, 10)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionSet");
  } else if ((iTmpHttpVersion = INKHttpHdrVersionGet(newHttpHdrBuf, newHttpHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionGet");
  } else if (INK_HTTP_MAJOR(iTmpHttpVersion) != 10 && INK_HTTP_MINOR(iTmpHttpVersion) != 10) {
    LOG_AUTO_ERROR("INKHttpHdrVersionSet", "GET version different from SET version");
  }

  printHttpHeader(newHttpHdrBuf, newHttpHdrLoc, RESP, 3);

  /* Restore the original values */

  /* Here we can't use strlen(sOldHttpReason) to set the length.  This would crash TS if 
   * sOldHttpReason happens to be NULL */
  if (INKHttpHdrReasonSet(newHttpHdrBuf, newHttpHdrLoc, sOldHttpReason, iHttpHdrReasonLength) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrReasonSet");
  }
  /*INKHttpHdrReasonSet (newHttpHdrBuf, newHttpHdrLoc, sOldHttpReason, strlen(sOldHttpReason)); */
  if (INKHttpHdrStatusSet(newHttpHdrBuf, newHttpHdrLoc, oldHttpStatus) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrStatusSet");
  }
  if (INKHttpHdrVersionSet(newHttpHdrBuf, newHttpHdrLoc, iOldHttpVersion) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrStatusSet");
  }

  if (!identical_hdr(pRespHdrInfo, pNewRespHdrInfo)) {
    LOG_AUTO_ERROR("INK..SET", "Hdr values not properly restored");
  }

  /* (3): clean-up */
  STR_RELEASE(newHttpHdrBuf, newHttpHdrLoc, sHttpHdrReason);
  FREE(sOldHttpReason);

resp_4:
    /******* (4): Now excercise some SETs on the response header ********/
  INKDebug(RESP, "--------------------------------");

    /*** INKHttpHdrReasonSet ***/
  /* save the original reason */
  if ((sHttpHdrReason = INKHttpHdrReasonGet(respHdrBuf, respHttpHdrLoc, &iHttpHdrReasonLength))
      == INK_ERROR_PTR) {
    LOG_API_ERROR("INKHttpHdrReasonGet");
  } else {
    sOldHttpReason = INKstrndup(sHttpHdrReason, iHttpHdrReasonLength);
  }

  /* change the reason phrase */
  if (INKHttpHdrReasonSet(respHdrBuf, respHttpHdrLoc, "dummy reason", strlen("dummy reason")) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrReasonSet");
  }

  if ((sTmpHttpHdrReason = INKHttpHdrReasonGet(respHdrBuf, respHttpHdrLoc, &iTmpHttpHdrReasonLength))
      == INK_ERROR_PTR) {
    LOG_API_ERROR("INKHttpHdrReasonGet");
  } else if (sTmpHttpHdrReason && strncmp(sTmpHttpHdrReason, "dummy reason", iTmpHttpHdrReasonLength)) {
    LOG_AUTO_ERROR("INKHttpHdrReasonSet/Get", "GET reason string different from SET reason");
  }
  STR_RELEASE(respHdrBuf, respHttpHdrLoc, sTmpHttpHdrReason);

    /*** INKHttpStatusSet ***/
  /* save the original value */
  if ((oldHttpStatus = INKHttpHdrStatusGet(respHdrBuf, respHttpHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrStatusGet");
  }

  /* change it to some unknown value */
  if (INKHttpHdrStatusSet(respHdrBuf, respHttpHdrLoc, INK_HTTP_STATUS_NONE) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrStatusSet");
  } else if (INKHttpHdrStatusGet(respHdrBuf, respHttpHdrLoc) != INK_HTTP_STATUS_NONE) {
    LOG_AUTO_ERROR("INKHttpHdrStatusSet/GET", "GET status value different from SET status");
  }


    /*** INKHttpHdrTypeSet ***/
  /* ERROR: 
   * 1. Setting type other than INK_HTTP_TYPE_UNKNOWN, INK_HTTP_TYPE_REQUEST, 
   * INK_HTTP_TYPE_RESPONSE and,
   * 2. Setting the type twice.  The hdr type has been already set during INKHttpTxnClientRespGet
   * above, so setting it again should fail */
  if (INKHttpHdrTypeSet(respHdrBuf, respHttpHdrLoc, INK_HTTP_TYPE_RESPONSE) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrTypeSet");
  }
  if (INKHttpHdrTypeGet(respHdrBuf, respHttpHdrLoc) == INK_HTTP_TYPE_UNKNOWN) {
    LOG_AUTO_ERROR("INKHttpHdrTypeSet/Get", "respHdrBuf CAN be set to INK_HTTP_TYPE_UNKNOWN");
  }

    /*** INKHttpHdrVersionSet ***/
  /* get the original version */
  if ((iOldHttpVersion = INKHttpHdrVersionGet(respHdrBuf, respHttpHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionGet");
  }

  /* change it to some unknown version */
  if (INKHttpHdrVersionSet(respHdrBuf, respHttpHdrLoc, INK_HTTP_VERSION(10, 10)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionSet");
  } else if ((iTmpHttpVersion = INKHttpHdrVersionGet(respHdrBuf, respHttpHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionGet");
  } else if (INK_HTTP_MAJOR(iTmpHttpVersion) != 10 && INK_HTTP_MINOR(iTmpHttpVersion) != 10) {
    LOG_AUTO_ERROR("INKHttpHdrVersionGet/Set", "GET HTTP version different from SET version");
  }

  printHttpHeader(respHdrBuf, respHttpHdrLoc, RESP, 4);

  /* restore the original values */

  /* For INKHttpHdrReasonSet, do NOT use strlen(sOldHttpReason) to set the length.  
   * This would crash TS if sOldHttpReason happened to be NULL */
  if (INKHttpHdrReasonSet(respHdrBuf, respHttpHdrLoc, sOldHttpReason, iHttpHdrReasonLength) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrReasonSet");
  }
  /*INKHttpHdrReasonSet (respHdrBuf, respHttpHdrLoc, sOldHttpReason, strlen(sOldHttpReason)); */
  if (INKHttpHdrStatusSet(respHdrBuf, respHttpHdrLoc, oldHttpStatus) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrStatusSet");
  }
  if (INKHttpHdrVersionSet(respHdrBuf, respHttpHdrLoc, iOldHttpVersion) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionSet");
  }

  FREE(pNewRespHdrInfo->hdrReason);
  getHdrInfo(pNewRespHdrInfo, respHdrBuf, respHttpHdrLoc);

  if (!identical_hdr(pRespHdrInfo, pNewRespHdrInfo)) {
    LOG_AUTO_ERROR("INK..SET", "Hdr values not properly restored");
  }

  /* (4): clean-up */
  STR_RELEASE(respHdrBuf, respHttpHdrLoc, sHttpHdrReason);
  FREE(sOldHttpReason);

    /********************************/
    /** (5): INKHttpHdrParseResp   **/
    /********************************/

  INKDebug(RESP, "--------------------------------");

  /* Create a parser Buffer and header location */
  if ((parseBuffer = INKMBufferCreate()) == INK_ERROR_PTR || parseBuffer == NULL) {
    LOG_API_ERROR_COMMENT("INKMBufferCreate", "abnormal exit");
    goto done;
  } else if ((parseHttpHdrLoc = INKHttpHdrCreate(parseBuffer)) == INK_ERROR_PTR || parseHttpHdrLoc == NULL) {
    LOG_API_ERROR_COMMENT("INKHttpHdrCreate", "abnormal exit");
    goto done;
  }

  pHttpParseStart = sRespHdrStr1;
  pHttpParseEnd = pHttpParseStart + strlen(pHttpParseStart);

  httpRespParser = INKHttpParserCreate();

  if (INKHttpHdrParseResp(httpRespParser, parseBuffer, parseHttpHdrLoc, &pHttpParseStart, pHttpParseEnd)
      == INK_PARSE_ERROR) {
    LOG_API_ERROR("INKHttpHdrParseResp");
  }

  printHttpHeader(parseBuffer, parseHttpHdrLoc, RESP, 5.1);

  if (INKHttpParserClear(httpRespParser) == INK_ERROR) {
    LOG_API_ERROR("INKHttpParseClear");
  }

  INKDebug(RESP, "--------------------------------");

  pHttpParseStart = sRespHdrStr2;
  pHttpParseEnd = pHttpParseStart + strlen(pHttpParseStart);

  /* httpRespParser = INKHttpParserCreate(); */

  if (INKHttpHdrParseResp(httpRespParser, parseBuffer, parseHttpHdrLoc, &pHttpParseStart, pHttpParseEnd)
      == INK_PARSE_ERROR) {
    LOG_API_ERROR("INKHttpHdrParseResp");
  }

  printHttpHeader(parseBuffer, parseHttpHdrLoc, RESP, 5.2);

  if (INKHttpParserClear(httpRespParser) == INK_ERROR) {
    LOG_API_ERROR("INKHttpParseClear");
  }

  INKDebug(RESP, "--------------------------------");

  pHttpParseStart = sRespHdrStr3;
  pHttpParseEnd = pHttpParseStart + strlen(pHttpParseStart);

  /* httpRespParser = INKHttpParserCreate(); */

  if (INKHttpHdrParseResp(httpRespParser, parseBuffer, parseHttpHdrLoc, &pHttpParseStart, pHttpParseEnd)
      == INK_PARSE_ERROR) {
    LOG_API_ERROR("INKHttpHdrParseResp");
  }

  printHttpHeader(parseBuffer, parseHttpHdrLoc, RESP, 5.3);


done:
  /* Clean-up */
  freeHdr(pRespHdrInfo);
  freeHdr(pNewRespHdrInfo);

  /* release hdrLoc */
  HANDLE_RELEASE(respHdrBuf, INK_NULL_MLOC, respHttpHdrLoc);
  HANDLE_RELEASE(newHttpHdrBuf, INK_NULL_MLOC, newHttpHdrLoc);
  HANDLE_RELEASE(parseBuffer, INK_NULL_MLOC, parseHttpHdrLoc);

  /* destroy hdrLoc */
  HDR_DESTROY(respHdrBuf, respHttpHdrLoc);
  HDR_DESTROY(parseBuffer, parseHttpHdrLoc);

  /* destroy mbuffer */
  BUFFER_DESTROY(newHttpHdrBuf);
  BUFFER_DESTROY(parseBuffer);

  /* destroy the parser */
  if (INKHttpParserDestroy(httpRespParser) == INK_ERROR) {
    LOG_API_ERROR("INKHttpParserDestroy");
  }

  if (INKHttpTxnReenable(pTxn, INK_EVENT_HTTP_CONTINUE) == INK_ERROR) {
    LOG_API_ERROR("INKHttpTxnReenable");
  }

  INKDebug(RESP, "......... exiting handleRespResponse .............\n");

}                               /* handleSendResponse */
Esempio n. 17
0
/* This routine copies a FITS file. */
static int CopyFFile (char *infile, char *outfile) {

    /* arguments:
       infile    i: name of input file
       outfile   i: name of output file
    */

    extern int status;

    FILE *ifp, *ofp;  /* for input and output files */
    void *buf;        /* buffer for copying blocks */
    int nin, nout;    /* number read and written */
    int done;
    IODescPtr im;
    Hdr phdr;

    /* function from lib */
    void UFilename (char *, Hdr *);

    if ((buf = calloc (FITS_BUFSIZE, sizeof(char))) == NULL)
        return (status = OUT_OF_MEMORY);

    if ((ofp = fopen (outfile, "wb")) == NULL) {
        sprintf (MsgText,"Can't create temporary file %s.", outfile);
        trlerror(MsgText);
        free (buf);
        return (status = INVALID_TEMP_FILE);
    }

    if ((ifp = fopen (infile, "rb")) == NULL) {
        sprintf (MsgText, "Can't open %s.", infile);
        trlerror (MsgText);
        (void)fcloseWithStatus(&ofp);
        remove (outfile);
        free (buf);
        return (status = OPEN_FAILED);
    }

    done = 0;
    while (!done) {
        nin = fread (buf, sizeof(char), FITS_BUFSIZE, ifp);
        if (ferror (ifp)) {
            sprintf (MsgText, "Can't read from %s (copying to %s).",
            infile, outfile);
            trlerror (MsgText);
            (void)fcloseWithStatus(&ofp);
            (void)fcloseWithStatus(&ifp);
            free (buf);
            return (status = FILE_NOT_READABLE);
        }
        if (feof (ifp))
            done = 1;

        nout = fwrite (buf, sizeof(char), nin, ofp);
        if (nout < nin) {
            sprintf (MsgText, "Can't copy %s to %s.", infile, outfile);
            trlerror (MsgText);
            (void)fcloseWithStatus(&ofp);
            (void)fcloseWithStatus(&ifp);
            free (buf);
            return (status = COPY_NOT_POSSIBLE);
        }
    }

    (void)fcloseWithStatus(&ofp);
    (void)fcloseWithStatus(&ifp);
    free (buf);

    /* Update the FILENAME keyword in the primary header of the output file. */
    initHdr (&phdr);
    im = openUpdateImage (outfile, "", 0, &phdr);
    UFilename (outfile, &phdr);
    putHeader (im);

    closeImage (im);
    freeHdr (&phdr);

    return (status);
}
Esempio n. 18
0
int CalStis11 (char *inwav, char *insci, char *output,
		int printtime, int verbose) {

	int status;

	StisInfo11 wavecal, scidata;	/* calibration switches, etc. */

	IODescPtr imWav;	/* descriptor for input wavecal */
	IODescPtr imSci;	/* descriptor for input science file */
	Hdr phdrWav;		/* primary header for input wavecal */
	Hdr phdrSci;		/* primary header for input science file */
	int subscicorr;		/* PERFORM if CCD and sclamp is HITM1 or 2 */

	int GetKeyInfo11 (StisInfo11 *, Hdr *);
	int SubSci (StisInfo11 *, StisInfo11 *);

	PrBegin (11);

	if (printtime)
	    TimeStamp ("CALSTIS-11 started", "");

	/* Initialize structure containing calstis information. */
	StisInit11 (&wavecal, &scidata);

	/* Copy command-line arguments into wavecal & scidata. */
	strcpy (wavecal.input, inwav);
	strcpy (scidata.input, insci);
	strcpy (wavecal.output, output);
	wavecal.printtime = printtime;
	scidata.printtime = printtime;
	wavecal.verbose = verbose;
	scidata.verbose = verbose;

	PrFileName ("wavecal", wavecal.input);
	PrFileName ("science", scidata.input);
	PrFileName ("output", wavecal.output);

	initHdr (&phdrWav);
	initHdr (&phdrSci);

	/* Check whether the output file already exists. */
	if ((status = FileExists (wavecal.output)))
	    return (status);

	/* Read primary header of input wavecal. */
	imWav = openInputImage (wavecal.input, "", 0);
	if (hstio_err())
	    return (OPEN_FAILED);
	getHeader (imWav, &phdrWav);
	if (hstio_err())
	    return (OPEN_FAILED);
	closeImage (imWav);

	/* Get keyword values from wavecal primary header. */
	if ((status = GetKeyInfo11 (&wavecal, &phdrWav)))
	    return (status);

	freeHdr (&phdrWav);

	/* Print information about the input wavecal. */
	PrHdrInfo (wavecal.obsmode, wavecal.aperture,
		wavecal.opt_elem, wavecal.det);

	/* Do we need to subtract the science image from the wavecal? */
	subscicorr = PERFORM;			/* initial value */
	if (wavecal.detector != CCD_DETECTOR) {
	    subscicorr = OMIT;
	    printf ("Warning  Detector is %s\n", wavecal.det);
	}
	if (strcmp (wavecal.sclamp, "HITM1") != 0 &&
		   strcmp (wavecal.sclamp, "HITM2") != 0) {
	    subscicorr = OMIT;
	    printf ("Warning  Wavecal SCLAMP is `%s', not HITM1 or HITM2\n",
			wavecal.sclamp);
	}
	if (wavecal.texpstrt >= EXT_SHUTTER_CLOSED) {
	    subscicorr = OMIT;
	    printf (
	"Warning  TEXPSTRT=%.2f implies external shutter is closed.\n",
		wavecal.texpstrt);
	}

	if (subscicorr != PERFORM) {
	    printf (
	"Warning  Science data will not be subtracted from wavecal.\n");
	    return (NOTHING_TO_DO);
	}

	/* Read primary header of input science file. */
	imSci = openInputImage (scidata.input, "", 0);
	if (hstio_err())
	    return (OPEN_FAILED);
	getHeader (imSci, &phdrSci);
	if (hstio_err())
	    return (OPEN_FAILED);
	closeImage (imSci);

	if (wavecal.printtime)
	    TimeStamp ("Begin processing", wavecal.rootname);

	/* Get keyword values from science file primary header. */
	if ((status = GetKeyInfo11 (&scidata, &phdrSci)))
	    return (status);

	freeHdr (&phdrSci);

	/* Detector, central wavelength, grating, and aperture must be
	   the same in the wavecal and science file.
	*/
	if (wavecal.detector != scidata.detector ||
	    wavecal.cenwave != scidata.cenwave ||
	    strcmp (wavecal.opt_elem, scidata.opt_elem) != 0 ||
	    strcmp (wavecal.aperture, scidata.aperture) != 0) {

	    printf ("Warning  Wavecal and science file do not match; \\\n");
	    printf ("Warning  the science file will not be subtracted.\n");
	    return (NOTHING_TO_DO);
	}

	/* Subtract the science image from the wavecal. */
	if ((status = SubSci (&wavecal, &scidata)))
	    return (status);

	printf ("\n");
	PrEnd (11);

	if (wavecal.printtime)
	    TimeStamp ("CALSTIS-11 completed", wavecal.rootname);

	return (0);
}
Esempio n. 19
0
int rej_init (IODescPtr ipsci[], IODescPtr ipdq[], clpar *par, int nimgs,
	      int dim_x, int dim_y, multiamp noise, multiamp gain, float efac[],
	      float skyval[], DataUnits bunit[], SingleGroup *sg, float *work) {

    extern int status;

    float  scale, val, raw, raw0, signal0;
    float  *buf;
    short  *bufdq;
    float  *exp2;
    int    i, j, n;
    int    dum;
    int    *npts, *ipts;
    float  noise2[NAMPS], rog2[NAMPS];
    float  gain2[NAMPS];
    float  nse[2], gn[2];
    int    ampx, ampy, detector, chip;
    int    k, p;
    short  dqpat;
    float  exp2n, expn;
    int    non_zero;
    Hdr    dqhdr;

    void ipiksrt (float [], int, int[]);
    void get_nsegn (int, int, int, int, float *, float*, float *, float *);

    /* -------------------------------- begin ------------------------------ */
    expn=0.0f;
    scale = par->scalense / 100.;
    ampx = gain.colx;
    ampy = gain.coly;
    detector = gain.detector;
    chip = gain.chip;
    dqpat = par->badinpdq;

    ipts = calloc (nimgs, sizeof(int));
    npts = calloc (dim_x, sizeof(int));
    buf = calloc (dim_x, sizeof(float));
    exp2 = (float *) calloc (nimgs, sizeof(float));
    bufdq = calloc (dim_x, sizeof(short));

    for (k = 0; k < NAMPS; k++) {
        gain2[k] = 0.;
        noise2[k] = 0.;
        /* Assumption: ALL images have the same noise/gain values */
        rog2[k] = SQ(noise.val[k]);
    }

    non_zero = 0;
    for (n = 0; n < nimgs; n++) {
        exp2[n] = SQ(efac[n]);
	if (efac[n] > 0.) non_zero++;
    }
    get_nsegn (detector, chip, ampx, ampy, gain.val, rog2, gain2, noise2); 

    /* Use the stack median to construct the initial average */
    if (strncmp(par->initgues, "median", 3) == 0) {
        for (j = 0; j < dim_y; j++) {
            memset (npts, 0, dim_x*sizeof(int));

            /* Set up the gain and noise values used for this line
	    ** in ALL images */
            if (j < ampy ) {
                gn[0] = gain2[AMP_C];
                gn[1] = gain2[AMP_D];
                nse[0] = noise2[AMP_C];
                nse[1] = noise2[AMP_D];
            } else {
                gn[0] = gain2[AMP_A];
                gn[1] = gain2[AMP_B];
                nse[0] = noise2[AMP_A];
                nse[1] = noise2[AMP_B];            
            }

            for (n = 0; n < nimgs; n++) {
                initHdr(&dqhdr);
                getHeader(ipdq[n],&dqhdr);
                getFloatLine (ipsci[n], j, buf);
                getShortLine (ipdq[n], j, bufdq);
                freeHdr(&dqhdr);

		/* Rescale SCI data, if necessary */
		if (bunit[n] == COUNTRATE) {
		    for (i = 0; i < dim_x; i++) {
			 buf[i] *= efac[n];
		    }
		}

                for (i = 0; i < dim_x; i++) {
		     if (efac[n] > 0.) {
                         /* Only use GOOD pixels to build initial image */
                         if ((bufdq[i] & dqpat) == OK) {
                             PIX(work,npts[i],i,nimgs) =
						(buf[i] - skyval[n]) / efac[n];
                             npts[i] += 1;
                         }
		     } else {
			 PIX(work,npts[i],i,nimgs) = 0.;
		     }
                }
            }
 
            for (i = 0; i < ampx; i++) {
                dum = npts[i];

                if (dum == 0)
                    Pix(sg->sci.data,i,j) = 0.0F;

                else {

		    /* Setup index array for sorting... */
		    for (p=0; p < nimgs; p++) ipts[p] = p;
		    /* Sort pixel stack and corresponding index array. */
                    ipiksrt (&PIX(work,0,i,nimgs), dum, ipts);

		    /* Use sorted index array to match proper exptimes to
		       selected pixels for use in ERR array calculation. */
                    if ((dum/2)*2 == dum) {
                        /* Even number of input images for this pixel */
                        Pix(sg->sci.data,i,j) = (PIX(work,dum/2-1,i,nimgs) +
						 PIX(work,dum/2,i,nimgs)) / 2.;
			expn = (exp2[ipts[dum/2-1]] + exp2[ipts[dum/2]]) / 2.;
                    } else {
			/* Odd number of input images for this pixel */
                        Pix(sg->sci.data,i,j) = PIX(work,dum/2,i,nimgs);
			expn = exp2[ipts[dum/2]];
		    }
                }
                
                raw0 = Pix(sg->sci.data,i,j);
		exp2n = (expn > 0.) ? expn : 1.;
                Pix(sg->err.data,i,j) = (nse[0]+ raw0/gn[0] + SQ(scale*raw0)) /
					exp2n;
            } /* End loop over FIRST AMP used on pixels in the line */
             
            for (i = ampx; i < dim_x; i++) {
                dum = npts[i];
                if (dum == 0)
                    Pix(sg->sci.data,i,j) = 0.0F;
                else {
		    for (p=0; p < nimgs; p++) ipts[p] = p;
                    ipiksrt (&PIX(work,0,i,nimgs), dum, ipts);
                    if ((dum/2)*2 == dum) {
                        /* Even number of input images for this pixel */
                        Pix(sg->sci.data,i,j) = (PIX(work,dum/2-1,i,nimgs) +
						 PIX(work,dum/2,i,nimgs)) / 2.;
			expn = (exp2[ipts[dum/2-1]] + exp2[ipts[dum/2]]) / 2.;
                    } else {
                        Pix(sg->sci.data,i,j) = PIX(work,dum/2,i,nimgs);
			expn = exp2[ipts[dum/2]];
		    }
                }
                
                raw0 = Pix(sg->sci.data,i,j);
		exp2n = (expn > 0.) ? expn : 1.;
                Pix(sg->err.data,i,j) = (nse[1]+ raw0/gn[1] + SQ(scale*raw0)) /
					exp2n;
            } /* End loop over SECOND AMP used on pixels in the line */
        } /* End loop over lines */

    } else {

        /* use the minimum to construct the initial average */
        if (strncmp(par->initgues, "minimum", 3) != 0) {
            sprintf (MsgText,"Invalid INITGUES value %s, reset it to 'minimum'",
		     par->initgues);
            trlwarn (MsgText);
            strcpy (par->initgues, "minimum");
        }

        for (n = 0; n < nimgs; n++) {
            initHdr(&dqhdr);
            getHeader(ipdq[n],&dqhdr);
            for (j = 0; j < dim_y; j++) { 
                /* Set up the gain and noise values used for this line
		** in ALL images */
                if (j < ampy ) {
                    gn[0] = gain2[AMP_C];
                    gn[1] = gain2[AMP_D];
                    nse[0] = noise2[AMP_C];
                    nse[1] = noise2[AMP_D];
                } else {
                    gn[0] = gain2[AMP_A];
                    gn[1] = gain2[AMP_B];
                    nse[0] = noise2[AMP_A];
                    nse[1] = noise2[AMP_B];            
                }

                getFloatLine (ipsci[n], j, buf);
		getShortLine (ipdq[n],  j, bufdq);

		/* Rescale SCI data, if necessary */
		if (bunit[n] == COUNTRATE) {
		    for (i = 0; i < dim_x; i++) {
			 buf[i] *= efac[n];
		    }
		}

                /* AMPS C and D */
                for (i = 0; i < ampx; i++) {
                    raw = buf[i];
                    raw0 = (raw > 0.)? raw : 0.;
		    signal0 = ((raw - skyval[n]) > 0.) ? (raw - skyval[n]) : 0.;

		    if (efac[n] > 0.) {
                        val = (raw - skyval[n]) / efac[n];
		    } else {
			val = 0.;
		    }
                    if ((n == 0) || (val < Pix(sg->sci.data,i,j)) ) {
			if ((bufdq[i] & dqpat) == OK && (efac[n] > 0.)) {
                             Pix(sg->sci.data,i,j) = val;
                             /*Pix(sg->err.data,i,j) =
			      (nse[0]+ raw0/gn[0] + SQ(scale*raw0)) / exp2[n];*/
			     Pix(sg->err.data,i,j) =
			    (nse[0] + raw0/gn[0] + SQ(scale*signal0)) / exp2[n];
			} else {
			     Pix(sg->sci.data,i,j) = 0.;
			     Pix(sg->err.data,i,j) = 0.;
			}
                    } 
                } /* End of loop over FIRST AMP for this line in each image */

                for (i = ampx; i < dim_x; i++) {
                    raw = buf[i];
                    raw0 = (raw > 0.)? raw : 0.;
		    signal0 = ((raw - skyval[n]) > 0.) ? (raw - skyval[n]) : 0.;

		    if (efac[n] > 0.) {
                        val = (raw - skyval[n]) / efac[n];
		    } else {
			val = 0.;
		    }
                    if ((n == 0) ||
		       (val<Pix(sg->sci.data,i,j) && ((bufdq[i]&dqpat)==OK))) {
                        Pix(sg->sci.data,i,j) = val;
			if (efac[n] > 0.) {
                            Pix(sg->err.data,i,j) = 
			     (nse[1]+ raw0/gn[1] + SQ(scale*signal0)) / exp2[n];
			} else {
			    Pix(sg->err.data,i,j) = 0.;
			}
                    } 
                } /* End of loop over SECOND AMP for this line in each image */

            } /* End of loop over lines in image (y) */
            freeHdr(&dqhdr);
        } /* End of loop over images in set */
    }

    /* free the memory */
    free (ipts);
    free (npts);
    free (buf);
    free (exp2);
    free (bufdq);

    return (status);
}
Esempio n. 20
0
int GetCCDInfo (WF3Info *wf3, CCD_Switch *sci_sw, RefFileInfo *sciref) {

/* arguments:
WF3Info *wf3          i: calibration flags and other info
CCD_Switch *sci_sw    o: all calibration switches (0 or 1) for science file
RefFileInfo *sciref  io: list of keyword,filename pairs for science file
*/

	extern int status;

	IODescPtr im;		/* descriptor for an image */
	Hdr phdr;		/* primary header */
	int nextend;		/* number of FITS extensions in rawfile */

	int GetKeyInt (Hdr *, char *, int, int, int *);
	int GetKeyFlt (Hdr *, char *, int, float, float *);
	int GetCCDSws (CCD_Switch *, Hdr *);
	int GetCCDRef (WF3Info *, CCD_Switch *, Hdr *, RefFileInfo *);
	
	/* Open input raw data file. */
	initHdr (&phdr);
	im = openInputImage (wf3->rawfile, "", 0);
	if (hstio_err()) {
	    sprintf (MsgText, "Member \"%s\" is not present", wf3->rawfile);
	    trlerror (MsgText);
        freeHdr(&phdr);
	    return (status = OPEN_FAILED);
	}

	/* Read primary header into pdhr. */
	getHeader (im, &phdr);
	if (hstio_err()) {
	    sprintf (MsgText, "Could not open PRIMARY header for \"%s\" ",
		     wf3->rawfile);
	    trlmessage (MsgText);
        closeImage(im);
        freeHdr(&phdr);
	    return (status = OPEN_FAILED);
	}
	closeImage (im);
	
	/* Get generic parameters: */

	/* Find out how many extensions there are in this file. */
	if (GetKeyInt (&phdr, "NEXTEND", USE_DEFAULT, EXT_PER_GROUP, &nextend)){
        freeHdr(&phdr);
        closeImage(im);
	    return (status = KEYWORD_MISSING);
    }
        
	wf3->nchips = nextend / EXT_PER_GROUP;

	/* Get binning and gain info.  We really only need this for the CCD. */
	if (GetKeyInt (&phdr, "BINAXIS1", USE_DEFAULT, 1, &wf3->scibin[0])) {
        closeImage(im);
        freeHdr(&phdr);
	    return (status = KEYWORD_MISSING);
    }
	if (GetKeyInt (&phdr, "BINAXIS2", USE_DEFAULT, 1, &wf3->scibin[1])){
        closeImage(im);
        freeHdr(&phdr);
	    return (status = KEYWORD_MISSING);
    }
	if (GetKeyFlt (&phdr, "CCDGAIN",  USE_DEFAULT, 1.5, &wf3->scigain)){
        closeImage(im);
        freeHdr(&phdr);
	    return (status = KEYWORD_MISSING);
    }
    
	wf3->samebin = 1;	/* default */

	/* Get calibration switches, and check that reference files exist. */
	if (GetCCDSws (sci_sw, &phdr))
	    return (status = KEYWORD_MISSING);
	if (GetCCDRef (wf3, sci_sw, &phdr, sciref))
	    return (status = CAL_FILE_MISSING);

	freeHdr (&phdr);
	return (status);
}
Esempio n. 21
0
int AcsSum (char *input, char *output, char *mtype, int printtime, int verbose)
{

    extern int status;

    IRAFPointer tpin;

    AcsSumInfo acs;
    IODescPtr im;        /* descriptor for input image */
    Hdr phdr;        /* primary header for input image */
    int nimgs;
    int i;
    char acs_input[CHAR_FNAME_LENGTH];

    int          FileExists (char *);
    void         TimeStamp (char *, char *);
    void         PrBegin (char *);
    void         PrEnd (char *);
    void         PrFileName (char *, char *);
    void         PrHdrInfo (char *, char *, char *, char *);
    int          MkName (char *, char *, char *, char *, char *, int);
    void         WhichError (int);
    void         InitSumTrl (char *input, char *output);
    void         FindAsnRoot (char *, char *);
    int          mkNewSpt (char *, char *, char *);

    /* Determine input and output trailer files, then initialize
        output file by combining inputs into output file */
    InitSumTrl (input, output);

    PrBegin ("ACSSUM");
    nimgs = 0;

    if (printtime)
        TimeStamp ("ACSSUM started", "");

    /* open the input file template */
    tpin = c_imtopen (input);

    nimgs = c_imtlen(tpin);

    /* Initialize structure containing acssum information. */
    AcsInit (&acs,nimgs);

    /* Copy command-line arguments into acs. */
    for (i = 0; i < nimgs; i++) {
        c_imtgetim (tpin, acs.input[i], CHAR_FNAME_LENGTH);
        PrFileName ("input", acs.input[i]);
    }

    /* close file template */
    c_imtclose (tpin);

    strcpy (acs.output, output);
    acs.printtime = printtime;
    acs.verbose = verbose;

    PrFileName ("output", acs.output);
    FindAsnRoot (output, acs.rootname);

    initHdr (&phdr);

    /* Check whether the output file already exists. */
    if (FileExists (acs.output)) {
        FreeAcsInput (acs.input, nimgs);
        return (status);
    }
    strcpy(acs_input,acs.input[0]);

    /* Open input image in order to read its primary header. */
    im = openInputImage (acs_input, "", 0);

    if (hstio_err()) {
        FreeAcsInput (acs.input, nimgs);
        return (status = OPEN_FAILED);
    }

    getHeader (im, &phdr);        /* get primary header */
    if (hstio_err()) {
        FreeAcsInput (acs.input, nimgs);
        return (status = OPEN_FAILED);
    }
    closeImage (im);

    /* Get keyword values from primary header. */
    if (GetSumKeyInfo (&acs, &phdr)) {
        FreeAcsInput (acs.input, nimgs);
        return (status);
    }
    freeHdr (&phdr);

    /* Print information about this image. */
    PrHdrInfo (acs.aperture, acs.filter1, acs.filter2, acs.det);

    if (acs.printtime)
        TimeStamp ("Begin processing", acs.rootname);

    /* Sum all imsets. */
    if (SumGrps (&acs,mtype)){
        FreeAcsInput (acs.input, nimgs);
        return (status);
    }

    /* create new SPT file for output product */
    if (mkNewSpt (input, mtype, output)) {
        return(status);
    }

    /* Done... */
    trlmessage ("\n");
    PrEnd ("ACSSUM");

    if (acs.printtime)
        TimeStamp ("ACSSUM completed", acs.rootname);

    /* Write out temp trailer file to final file */
    WriteTrlFile ();

    FreeAcsInput (acs.input, nimgs);
    return (status);
}
Esempio n. 22
0
int WF3cte (char *input, char *output, CCD_Switch *cte_sw,
        RefFileInfo *refnames, int printtime, int verbose, int onecpu) {

/*
input: filename
output: filename
cte_sw: the calibration flags
refnames: the names of the calibration reference files
onecpu: use parallel processing?

The following are new primary header keywords which will be added to the data
so that they can be updated by the code. They are also specified in the PCTETAB
reference file.

These are taken from the PCTETAB
CTE_NAME - name of cte algorithm
CTE_VER - version number of cte algorithm
CTEDATE0 - date of wfc3/uvis installation in HST, in MJD
CTEDATE1 - reference date of CTE model pinning, in MJD

PCTETLEN - max length of CTE trail
PCTERNOI - readnoise amplitude for clipping
PCTESMIT - number of iterations used in CTE forward modeling
PCTESHFT - number of iterations used in the parallel transfer
PCTENSMD - readnoise mitigation algorithm
PCTETRSH - over-subtraction threshold
PCTEFRAC - cte scaling frac calculated from expstart
PCTERNOI - the readnoise clipping level to use      

#These are taken from getreffiles.c
DRKCFILE is a new dark reference file used only in the CTE branch *_DRC.fits
BIACFILE is a new super-bias reference file used only in the CTE branch *_BIC.fits
PCTETAB is a new reference file FITS table which will contain the software parameter switches for the CTE correction *_CTE.fit

This is the main workhorse function for removing the CTE from WFC3 UVIS images  

Unfortunately this happens before anything else in wfc3, so there's a lot of reading files
at the beginning in order to populate needed information. The rest of the pipeline works
on one chip at a time and the structures are all defined to support that. None of these 
structures are defined until the code enters the single chip loops. This differs from the
CTE correction in ACS which occurs later in the process after basic structures are defined.

*/

    extern int status;

    WF3Info wf3; /*structure with calibration switches and reference files for passing*/
    Hdr phdr; /*primary header for input image, all output information saved here*/
        
    CTEParams cte_pars; /*STRUCTURE HOLDING THE MODEL PARAMETERS*/
    SingleGroup cd; /*SCI 1*/
    SingleGroup ab; /*SCI 2*/
    SingleGroup raz; /* THE LARGE FORMAT COMBINATION OF CDAB*/
    SingleGroup rsz; /* LARGE FORMAT READNOISE CORRECTED IMAGE */
    SingleGroup rsc; /* CTE CORRECTED*/
    SingleGroup rzc; /* FINAL CTE CORRECTED IMAGE */
    SingleGroup chg; /* THE CHANGE DUE TO CTE  */
    SingleGroup raw; /* THE RAW IMAGE IN RAZ FORMAT */
    
    int i,j; /*loop vars*/
    int max_threads=1;
    clock_t begin;
    double  time_spent;
    float hardset=0.0;

    begin = (double)clock();
        
    Bool subarray; /* to verify that no subarray is being used, it's not implemented yet*/

    /*CONTAIN PARALLEL PROCESSING TO A SINGLE THREAD AS USER OPTION*/
#   ifdef _OPENMP
    trlmessage("Using parallel processing provided by OpenMP inside CTE routine");
    if (onecpu){
        omp_set_dynamic(0);
        max_threads=1;
        sprintf(MsgText,"onecpu == TRUE, Using only %i threads/cpu", max_threads);
    } else {
        omp_set_dynamic(0);
        max_threads = omp_get_num_procs(); /*be nice, use 1 less than avail?*/
        sprintf(MsgText,"Setting max threads to %i of %i cpus",max_threads, omp_get_num_procs()); 
    }
    omp_set_num_threads(max_threads);
    trlmessage(MsgText);
#   endif
   

    /* COPY COMMAND-LINE ARGUMENTS INTO WF3. */ 
    WF3Init (&wf3);
    strcpy (wf3.input, input);
    strcpy (wf3.output, output);

    PrBegin ("WFC3CTE");
    if (wf3.printtime)
        TimeStamp("WFC3CTE Started: ",wf3.rootname);

    /* CHECK WHETHER THE OUTPUT FILE ALREADY EXISTS. */
    if (FileExists (wf3.output)){
        WhichError(status);
        return (ERROR_RETURN);
    }

    wf3.pctecorr = cte_sw->pctecorr;
    wf3.darkcorr = cte_sw->darkcorr;
    wf3.biascorr = cte_sw->biascorr;
    wf3.blevcorr = cte_sw->blevcorr;
    wf3.printtime = printtime;
    wf3.verbose = verbose;
    wf3.refnames = refnames;

    PrFileName ("input", wf3.input);
    PrFileName ("output", wf3.output);

    if (wf3.biascorr == COMPLETE){
        trlmessage("BIASCORR complete for input image, CTE can't be performed");
        return(ERROR_RETURN);
    }
    if (wf3.darkcorr == COMPLETE){
        trlmessage("DARKCORR complete for input image, CTE can't be performed");
        return(ERROR_RETURN);
    }
    if (wf3.blevcorr == COMPLETE){
        trlmessage("BLEVCORR complete for input image, CTE can't be performed");
        return(ERROR_RETURN);
    }


    /* DETERMINE THE NAMES OF THE TRAILER FILES BASED ON THE INPUT
       AND OUTPUT FILE NAMES, THEN INITIALIZE THE TRAILER FILE BUFFER
       WITH THOSE NAMES.
       */
    if (initCTETrl (input, output))
        return (status);
    

    /* OPEN INPUT IMAGE IN ORDER TO READ ITS PRIMARY HEADER. */
    if (LoadHdr (wf3.input, &phdr) ){
        WhichError(status);
        return (ERROR_RETURN);
    }

    /* GET KEYWORD VALUES FROM PRIMARY HEADER. */
    if (GetKeys (&wf3, &phdr)) {
        freeHdr (&phdr);
        return (status);
    }

    if (GetCTEFlags (&wf3, &phdr)) {
        freeHdr(&phdr);
        return (status);
    }

    /* OPEN THE INPUT IMAGES AND GET THE  SCIENCE EXTENSIONS  */
    initSingleGroup (&cd);
    getSingleGroup (wf3.input, 1, &cd);
    
    if (hstio_err())
        return (status = OPEN_FAILED);

    /*** MAKE SURE THIS IS NOT A SUBARRAY ***/
    if (GetKeyBool (cd.globalhdr, "SUBARRAY", NO_DEFAULT, 0, &subarray))
        return (status=KEYWORD_MISSING);

    if (subarray) {
        sprintf(MsgText,"**SUBARRAY FOUND!; SUBARRAY images are not yet supported for CTE**");
        trlmessage(MsgText);
        status=ERROR_RETURN;
        return(status);
    }   
    
    initSingleGroup (&ab);
    getSingleGroup (wf3.input, 2, &ab);
    if (hstio_err())
        return (status = OPEN_FAILED);

    if (GetKeyBool (ab.globalhdr, "SUBARRAY", NO_DEFAULT, 0, &subarray))
        return (status=KEYWORD_MISSING);

    if (subarray) {
        sprintf(MsgText,"SUBARRAY FOUND; **SUBARRAY images are not yet supported for CTE**");
        trlmessage(MsgText);
        status=ERROR_RETURN;
        return(status);
    }

    /*READ IN THE CTE PARAMETER TABLE*/
    initCTEParams(&cte_pars);
    if (GetCTEPars (wf3.pctetab.name,&cte_pars))
        return (status);

    if (verbose){
        PrRefInfo ("pctetab", wf3.pctetab.name, wf3.pctetab.pedigree,
                wf3.pctetab.descrip, wf3.pctetab.descrip2);
    }
    
    
    /*SAVE THE PCTETABLE INFORMATION TO THE HEADER OF THE SCIENCE IMAGE
      AFTER CHECKING TO SEE IF THE USER HAS SPECIFIED ANY CHANGES TO THE
      CTE CODE VARIABLES.
    */
    if (CompareCTEParams(&cd, &cte_pars)){
        return (status);
    }    


    /*SET UP THE ARRAYS WHICH WILL BE PASSED AROUND*/  
    initSingleGroup(&raz);
    allocSingleGroup(&raz, RAZ_COLS, RAZ_ROWS);

    initSingleGroup(&rsz);
    allocSingleGroup(&rsz, RAZ_COLS, RAZ_ROWS);

    initSingleGroup(&rsc);
    allocSingleGroup(&rsc, RAZ_COLS, RAZ_ROWS);

    initSingleGroup(&rzc);
    allocSingleGroup(&rzc, RAZ_COLS, RAZ_ROWS);
    
    initSingleGroup(&raw);
    allocSingleGroup(&raw, RAZ_COLS, RAZ_ROWS);
    
    initSingleGroup(&chg);    
    allocSingleGroup(&chg, RAZ_COLS, RAZ_ROWS);

    for (i=0;i<RAZ_COLS;i++){ 
        for(j=0;j<RAZ_ROWS;j++){ 
            Pix(raw.sci.data,i,j)=hardset;
            Pix(rsz.sci.data,i,j)=hardset;
            Pix(raz.sci.data,i,j)=hardset;
            Pix(rsc.sci.data,i,j)=hardset;
            Pix(rzc.sci.data,i,j)=hardset;
            Pix(chg.sci.data,i,j)=hardset;
        }
    }            


    /* SAVE A COPY OF THE RAW IMAGE FOR LATER */
    makesciRAZ(&cd,&ab,&raw);

    /***SUBTRACT THE CTE BIAS FROM BOTH CHIPS IN PLACE***/
    if (doCteBias(&wf3,&cd)){
        freeSingleGroup(&cd);
        return(status);
    }

    
    if (doCteBias(&wf3,&ab)){
        freeSingleGroup(&ab);
        return(status);
    }
      

    /*CONVERT TO RAZ FORMAT AND CORRECT FOR GAIN*/
    if (raw2raz(&wf3, &cd, &ab, &raz))
        return (status);

    /***CALCULATE THE SMOOTH READNOISE IMAGE***/
    trlmessage("CTE: Calculating smooth readnoise image");
    
 
    /***CREATE THE NOISE MITIGATION MODEL ***/
    if (cte_pars.noise_mit == 0) {
        if (raz2rsz(&wf3, &raz, &rsz, cte_pars.rn_amp, max_threads))
            return (status);
    } else {
        trlmessage("Only noise model 0 implemented!");
        return (status=ERROR_RETURN);
    }

    /***CONVERT THE READNOISE SMNOOTHED IMAGE TO RSC IMAGE
        THIS IS WHERE THE CTE GETS CALCULATED         ***/
    if (rsz2rsc(&wf3, &rsz, &rsc, &cte_pars))
        return (status);
       
    /*** SAVE USEFULL HEADER INFORMATION ***/
    if (cteHistory (&wf3, cd.globalhdr))
        return (status);
    
    /*** CREATE THE FINAL CTE CORRECTED IMAGE, PUT IT BACK INTO ORIGNAL RAW FORMAT***/
    for (i=0;i<RAZ_COLS;i++){
        for(j=0; j<RAZ_ROWS; j++){
            Pix(chg.sci.data,i,j) = (Pix(rsc.sci.data,i,j) - Pix(rsz.sci.data,i,j))/wf3.ccdgain;
            Pix(rzc.sci.data,i,j) =  Pix(raw.sci.data,i,j) + Pix(chg.sci.data,i,j);
        }
    }
            
    /*BACK TO NORMAL FORMATTING*/
    undosciRAZ(&cd,&ab,&rzc);
    
    /*UPDATE THE OUTPUT HEADER ONE FINAL TIME*/
    PutKeyDbl(cd.globalhdr, "PCTEFRAC", cte_pars.scale_frac,"CTE scaling fraction based on expstart");
    trlmessage("PCTEFRAC saved to header");
    
    /*SAVE THE NEW RAW FILE WITH UPDATED SCIENCE ARRAYS AND PRIMARY HEADER TO RAC*/
    putSingleGroup(output,cd.group_num, &cd,0);
    putSingleGroup(output,ab.group_num, &ab,0);


    /** CLEAN UP **/    
    freeSingleGroup(&rzc);
    freeSingleGroup(&rsc);
    freeSingleGroup(&chg);
    freeSingleGroup(&raz);
    freeSingleGroup(&rsz);
    freeSingleGroup(&raw);
    
    time_spent = ((double) clock()- begin +0.0) / CLOCKS_PER_SEC;
    if (verbose){
        sprintf(MsgText,"CTE run time: %.2f(s) with %i procs/threads\n",time_spent,max_threads);
        trlmessage(MsgText);
    }

    PrSwitch("pctecorr", COMPLETE);
    if(wf3.printtime)
        TimeStamp("PCTECORR Finished",wf3.rootname);

    return (status);
}