IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 9, 2013, 12:26:48 PM (13 years ago)
Author:
eugene
Message:

major upgrades to pswarp to enable skycell warp -> chip

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pswarp/src/pswarpParseCamera.c

    r34800 r35563  
    1313#include "pswarp.h"
    1414
    15 // Define an input file
    16 static pmFPAfile *defineInputFile(pmConfig *config,// Configuration
    17                                   pmFPAfile *bind,    // File to which to bind, or NULL
    18                                   char *filerule,     // Name of file rule
    19                                   char *argname,      // Argument name
    20                                   pmFPAfileType fileType // Type of file
    21     )
    22 {
    23     bool status;
    24 
    25     pmFPAfile *file = NULL;
    26     // look for the file on the argument list
    27     if (bind) {
    28         file = pmFPAfileBindFromArgs(&status, bind, config, filerule, argname);
    29     } else {
    30         file = pmFPAfileDefineFromArgs(&status, config, filerule, argname);
    31     }
    32     if (!status) {
    33         psError(psErrorCodeLast(), false, "Failed to load file definition for %s", filerule);
    34         return false;
    35     }
    36     if (!file) {
    37         // look for the file on the RUN metadata
    38         file = pmFPAfileDefineFromRun(&status, bind, config, filerule); // File to return
    39         if (!status) {
    40             psError(psErrorCodeLast(), false, "Failed to load file definition for %s", filerule);
    41             return NULL;
    42         }
    43     }
    44 
    45     if (!file) {
    46         return NULL;
    47     }
    48 
    49     if (file->type != fileType) {
    50         psError(PSWARP_ERR_CONFIG, true, "%s is not of type %s", filerule, pmFPAfileStringFromType(fileType));
    51         return NULL;
    52     }
    53 
    54     return file;
    55 }
    56 
    57 
    58 
     15static bool foundAstrom = false;
     16static bool foundVariance = false;
     17static bool foundBackground = false;
    5918
    6019bool pswarpParseCamera(pmConfig *config)
    6120{
    6221    psAssert(config, "Require configuration");
    63     bool mdok;                          // Status of MD lookup
    64 
    65     // The input image(s) is required: it defines the camera
    66     pmFPAfile *input = defineInputFile(config, NULL, "PSWARP.INPUT", "INPUT", PM_FPA_FILE_IMAGE);
    67     if (!input) {
    68         psError(psErrorCodeLast(), false, "Failed to build FPA from PSWARP.INPUT");
    69         return false;
    70     }
    71 
    72     pmFPAfile *astrom = defineInputFile(config, NULL, "PSWARP.ASTROM", "ASTROM", PM_FPA_FILE_CMF);
    73     psLogMsg("pswarp", PS_LOG_INFO, "Astrometry source: %s", astrom ? "supplied" : "header");
    74 
    75     pmFPAfile *inMask = defineInputFile(config, input, "PSWARP.MASK", "MASK", PM_FPA_FILE_MASK);
    76     if (!inMask) {
    77         psLogMsg("pswarp", PS_LOG_INFO, "No mask supplied");
    78     }
    79 
    80     pmFPAfile *inVariance = defineInputFile(config, input, "PSWARP.VARIANCE", "VARIANCE",
    81                                             PM_FPA_FILE_VARIANCE);
    82     if (!inVariance) {
    83         psLogMsg("pswarp", PS_LOG_INFO, "No variance supplied");
    84     }
    85 
    86     pmFPAfile *inBackground = defineInputFile(config, NULL, "PSWARP.BKGMODEL", "BACKGROUND",
    87                                               PM_FPA_FILE_IMAGE);
    88     if (!inBackground) {
    89       psLogMsg("pswarp", PS_LOG_INFO, "No background models supplied");
    90     }
    91    
     22    bool status;                          // Status of MD lookup
     23
     24    // *** parse the input information (either from -file or from -input)
     25
     26    // if INPUTS exists, we have a metadata file to provide input, variance, mask, etc
     27    psMetadata *inputFile = psMetadataLookupPtr(&status, config->arguments, "INPUTS");
     28    if (inputFile) {
     29        if (!pswarpParseMultiInput (config, inputFile)) {
     30            psError(PSWARP_ERR_CONFIG, false, "failed to parse multi-input file");
     31            return false;
     32        }
     33    } else {
     34        if (!pswarpParseSingleInput (config)) {
     35            psError(PSWARP_ERR_CONFIG, false, "failed to parse multi-input file");
     36            return false;
     37        }
     38    }
     39
     40    // once we have read the input mask headers (if any), we can use that to set the mask bits
     41    if (!pswarpSetMaskBits(config)) {
     42        psError(psErrorCodeLast(), false, "failed to set mask bits");
     43        return false;
     44    }
     45
     46    // *** parse the output information (output target, output astrometry [skycell])
     47
    9248    // The input skycell is a required argument: it defines the output image
    93     // XXX we may need a different skycell structure here
     49    pmConfig *skyConfig = NULL;
    9450    pmFPAfile *skycell = NULL;
    95     pmConfig *skyConfig = NULL;
    96     bool status = pswarpDefineSkycell(&skycell, &skyConfig, config, "PSWARP.SKYCELL", "SKYCELL");
    97     if (!status) {
    98         psError(psErrorCodeLast(), false, "Failed to build FPA from PSWARP.SKYCELL");
    99         return false;
    100     }
     51
     52    skycell = pmFPAfileDefineNewConfig(&status, &skyConfig, config, "PSWARP.SKYCELL", "SKYCELL");
     53    if (!status || !skycell) {
     54      psError(psErrorCodeLast(), false, "Failed to build FPA from PSWARP.SKYCELL");
     55      return false;
     56      }
     57    psMetadataAddStr(config->arguments, PS_LIST_TAIL, "SKYCELL.CAMERA", 0, "Name of camera for skycell", skyConfig->cameraName);
    10158    psFree(skyConfig);
    10259
     
    10764    }
    10865
     66    // use the skycell camera or the input camera?
     67    bool useInputCamera = !strcmp (skycell->cameraName, "SIMPLE");
     68
    10969    // The output skycell
    110     pmFPAfile *output = pmFPAfileDefineSkycell(config, NULL, "PSWARP.OUTPUT");
     70    pmFPAfile *output = useInputCamera ?
     71        pmFPAfileDefineSkycell(config, NULL, "PSWARP.OUTPUT") :
     72        pmFPAfileDefineOutputForFormat(config, NULL, "PSWARP.OUTPUT", skycell->cameraName, skycell->formatName);
    11173    if (!output) {
    11274        psError(psErrorCodeLast(), false, "Failed to build FPA from PSWARP.OUTPUT");
     
    11476    }
    11577    output->save = true;
    116 
    117     pmFPAfile *outMask = pmFPAfileDefineSkycell(config, output->fpa, "PSWARP.OUTPUT.MASK");
     78    pmFPAAddSourceFromFormat(output->fpa, output->format); // ** builds the HDUs, is this OK?
     79
     80    pmFPAfile *outMask = useInputCamera ?
     81        pmFPAfileDefineSkycell(config, output->fpa, "PSWARP.OUTPUT.MASK"):
     82        pmFPAfileDefineOutputForFormat(config, output->fpa, "PSWARP.OUTPUT.MASK", skycell->cameraName, skycell->formatName);
    11883    if (!outMask) {
    11984        psError(psErrorCodeLast(), false, "Failed to build FPA from PSWARP.OUTPUT.MASK");
     
    12287    outMask->save = true;
    12388
    124     if (inVariance) {
    125         pmFPAfile *outVariance = pmFPAfileDefineSkycell(config, output->fpa, "PSWARP.OUTPUT.VARIANCE");
     89    // only create an output variance in we supply an input variance
     90    if (foundVariance) {
     91        pmFPAfile *outVariance = useInputCamera ?
     92            pmFPAfileDefineSkycell(config, output->fpa, "PSWARP.OUTPUT.VARIANCE"):
     93            pmFPAfileDefineOutputForFormat(config, output->fpa, "PSWARP.OUTPUT.VARIANCE", skycell->cameraName, skycell->formatName);
    12694        if (!outVariance) {
    12795            psError(psErrorCodeLast(), false, "Failed to build FPA from PSWARP.OUTPUT.VARIANCE");
     
    13199    }
    132100
    133     if (astrom && psMetadataLookupBool(&mdok, recipe, "SOURCES")) {
    134         pmFPAfile *outSources = pmFPAfileDefineSkycell(config, output->fpa, "PSWARP.OUTPUT.SOURCES");
     101    // XXX we assume input sources come from the input astrom description, but this need not be true (we could define an input sources file)
     102    if (foundAstrom && psMetadataLookupBool(&status, recipe, "SOURCES")) {
     103        pmFPAfile *outSources = useInputCamera ?
     104            pmFPAfileDefineSkycell(config, output->fpa, "PSWARP.OUTPUT.SOURCES"):
     105            pmFPAfileDefineOutputForFormat(config, output->fpa, "PSWARP.OUTPUT.SOURCES", skycell->cameraName, skycell->formatName);
    135106        if (!outSources) {
    136107            psError(psErrorCodeLast(), false, "Failed to build FPA from PSWARP.OUTPUT.SOURCES");
     
    140111    }
    141112
    142     if (inBackground && psMetadataLookupBool(&mdok, recipe, "BACKGROUND.MODEL")) {
    143       pmFPAfile *outBackground = pmFPAfileDefineSkycell(config, NULL, "PSWARP.OUTPUT.BKGMODEL");
    144 /*       pmFPAfile *outBackground = pmFPAfileDefineFromFPA(config,output->fpa, */
    145 /*                                                      psMetadataLookupS32(&mdok,recipe,"BKG.XGRID"), */
    146 /*                                                      psMetadataLookupS32(&mdok,recipe,"BKG.YGRID"), */
    147 /*                                                      "PSWARP.OUTPUT.BKGMODEL"); */
    148       outBackground->xBin = psMetadataLookupS32(&mdok, recipe, "BKG.XGRID");
    149       outBackground->yBin = psMetadataLookupS32(&mdok, recipe, "BKG.YGRID");
    150      
    151       if (!outBackground) {
    152         psError(psErrorCodeLast(), false, "Failed to build FPA from PSWARP.OUTPUT.BKGMODEL");
    153         return false;
    154       }
    155       outBackground->save = true;
    156     }
    157    
    158     if (psMetadataLookupBool(&mdok, recipe, "PSF")) {
    159         // This file, PSPHOT.INPUT, is just used as a carrier; output files (eg, PSPHOT.RESID) are defined by
    160         // psphotDefineFiles
    161         pmFPAfile *psphotInput = pmFPAfileDefineSkycell(config, NULL, "PSPHOT.INPUT");
     113    // only create an output background if an input background is supplied
     114    if (foundBackground && psMetadataLookupBool(&status, recipe, "BACKGROUND.MODEL")) {
     115        // pmFPAfile *outBackground = pmFPAfileDefineSkycell(config, NULL, "PSWARP.OUTPUT.BKGMODEL");
     116        pmFPAfile *outBackground = useInputCamera ?
     117            pmFPAfileDefineSkycell(config, NULL, "PSWARP.OUTPUT.BKGMODEL"):
     118            pmFPAfileDefineOutputForFormat(config, NULL, "PSWARP.OUTPUT.BKGMODEL", skycell->cameraName, skycell->formatName);
     119        if (!outBackground) {
     120            psError(psErrorCodeLast(), false, "Failed to build FPA from PSWARP.OUTPUT.BKGMODEL");
     121            return false;
     122        }
     123        outBackground->xBin = psMetadataLookupS32(&status, recipe, "BKG.XGRID");
     124        outBackground->yBin = psMetadataLookupS32(&status, recipe, "BKG.YGRID");
     125        outBackground->save = true;
     126        pmFPAAddSourceFromFormat(outBackground->fpa, outBackground->format); // ** builds the HDUs, is this OK?
     127    }
     128
     129    if (psMetadataLookupBool(&status, recipe, "PSF")) {
     130        // This file, PSPHOT.INPUT, is just used as a carrier; output files (eg, PSPHOT.RESID) are defined by psphotDefineFiles
     131        pmFPAfile *psphotInput = useInputCamera ?
     132            pmFPAfileDefineSkycell(config, NULL, "PSPHOT.INPUT"):
     133            pmFPAfileDefineOutputForFormat(config, NULL, "PSPHOT.INPUT", skycell->cameraName, skycell->formatName);
    162134        if (!psphotInput) {
    163135            psError(psErrorCodeLast(), false, _("Unable to generate output file from PSPHOT.INPUT"));
     
    165137        }
    166138        psphotInput->src = psMemIncrRefCounter(output->fpa);
    167         // specify the number of psphot input images
     139        pmFPAAddSourceFromFormat(psphotInput->fpa, psphotInput->format); // ** builds the HDUs, is this OK?
     140
     141        // specify the number of psphot input images (psphotReadout loops over all input images)
    168142        psMetadataAddS32 (config->arguments, PS_LIST_TAIL, "PSPHOT.INPUT.NUM", PS_META_REPLACE, "number of inputs", 1);
    169143
    170         pmFPAfile *psphotInSources = pmFPAfileDefineSkycell(config, output->fpa, "PSPHOT.INPUT.CMF");
     144        // the input sources (read from the input astrometry file) are transformed (in pswarpLoop) to the readout->analysis
     145        // entries of the output file PSWARP.OUTPUT.SOURCES, associated with the main output pmFPAfile PSWARP.OUTPUT
     146
     147        // the PSPHOT.INPUT.CMF is used to supply those sources to psphot (specifically psphotLoadPSFSources). 
     148
     149        // pmFPAfile *psphotInSources = pmFPAfileDefineSkycell(config, output->fpa, "PSPHOT.INPUT.CMF");
     150        pmFPAfile *psphotInSources = useInputCamera ?
     151            pmFPAfileDefineSkycell(config, output->fpa, "PSPHOT.INPUT.CMF"):
     152            pmFPAfileDefineOutputForFormat(config, output->fpa, "PSPHOT.INPUT.CMF", skycell->cameraName, skycell->formatName);
    171153        if (!psphotInSources) {
    172154            psError(psErrorCodeLast(), false, _("Unable to generate output file from PSPHOT.INPUT.CMF"));
     
    195177        psphotSources->save = false;
    196178    }
     179 
     180    // only keep the necessary (output) camera configs and relevant recipes
     181    const char *skyCamera = psMetadataLookupStr(NULL, config->arguments, "SKYCELL.CAMERA");
     182    pmConfigCamerasCull(config, skyCamera);
     183    pmConfigRecipesCull(config, "PSWARP,PPSTATS,PSPHOT,PSASTRO,MASKS,JPEG");
     184
     185    psTrace("pswarp", 1, "Done with pswarpParseCamera...\n");
     186    return true;
     187}
     188
     189bool pswarpParseSingleInput (pmConfig *config) {
     190
     191    // The input image(s) is required: it defines the camera
     192    pmFPAfile *input = pswarpDefineInputFile(config, NULL, "PSWARP.INPUT", "INPUT", PM_FPA_FILE_IMAGE);
     193    if (!input) {
     194        psError(psErrorCodeLast(), false, "Failed to build FPA from PSWARP.INPUT");
     195        return false;
     196    }
     197
     198    // Define the input astrometry file(s) (may be either WCS or CMF)
     199    bool status = false;
     200    pmFPAfile *astrom = pmFPAfileDefineFromArgs(&status, config, "PSWARP.ASTROM", "ASTROM");
     201    if (!astrom) {
     202        // look for the file on the RUN metadata
     203        astrom = pmFPAfileDefineFromRun(&status, NULL, config, "PSWARP.ASTROM"); // File to return
     204        if (!status) {
     205            psLogMsg("pswarp", PS_LOG_INFO, "Failed to load file definition for %s", "PSWARP.ASTROM");
     206        }
     207    }
     208    if (astrom) {
     209        if ((astrom->type != PM_FPA_FILE_CMF) && (astrom->type != PM_FPA_FILE_WCS)) {
     210            psLogMsg("pswarp", PS_LOG_INFO, "%s is neither CMF nor WCS", "PSWARP.ASTROM");
     211            // XXX raise an error here??
     212        } else {
     213            foundAstrom = true;
     214        }
     215    }
     216    psLogMsg("pswarp", PS_LOG_INFO, "Astrometry source: %s", astrom ? "supplied" : "header");
     217
     218    // Define the input mask file(s)
     219    pmFPAfile *inMask = pswarpDefineInputFile(config, input, "PSWARP.MASK", "MASK", PM_FPA_FILE_MASK);
     220    if (!inMask) {
     221        psLogMsg("pswarp", PS_LOG_INFO, "No mask supplied");
     222    }
     223
     224    // Define the input variance file(s)
     225    pmFPAfile *inVariance = pswarpDefineInputFile(config, input, "PSWARP.VARIANCE", "VARIANCE", PM_FPA_FILE_VARIANCE);
     226    if (inVariance) {
     227        foundVariance = true;
     228    } else {
     229        psLogMsg("pswarp", PS_LOG_INFO, "No variance supplied");
     230    }
     231
     232    pmFPAfile *inBackground = pswarpDefineInputFile(config, NULL, "PSWARP.BKGMODEL", "BACKGROUND", PM_FPA_FILE_IMAGE);
     233    if (inBackground) {
     234        foundBackground = true;
     235    } else {
     236      // cannot do the background model if an input is not supplied.
     237      psMetadataAddBool (config->arguments, PS_LIST_TAIL, "BACKGROUND.MODEL", PS_META_REPLACE, "no input background supplied", false);
     238      psLogMsg("pswarp", PS_LOG_INFO, "No background models supplied");
     239    }
    197240
    198241    // Chip selection: turn on only the chips specified
    199     char *chipLine = psMetadataLookupStr(&mdok, config->arguments, "CHIP_SELECTIONS");
    200     if (mdok) {
     242    char *chipLine = psMetadataLookupStr(&status, config->arguments, "CHIP_SELECTIONS");
     243    if (status) {
    201244        psArray *chips = psStringSplitArray (chipLine, ",", false);
    202245        if (chips->n > 0) {
     
    213256    }
    214257
    215     psTrace("pswarp", 1, "Done with pswarpParseCamera...\n");
     258    psMetadataAddS32(config->arguments, PS_LIST_TAIL, "NUM_INPUTS", PS_META_REPLACE, "input file sets", 1);
    216259    return true;
    217260}
     261
     262// read input file information from a metadata file
     263// XXX for now, in the multi-input format, we disable PSF, BACKGROUND, and CHIP_SELECTIONS
     264bool pswarpParseMultiInput (pmConfig *config, psMetadata *fileListMD) {
     265
     266    // the multi-input file consists of a set of metadata blocks, each with entries
     267    // for INPUT and (possibly) ASTROM, MASK, VARIANCE, ???
     268
     269    bool status = false;
     270
     271    for (int i = 0; i < fileListMD->list->n; i++) {
     272        psMetadataItem *item = psMetadataGet(fileListMD, i);
     273        if (item->type != PS_DATA_METADATA) {
     274            psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Component %s of the input metadata is not of type METADATA", item->name);
     275            return false;
     276        }
     277
     278        // next input metadata block of interest
     279        psMetadata *fileBlockMD = item->data.md;
     280
     281        psString inputName = psMetadataLookupStr(&status, fileBlockMD, "INPUT"); // Name of image
     282        if (!inputName || !strlen(inputName)) {
     283            psError(PS_ERR_UNKNOWN, false, "input file not found in metadata block %d (%s)", i, item->name);
     284            return false;
     285        }
     286        AddStringAsArray (config->arguments, inputName, "FILENAMES");
     287
     288        pmFPAfile *input = pswarpDefineInputFile (config, NULL, "PSWARP.INPUT", "FILENAMES", PM_FPA_FILE_IMAGE);
     289        if (!input) {
     290            psError(psErrorCodeLast(), false, "Failed to build FPA from PSWARP.INPUT");
     291            return false;
     292        }
     293
     294        psString astromName = psMetadataLookupStr(&status, fileBlockMD, "ASTROM"); // Name of astrom file
     295        if (astromName && strlen(astromName)) {
     296            AddStringAsArray (config->arguments, astromName, "FILENAMES");
     297
     298            pmFPAfile *astrom = pmFPAfileDefineFromArgs (&status, config, "PSWARP.ASTROM", "FILENAMES");
     299            if (!status) {
     300                psLogMsg("pswarp", PS_LOG_INFO, "Failed to load file definition for %s", "PSWARP.ASTROM");
     301            }
     302            if (astrom) {
     303                if ((astrom->type != PM_FPA_FILE_CMF) && (astrom->type != PM_FPA_FILE_WCS)) {
     304                    psLogMsg("pswarp", PS_LOG_INFO, "%s is neither CMF nor WCS", "PSWARP.ASTROM");
     305                } else {
     306                    foundAstrom = true;
     307                }
     308            }
     309            psLogMsg("pswarp", PS_LOG_INFO, "Astrometry source: %s", astrom ? "supplied" : "header");
     310        }
     311
     312        pmFPAfile *mask = NULL;
     313        psString maskName = psMetadataLookupStr(&status, fileBlockMD, "MASK"); // Name of mask
     314        if (maskName && strlen(maskName)) {
     315            AddStringAsArray (config->arguments, maskName, "FILENAMES");
     316
     317            mask = pswarpDefineInputFile (config, input, "PSWARP.MASK", "FILENAMES", PM_FPA_FILE_MASK);
     318        }
     319        if (!mask) {
     320            psLogMsg("pswarp", PS_LOG_INFO, "No mask supplied");
     321        }
     322
     323        pmFPAfile *variance = NULL;
     324        psString varianceName = psMetadataLookupStr(&status, fileBlockMD, "VARIANCE"); // Name of variance
     325        if (varianceName && strlen(varianceName)) {
     326            AddStringAsArray (config->arguments, varianceName, "FILENAMES");
     327
     328            variance = pswarpDefineInputFile (config, input, "PSWARP.VARIANCE", "FILENAMES", PM_FPA_FILE_VARIANCE);
     329        }
     330        if (variance) {
     331            foundVariance = true;
     332        } else {
     333            psLogMsg("pswarp", PS_LOG_INFO, "No variance supplied");
     334        }
     335
     336        // XXX for now we do not have an option to supply a background model set in multi input mode
     337        psMetadataAddBool (config->arguments, PS_LIST_TAIL, "BACKGROUND.MODEL", PS_META_REPLACE, "no input background supplied", false);
     338    }
     339
     340    psMetadataAddS32(config->arguments, PS_LIST_TAIL, "NUM_INPUTS", PS_META_REPLACE, "input file sets", fileListMD->list->n);
     341    return true;
     342}
     343
     344// Define an input file based on name in config->arguments or config RUN block
     345pmFPAfile *pswarpDefineInputFile(pmConfig *config,// Configuration
     346                                 pmFPAfile *bind,    // File to which to bind, or NULL
     347                                 char *filerule,     // Name of file rule
     348                                 char *argname,      // Argument name
     349                                 pmFPAfileType fileType // Type of file
     350    )
     351{
     352    bool status;
     353
     354    pmFPAfile *file = NULL;
     355    // look for the file on the argument list
     356    if (bind) {
     357        file = pmFPAfileBindFromArgs(&status, bind, config, filerule, argname);
     358    } else {
     359        file = pmFPAfileDefineFromArgs(&status, config, filerule, argname);
     360    }
     361    if (!status) {
     362        psError(psErrorCodeLast(), false, "Failed to load file definition for %s", filerule);
     363        return false;
     364    }
     365    if (!file) {
     366        // look for the file on the RUN metadata
     367        file = pmFPAfileDefineFromRun(&status, bind, config, filerule); // File to return
     368        if (!status) {
     369            psError(psErrorCodeLast(), false, "Failed to load file definition for %s", filerule);
     370            return NULL;
     371        }
     372    }
     373
     374    if (!file) {
     375        return NULL;
     376    }
     377
     378    if (file->type != fileType) {
     379        psError(PSWARP_ERR_CONFIG, true, "%s is not of type %s", filerule, pmFPAfileStringFromType(fileType));
     380        return NULL;
     381    }
     382
     383    return file;
     384}
     385
     386bool AddStringAsArray (psMetadata *md, char *string, char *name) {
     387
     388    psArray *dummy = psArrayAlloc(1);   // Dummy array of filenames for this FPA
     389    dummy->data[0] = psStringCopy(string);
     390
     391    psMetadataAddArray(md, PS_LIST_TAIL, name, PS_META_REPLACE, "string added as array", dummy);
     392    psFree(dummy);
     393    return true;
     394}
     395
Note: See TracChangeset for help on using the changeset viewer.