IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 8, 2006, 10:13:03 AM (20 years ago)
Author:
magnier
Message:

adding pmFPAfile handling functions for arguments, etc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/rel10_ifa/psModules/src/astrom/pmFPAfile.c

    r6755 r6819  
    6767}
    6868
    69 pmFPAfile *pmFPAfileDefine (psMetadata *files, psMetadata *format, pmFPA *fpa, char *name)
     69pmFPAfile *pmFPAfileDefine (psMetadata *files, psMetadata *camera, pmFPA *fpa, char *name)
    7070{
    7171
     
    7474    char *type;
    7575
    76     // select the FILERULES from the camera format
    77     psMetadata *filerules = psMetadataLookupPtr (&status, format, "FILERULES");
     76    // select the FILERULES from the camera config
     77    psMetadata *filerules = psMetadataLookupPtr (&status, camera, "FILERULES");
    7878    if (filerules == NULL) {
    79         psErrorStackPrint(stderr, "Can't find FILERULES in the FORMAT configuration!\n");
     79        psErrorStackPrint(stderr, "Can't find FILERULES in the CAMERA configuration!\n");
    8080        return NULL;
    8181    }
    8282
    8383    // select the name from the FILERULES
    84     // check for indirect name (type == STR, name is aliased name)
     84    // check for alias name (type == STR, name is aliased name)
    8585    char *realname = psMetadataLookupStr (&status, filerules, name);
    8686    if (realname == NULL)
     
    179179    }
    180180
    181     file->fpa = psMemIncrRefCounter(fpa);
     181    if (fpa != NULL) {
     182        file->fpa = psMemIncrRefCounter(fpa);
     183    }
    182184
    183185    psMetadataAddPtr (files, PS_LIST_TAIL, name, PS_DATA_UNKNOWN, "", file);
     
    190192}
    191193
     194/*
    192195pmFPAfile *pmFPAfileConstruct (psMetadata *files, psMetadata *format, psMetadata *camera, char *name)
    193196{
     
    197200    return file;
    198201}
     202*/
    199203
    200204// open file (if not already opened)
     
    228232
    229233    // indirect filenames
    230     if (!strcasecmp (file->filename, "@FPAIO")) {
     234    if (!strcasecmp (file->filename, "@FILES")) {
    231235        psFree (file->filename);
    232236        extra = pmFPAfileNameFromRule (file->filextra, file, view);
     
    252256    case PM_FPA_FILE_IMAGE:
    253257    case PM_FPA_FILE_CMF:
     258        psTrace ("pmFPAfile", 5, "opening %s (type: %d)\n", file->filename, file->type);
    254259        file->fits = psFitsOpen (file->filename, mode);
    255         psTrace ("pmFPAfile", 5, "opening %s (type: %d)\n", file->filename, file->type);
     260        if (file->fits == NULL)
     261            psAbort (__func__, "error opening file %s\n", file->filename);
    256262        file->state = PM_FPA_STATE_OPEN;
    257263        break;
     
    410416    psFree (iter);
    411417    return true;
    412 }
    413 
    414 // select the rule from the camera configuration, perform substitutions as needed
    415 char *pmFPAfileNameFromRule (char *rule, pmFPAfile *file, const pmFPAview *view)
    416 {
    417 
    418     char *newName = NULL;     // destination for resulting name
    419 
    420     newName = psStringCopy (rule);
    421 
    422     if (strstr (newName, "{CHIP.NAME}") != NULL) {
    423         pmChip *chip = pmFPAviewThisChip (view, file->fpa);
    424         if (chip != NULL) {
    425             char *name = psMetadataLookupStr (NULL, chip->concepts, "CHIP.NAME");
    426             if (name != NULL) {
    427                 newName = psStringSubstitute (newName, name, "{CHIP.NAME}");
    428             }
    429         }
    430     }
    431     if (strstr (newName, "{CELL.NAME}") != NULL) {
    432         pmCell *cell = pmFPAviewThisCell (view, file->fpa);
    433         if (cell != NULL) {
    434             char *name = psMetadataLookupStr (NULL, cell->concepts, "CELL.NAME");
    435             if (name != NULL) {
    436                 newName = psStringSubstitute (newName, name, "{CELL.NAME}");
    437             }
    438         }
    439     }
    440     if (strstr (newName, "{EXTNAME}") != NULL) {
    441         pmHDU *hdu = pmFPAviewThisHDU (view, file->fpa);
    442         if (hdu->extname != NULL) {
    443             newName = psStringSubstitute (newName, hdu->extname, "{EXTNAME}");
    444         }
    445     }
    446     if (strstr (newName, "{OUTPUT}") != NULL) {
    447         char *name = psMetadataLookupStr (NULL, file->names, "OUTPUT");
    448         if (name != NULL) {
    449             newName = psStringSubstitute (newName, name, "{OUTPUT}");
    450         }
    451     }
    452     return newName;
    453418}
    454419
     
    606571    # endif
    607572}
     573
     574// look for the given name on the argument list.
     575// returns the file (a view to the one saved on config->files)
     576pmFPAfile *pmFPAfileFromArgs (bool *found, pmConfig *config, char *filename, char *argname)
     577{
     578    bool status;
     579    pmFPA *fpa = NULL;
     580    psFits *fits = NULL;
     581    pmFPAfile *file = NULL;
     582    psMetadata *phu = NULL;
     583    psMetadata *format = NULL;
     584
     585    if (*found)
     586        return NULL;
     587
     588    // we search the argument data for the named fileset (argname)
     589    psArray *infiles = psMetadataLookupPtr(&status, config->arguments, argname);
     590    if (!status)
     591        return NULL;
     592    if (infiles->n < 1)
     593        return NULL;
     594
     595    // determine the current format from the header
     596    // if no camera has been specified, use the first image as a template for the rest.
     597    fits = psFitsOpen (infiles->data[0], "r");
     598    phu = psFitsReadHeader (NULL, fits);
     599    format = pmConfigCameraFormatFromHeader (config, phu);
     600    psFitsClose (fits);
     601
     602    // build the template fpa, set up the basic view
     603    fpa = pmFPAConstruct (config->camera);
     604
     605    // load the given filerule (from config->camera) and associate it with the fpa
     606    // the output file is just a view to the file on config->files
     607    file = pmFPAfileDefine (config->files, config->camera, fpa, filename);
     608    if (!file) {
     609        psErrorStackPrint(stderr, "file %s not defined\n", filename);
     610        psFree (fpa);
     611        psFree (format);
     612        return NULL;
     613    }
     614
     615    // this file is (by virtue of being supplied in the argument list) coming from the fileset
     616    psFree (file->filerule);
     617    psFree (file->filextra);
     618
     619    // adjust the rules to identify these files in the file->names data
     620    file->filerule = psStringCopy ("@FILES");
     621    // XXX this rule does not work in general
     622    file->filextra = psStringCopy ("{CHIP.NAME}.{CELL.NAME}");
     623
     624    // examine the list of input files and validate their cameras
     625    for (int i = 0; i < infiles->n; i++) {
     626        if (i > 0) {
     627            fits = psFitsOpen (infiles->data[i], "r");
     628            phu = psFitsReadHeader (NULL, fits);
     629            pmConfigValidateCameraFormat (format, phu);
     630            psFitsClose (fits);
     631        }
     632
     633        // set the view to the corresponding entry for this phu
     634        pmFPAview *view = pmFPAAddSource (fpa, phu, format);
     635
     636        // XXX is this the correct psMD to save the filename?
     637        char *name = pmFPAfileNameFromRule (file->filextra, file, view);
     638        psMetadataAddStr (file->names, PS_LIST_TAIL, name, 0, "", infiles->data[i]);
     639
     640        psFree (view);
     641        psFree (name);
     642        psFree (phu);
     643    }
     644    psFree (fpa);
     645    psFree (format);
     646    *found = true;
     647
     648    return file;
     649}
     650
     651// look for the given name on the argument list.
     652pmFPAfile *pmFPAfileFromConf (bool *found, pmConfig *config, char *filename, pmFPA *input)
     653{
     654    psFits *fits = NULL;
     655    pmFPAfile *file = NULL;
     656    psMetadata *phu = NULL;
     657    psMetadata *format = NULL;
     658    psArray *infiles = NULL;
     659
     660    if (*found)
     661        return NULL;
     662
     663    // a camera config is needed (as source of file rule)
     664    if (config->camera == NULL) {
     665        psErrorStackPrint (stderr, "camera is not defined\n");
     666        return NULL;
     667    }
     668
     669    // expect @DETDB in the config filerule
     670    file = pmFPAfileDefine (config->files, config->camera, NULL, filename);
     671    if (!file) {
     672        psErrorStackPrint(stderr, "file %s not defined\n", filename);
     673        return NULL;
     674    }
     675
     676    // image names come from the file->name list?
     677    if (!strcasecmp (file->filerule, "@FILES"))
     678        psAbort ("pmFPAfileFromConfig", "programming error");
     679
     680    // image needs to come from the detrend database
     681    if (!strcasecmp (file->filerule, "@DETDB")) {
     682        // char *extra = pmFPAfileNameFromRule (file->filextra, file, view);
     683        // psArray *infiles = pmDetrendSelect (extra, input);
     684        psAbort ("pmFPAfileFromConfig", "@DETDB not yet defined");
     685    } else {
     686        infiles = psArrayAlloc(1);
     687        infiles->n = 1;
     688        infiles->data[0] = psStringCopy (file->filerule);
     689    }
     690    if (infiles == NULL)
     691        return NULL;
     692    if (infiles->n < 1) {
     693        psFree (infiles);
     694        return NULL;
     695    }
     696
     697    // determine the current format from the header
     698    // if no camera has been specified, use the first image as a template for the rest.
     699    fits = psFitsOpen (infiles->data[0], "r");
     700    phu = psFitsReadHeader (NULL, fits);
     701    format = pmConfigCameraFormatFromHeader (config, phu);
     702    psFitsClose (fits);
     703
     704    // build the template fpa, set up the basic view
     705    file->fpa = pmFPAConstruct (config->camera);
     706
     707    // this file is (by virtue of being supplied in the argument list) coming from the fileset
     708    psFree (file->filerule);
     709    psFree (file->filextra);
     710
     711    // adjust the rules to identify these files in the file->names data
     712    file->filerule = psStringCopy ("@FILES");
     713    // XXX this rule does not work in general
     714    file->filextra = psStringCopy ("{CHIP.NAME}.{CELL.NAME}");
     715
     716    // examine the list of input files and validate their cameras
     717    for (int i = 0; i < infiles->n; i++) {
     718        if (i > 0) {
     719            fits = psFitsOpen (infiles->data[i], "r");
     720            phu = psFitsReadHeader (NULL, fits);
     721            pmConfigValidateCameraFormat (format, phu);
     722            psFitsClose (fits);
     723        }
     724
     725        // set the view to the corresponding entry for this phu
     726        pmFPAview *view = pmFPAAddSource (file->fpa, phu, format);
     727
     728        // XXX is this the correct psMD to save the filename?
     729        char *name = pmFPAfileNameFromRule (file->filextra, file, view);
     730        psMetadataAddStr (file->names, PS_LIST_TAIL, name, 0, "", infiles->data[i]);
     731
     732        psFree (view);
     733        psFree (name);
     734        psFree (phu);
     735    }
     736    psFree (format);
     737    psFree (infiles);
     738    *found = true;
     739    return file;
     740}
     741
     742bool pmFPAfileAddFileNames (psMetadata *files, char *name, char *value, int mode)
     743{
     744
     745    // add the output names to the output-type files
     746    psMetadataItem *item = NULL;
     747    psMetadataIterator *iter = psMetadataIteratorAlloc (files, PS_LIST_HEAD, NULL);
     748    while ((item = psMetadataGetAndIncrement (iter)) != NULL) {
     749        pmFPAfile *file = item->data.V;
     750
     751        if (file->mode == mode) {
     752            psMetadataAddStr (file->names, PS_LIST_TAIL, name, 0, "", value);
     753        }
     754    }
     755    psFree (iter);
     756    return true;
     757}
     758
     759// select the rule from the camera configuration, perform substitutions as needed
     760char *pmFPAfileNameFromRule (char *rule, pmFPAfile *file, const pmFPAview *view)
     761{
     762
     763    char *newName = NULL;     // destination for resulting name
     764
     765    newName = psStringCopy (rule);
     766
     767    if (strstr (newName, "{CHIP.NAME}") != NULL) {
     768        pmChip *chip = pmFPAviewThisChip (view, file->fpa);
     769        if (chip != NULL) {
     770            char *name = psMetadataLookupStr (NULL, chip->concepts, "CHIP.NAME");
     771            if (name != NULL) {
     772                newName = psStringSubstitute (newName, name, "{CHIP.NAME}");
     773            }
     774        }
     775    }
     776    if (strstr (newName, "{CELL.NAME}") != NULL) {
     777        pmCell *cell = pmFPAviewThisCell (view, file->fpa);
     778        if (cell != NULL) {
     779            char *name = psMetadataLookupStr (NULL, cell->concepts, "CELL.NAME");
     780            if (name != NULL) {
     781                newName = psStringSubstitute (newName, name, "{CELL.NAME}");
     782            }
     783        }
     784    }
     785    if (strstr (newName, "{EXTNAME}") != NULL) {
     786        pmHDU *hdu = pmFPAviewThisHDU (view, file->fpa);
     787        if (hdu->extname != NULL) {
     788            newName = psStringSubstitute (newName, hdu->extname, "{EXTNAME}");
     789        }
     790    }
     791    if (strstr (newName, "{OUTPUT}") != NULL) {
     792        char *name = psMetadataLookupStr (NULL, file->names, "OUTPUT");
     793        if (name != NULL) {
     794            newName = psStringSubstitute (newName, name, "{OUTPUT}");
     795        }
     796    }
     797    return newName;
     798}
     799
Note: See TracChangeset for help on using the changeset viewer.