示例#1
0
文件: sink.c 项目: sosey/hstcal
int SinkDetect(WF3Info *wf3, SingleGroup *x){

    extern int status;
    int i,j, jj;
    short dqval=0;
    float scipix; /*to save the value of the science pixel*/
    float refdate=50000.;
    int keep_going=1;
    
    sprintf(MsgText,"\nPerforming SINK pixel detection for imset %i",x->group_num);
    trlmessage(MsgText);
    

    /*THE SCIENCE IMAGE*/
    SingleGroup raz; /*quad rotated image to work with*/    

    /* INIT THE SCIENCE INPUT  */
    initSingleGroup (&raz);
    allocSingleGroup (&raz,RAZ_COLS/2, RAZ_ROWS);
        
    /*CONVERT DQ DATA TO RAZ FORMAT FOR SCIENCE FILE*/
    makedqRAZ(x, &raz);
    makeSciSingleRAZ(x, &raz);

	/* GET THE SINK FILE REFERENCE IMAGE FROM SINKFILE AND INITIALIZE */
    FloatHdrData sinkref;
    initFloatHdrData(&sinkref);
    getFloatHD(wf3->sink.name,"SCI",x->group_num,&sinkref);
                 
    /*NOW TURN THE SINK REFERENCE IMAGES INTO RAZ FORMAT*/
    FloatTwoDArray sinkraz;    
    initFloatData(&sinkraz); /*float 2d arrays*/
    allocFloatData(&sinkraz,RAZ_COLS/2, RAZ_ROWS);     

    makeFloatRaz(&sinkref.data,&sinkraz,x->group_num);

       
    /*THE MJD OF THE SCIENCE EXPOSURE IS THE COMPARISON DATE
     THE FOLLOWING TRANSLATION TAKEN FROM ISR WFC3-2014-22.PDF */
    
    scipix=0.;
    for (i=0;i<(RAZ_COLS/2);i++){
        for (j=0; j<RAZ_ROWS; j++){
        
            if (  (PPix(&sinkraz,i,j) > refdate)  &&  ( wf3->expstart > PPix(&sinkraz,i,j))  ){
                keep_going=1;
                
                /*FLAG THE PRIMARY SINK PIXEL*/
                dqval = TRAP | DQPix (raz.dq.data, i, j);
                DQSetPix (raz.dq.data, i, j, dqval);
                scipix = Pix(raz.sci.data,i,j);
                
                /*FLAG THE DOWNSTREAM PIXEL*/
                if (PPix(&sinkraz,i,j-1) < 0 ){
                    dqval = TRAP | DQPix (raz.dq.data, i, j-1);
                    DQSetPix (raz.dq.data, i, j-1, dqval);
                }

                /*FLAG THE UPSTREAM PIXELS*/
                for (jj=j+1; jj<RAZ_ROWS; jj++){
                    if ((int) PPix(&sinkraz,i,jj) == 0)
                        keep_going=0;
                    if ( PPix(&sinkraz,i,jj) > refdate)
                        keep_going=0;
                    if ( 0. < PPix(&sinkraz,i,jj) &&  PPix(&sinkraz,i,jj) < 1000. && keep_going){
                        if (scipix <= PPix(&sinkraz,i,jj) ){
                           dqval = TRAP | DQPix (raz.dq.data, i, jj);
                           DQSetPix (raz.dq.data, i, jj, dqval);
                        }                
                    } else {
                        keep_going=0;
                    }
                }                
            } /*end if*/ 
        } /*end j*/
    }/*end i*/   

    
    /*format the dq data back to expected orientation*/
    undodqRAZ(x,&raz);

    freeSingleGroup(&raz);
    freeFloatData(&sinkraz);
    freeFloatHdrData(&sinkref);
    trlmessage("Sink pixel flagging complete");
    return(status);
}
示例#2
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);
}