IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 6819


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

adding pmFPAfile handling functions for arguments, etc

Location:
branches/rel10_ifa/psModules/src
Files:
4 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
  • branches/rel10_ifa/psModules/src/astrom/pmFPAfile.h

    r6726 r6819  
    77*  @author EAM, IfA
    88*
    9 *  @version $Revision: 1.1.2.8 $ $Name: not supported by cvs2svn $
    10 *  @date $Date: 2006-03-29 20:55:42 $
     9*  @version $Revision: 1.1.2.9 $ $Name: not supported by cvs2svn $
     10*  @date $Date: 2006-04-08 20:13:03 $
    1111*
    1212*  Copyright 2004-2005 Institute for Astronomy, University of Hawaii
     
    1717
    1818#include "pslib.h"
     19#include "pmConfig.h"
    1920#include "pmFPA.h"
    2021#include "pmFPAview.h"
     
    7273pmFPAfile;
    7374
     75// allocate an empty pmFPAfile structure
    7476pmFPAfile *pmFPAfileAlloc ();
     77
     78// load the pmFPAfile information from the camera configuration data
    7579pmFPAfile *pmFPAfileDefine (psMetadata *files, psMetadata *camera, pmFPA *fpa, char *name);
     80
     81// load the pmFPAfile information from the camera configuration data, constructing the needed fpa structure
     82// XXX deprecate this function?
     83pmFPAfile *pmFPAfileConstruct (psMetadata *files, psMetadata *format, psMetadata *camera, char *name);
     84
     85// open the real file corresponding to the given pmFPAfile appropriate to the current view
    7686bool pmFPAfileOpen (pmFPAfile *file, const pmFPAview *view);
     87
     88// read from the real file corresponding to the given pmFPAfile for the current view
    7789bool pmFPAfileRead (pmFPAfile *file, const pmFPAview *view);
     90
     91// write to the real file corresponding to the given pmFPAfile for the current view
    7892bool pmFPAfileWrite (pmFPAfile *file, const pmFPAview *view);
     93
     94// close the real file corresponding to the given pmFPAfile appropriate to the current view
    7995bool pmFPAfileClose (pmFPAfile *file, const pmFPAview *view);
    8096
    81 bool pmFPAfileReadChecks (psMetadata *files, const pmFPAview *view);
     97// examine all pmFPAfiles listed in the files and perform the needed I/O operations (open,read,write,close)
    8298bool pmFPAfileIOChecks (psMetadata *files, const pmFPAview *view, pmFPAfilePlace place);
    8399
     100// return an image corresponding to the current readout, from the specified file.  if the pmFPAfile does not
     101// exist, construct the image using the given size and type (save it in a pmFPAfile??)
    84102psImage *pmFPAfileReadoutImage (psMetadata *files, const pmFPAview *view, char *name, int Nx, int Ny, int type);
    85103
     
    90108bool pmFPAviewWriteFitsImage (const pmFPAview *view, pmFPAfile *file);
    91109
     110// look for the given argname on the argument list.  find the give filename from the file rules
     111pmFPAfile *pmFPAfileFromArgs (bool *found, pmConfig *config, char *filename, char *argname);
     112
     113// look for the given argname on the argument list.  find the give filename from the file rules
     114pmFPAfile *pmFPAfileFromConf (bool *found, pmConfig *config, char *filename, pmFPA *input);
     115
     116// add the specified filename info (value) to the files of the given mode using the given reference name
     117bool pmFPAfileAddFileNames (psMetadata *files, char *name, char *value, int mode);
     118
    92119// convert the rule to a name based on the current view
    93120char *pmFPAfileNameFromRule (char *rule, pmFPAfile *file, const pmFPAview *view);
    94121
    95 pmFPAfile *pmFPAfileConstruct (psMetadata *files, psMetadata *format, psMetadata *camera, char *name);
    96 
    97122# endif
  • branches/rel10_ifa/psModules/src/config/pmConfig.c

    r6773 r6819  
    33 *  @author PAP, IfA
    44 *
    5  *  @version $Revision: 1.7.4.9 $ $Name: not supported by cvs2svn $
    6  *  @date $Date: 2006-04-04 22:14:58 $
     5 *  @version $Revision: 1.7.4.10 $ $Name: not supported by cvs2svn $
     6 *  @date $Date: 2006-04-08 20:13:03 $
    77 *
    88 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1515#include <sys/types.h>
    1616#include <sys/stat.h>
     17#include <glob.h>
    1718#include "pslib.h"
    1819#include "pmConfig.h"
     
    4041    // Initialise
    4142    config->site = NULL;
    42     config->files = NULL;
    4343    config->camera = NULL;
    4444    config->recipes = NULL;
     
    4646    config->database = NULL;
    4747
     48    // the file structure is used to carry pmFPAfiles
     49    config->files = psMetadataAlloc ();
    4850    return config;
    4951}
     
    656658    return true;
    657659}
     660
     661// given the 'file' and 'list' words, find the arguments associated with these words
     662// and interpret them as lists of files.  return an array of the resulting filenames.
     663psArray *pmConfigFileSets (int *argc, char **argv, char *file, char *list)
     664{
     665
     666    int Narg;
     667
     668    // we load all input files onto a psArray, to be parsed later
     669    psArray *input = psArrayAlloc (16);
     670    input->n = 0;
     671
     672    // load the list of filenames the supplied file (may be a glob: "file*.fits")
     673    if ((Narg = psArgumentGet (*argc, argv, file))) {
     674        glob_t globList;
     675        psArgumentRemove (Narg, argc, argv);
     676        globList.gl_offs = 0;
     677        glob (argv[Narg], 0, NULL, &globList);
     678        for (int i = 0; i < globList.gl_pathc; i++) {
     679            char *filename = psStringCopy (globList.gl_pathv[i]);
     680            psArrayAdd (input, 16, filename);
     681            psFree (filename);
     682        }
     683        psArgumentRemove (Narg, argc, argv);
     684    }
     685
     686    // load the list from the supplied text file
     687    if ((Narg = psArgumentGet (*argc, argv, list))) {
     688        int nItems;
     689        char line[1024]; // XXX limits the list lines to 1024 chars
     690        char word[1024];
     691        char *filename;
     692
     693        psArgumentRemove (Narg, argc, argv);
     694        FILE *f = fopen (argv[Narg], "r");
     695        if (f == NULL) {
     696            psAbort ("psphot", "unable to open specified list file");
     697        }
     698        while (fgets (line, 1024, f) != NULL) {
     699            nItems = sscanf (line, "%s", word);
     700            switch (nItems) {
     701            case 0:
     702                break;
     703            case 1:
     704                filename = psStringCopy (word);
     705                psArrayAdd (input, 16, filename);
     706                psFree (filename);
     707                break;
     708            default:
     709                // rigid format, no comments allowed?
     710                psAbort ("pmConfig", "error parsing input list file");
     711                break;
     712            }
     713        }
     714        psArgumentRemove (Narg, argc, argv);
     715    }
     716
     717    return input;
     718}
     719
     720bool pmConfigFileSetsMD (psMetadata *metadata, int *argc, char **argv, char *name, char *file, char *list)
     721{
     722
     723    psArray *files = pmConfigFileSets (argc, argv, file, list);
     724    if (files->n == 0) {
     725        psFree (files);
     726        return false;
     727    }
     728
     729    psMetadataAddPtr (metadata, PS_LIST_TAIL, name,  PS_DATA_ARRAY, "", files);
     730    psFree (files);
     731    return true;
     732}
  • branches/rel10_ifa/psModules/src/config/pmConfig.h

    r6773 r6819  
    33 *  @author PAP, IfA
    44 *
    5  *  @version $Revision: 1.3.4.6 $ $Name: not supported by cvs2svn $
    6  *  @date $Date: 2006-04-04 22:14:58 $
     5 *  @version $Revision: 1.3.4.7 $ $Name: not supported by cvs2svn $
     6 *  @date $Date: 2006-04-08 20:13:03 $
    77 *
    88 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2323    psMetadata *arguments;              // Command-line arguments
    2424    psMetadata *files;                  // pmFPAfiles used for analysis
     25    //    psMetadata *format;   // camera format for primary image
    2526    psDB *database;                     // Database handle
    2627}
     
    112113
    113114
     115psArray *pmConfigFileSets (int *argc, char **argv, char *file, char *list);
     116bool pmConfigFileSetsMD (psMetadata *metadata, int *argc, char **argv, char *name, char *file, char *list);
     117
     118
    114119#endif
Note: See TracChangeset for help on using the changeset viewer.