예제 #1
0
int main (int argc, char **argv) {

    char *inlist;		/* input file name */
    char *outlist;		/* output blev file name */
    /*int switch_on = 0;*/	/* was any switch specified? */
    int printtime = NO;	/* print time after each step? */
    int verbose = NO;	/* print additional info? */
    int quiet = NO;	/* print additional info? */
    int too_many = 0;	/* too many command-line arguments? */
    int i, j;		/* loop indexes */
    int k;

    IRAFPointer i_imt, o_imt;	/* imt list pointers */
    char *input;		/* name of input science file */
    char *output;		/* name of output file */
    int n_in, n_out;	/* number of files in each list */
    int n;

    /* Input and output suffixes. */
    char isuffix[] = "_raw";
    char osuffix[] = "_blv_tmp";

    /* A structure to pass the calibration switches to ACSCCD */
    CalSwitch ccd_sw;

    /* reference file keywords and names */
    RefFileInfo refnames;

    void InitRefFile (RefFileInfo *);
    void FreeRefFile (RefFileInfo *);
    void initSwitch (CalSwitch *);

    int ACSccd (char *, char *, CalSwitch *, RefFileInfo *, int, int);
    int DefSwitch (char *);
    int MkName (char *, char *, char *, char *, char *, int);
    void WhichError (int);
    int CompareNumbers (int, int, char *);

    /* For image header access */
    Hdr phdr;
    int LoadHdr (char *, Hdr *);
    int GetSwitch (Hdr *, char *, int *);

    c_irafinit (argc, argv);

    /* Allocate space for file names. */
    inlist = calloc (ACS_LINE+1, sizeof (char));
    outlist = calloc (ACS_LINE+1, sizeof (char));
    input = calloc (ACS_LINE+1, sizeof (char));
    output = calloc (ACS_LINE+1, sizeof (char));

    if (inlist == NULL || outlist == NULL ||
        input == NULL || output == NULL) {
        printf ("Can't even begin; out of memory.\n");
        exit (ERROR_RETURN);
    }
    inlist[0] = '\0';
    outlist[0] = '\0';
    input[0] = '\0';
    output[0] = '\0';

    /* Initialize the lists of reference file keywords and names. */
    InitRefFile (&refnames);

    /* Initial values. */
    initSwitch (&ccd_sw);

    for (i = 1;  i < argc;  i++) {

        /**********
        if (strcmp (argv[i], "-dqi") == 0) {
            ccd_sw.dqicorr = PERFORM;
            switch_on = 1;
        } else if (strcmp (argv[i], "-atod") == 0) {
            ccd_sw.atodcorr = PERFORM;
            switch_on = 1;
        } else if (strcmp (argv[i], "-blev") == 0) {
            ccd_sw.blevcorr = PERFORM;
            switch_on = 1;
        } else if (strcmp (argv[i], "-bias") == 0) {
            ccd_sw.biascorr = PERFORM;
            switch_on = 1;
        } else if (argv[i][0] == '-') {
        **********/
        if (argv[i][0] == '-') {
            for (j = 1;  argv[i][j] != '\0';  j++) {
                if (argv[i][j] == 't') {
                    printtime = YES;
                } else if (argv[i][j] == 'v') {
                    verbose = YES;
                } else if (argv[i][j] == 'q') {
                    quiet = YES;
                } else {
                    printf (MsgText, "Unrecognized option %s\n", argv[i]);
                    FreeNames (inlist, outlist, input, output);
                    exit (1);
                }
            }
        } else if (inlist[0] == '\0') {
            strcpy (inlist, argv[i]);
        } else if (outlist[0] == '\0') {
            strcpy (outlist, argv[i]);
        } else {
            too_many = 1;
        }
    }
    if (inlist[0] == '\0' || too_many) {
        printf ("syntax:  acsccd [-t] [-v] [-q] input output\n");
        /*
        printf ("  command-line switches:\n");
        printf ("       -dqi -atod -blev -bias\n");
        */
        FreeNames (inlist, outlist, input, output);
        exit (ERROR_RETURN);
    }
    /* Initialize the structure for managing trailer file comments */
    InitTrlBuf ();

    /* Copy command-line value for QUIET to structure */
    SetTrlQuietMode(quiet);

    /* Was no calibration switch specified on command line?
       default values (mostly PERFORM) except ATODCORR
    if (!switch_on) {*/
    ccd_sw.dqicorr  = DefSwitch ("dqicorr");
    ccd_sw.atodcorr = DefSwitch ("atodcorr");
    ccd_sw.blevcorr = DefSwitch ("blevcorr");
    ccd_sw.biascorr = DefSwitch ("biascorr");
    /*}*/

    /* Expand the templates. */
    i_imt = c_imtopen (inlist);
    o_imt = c_imtopen (outlist);
    n_in = c_imtlen (i_imt);
    n_out = c_imtlen (o_imt);

    /* The number of input and output files must be the same. */
    if (CompareNumbers (n_in, n_out, "output"))
        status = 1;
    if (status) {
        FreeNames (inlist, outlist, input, output);
        CloseTrlBuf();
        exit (ERROR_RETURN);
    }

    /* Loop over the list of input files. */
    for (n = 0;  n < n_in;  n++) {

        k = c_imtgetim (i_imt, input, ACS_LINE);

        if (n_out > 0)
            k = c_imtgetim (o_imt, output, ACS_LINE);
        else
            output[0] = '\0';

        /* Open input image in order to read its primary header. */
        if (LoadHdr (input, &phdr)) {
            WhichError (status);
            sprintf (MsgText, "Skipping %s", input);
            trlmessage (MsgText);
            continue;
        }

        /* Determine osuffix. */
        strcpy(osuffix, "_blv_tmp");

        if (MkName (input, isuffix, osuffix, "", output, ACS_LINE)) {
            WhichError (status);
            sprintf (MsgText, "Skipping %s", input);
            trlmessage (MsgText);
            continue;
        }

        /* Calibrate the current input file. */
        if (ACSccd (input, output, &ccd_sw, &refnames, printtime, verbose)) {
            sprintf (MsgText, "Error processing %s.", input);
            trlerror (MsgText);
            WhichError (status);
        }
    }

    /* Close lists of file names, and free name buffers. */
    c_imtclose (i_imt);
    c_imtclose (o_imt);
    CloseTrlBuf();
    FreeRefFile (&refnames);
    FreeNames (inlist, outlist, input, output);

    if (status)
        exit (ERROR_RETURN);
    else
        exit (0);
}
예제 #2
0
파일: calacs.c 프로젝트: jhunkeler/hstcal
int ProcessACSCCD (AsnInfo *asn, ACSInfo *acshdr, int *save_tmp, int printtime, const unsigned nThreads, const unsigned cteAlgorithmGen, const char * pcteTabNameFromCmd) {

    extern int status;

    RefFileInfo sciref;       /* ref file keywords and names */
    CalSwitch sci_sw;         /* all cal switches for science file */
    CalSwitch acsccd_sci_sw;  /* ACSCCD switches for science file */
    CalSwitch acscte_sci_sw;  /* ACSCTE switches for science file */
    CalSwitch acs2d_sci_sw;   /* ACS2D switches for science file */
    int save_crj;             /* don't delete crj_tmp file? */

    int prod;             /* which CR-split/Rptobs product? */
    int posid;            /* counter for input products */
    int expid;            /* counter for input exposures */
    int newpreface = NO;  /* switch for keeping previous comments for
                             all remaining trailer files */

    char *acsrej_input;     /* list of input names for ACSREJ */
    char *acsrejc_input;    /* list of CTE corrected input names for ACSREJ */
    char *acsrej_msgtext;   /* str for list of input filenames */
    char *acsrejc_msgtext;  /* str for list of CTE corrected input filenames */
    int nchars;             /* Number of chars for input string to ACSREJ */

    void ACSDefaults (ACSInfo *);
    void InitRefFile (RefFileInfo *);
    void FreeRefFile (RefFileInfo *);
    int ACSRefInit (ACSInfo *, CalSwitch *, RefFileInfo *);
    int ACSccd (char *, char *, CalSwitch *, RefFileInfo *, int, int);
    int ACScte (char *, char *, CalSwitch *, RefFileInfo *, int, int, int, const unsigned cteAlgorithmGen, const char * pcteTabNameFromCmd, const bool forwardModelOnly);
    int ACS2d (char *, char *,CalSwitch *, RefFileInfo *, int, int);
    int GetAsnMember (AsnInfo *, int, int, int, ACSInfo *);
    int GetSingle (AsnInfo *, ACSInfo *);
    int CheckCorr (AsnInfo *, ACSInfo *);
    int InsertACSSuffix (ACSInfo *);

    save_crj = *save_tmp;  /* initial value; */

    /* Loop over the products/positions for each CR-split/Repeat-Obs set. */
    for (prod = 0; prod < asn->numprod; prod++) {
        if (asn->verbose){
            sprintf (MsgText, "CALACS: processing CCD product %d", prod);
            trlmessage (MsgText);
        }

        /* Process this PARTIAL/SINGLE/FULL product... */
        for (posid = 1; posid <= asn->numsp; posid++) {

            if (asn->verbose) {
                sprintf (MsgText,"CALACS: processing posid = %d", posid);
                trlmessage (MsgText);
            }
            /*  Allocate space for ACSREJ input image list */
            if ( (asn->crcorr == PERFORM) || (asn->rptcorr == PERFORM) ) {
                nchars = asn->spmems[posid] * CHAR_FNAME_LENGTH;
                acsrej_input = (char *) calloc( nchars + 1, sizeof(char));
                /* Initialize this string to NULL */
                acsrej_input[0] = '\0';

                acsrejc_input = (char *) calloc( nchars + 1, sizeof(char));
                /* Initialize this string to NULL */
                acsrejc_input[0] = '\0';
            }

            /* Run ACSCCD on individual EXPosures. */
            for (expid = 1; expid <= asn->spmems[posid]; expid++) {
                /* From this point on, we are working with ONE image at a time */

                /* Read in Member keyword info from --FIRST-- EXP image header */
                /*if(prod == 0 && posid == 1 && expid == 1) { */
                if(expid == 1) {
                    /* (Re-)Initialize the lists of reference file keywords
                       and names. */
                    InitRefFile (&sciref);

                    /* Assign default values in acs. */
                    ACSDefaults (acshdr);

                    if (asn->process == SINGLE ) {
                        if (GetSingle (asn, acshdr) )
                            return (status);
                    } else {
                        if( GetAsnMember(asn, prod, posid, expid, acshdr))
                            return (status);
                    }
                    /* Initialize variables, and get info (cal switches and
                       reference file names) from primary headers.
                    */
                    if (ACSRefInit (acshdr, &sci_sw, &sciref))
                        return (status);

                    /* Make sure 'save_tmp' and 'save_crj' are no
                       longer set to 'DUMMY'.

                       If save_tmp was set from the command-line, then
                       ignore what is set in the header keyword
                       EXPSCORR.
                    */
                    if (*save_tmp == DUMMY) {
                        *save_tmp = NO;
                        save_crj = *save_tmp;
                    }

                    if (asn->verbose) {
                        trlmessage ("CALACS: Got reference file information");
                    }
                    /* Store the trailer file comments into preface */
                    newpreface = YES;

                } else {

                    if (asn->process == SINGLE ) {
                        if (GetSingle (asn, acshdr))
                            return (status);
                    } else {
                        if( GetAsnMember(asn, prod, posid, expid, acshdr))
                            return (status);
                    }

                    /* Construct output and temporary file names. */
                    if (InsertACSSuffix (acshdr))
                        return (status);
                }

                /* Check ASN table settings for CRCORR/RPTCORR
                   against image header values.  If they are not
                   consistent, print out a ERROR message and quit.
                   Only need to perform this check on the first image
                   in the association. WJH 21 May 1999
                */
                if (prod == 0 && posid == 1 && expid == 1) {
                    if (CheckCorr(asn, acshdr))
                        return (status);
                }

                /* Copy switch values for ACSCCD, ACSCTE, and ACS2D
                   argument lists. */
                SetACSSw(&sci_sw, &acsccd_sci_sw, &acscte_sci_sw, &acs2d_sci_sw);

                if (asn->verbose) {
                    trlmessage ("CALACS: Processing switches set... ");
                }

                /* Ready to process input image now... */
                /* Do we have anything to do with this image? */
                if (acshdr->sci_basic_2d != PERFORM &&
                        acshdr->sci_basic_ccd != PERFORM &&
                        acshdr->sci_basic_cte != PERFORM &&
                        acshdr->sci_crcorr != PERFORM &&
                        acshdr->sci_rptcorr != PERFORM ){
                    trlwarn ("No calibration switch was set to PERFORM.");
                    FreeRefFile (&sciref);
                    return (status = NOTHING_TO_DO);
                }

                /* First, initialize the Input CCD image with ACSCCD.
                   Then, perform PCTECORR if needed.
                   Then, either do cosmic ray rejection followed by ACS2D,
                   or just run ACS2D. */

                /* First do atodcorr, dqicorr, and blevcorr
                   (or copy the name). */
                if (acshdr->sci_basic_ccd == PERFORM) {
                    /* This is acsccd; note that we use acsccd_sci_sw. */

                    /* Copy all messages up to here into preface buffer
                       to be prepended to all input file trailer files */
                    if (newpreface == YES) {
                        InitTrlPreface();
                    }

                    SetTrlPrefaceMode (YES);

                    if (ACSccd (acshdr->rawfile, acshdr->blv_tmp,
                                &acsccd_sci_sw, &sciref, printtime,
                                asn->verbose))
                        return (status);

                    /* Reset switches */
                    ResetACSSw (&acsccd_sci_sw, &acs2d_sci_sw);

                } else {
                    /* Copy the input file to blv_tmp. We need to do this
                       because acsrej may (depending on the crmask column
                       in the crrejtab) modify the DQ flags in its input file.
                    */
                    if (CopyFFile (acshdr->rawfile, acshdr->blv_tmp))
                        return (status);
                }

                /* Do PCTECORR now. */
                if (acshdr->sci_basic_cte == PERFORM) {
                    // ``forwardModelOnly = false`` because it is not part of the pipeline processing.
                    if (ACScte(acshdr->blv_tmp, acshdr->blc_tmp,
                               &acscte_sci_sw, &sciref, printtime,
                               asn->verbose, nThreads, cteAlgorithmGen, pcteTabNameFromCmd, false)) {
                        return (status);
                    }
                }

                /* We now are working with blv_tmp here... */
                /* Copy (cat) blv_tmp name to acsrej_input string */
                if ( (acshdr->sci_crcorr == PERFORM) ||
                     (acshdr->sci_rptcorr == PERFORM) ) {
                    strcat(acsrej_input, acshdr->blv_tmp);
                    if (expid < asn->spmems[posid]) {
                        /* Don't add a comma to the end of the last filename*/
                        strcat(acsrej_input, ",");
                    }
                    /* Also, add blv_tmp to list of files to be deleted */
                    strcpy(asn->product[prod].subprod[posid].exp[expid].blv_tmp,
                           acshdr->blv_tmp);

                    /* do this again for CTE corrected exposures */
                    if (acscte_sci_sw.pctecorr == PERFORM) {
                        strcat(acsrejc_input, acshdr->blc_tmp);
                        if (expid < asn->spmems[posid]) {
                            /* Don't add a comma to the end of the last filename*/
                            strcat(acsrejc_input, ",");
                        }
                        /* Also, add blc_tmp to list of files to be deleted */
                        strcpy(asn->product[prod].subprod[posid].exp[expid].blc_tmp, acshdr->blc_tmp);
                    }
                }
            }  /* end loop of individual EXPosures for running ACSCCD */

            /* Cosmic ray rejection, followed by basic 2-D processing
               of _crj_tmp. */
            if ( (acshdr->sci_crcorr == PERFORM) ||
                 (acshdr->sci_rptcorr == PERFORM) ) {
                if (asn->verbose) {
                    sprintf (MsgText,"CALACS: Now process position %d from product %d", posid, prod);
                    trlmessage (MsgText);
                }

                /* Commented out, not sure why these need to be reset.
                   Everything should have correct settings after ACSCCD.
                   MRD 17-May-2011 */
                /* Copy switch values for ACSCCD, ACSCTE, and ACS2D
                   argument lists. */
                /*SetACSSw (&sci_sw, &acsccd_sci_sw, &acscte_sci_sw, &acs2d_sci_sw); */
                /* Reset DQICORR, since it will already have been done...
                   WJH  23-Apr-2002 */
                /*acs2d_sci_sw.dqicorr = COMPLETE; */

                if (asn->debug){
                    sprintf(MsgText,"Non-CTE corrected Input to ACSREJ is:");
                    trlmessage(MsgText);

                    /* Need to allocate memory for a separate string to be
                       long enough to hold all the input names when printing
                       it out. Caused pipeline problems otherwise.
                       WJH 19-Mar-04 */
                    acsrej_msgtext = calloc(strlen(acsrej_input)+25,
                                            sizeof(char));
                    sprintf (acsrej_msgtext, "%s", acsrej_input);
                    trlmessage(acsrej_msgtext);
                    free(acsrej_msgtext);

                    sprintf(MsgText,"Non-CTE corrected Output to ACSREJ is: %s", acshdr->crj_tmp);
                    trlmessage(MsgText);

                    if (acscte_sci_sw.pctecorr == PERFORM) {
                        sprintf(MsgText,"CTE corrected Input to ACSREJ is:");
                        trlmessage(MsgText);

                        /* Need to allocate memory for a separate string to be
                           long enough to hold all the input names when printing
                           it out. Caused pipeline problems otherwise.
                           WJH 19-Mar-04 */
                        acsrejc_msgtext = calloc(strlen(acsrejc_input)+25, sizeof(char));
                        sprintf (acsrejc_msgtext, "%s",acsrejc_input);
                        trlmessage(acsrejc_msgtext);
                        free(acsrejc_msgtext);

                        sprintf(MsgText,"CTE corrected Output to ACSREJ is: %s", acshdr->crc_tmp);
                        trlmessage(MsgText);
                    }
                }

                /* Set up the correct value of ASN_MTYP for ACSREJ
                   output image. */
                if ( strncmp(acshdr->asn_table, acshdr->crj_tmp, 9) ) {
                    /* crj_tmp is a sub-product */
                    strcpy (acshdr->mtype, asn->product[prod].subprod[posid].mtype);
                } else {
                    /* crj_tmp leads to a product with same rootname as asn_table */
                    strcpy (acshdr->mtype, asn->product[prod].mtype);
                }

                // Reject cosmic rays.
                int pctecorrSwitchState = acscte_sci_sw.pctecorr;
                if (!OmitStep(pctecorrSwitchState))
                {
                    // Temporarily set this to OMIT so that ACS2d, called within
                    // the ACSRej_oWrapper call stack, uses the non-CTE darks.
                    // This is not thread safe.
                    acs2d_sci_sw.pctecorr = OMIT;
                }
                if ((status = ACSRej_0Wrapper(acsrej_input,
                        acshdr->crj_tmp,
                        acshdr->crjfile,
                        asn->product[prod].subprod[posid].crj_tmp,
                        acshdr,
                        asn,
                        prod,
                        posid,
                        &acs2d_sci_sw,
                        &sciref,
                        printtime,
                        save_crj,
                        True)))
                {
                    //freeStuff
                    delete((void**)&acsrej_input);
                    return status;
                }
                if (!OmitStep(pctecorrSwitchState))
                    acs2d_sci_sw.pctecorr = pctecorrSwitchState;
                delete((void**)&acsrej_input);



                /* now repeat the same thing with CTE corrected products */
                if (!OmitStep(acscte_sci_sw.pctecorr))
                {
                    if ((status = ACSRej_0Wrapper(acsrejc_input,
                            acshdr->crc_tmp,
                            acshdr->crcfile,
                            asn->product[prod].subprod[posid].crc_tmp,
                            acshdr,
                            asn,
                            prod,
                            posid,
                            &acs2d_sci_sw,
                            &sciref,
                            printtime,
                            save_crj,
                            False)))
                    {
                        //freeStuff
                        delete((void**)&acsrejc_input);
                        return status;
                    }
                }
                delete((void**)&acsrejc_input);
            }  /* End of CRCORR processing */

            if (asn->debug){
                trlmessage("Run ACS2D for CCD next...");
            }

            /* Run ACS2D on individual EXPosures. */
            for (expid = 1; expid <= asn->spmems[posid]; expid++) {
                /* From this point on, we are working with ONE image
                   at a time... */

                /* load acs file info */
                if (asn->process == SINGLE ) {
                    if (GetSingle (asn, acshdr) )
                        return (status);
                } else {
                    if( GetAsnMember(asn, prod, posid, expid, acshdr))
                        return (status);
                }

                /* Construct output and temporary file names. */
                if (InsertACSSuffix (acshdr))
                    return (status);

                /* If we are not performing CRCORR or RPTCORR or
                   EXPSCORR is set to PERFORM, then finish
                   processing individual EXP files with ACS2D...
                */
                if ( (acshdr->sci_crcorr != PERFORM) ||
                     (acshdr->sci_rptcorr != PERFORM) ||
                     (sci_sw.expscorr == PERFORM) ) {
                    if (asn->debug) {
                        trlmessage("CRCORR not set to Perform or EXPSCORR turned on...");
                    }

                    if (acshdr->sci_basic_2d == PERFORM) {
                        SetTrlPrefaceMode (NO);
                        if (asn->copy_input == PERFORM) {
                            if (CopyFFile (acshdr->fltfile, acshdr->blv_tmp))
                                return (status);
                            remove (acshdr->fltfile);
                        }

                        /* Basic 2-D processing (flat field, etc). */
                        /* temporarily set acs2d_sci_sw.pctecorr to omit so the
                           correct acs2d processing is done */
                        if (acs2d_sci_sw.pctecorr == PERFORM) {
                            acs2d_sci_sw.pctecorr = OMIT;

                            if (ACS2d (acshdr->blv_tmp, acshdr->fltfile,
                                       &acs2d_sci_sw, &sciref, printtime,
                                       asn->verbose))
                                return (status);

                            acs2d_sci_sw.pctecorr = PERFORM;
                        } else {
                            if (ACS2d (acshdr->blv_tmp, acshdr->fltfile,
                                       &acs2d_sci_sw, &sciref, printtime,
                                       asn->verbose))
                                return (status);
                        }

                    } else {
                        /* If we are not performing CRCORR, then copy
                           blv_tmp file into a final _flt file. */
                        if (acshdr->sci_crcorr != PERFORM ||
                            acshdr->sci_rptcorr != PERFORM) {
                            if (CopyFFile (acshdr->blv_tmp, acshdr->fltfile))
                                return (status);
                        }
                    }

                    /* now repeat ACS2D processing for CTE products */
                    if (acshdr->sci_basic_2d == PERFORM &&
                            acscte_sci_sw.pctecorr == PERFORM) {
                        if (asn->copy_input == PERFORM) {
                            if (CopyFFile (acshdr->flcfile, acshdr->blc_tmp))
                                return (status);
                            remove (acshdr->flcfile);
                        }

                        /* Basic 2-D processing (flat field, etc). */
                        if (ACS2d (acshdr->blc_tmp, acshdr->flcfile,
                                   &acs2d_sci_sw, &sciref, printtime,
                                   asn->verbose))
                            return (status);
                    } else if (acscte_sci_sw.pctecorr == PERFORM) {
                        /* If we are not performing CRCORR, then copy
                           blc_tmp file into a final _flc file. */
                        if (acshdr->sci_crcorr != PERFORM ||
                            acshdr->sci_rptcorr != PERFORM) {
                            if (CopyFFile (acshdr->blc_tmp, acshdr->flcfile))
                                return (status);
                        }
                    }

                }  /* Finished processing blv_tmp file through ACS2D */

                /* Now, delete _blv_tmp and _blc_tmp files */
                if (*save_tmp != YES) {
                    remove (acshdr->blv_tmp);
                    if (acscte_sci_sw.pctecorr == PERFORM) {
                        remove (acshdr->blc_tmp);
                    }
                }
            }  /* End loop over ind EXP for making _flt and _flc files. */

            /* Reset the trailer file preface to NULL since
               it has already been copied into trailer files */
            ResetTrlPreface();

        }  /* End of processing for SINGLE/PARTIAL product */
    }  /* End loop over CCD products here... */

    /* Done with lists of reference file keywords and names. */
    FreeRefFile (&sciref);

    return (status);
}