IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

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

WCS Newton-Raphson analysis has Alpha cross terms inverted; mods to fpaFileDefineFromArray, fpaFileDefineFromArray for readability and to allow for output config to differ from input config; add new function pmFPAfileDefineNewConfig; add pmChipSelectCells, pmFPAExcludeChips; set target chip and cell attributes to match source in pmFPACopy; split out errors in addSource

Location:
trunk/psModules
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules

  • trunk/psModules/src/camera/pmFPAfileDefine.c

    r34085 r35561  
    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    psFree(realName);
     482    return phu;
     483}
     484
     485// this this function wants to return:
     486// pmFPA, PHU, fileLevel, outConfig
     487// camera, cameraName, formatName
     488typedef struct {
     489    pmFPA *fpa;
     490    psMetadata *phu;
     491    psMetadata *format;
     492    pmFPALevel fileLevel;
     493    psString cameraName;
     494    psString formatName;
     495} pmFPAfromFilenameOutput;
     496
     497// for the given filename, read PHU and determine camera format; build an FPA for the file
     498bool pmFPAfromFilename (pmFPAfromFilenameOutput *output, pmConfig **outConfig, pmConfig *sysConfig, char *filename){
     499
     500    // Need to generate an FPA
     501    psMetadata *phu = readPHUfromFilename (filename, sysConfig);
     502    if (!phu) {
     503        psError(psErrorCodeLast(), false, "Failed to read PHU for %s", filename);
     504        return false;
     505    }
     506
     507    // if we expect the loaded FPA to differ in configuration from the current system configuration
     508    // generate an output config for this FPA
     509    pmConfig *config = NULL;
     510    if (outConfig) {
     511        config = pmConfigAlloc();
     512        config->user = psMemIncrRefCounter(sysConfig->user);
     513        config->system = psMemIncrRefCounter(sysConfig->system);
     514
     515        psFree (config->files);
     516        config->files = psMemIncrRefCounter(sysConfig->files);
     517        psFree (config->arguments);
     518        config->arguments = psMemIncrRefCounter(sysConfig->arguments);
     519
     520        *outConfig = config;
     521    } else {
     522        config = sysConfig;
     523    }
     524
     525    // values which are returned to calling function
     526    psString formatName = NULL; // Name of camera format
     527    psString cameraName = NULL; // Name of camera
     528    psMetadata *camera = NULL;  // Camera configuration
     529
     530    // Determine the current format from the header; determine camera if not specified already.
     531    psMetadata *format = pmConfigCameraFormatFromHeader(&camera, &cameraName, &formatName, config, phu, true);
     532    if (!format) {
     533        psError(psErrorCodeLast(), false, "Failed to determine camera format for %s", filename);
     534        psFree(camera);
     535        psFree(formatName);
     536        psFree(phu);
     537        return false;
     538    }
     539
     540    pmFPALevel fileLevel = pmFPAPHULevel(format);
     541    if (fileLevel == PM_FPA_LEVEL_NONE) {
     542        psError(PM_ERR_CONFIG, true, "Unable to determine file level for %s", filename);
     543        psFree(camera);
     544        psFree(formatName);
     545        psFree(phu);
     546        return false;
     547    }
     548
     549    // build the template fpa, set up the basic view
     550    // we supply the metaCamera name (if NULL, baseCamera name is used)
     551    pmFPA *fpa = pmFPAConstruct(camera, cameraName);
     552    psFree(camera);
     553
     554    if (!fpa) {
     555        psError(psErrorCodeLast(), false, "Failed to construct FPA from %s", filename);
     556        psFree(formatName);
     557        psFree(format);
     558        psFree(phu);
     559        return NULL;
     560    }
     561
     562    output->fpa = fpa;
     563    output->phu = phu;
     564    output->format = format;
     565    output->fileLevel = fileLevel;
     566    output->cameraName = cameraName;
     567    output->formatName = formatName;
     568
     569    return true;
     570
     571}
    448572
    449573/// Define a file from an array of filenames
    450 static pmFPAfile *fpaFileDefineFromArray(pmConfig *config, // Configuration
     574static pmFPAfile *fpaFileDefineFromArray(pmConfig **outConfig, // output configuration
     575                                         pmConfig *sysConfig, // global configuration
    451576                                         pmFPAfile *bind, // File to bind to, or NULL
    452577                                         const char *name, // Name of file
     
    454579    )
    455580{
    456     PS_ASSERT_PTR_NON_NULL(config, NULL);
     581    PS_ASSERT_PTR_NON_NULL(sysConfig, NULL);
    457582    PS_ASSERT_STRING_NON_EMPTY(name, NULL);
    458583
     
    463588    pmFPALevel fileLevel = PM_FPA_LEVEL_NONE; // Level for files
    464589    psMetadata *phu = NULL;             // Primary header
     590
    465591    if (bind) {
    466592        // Use the FPA we're binding to
     
    468594        fileLevel = bind->fileLevel;
    469595    } 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     }
     596        pmFPAfromFilenameOutput output;
     597        if (!pmFPAfromFilename (&output, outConfig, sysConfig, filenames->data[0])) {
     598            return NULL;
     599        }
     600        fpa = output.fpa;
     601        phu = output.phu;
     602        format = output.format;
     603        fileLevel = output.fileLevel;
     604        cameraName = output.cameraName;
     605        formatName = output.formatName;
     606    }
     607
     608    pmConfig *config = outConfig ? *outConfig : sysConfig;
    533609
    534610    // load the given filerule (from config->camera) and bind it to the fpa
     
    561637        // Check that the file corresponds to the same camera and format
    562638        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]);
     639            phu = readPHUfromFilename (filenames->data[i], config);
     640            if (!phu) {
     641                psError(psErrorCodeLast(), false, "Failed to read PHU for %s", (char *)filenames->data[i]);
    566642                return NULL;
    567643            }
    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);
    586644        }
    587645
     
    651709}
    652710
    653 
    654 pmFPAfile *pmFPAfileDefineFromArgs(bool *success, pmConfig *config,
    655                                    const char *filename, const char *argname)
     711// find the file associated with the argname & generate a pmFPAfile for it based on the filerule
     712pmFPAfile *pmFPAfileDefineFromArgs(bool *success, pmConfig *config, const char *filename, const char *argname)
    656713{
    657714    PS_ASSERT_PTR_NON_NULL(config, NULL);
     
    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) {
     
    685742}
    686743
    687 pmFPAfile *pmFPAfileBindFromArgs(bool *success, pmFPAfile *input, pmConfig *config,
    688                                 const char *filename, const char *argname)
     744// find the file associated with the argname & bind it to the given pmFPAfile for it based on the filerule
     745pmFPAfile *pmFPAfileBindFromArgs(bool *success, pmFPAfile *input, pmConfig *config, const char *filename, const char *argname)
    689746{
    690747    PS_ASSERT_PTR_NON_NULL(input, NULL);
     
    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) {
     
    719776}
    720777
    721 pmFPAfile *pmFPAfileDefineSingleFromArgs(bool *success, pmConfig *config, const char *filename,
    722                                         const char *argname, int entry)
     778// find the specific file associated with the argname & generate a pmFPAfile for it based on the filerule
     779pmFPAfile *pmFPAfileDefineSingleFromArgs(bool *success, pmConfig *config, const char *filename, const char *argname, int entry)
    723780{
    724781    PS_ASSERT_PTR_NON_NULL(config, NULL);
     
    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
     
    756813}
    757814
     815// find the file in the config list & generate a pmFPAfile for it based on the filerule
    758816pmFPAfile *pmFPAfileDefineFromRun(bool *success, pmFPAfile *bind, pmConfig *config, const char *filename)
    759817{
     
    769827    }
    770828
    771     pmFPAfile *file = fpaFileDefineFromArray(config, bind, filename, filenames); // File of interest
     829    pmFPAfile *file = fpaFileDefineFromArray(NULL, config, bind, filename, filenames); // File of interest
    772830    psFree(filenames);
    773831
     
    779837}
    780838
     839// find the files in the config list & generate an array of pmFPAfiles for them based on the filerule
    781840psArray *pmFPAfileDefineMultipleFromRun(bool *success, psArray *bind, pmConfig *config, const char *filename)
    782841{
     
    808867        dummy->data[0] = files->data[i];
    809868        pmFPAfile *bindFile = bind ? bind->data[i] : NULL; // File to which to bind
    810         files->data[i] = psMemIncrRefCounter(fpaFileDefineFromArray(config, bindFile, filename, dummy));
     869        files->data[i] = psMemIncrRefCounter(fpaFileDefineFromArray(NULL, config, bindFile, filename, dummy));
    811870        if (!files->data[i]) {
    812871            psError(psErrorCodeLast(), false, "Unable to define file %s %d", filename, i);
     
    823882
    824883    return files;
     884}
     885
     886// find the file associated with the argname & generate a pmFPAfile for it based on the filerule
     887pmFPAfile *pmFPAfileDefineNewConfig(bool *success, pmConfig **outConfig, pmConfig *sysConfig, const char *filename, const char *argname)
     888{
     889    PS_ASSERT_PTR_NON_NULL(outConfig, NULL);
     890    PS_ASSERT_PTR_NON_NULL(sysConfig, NULL);
     891    PS_ASSERT_STRING_NON_EMPTY(filename, NULL);
     892    PS_ASSERT_STRING_NON_EMPTY(argname, NULL);
     893
     894    // Search the argument data for the named fileset (argname)
     895    bool status;                        // Status of MD lookup
     896    psArray *filenames = psMetadataLookupPtr(&status, sysConfig->arguments, argname); // Filenames for file
     897    if (!status) {
     898        if (success) {
     899            *success = true;
     900        }
     901        return NULL;
     902    }
     903    if (filenames->n == 0) {
     904        psError(PM_ERR_CONFIG, true, "No files in array in %s in arguments", argname);
     905        if (success) {
     906            *success = false;
     907        }
     908        return NULL;
     909    }
     910
     911    pmFPAfile *file = fpaFileDefineFromArray(outConfig, sysConfig, NULL, filename, filenames); // File of interest
     912
     913    if (success) {
     914        *success = file ? true : false;
     915    }
     916
     917    return file;
    825918}
    826919
Note: See TracChangeset for help on using the changeset viewer.