IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 35379


Ignore:
Timestamp:
Apr 9, 2013, 11:11:27 AM (13 years ago)
Author:
eugene
Message:

re-factor fpaFileDefineFromArray and all output config

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20130307/psModules/src/camera/pmFPAfileDefine.c

    r34085 r35379  
    446446}
    447447
     448// given a filename, convert to UNIX namespace and read the PHU
     449psMetadata *readPHUfromFilename (char *filename, pmConfig *config) {
     450
     451    // Need to generate an FPA
     452    psString realName = pmConfigConvertFilename(filename, config, false, false);
     453    if (!realName) {
     454        psError(psErrorCodeLast(), false, "Failed to convert file name %s", filename);
     455        return NULL;
     456    }
     457
     458    // load the header of the first image
     459    // EXTWORD (fits->extword) is not relevant to the PHU
     460    psFits *fits = psFitsOpen(realName, "r"); // FITS file
     461    if (!fits) {
     462        psError(psErrorCodeLast(), false, "Failed to open file %s", realName);
     463        psFree(realName);
     464        return NULL;
     465    }
     466
     467    psMetadata *phu = psFitsReadHeader (NULL, fits); // Primary header
     468    if (!phu) {
     469        psError(psErrorCodeLast(), false, "Failed to read file header %s", realName);
     470        psFree(realName);
     471        return NULL;
     472    }
     473
     474    if (!psFitsClose(fits)) {
     475        psError(psErrorCodeLast(), false, "Failed to close file %s", realName);
     476        psFree(realName);
     477        psFree(phu);
     478        return NULL;
     479    }
     480
     481    return phu;
     482}
     483
     484// this this function wants to return:
     485// pmFPA, PHU, fileLevel, outConfig
     486// camera, cameraName, formatName
     487typedef struct {
     488    pmFPA *fpa;
     489    psMetadata *phu;
     490    psMetadata *format;
     491    pmFPALevel fileLevel;
     492    psString cameraName;
     493    psString formatName;
     494} pmFPAfromFilenameOutput;
     495
     496// for the given filename, read PHU and determine camera format; build an FPA for the file
     497bool pmFPAfromFilename (pmFPAfromFilenameOutput *output, pmConfig **outConfig, pmConfig *sysConfig, char *filename){
     498
     499    // Need to generate an FPA
     500    psMetadata *phu = readPHUfromFilename (filename, sysConfig);
     501    if (!phu) {
     502        psError(psErrorCodeLast(), false, "Failed to read PHU for %s", filename);
     503        return false;
     504    }
     505
     506    // if we expect the loaded FPA to differ in configuration from the current system configuration
     507    // generate an output config for this FPA
     508    pmConfig *config = NULL;
     509    if (outConfig) {
     510        config = pmConfigAlloc();
     511        config->user = psMemIncrRefCounter(sysConfig->user);
     512        config->system = psMemIncrRefCounter(sysConfig->system);
     513
     514        psFree (config->files);
     515        config->files = psMemIncrRefCounter(sysConfig->files);
     516        psFree (config->arguments);
     517        config->arguments = psMemIncrRefCounter(sysConfig->arguments);
     518
     519        *outConfig = config;
     520    } else {
     521        config = sysConfig;
     522    }
     523
     524    // values which are returned to calling function
     525    psString formatName = NULL; // Name of camera format
     526    psString cameraName = NULL; // Name of camera
     527    psMetadata *camera = NULL;  // Camera configuration
     528
     529    // Determine the current format from the header; determine camera if not specified already.
     530    psMetadata *format = pmConfigCameraFormatFromHeader(&camera, &cameraName, &formatName, config, phu, true);
     531    if (!format) {
     532        psError(psErrorCodeLast(), false, "Failed to determine camera format for %s", filename);
     533        psFree(camera);
     534        psFree(formatName);
     535        psFree(phu);
     536        return false;
     537    }
     538
     539    pmFPALevel fileLevel = pmFPAPHULevel(format);
     540    if (fileLevel == PM_FPA_LEVEL_NONE) {
     541        psError(PM_ERR_CONFIG, true, "Unable to determine file level for %s", filename);
     542        psFree(camera);
     543        psFree(formatName);
     544        psFree(phu);
     545        return false;
     546    }
     547
     548    // build the template fpa, set up the basic view
     549    // we supply the metaCamera name (if NULL, baseCamera name is used)
     550    pmFPA *fpa = pmFPAConstruct(camera, cameraName);
     551    psFree(camera);
     552
     553    if (!fpa) {
     554        psError(psErrorCodeLast(), false, "Failed to construct FPA from %s", filename);
     555        psFree(formatName);
     556        psFree(format);
     557        psFree(phu);
     558        return NULL;
     559    }
     560
     561    output->fpa = fpa;
     562    output->phu = phu;
     563    output->format = format;
     564    output->fileLevel = fileLevel;
     565    output->cameraName = cameraName;
     566    output->formatName = formatName;
     567
     568    return true;
     569
     570}
    448571
    449572/// Define a file from an array of filenames
    450 static pmFPAfile *fpaFileDefineFromArray(pmConfig *config, // Configuration
     573static pmFPAfile *fpaFileDefineFromArray(pmConfig **outConfig, // output configuration
     574                                         pmConfig *sysConfig, // global configuration
    451575                                         pmFPAfile *bind, // File to bind to, or NULL
    452576                                         const char *name, // Name of file
     
    454578    )
    455579{
    456     PS_ASSERT_PTR_NON_NULL(config, NULL);
     580    PS_ASSERT_PTR_NON_NULL(sysConfig, NULL);
    457581    PS_ASSERT_STRING_NON_EMPTY(name, NULL);
    458582
     
    463587    pmFPALevel fileLevel = PM_FPA_LEVEL_NONE; // Level for files
    464588    psMetadata *phu = NULL;             // Primary header
     589
    465590    if (bind) {
    466591        // Use the FPA we're binding to
     
    468593        fileLevel = bind->fileLevel;
    469594    } else {
    470         // Need to generate an FPA
    471         psString realName = pmConfigConvertFilename(filenames->data[0], config, false, false);
    472         if (!realName) {
    473             psError(psErrorCodeLast(), false, "Failed to convert file name %s", (char *)filenames->data[0]);
    474             return NULL;
    475         }
    476 
    477         // load the header of the first image
    478         // EXTWORD (fits->extword) is not relevant to the PHU
    479         psFits *fits = psFitsOpen(realName, "r"); // FITS file
    480         if (!fits) {
    481             psError(psErrorCodeLast(), false, "Failed to open file %s", realName);
    482             psFree(realName);
    483             return NULL;
    484         }
    485         phu = psFitsReadHeader (NULL, fits); // Primary header
    486         if (!phu) {
    487             psError(psErrorCodeLast(), false, "Failed to read file header %s", realName);
    488             psFree(realName);
    489             return NULL;
    490         }
    491         if (!psFitsClose(fits)) {
    492             psError(psErrorCodeLast(), false, "Failed to close file %s", realName);
    493             psFree(realName);
    494             return NULL;
    495         }
    496 
    497         // Determine the current format from the header; determine camera if not specified already.
    498         psMetadata *camera = NULL;      // Camera configuration
    499         format = pmConfigCameraFormatFromHeader(&camera, &cameraName, &formatName, config, phu, true);
    500         if (!format) {
    501             psError(psErrorCodeLast(), false, "Failed to determine camera format for %s", realName);
    502             psFree(camera);
    503             psFree(formatName);
    504             psFree(realName);
    505             psFree(phu);
    506             return NULL;
    507         }
    508 
    509         fileLevel = pmFPAPHULevel(format);
    510         if (fileLevel == PM_FPA_LEVEL_NONE) {
    511             psError(PM_ERR_CONFIG, true, "Unable to determine file level for %s", realName);
    512             psFree(camera);
    513             psFree(formatName);
    514             psFree(realName);
    515             psFree(phu);
    516             return NULL;
    517         }
    518 
    519         // build the template fpa, set up the basic view
    520         // XXX do we want this to be the baseCamera name or the metaCamera name?
    521         fpa = pmFPAConstruct(camera, cameraName);
    522         psFree(camera);
    523         if (!fpa) {
    524             psError(psErrorCodeLast(), false, "Failed to construct FPA from %s", realName);
    525             psFree(formatName);
    526             psFree(realName);
    527             psFree(format);
    528             psFree(phu);
    529             return NULL;
    530         }
    531         psFree(realName);
    532     }
     595        pmFPAfromFilenameOutput output;
     596        if (!pmFPAfromFilename (&output, outConfig, sysConfig, filenames->data[0])) {
     597            return NULL;
     598        }
     599        fpa = output.fpa;
     600        phu = output.phu;
     601        format = output.format;
     602        fileLevel = output.fileLevel;
     603        cameraName = output.cameraName;
     604        formatName = output.formatName;
     605    }
     606
     607    pmConfig *config = outConfig ? *outConfig : sysConfig;
    533608
    534609    // load the given filerule (from config->camera) and bind it to the fpa
     
    561636        // Check that the file corresponds to the same camera and format
    562637        if (!phu) {
    563             psString realName = pmConfigConvertFilename(filenames->data[i], config, false, false);
    564             if (!realName) {
    565                 psError(psErrorCodeLast(), false, "Failed to convert file name %s", (char*)filenames->data[i]);
     638            phu = readPHUfromFilename (filenames->data[i], config);
     639            if (!phu) {
     640                psError(psErrorCodeLast(), false, "Failed to read PHU for %s", (char *)filenames->data[i]);
    566641                return NULL;
    567642            }
    568             psFits *fits = psFitsOpen(realName, "r"); // FITS file
    569             if (!fits) {
    570                 psError(psErrorCodeLast(), false, "Failed to open file %s", realName);
    571                 psFree(realName);
    572                 return NULL;
    573             }
    574             phu = psFitsReadHeader(NULL, fits);
    575             if (!psFitsClose(fits)) {
    576                 psError(psErrorCodeLast(), false, "Failed to close file %s", realName);
    577                 psFree(realName);
    578                 return NULL;
    579             }
    580             if (!phu) {
    581                 psError(psErrorCodeLast(), false, "Failed to read file header %s", realName);
    582                 psFree(realName);
    583                 return NULL;
    584             }
    585             psFree(realName);
    586643        }
    587644
     
    676733    }
    677734
    678     pmFPAfile *file = fpaFileDefineFromArray(config, NULL, filename, filenames); // File of interest
     735    pmFPAfile *file = fpaFileDefineFromArray(NULL, config, NULL, filename, filenames); // File of interest
    679736
    680737    if (success) {
     
    710767    }
    711768
    712     pmFPAfile *file = fpaFileDefineFromArray(config, input, filename, filenames); // File of interest
     769    pmFPAfile *file = fpaFileDefineFromArray(NULL, config, input, filename, filenames); // File of interest
    713770
    714771    if (success) {
     
    746803    psArray *single = psArrayAlloc(1);  // Array of single filename of interest
    747804    single->data[0] = psMemIncrRefCounter(filenames->data[entry]);
    748     pmFPAfile *file = fpaFileDefineFromArray(config, NULL, filename, single); // File of interest
     805    pmFPAfile *file = fpaFileDefineFromArray(NULL, config, NULL, filename, single); // File of interest
    749806    psFree(single);
    750807
     
    769826    }
    770827
    771     pmFPAfile *file = fpaFileDefineFromArray(config, bind, filename, filenames); // File of interest
     828    pmFPAfile *file = fpaFileDefineFromArray(NULL, config, bind, filename, filenames); // File of interest
    772829    psFree(filenames);
    773830
     
    808865        dummy->data[0] = files->data[i];
    809866        pmFPAfile *bindFile = bind ? bind->data[i] : NULL; // File to which to bind
    810         files->data[i] = psMemIncrRefCounter(fpaFileDefineFromArray(config, bindFile, filename, dummy));
     867        files->data[i] = psMemIncrRefCounter(fpaFileDefineFromArray(NULL, config, bindFile, filename, dummy));
    811868        if (!files->data[i]) {
    812869            psError(psErrorCodeLast(), false, "Unable to define file %s %d", filename, i);
Note: See TracChangeset for help on using the changeset viewer.