#include <stdio.h>
#include <pslib.h>

#include "pmConceptsPhotcode.h"

char *pmConceptsPhotcodeForView (pmConfig *config, pmFPAfile *file, pmFPAview *view)
{

    if (view->chip < -1) {
        psError(PS_ERR_IO, true, "photcodes undefined for FPA: defined by chip\n");
        return NULL;
    }

    // select recipe options supplied on command line
    psMetadata *recipe  = psMetadataLookupPtr (NULL, config->recipes, "PHASE2");
    if (recipe == NULL) {
        psError(PS_ERR_IO, true, "recipe PHASE2 not found\n");
        return NULL;
    }

    // select photcode rule from recipe
    char *rule = psMetadataLookupStr (NULL, recipe, "PHOTCODE.RULE");
    if (rule == NULL) {
        psError(PS_ERR_IO, true, "PHOTCODE.RULE not found in PHASE2 recipe\n");
        return NULL;
    }

    // convert rule to real photcode
    char *photcode = pmFPAfileNameFromRule (rule, file, view);

    return photcode;
}
