IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 17, 2009, 12:07:42 PM (17 years ago)
Author:
beaumont
Message:

merged with head

Location:
branches/cnb_branches/cnb_branch_20090301/psModules
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/cnb_branches/cnb_branch_20090301/psModules

  • branches/cnb_branches/cnb_branch_20090301/psModules/src/camera/pmFPAfileDefine.c

    r22699 r23351  
    1111#include "pmConfig.h"
    1212#include "pmConfigMask.h"
     13#include "pmConfigRun.h"
    1314#include "pmDetrendDB.h"
    1415
     
    216217        file->imageId = config->imageId;
    217218    }
    218 
    219     // XXX this seems a bit of a hack: use the cameraName to determine the mosaic level...
    220     # if (0)
    221     if (cameraName) {
    222         if (!strcmp(cameraName + strlen(cameraName) - 5, "-CHIP")) {
    223             file->mosaicLevel = PM_FPA_LEVEL_CHIP;
    224         }
    225         if (!strcmp(cameraName + strlen(cameraName) - 5, "-FPA")) {
    226             file->mosaicLevel = PM_FPA_LEVEL_FPA;
    227         }
    228     }
    229     # endif
    230219
    231220    // Use the format we were told to, the format specified in the file rule, or default to the default format
     
    383372    }
    384373
    385     psTrace ("psModules.camera", 5, "file: %s, format: %s, fileLevel: %s, extLevel: %s, dataLevel: %s, freeLevel: %s\n",
    386              file->name, file->formatName, pmFPALevelToName (file->fileLevel), pmFPALevelToName(extLevel), pmFPALevelToName (file->dataLevel), pmFPALevelToName (file->freeLevel));
     374    psTrace("psModules.camera", 5,
     375            "file: %s, format: %s, fileLevel: %s, extLevel: %s, dataLevel: %s, freeLevel: %s\n",
     376            file->name, file->formatName, pmFPALevelToName(file->fileLevel), pmFPALevelToName(extLevel),
     377            pmFPALevelToName(file->dataLevel), pmFPALevelToName(file->freeLevel));
    387378
    388379    // add argument-supplied OUTPUT name to this file
    389380    char *outname = psMetadataLookupStr(&status, config->arguments, "OUTPUT");
    390     psMetadataAddStr(file->names, PS_LIST_TAIL, "OUTPUT", PS_META_NO_REPLACE, "", outname);
     381    psMetadataAddStr(file->names, PS_LIST_TAIL, "OUTPUT", PS_META_NO_REPLACE, "Output file name", outname);
    391382
    392383    // place the resulting file in the config system
    393     psMetadataAddPtr (config->files, PS_LIST_TAIL, name, PS_DATA_UNKNOWN, "", file);
     384    psMetadataAddPtr(config->files, PS_LIST_TAIL, name, PS_DATA_UNKNOWN, "Output file", file);
    394385    psFree(file);                       // we free this copy of file, but 'files' still has a copy
    395386    return file;                        // the returned value is a view into the version on 'files'
     
    426417}
    427418
    428 // search for argname on the config->argument list
    429 // construct an FPA based on the files in this list (must represent a single FPA)
    430 // built the association between the FPA elements (CHIP/CELL) and the files
    431 // define the pmFPAfile filename and bind it to this FPA
    432 // save the pmFPAfile on config->files
    433 // return the pmFPAfile (a view to the one saved on config->files)
    434 pmFPAfile *pmFPAfileDefineFromArgs(bool *success, pmConfig *config, const char *filename, const char *argname)
     419
     420/// Define a file from an array of filenames
     421static pmFPAfile *fpaFileDefineFromArray(pmConfig *config, // Configuration
     422                                         pmFPAfile *bind, // File to bind to, or NULL
     423                                         const char *name, // Name of file
     424                                         const psArray *filenames // Array of file names
     425    )
     426{
     427    PS_ASSERT_PTR_NON_NULL(config, NULL);
     428    PS_ASSERT_STRING_NON_EMPTY(name, NULL);
     429
     430    pmFPA *fpa = NULL;                  // FPA for file
     431    psMetadata *format = NULL;          // Camera format configuration
     432    psString formatName = NULL;         // Name of camera format
     433    psString cameraName = NULL;         // Name of camera
     434    pmFPALevel fileLevel = PM_FPA_LEVEL_NONE; // Level for files
     435    psMetadata *phu = NULL;             // Primary header
     436    if (bind) {
     437        // Use the FPA we're binding to
     438        fpa = psMemIncrRefCounter(bind->fpa);
     439        fileLevel = bind->fileLevel;
     440    } else {
     441        // Need to generate an FPA
     442        psString realName = pmConfigConvertFilename(filenames->data[0], config, false, false);
     443        if (!realName) {
     444            psError(PS_ERR_IO, false, "Failed to convert file name %s", (char *)filenames->data[0]);
     445            return NULL;
     446        }
     447
     448        // load the header of the first image
     449        // EXTWORD (fits->extword) is not relevant to the PHU
     450        psFits *fits = psFitsOpen(realName, "r"); // FITS file
     451        if (!fits) {
     452            psError(PS_ERR_IO, false, "Failed to open file %s", realName);
     453            psFree(realName);
     454            return NULL;
     455        }
     456        phu = psFitsReadHeader (NULL, fits); // Primary header
     457        if (!phu) {
     458            psError(PS_ERR_IO, false, "Failed to read file header %s", realName);
     459            psFree(realName);
     460            return NULL;
     461        }
     462        psFitsClose(fits);
     463
     464        // Determine the current format from the header; determine camera if not specified already.
     465        psMetadata *camera = NULL;      // Camera configuration
     466        format = pmConfigCameraFormatFromHeader(&camera, &cameraName, &formatName, config, phu, true);
     467        if (!format) {
     468            psError(PS_ERR_IO, false, "Failed to determine camera format for %s", realName);
     469            psFree(camera);
     470            psFree(formatName);
     471            psFree(realName);
     472            psFree(phu);
     473            return NULL;
     474        }
     475
     476        fileLevel = pmFPAPHULevel(format);
     477        if (fileLevel == PM_FPA_LEVEL_NONE) {
     478            psError(PS_ERR_IO, true, "Unable to determine file level for %s", realName);
     479            psFree(camera);
     480            psFree(formatName);
     481            psFree(realName);
     482            psFree(phu);
     483            return NULL;
     484        }
     485
     486        // build the template fpa, set up the basic view
     487        // XXX do we want this to be the baseCamera name or the metaCamera name?
     488        fpa = pmFPAConstruct(camera, cameraName);
     489        psFree(camera);
     490        if (!fpa) {
     491            psError(PS_ERR_IO, false, "Failed to construct FPA from %s", realName);
     492            psFree(formatName);
     493            psFree(realName);
     494            psFree(format);
     495            psFree(phu);
     496            return NULL;
     497        }
     498        psFree(realName);
     499    }
     500
     501    // load the given filerule (from config->camera) and bind it to the fpa
     502    // the returned file is just a view to the entry on config->files
     503    pmFPAfile *file = pmFPAfileDefineInput(config, fpa, cameraName, name); // File, to return
     504    if (!file) {
     505        psError(PS_ERR_IO, false, "File %s not defined", name);
     506        psFree(formatName);
     507        psFree(format);
     508        psFree(fpa);
     509        psFree(phu);
     510        return NULL;
     511    }
     512    psFree(cameraName);
     513    psFree(fpa);                        // Drop reference
     514
     515    file->format = format;
     516    file->formatName = formatName;
     517    file->fileLevel = fileLevel;
     518
     519    // We use the filerule and filesrc to identify the files in the file->names data
     520    psFree(file->filerule); // this is set in pmFPAfileDefineInput
     521    file->filerule = psStringCopy("@FILES");
     522    file->filesrc = psStringCopy("{CHIP.NAME}.{CELL.NAME}");
     523
     524    // Examine the list of input files and validate their cameras
     525    // Associate each filename with an element of the FPA
     526    // Save the association on file->names
     527    for (int i = 0; i < filenames->n; i++) {
     528        // Check that the file corresponds to the same camera and format
     529        if (!phu) {
     530            psString realName = pmConfigConvertFilename(filenames->data[i], config, false, false);
     531            if (!realName) {
     532                psError(PS_ERR_IO, false, "Failed to convert file name %s", (char*)filenames->data[i]);
     533                return NULL;
     534            }
     535            psFits *fits = psFitsOpen(realName, "r"); // FITS file
     536            if (!fits) {
     537                psError(PS_ERR_IO, false, "Failed to open file %s", realName);
     538                psFree(realName);
     539                return NULL;
     540            }
     541            phu = psFitsReadHeader(NULL, fits);
     542            psFitsClose(fits);
     543            if (!phu) {
     544                psError(PS_ERR_IO, false, "Failed to read file header %s", realName);
     545                psFree(realName);
     546                return NULL;
     547            }
     548            psFree(realName);
     549        }
     550
     551        if (i == 0 && file->type == PM_FPA_FILE_MASK) {
     552            if (!pmConfigMaskReadHeader(config, phu)) {
     553                psError(PS_ERR_IO, false, "Error reading mask bits");
     554                psFree(phu);
     555                return NULL;
     556            }
     557        }
     558
     559        if (bind || i > 0) {
     560            if (format) {
     561                bool valid = false;
     562                if (!pmConfigValidateCameraFormat(&valid, format, phu)) {
     563                    psError(PS_ERR_UNKNOWN, false, "Error in config scripts\n");
     564                    psFree(phu);
     565                    return NULL;
     566                }
     567                if (!valid) {
     568                    psError(PS_ERR_IO, false, "File %s is not from the required camera",
     569                            (char*)filenames->data[i]);
     570                    psFree(phu);
     571                    return NULL;
     572                }
     573            } else {
     574                format = pmConfigCameraFormatFromHeader(NULL, NULL, NULL, config, phu, true);
     575                if (!format) {
     576                    psError(PS_ERR_IO, false, "Failed to determine camera format from %s",
     577                            (char*)filenames->data[i]);
     578                    psFree(phu);
     579                    return NULL;
     580                }
     581            }
     582        }
     583
     584        // Ensure the format is set
     585        if (!file->format) {
     586            file->format = format;
     587        }
     588        if (!file->formatName) {
     589            file->formatName = formatName;
     590        }
     591
     592        // Set the view to the corresponding entry for this phu
     593        pmFPAview *view = NULL;         // View to PHU
     594        if (bind) {
     595            view = pmFPAIdentifySourceFromHeader(bind->fpa, phu, format);
     596        } else {
     597            view = pmFPAAddSourceFromHeader(fpa, phu, format);
     598        }
     599        psFree(phu);
     600        phu = NULL;
     601        if (!view) {
     602            psError(PS_ERR_IO, false, "Unable to determine source for %s", name);
     603            return NULL;
     604        }
     605
     606        // Associate the filename with the FPA element
     607        psString location = pmFPAfileNameFromRule(file->filesrc, file, view);
     608        psFree(view);
     609        psMetadataAddStr(file->names, PS_LIST_TAIL, location, 0, "Location of file", filenames->data[i]);
     610        psFree(location);
     611    }
     612
     613    return file;
     614}
     615
     616
     617pmFPAfile *pmFPAfileDefineFromArgs(bool *success, pmConfig *config,
     618                                   const char *filename, const char *argname)
    435619{
    436620    PS_ASSERT_PTR_NON_NULL(config, NULL);
     
    438622    PS_ASSERT_STRING_NON_EMPTY(argname, NULL);
    439623
    440     bool status;
    441     pmFPA *fpa = NULL;
    442     psFits *fits = NULL;
    443     pmFPAfile *file = NULL;
    444     psMetadata *phu = NULL;
    445     psMetadata *format = NULL;
    446     psMetadata *camera = NULL;
    447     psString formatName = NULL;
    448 
    449     // use success to identify valid exit conditions (as opposed to 'argument not supplied')
    450     if (success) {
    451         *success = false;
    452     }
    453 
    454     // we search the argument data for the named fileset (argname)
    455     psArray *infiles = psMetadataLookupPtr(&status, config->arguments, argname);
     624    // Search the argument data for the named fileset (argname)
     625    bool status;                        // Status of MD lookup
     626    psArray *filenames = psMetadataLookupPtr(&status, config->arguments, argname); // Filenames for file
    456627    if (!status) {
    457628        if (success) {
     
    460631        return NULL;
    461632    }
    462     if (infiles->n < 1) {
    463         psError(PS_ERR_IO, false, "Found n == %ld files in %s in arguments\n", infiles->n, argname);
    464         return NULL;
    465     }
    466 
    467     // this function is implicitly an INPUT operation: do not create the file
    468     psString realName = pmConfigConvertFilename (infiles->data[0], config, false, false);
    469     if (!realName) {
    470         psError(PS_ERR_IO, false, "Failed to convert file name %s\n", (char *) infiles->data[0]);
    471         return NULL;
    472     }
    473 
    474     // load the header of the first image
    475     // EXTWORD (fits->extword) is not relevant to the PHU
    476     fits = psFitsOpen (realName, "r");
    477     if (!fits) {
    478         psError(PS_ERR_IO, false, "Failed to open file %s\n", realName);
    479         psFree (realName);
    480         return NULL;
    481     }
    482     phu = psFitsReadHeader (NULL, fits);
    483 
    484     if (!phu) {
    485         psError(PS_ERR_IO, false, "Failed to read file header %s\n", realName);
    486         psFree (realName);
    487         return NULL;
    488     }
    489     psFitsClose(fits);
    490 
    491     // Determine the current format from the header; Determine camera if not specified already.
    492     // the returned pointers 'camera' and 'formatName' are allocated here
    493     psString cameraName = NULL;
    494     format = pmConfigCameraFormatFromHeader(&camera, &cameraName, &formatName, config, phu, true);
    495     if (!format) {
    496         psError(PS_ERR_IO, false, "Failed to read CCD format from %s\n", realName);
    497         psFree(phu);
    498         psFree(camera);
    499         psFree(formatName);
    500         psFree(realName);
    501         return NULL;
    502     }
    503 
    504     // build the template fpa, set up the basic view
    505     // XXX do we want this to be the baseCamera name or the metaCamera name?
    506     fpa = pmFPAConstruct(camera, cameraName);
    507     if (!fpa) {
    508         psError(PS_ERR_IO, false, "Failed to construct FPA from %s", realName);
    509         psFree(phu);
    510         psFree(camera);
    511         psFree(formatName);
    512         psFree(realName);
    513         psFree(format);
    514         return NULL;
    515     }
    516     psFree (realName);
    517     psFree (camera);
    518 
    519     // load the given filerule (from config->camera) and bind it to the fpa
    520     // the returned file is just a view to the entry on config->files
    521     file = pmFPAfileDefineInput(config, fpa, cameraName, filename);
    522     if (!file) {
    523         psError(PS_ERR_IO, false, "file %s not defined\n", filename);
    524         psFree(phu);
    525         psFree(formatName);
    526         psFree(format);
    527         psFree(fpa);
    528         return NULL;
    529     }
    530     psFree (cameraName);
    531     psFree (file->filerule); // this is set in pmFPAfileDefineInput
    532 
    533     file->format = format;
    534     file->formatName = formatName;
    535     file->filerule = psStringCopy("@FILES");
    536     file->filesrc = psStringCopy("{CHIP.NAME}.{CELL.NAME}");
    537     // we use the above rules to identify these files in the file->names data
    538 
    539     file->fileLevel = pmFPAPHULevel(format);
    540     if (file->fileLevel == PM_FPA_LEVEL_NONE) {
    541         psError(PS_ERR_IO, true, "Unable to determine file level for %s\n", file->name);
    542         psFree(phu);
    543         psFree(fpa);
    544         return NULL;
    545     }
    546 
    547     if (file->type == PM_FPA_FILE_MASK) {
    548         if (!pmConfigMaskReadHeader (config, phu)) {
    549             psError(PS_ERR_IO, false, "error in mask bits");
    550             return NULL;
    551         }
    552     }
    553 
    554     // examine the list of input files and validate their cameras
    555     // associated each filename with an element of the FPA
    556     // save the association on file->names
    557     for (int i = 0; i < infiles->n; i++) {
    558         if (i > 0) {
    559             // this function is implicitly an INPUT operation: do not create the file
    560             psString realName = pmConfigConvertFilename (infiles->data[i], config, false, false);
    561             if (!realName) {
    562                 psError(PS_ERR_IO, false, "Failed to convert file name %s", (char *) infiles->data[i]);
    563                 psFree(phu);
    564                 psFree(fpa);
    565                 return NULL;
    566             }
    567             // EXTWORD (fits->extword) is not relevant to the PHU
    568             fits = psFitsOpen (realName, "r");
    569             if (!fits) {
    570                 psError(PS_ERR_IO, false, "Failed to open file %s\n", realName);
    571                 psFree(realName);
    572                 psFree(phu);
    573                 psFree(fpa);
    574                 return NULL;
    575             }
    576             phu = psFitsReadHeader (NULL, fits);
    577             if (!phu) {
    578                 psError(PS_ERR_IO, false, "Failed to read file header %s", realName);
    579                 psFree(realName);
    580                 psFitsClose(fits);
    581                 psFree(phu);
    582                 psFree(fpa);
    583                 return NULL;
    584             }
    585             bool valid = false;
    586             if (!pmConfigValidateCameraFormat (&valid, format, phu)) {
    587                 psError(PS_ERR_UNKNOWN, false, "Error in config scripts\n");
    588                 psFree(realName);
    589                 psFitsClose(fits);
    590                 psFree(phu);
    591                 psFree(fpa);
    592                 return NULL;
    593             }
    594             if (!valid) {
    595                 psError(PS_ERR_IO, false, "file %s is not from the required camera", realName);
    596                 psFree(realName);
    597                 psFitsClose(fits);
    598                 psFree(phu);
    599                 psFree(fpa);
    600                 return NULL;
    601             }
    602             psFree(realName);
    603             psFitsClose(fits);
    604         }
    605 
    606         // set the view to the corresponding entry for this phu
    607         pmFPAview *view = pmFPAAddSourceFromHeader (fpa, phu, format);
    608         if (!view) {
    609             psError(PS_ERR_IO, false, "Unable to determine source for %s", file->name);
    610             psFree(phu);
    611             psFree(fpa);
    612             return NULL;
    613         }
    614 
    615         // associate the filename with the FPA element
    616         char *name = pmFPAfileNameFromRule(file->filesrc, file, view);
    617 
    618         // save the name association in the pmFPAfile structure
    619         psMetadataAddStr(file->names, PS_LIST_TAIL, name, 0, "", infiles->data[i]);
    620 
    621         psFree(view);
    622         psFree(name);
    623         psFree(phu);
    624     }
    625     psFree(fpa);
     633    if (filenames->n == 0) {
     634        psError(PS_ERR_IO, false, "No files in array in %s in arguments", argname);
     635        if (success) {
     636            *success = false;
     637        }
     638        return NULL;
     639    }
     640
     641    pmFPAfile *file = fpaFileDefineFromArray(config, NULL, filename, filenames); // File of interest
     642
    626643    if (success) {
    627         *success = true;
    628     }
    629 
    630     return file;
    631 }
    632 
    633 // search for argname on the config->argument list
    634 // construct an FPA based on the files in this list (must represent a single FPA)
    635 // built the association between the FPA elements (CHIP/CELL) and the files
    636 // define the pmFPAfile filename and bind it to this FPA
    637 // save the pmFPAfile on config->files
    638 // return the pmFPAfile (a view to the one saved on config->files)
    639 pmFPAfile *pmFPAfileBindFromArgs (bool *success, pmFPAfile *input, pmConfig *config, const char *filename, const char *argname)
     644        *success = file ? true : false;
     645    }
     646
     647    return file;
     648}
     649
     650pmFPAfile *pmFPAfileBindFromArgs(bool *success, pmFPAfile *input, pmConfig *config,
     651                                 const char *filename, const char *argname)
    640652{
    641653    PS_ASSERT_PTR_NON_NULL(input, NULL);
     
    644656    PS_ASSERT_STRING_NON_EMPTY(argname, NULL);
    645657
    646     bool status;
    647     psFits *fits = NULL;
    648     pmFPAfile *file = NULL;
    649     psMetadata *phu = NULL;
    650 
    651     // use success to identify valid exit conditions (as opposed to 'argument not supplied')
    652     if (success) {
    653         *success = false;
    654     }
    655 
    656     // we search the argument data for the named fileset (argname)
    657     psArray *infiles = psMetadataLookupPtr(&status, config->arguments, argname);
     658    // Search the argument data for the named fileset (argname)
     659    bool status;                        // Status of MD lookup
     660    psArray *filenames = psMetadataLookupPtr(&status, config->arguments, argname); // Filenames for file
    658661    if (!status) {
    659         // this is not an error: this just means no matching argument was supplied
    660662        if (success) {
    661663            *success = true;
     
    663665        return NULL;
    664666    }
    665     if (infiles->n < 1) {
    666         psError(PS_ERR_IO, false, "Found n == %ld files in %s in arguments\n", infiles->n, argname);
    667         return NULL;
    668     }
    669 
    670     // load the given filerule (from config->camera) and bind it to the fpa
    671     // the returned file is just a view to the entry on config->files
    672     file = pmFPAfileDefineInput (config, input->fpa, NULL, filename);
    673     if (!file) {
    674         psError(PS_ERR_IO, false, "file %s not defined\n", filename);
    675         psFree(phu);
    676         return NULL;
    677     }
    678 
    679     // set derived values
    680     file->fileLevel = input->fileLevel;
    681 
    682 
    683     // define the rule to identify these files in the file->names data
    684     psFree (file->filerule);
    685     psFree (file->filesrc);
    686     file->filerule = psStringCopy ("@FILES");
    687     file->filesrc = psStringCopy ("{CHIP.NAME}.{CELL.NAME}");
    688 
    689     // examine the list of input files and validate their cameras
    690     // associated each filename with an element of the FPA
    691     // save the association on file->names
    692     psMetadata *format = NULL;
    693     for (int i = 0; i < infiles->n; i++) {
    694         // this function is implicitly an INPUT operation: do not create the file
    695         psString realName = pmConfigConvertFilename (infiles->data[i], config, false, false);
    696         if (!realName) {
    697             psError(PS_ERR_IO, false, "Failed to convert file name %s", (char *) infiles->data[i]);
    698             return NULL;
    699         }
    700         // EXTWORD (fits->extword) is not relevant to the PHU
    701         fits = psFitsOpen (realName, "r");
    702         if (!fits) {
    703             psError(PS_ERR_IO, false, "Failed to open file %s\n", realName);
    704             psFree(realName);
    705             return NULL;
    706         }
    707         phu = psFitsReadHeader (NULL, fits);
    708         if (!phu) {
    709             psError(PS_ERR_IO, false, "Failed to read file header %s", realName);
    710             psFree(realName);
    711             psFitsClose(fits);
    712             return NULL;
    713         }
    714 
    715         if (!format) {
    716             format = pmConfigCameraFormatFromHeader(NULL, NULL, NULL, config, phu, true);
    717             if (!format) {
    718                 psError(PS_ERR_IO, false, "Failed to read CCD format from %s\n", realName);
    719                 psFree(phu);
    720                 psFree(realName);
    721                 psFitsClose(fits);
    722                 return NULL;
    723             }
    724         } else {
    725             bool valid = false;
    726             if (!pmConfigValidateCameraFormat(&valid, format, phu)) {
    727                 psError(PS_ERR_UNKNOWN, false, "Error in config scripts\n");
    728                 psFree(realName);
    729                 psFitsClose(fits);
    730                 return NULL;
    731             }
    732             if (!valid) {
    733                 psError(PS_ERR_IO, false, "specified data file %s does not match format of supplied INPUT\n",
    734                         realName);
    735                 psFree(realName);
    736                 psFitsClose(fits);
    737                 return NULL;
    738             }
    739         }
    740 
    741         psFree(realName);
    742         psFitsClose(fits);
    743 
    744         // set the view to the corresponding entry for this phu
    745         pmFPAview *view = pmFPAIdentifySourceFromHeader (input->fpa, phu, format);
    746         if (!view) {
    747             psError(PS_ERR_IO, false, "Unable to determine source for %s", file->name);
    748             psFree(phu);
    749             return NULL;
    750         }
    751 
    752         // associate the filename with the FPA element
    753         char *name = pmFPAfileNameFromRule(file->filesrc, file, view);
    754 
    755         // save the name association in the pmFPAfile structure
    756         psMetadataAddStr (file->names, PS_LIST_TAIL, name, 0, "", infiles->data[i]);
    757 
    758         if ((i == 0) && (file->type == PM_FPA_FILE_MASK)) {
    759             if (!pmConfigMaskReadHeader (config, phu)) {
    760                 psError(PS_ERR_IO, false, "error in mask bits");
    761                 return NULL;
    762             }
    763         }
    764 
    765         psFree(view);
    766         psFree(name);
    767         psFree(phu);
    768     }
    769     file->format = format;
    770     file->formatName = psStringCopy(config->formatName);
     667    if (filenames->n == 0) {
     668        psError(PS_ERR_IO, false, "No files in array in %s in arguments", argname);
     669        if (success) {
     670            *success = false;
     671        }
     672        return NULL;
     673    }
     674
     675    pmFPAfile *file = fpaFileDefineFromArray(config, input, filename, filenames); // File of interest
    771676
    772677    if (success) {
    773         *success = true;
    774     }
    775     return file;
    776 }
    777 
    778 // search for argname on the config->argument list
    779 // construct an FPA based on the files in this list (each represents the same FPA)
    780 // built the association between the FPA elements (CHIP/CELL) and the files
    781 // define the pmFPAfile filenames and bind them to the FPAs
    782 // save the pmFPAfiles on config->files
    783 // return the pmFPAfiles (a view to the one saved on config->files)
    784 pmFPAfile *pmFPAfileDefineSingleFromArgs (bool *success, pmConfig *config, const char *filename,
    785         const char *argname, int entry)
     678        *success = file ? true : false;
     679    }
     680
     681    return file;
     682}
     683
     684pmFPAfile *pmFPAfileDefineSingleFromArgs(bool *success, pmConfig *config, const char *filename,
     685                                         const char *argname, int entry)
    786686{
    787687    PS_ASSERT_PTR_NON_NULL(config, NULL);
     
    789689    PS_ASSERT_STRING_NON_EMPTY(argname, NULL);
    790690
    791     bool status;
    792     pmFPA *fpa = NULL;
    793     psFits *fits = NULL;
    794     pmFPAfile *file = NULL;
    795     psMetadata *phu = NULL;
    796     psMetadata *format = NULL;
    797 
    798     if (success) {
    799         *success = false;
    800     }
    801 
    802     // we search the argument data for the named fileset (argname)
    803     psArray *infiles = psMetadataLookupPtr(&status, config->arguments, argname);
     691    // Search the argument data for the named fileset (argname)
     692    bool status;                        // Status from MD lookup
     693    psArray *filenames = psMetadataLookupPtr(&status, config->arguments, argname); // Filenames for file
    804694    if (!status) {
    805         psTrace("psModules.camera", 5, "Failed to find %s in argument list", argname);
    806695        if (success) {
    807696            *success = true;
     
    809698        return NULL;
    810699    }
    811     if (infiles->n <= entry) {
    812         psError(PS_ERR_IO, false, "only %ld files in %s in argument, entry %d requested\n",
    813                 infiles->n, argname, entry);
    814         return NULL;
    815     }
    816 
    817     // examine the list of input files and validate their cameras
    818     // associated each filename with an element of the FPA
    819     // save the association on file->names
    820     // EXTWORD (fits->extword) is not relevant to the PHU
    821     fits = psFitsOpen (infiles->data[entry], "r");
    822     phu = psFitsReadHeader (NULL, fits);
    823     psFitsClose (fits);
    824 
    825     // on first call to this function, config->camera is not set.
    826     // later calls will give an error if the cameras do not match
    827     psMetadata *camera = NULL;
    828     psString formatName = NULL;
    829     psString cameraName = NULL;
    830     format = pmConfigCameraFormatFromHeader (&camera, &cameraName, &formatName, config, phu, true);
    831     if (!format) {
    832         psError(PS_ERR_IO, false, "Failed to read CCD format from %s\n", (char *)infiles->data[0]);
    833         psFree(phu);
    834         psFree(camera);
    835         psFree(formatName);
    836         return NULL;
    837     }
    838 
    839     // build the template fpa, set up the basic view
    840     fpa = pmFPAConstruct (camera, cameraName);
    841     if (!fpa) {
    842         psError(PS_ERR_IO, false, "Failed to construct FPA from %s", (char *)infiles->data[0]);
    843         psFree(phu);
    844         psFree(camera);
    845         psFree(formatName);
    846         psFree(format);
    847         return NULL;
    848     }
    849     psFree(camera);
    850 
    851     // load the given filerule (from config->camera) and bind it to the fpa
    852     // the returned file is just a view to the entry on config->files
    853     // we need a variable name here... (but in filerule)
    854     file = pmFPAfileDefineInput (config, fpa, cameraName, filename);
    855     if (!file) {
    856         psError(PS_ERR_IO, false, "file %s not defined\n", filename);
    857         psFree(phu);
    858         psFree(fpa);
    859         psFree(format);
    860         return NULL;
    861     }
    862     psFree(cameraName);
    863     FPA_TEST_ASSERT (file);
    864 
    865     file->format = format;
    866     file->formatName = formatName;
    867     file->filerule = psStringCopy ("@FILES");
    868     file->filesrc = psStringCopy ("{CHIP.NAME}.{CELL.NAME}");
    869     // adjust the above rules to identify these files in the file->names data
    870 
    871     file->fileLevel = pmFPAPHULevel(format);
    872     if (file->fileLevel == PM_FPA_LEVEL_NONE) {
    873         psError(PS_ERR_IO, true, "Unable to determine file level for %s\n", file->name);
    874         psFree(phu);
    875         psFree(fpa);
    876         return NULL;
    877     }
    878 
    879     if (file->type == PM_FPA_FILE_MASK) {
    880         if (!pmConfigMaskReadHeader (config, phu)) {
    881             psError(PS_ERR_IO, false, "error in mask bits");
    882             return NULL;
    883         }
    884     }
    885 
    886     // set the view to the corresponding entry for this phu
    887     pmFPAview *view = pmFPAAddSourceFromHeader (fpa, phu, format);
    888     if (!view) {
    889         psError(PS_ERR_IO, false, "Unable to determine source for %s", file->name);
    890         psFree(phu);
    891         psFree(fpa);
    892         return NULL;
    893     }
    894 
    895     // associate the filename with the FPA element
    896     char *name = pmFPAfileNameFromRule (file->filesrc, file, view);
    897 
    898     // save the name association in the pmFPAfile structure
    899     psMetadataAddStr (file->names, PS_LIST_TAIL, name, 0, "", infiles->data[entry]);
    900 
    901     psFree(phu);
    902     psFree(fpa);
    903     psFree(view);
    904     psFree(name);
     700    if (filenames->n <= entry) {
     701        psError(PS_ERR_IO, false, "Insufficient files (%ld) in array in %s in arguments",
     702                filenames->n, argname);
     703        if (success) {
     704            *success = false;
     705        }
     706        return NULL;
     707    }
     708
     709    psArray *single = psArrayAlloc(1);  // Array of single filename of interest
     710    single->data[0] = psMemIncrRefCounter(filenames->data[entry]);
     711    pmFPAfile *file = fpaFileDefineFromArray(config, NULL, filename, single); // File of interest
     712    psFree(single);
    905713
    906714    if (success) {
    907         *success = true;
    908     }
    909     return file;
    910 }
     715        *success = file ? true : false;
     716    }
     717
     718    return file;
     719}
     720
     721pmFPAfile *pmFPAfileDefineFromRun(bool *success, pmConfig *config, const char *filename)
     722{
     723    PS_ASSERT_PTR_NON_NULL(config, NULL);
     724    PS_ASSERT_STRING_NON_EMPTY(filename, NULL);
     725
     726    psArray *filenames = pmConfigRunFileGet(config, filename); // Filenames used, or NULL
     727    if (!filenames) {
     728        if (success) {
     729            *success = true;
     730        }
     731        return NULL;
     732    }
     733
     734    pmFPAfile *file = fpaFileDefineFromArray(config, NULL, filename, filenames); // File of interest
     735    psFree(filenames);
     736
     737    if (success) {
     738        *success = file ? true : false;
     739    }
     740
     741    return file;
     742}
     743
    911744
    912745// define the named pmFPAfile from the camera->config
    913746// only valid for pmFPAfile->mode = READ
    914 pmFPAfile *pmFPAfileDefineFromConf (bool *success, const pmConfig *config, const char *filename)
     747pmFPAfile *pmFPAfileDefineFromConf(bool *success, const pmConfig *config, const char *filename)
    915748{
    916749    PS_ASSERT_PTR_NON_NULL(config, false);
     
    991824    pmFPA *fpa = NULL;
    992825    pmFPAfile *file = NULL;
     826
     827    if (type == PM_DETREND_TYPE_NONE) {
     828        return NULL;
     829    }
    993830
    994831    if (success) {
     
    11771014    PS_ASSERT_STRING_NON_EMPTY(filename, NULL);
    11781015
    1179     pmFPAfile *file = pmFPAfileDefineOutputForFormat(config, NULL, filename, src->cameraName,
    1180                                                      src->formatName);
     1016    pmFPAfile *file = pmFPAfileDefineOutputForFormat(config, NULL, filename, src->cameraName, src->formatName);
     1017    if (!file) {
     1018        psError(PS_ERR_UNEXPECTED_NULL, false, "file %s not defined\n", filename);
     1019        return NULL;
     1020    }
    11811021    file->src = psMemIncrRefCounter(src->fpa); // inherit output elements from this source pmFPA
    11821022    file->xBin = xBin;
Note: See TracChangeset for help on using the changeset viewer.