IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 5104


Ignore:
Timestamp:
Sep 22, 2005, 4:54:52 PM (21 years ago)
Author:
Paul Price
Message:

Working under pslib-0.7.0, but don't have the inner loop behaving yet.

Location:
trunk/archive/scripts/src/phase2
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/archive/scripts/src/phase2/Makefile

    r4820 r5104  
    55DEVFLAGS += -DTESTING           # Testing version
    66
    7 CFLAGS += -O0 -g -std=c99 -Werror -I/home/mithrandir/price/pan-starrs/jhroot/i686-pc-linux-gnu/include/ -D_GNU_SOURCE $(DEVFLAGS)
    8 PSLIB += -L/home/mithrandir/price/pan-starrs/jhroot/i686-pc-linux-gnu/lib/ -lpslib -lgsl -lgslcblas -lfftw3f -lsla -lcfitsio -lm -lxml2 -lmysqlclient
    9 PSMODULE += -L/home/mithrandir/price/pan-starrs/jhroot/i686-pc-linux-gnu/lib/ -lpsmodule
     7CFLAGS += -O0 -g -std=c99 -Werror -I$(JHBUILD)/include/ -I$(JHBUILD)/include/libxml2/ -DPS_NO_TRACE -D_GNU_SOURCE $(DEVFLAGS)
     8PSLIB += -L$(JHBUILD)/lib/ -lpslib -lgsl -lgslcblas -lfftw3f -lsla -lcfitsio -lm -L$(JHBUILD)/lib/libxml2/ -L$(JHBUILD)/lib/mysql/ -lxml2 -lmysqlclient
     9PSMODULE += -L$(JHBUILD)/lib/ -lpsmodule
    1010LDFLAGS += $(PSLIB)
    1111
    12 OBJS = papPhase2.o psAdditionals.o pmConfig.o pmFPA.o pmFPAConstruct.o papStuff.o
     12OBJS = papPhase2.o psAdditionals.o pmConfig.o pmFPA.o pmFPAConstruct.o papStuff.o pmFPARead.o                \
     13        pmFPAConceptsGet.o pmFPAConceptsSet.o pmFPAWrite.o                                                   \
     14        pmFlatField.o pmMaskBadPixels.o pmNonLinear.o pmSubtractBias.o
    1315TARGET = papPhase2
    1416
  • trunk/archive/scripts/src/phase2/papPhase2.c

    r4820 r5104  
    44#include "pslib.h"
    55#include "psAdditionals.h"
    6 
    7 #include "pmSubtractBias.h"
    86
    97#include "pmFPA.h"
    108#include "pmConfig.h"
    119#include "pmFPAConstruct.h"
     10#include "pmFPARead.h"
     11#include "pmFPAConceptsGet.h"
     12#include "pmFPAWrite.h"
     13
     14#include "pmFlatField.h"
     15#include "pmMaskBadPixels.h"
     16#include "pmNonLinear.h"
     17#include "pmSubtractBias.h"
    1218
    1319// Phase 2 needs to:
     
    3743#define RECIPE "PHASE2"                 // Name of the recipe to use
    3844
    39 static psMemoryId memPrintAlloc(const psMemBlock *mb)
     45static psMemId memPrintAlloc(const psMemBlock *mb)
    4046{
    4147    printf("Allocated memory block %lld (%lld):\n"
     
    4652    return 0;
    4753}
    48 static psMemoryId memPrintFree(const psMemBlock *mb)
     54static psMemId memPrintFree(const psMemBlock *mb)
    4955{
    5056    printf("Freed memory block %lld (%lld):\n"
     
    6975#if 0
    7076    // Hunting memory leaks
    71     psMemAllocateCallbackSetID(3539);
    72     psMemFreeCallbackSetID(3539);
     77    psMemAllocateCallbackSetID(23289);
     78    psMemFreeCallbackSetID(23289);
    7379    psMemAllocateCallbackSet(memPrintAlloc);
    7480    psMemFreeCallbackSet(memPrintFree);
    7581#endif
    7682
    77 //    psTraceSetLevel(".", 10);
     83    psTraceSetLevel("pmConfigCameraFromHeader", 10);
    7884
    7985    // Parse the configurations
     
    8894    // Parse other command-line arguments
    8995    psMetadata *arguments = psMetadataAlloc(); // The arguments, with default values
    90     psMetadataAddStr(arguments, PS_LIST_TAIL, "-bias", "Name of the bias image", "");
    91     psMetadataAddStr(arguments, PS_LIST_TAIL, "-dark", "Name of the dark image", "");
    92     psMetadataAddStr(arguments, PS_LIST_TAIL, "-flat", "Name of the flat-field image", "");
    93     psMetadataAddStr(arguments, PS_LIST_TAIL, "-mask", "Name of the mask image", "");
    94     psMetadataAddS32(arguments, PS_LIST_TAIL, "-chip", "Chip number to process (if positive)", -1);
     96    psMetadataAddStr(arguments, PS_LIST_TAIL, "-bias", 0, "Name of the bias image", "");
     97    psMetadataAddStr(arguments, PS_LIST_TAIL, "-dark", 0, "Name of the dark image", "");
     98    psMetadataAddStr(arguments, PS_LIST_TAIL, "-flat", 0, "Name of the flat-field image", "");
     99    psMetadataAddStr(arguments, PS_LIST_TAIL, "-mask", 0, "Name of the mask image", "");
     100    psMetadataAddS32(arguments, PS_LIST_TAIL, "-chip", 0, "Chip number to process (if positive)", -1);
    95101    if (! psArgumentParse(arguments, &argc, argv) || argc != 3) {
    96102        printf("\nPan-STARRS Phase 2 processing\n\n");
     
    113119    printf("Mask: %s\n", maskName);
    114120    printf("Chip: %d\n", chipNum);
    115     psFree(arguments);
    116121
    117122    // Open the input
     
    132137#endif
    133138
    134 #if 0
    135139    // Open the output and output mask
    136140    // We do it here so that we don't process the whole lot and then find out we can't open the output file
     
    144148        exit(EXIT_FAILURE);
    145149    }
     150
     151#if 0
    146152    psString outputMaskName = psStringCopy(outputName);
    147153    (void)psStringAppend(&outputMaskName, ".mask");
     
    158164#endif
    159165
    160        
    161166    // Get camera configuration from header if not already defined
    162167    if (! camera) {
     
    176181
    177182    // Construct camera in preparation for reading
    178     pmFPA *input = pmFPAConstruct(camera, database);
    179     pmFPA *mask = pmFPAConstruct(camera, database);
    180     pmFPA *bias = pmFPAConstruct(camera, database);
    181     pmFPA *dark = pmFPAConstruct(camera, database);
    182     pmFPA *flat = pmFPAConstruct(camera, database);
     183    pmFPA *input = pmFPAConstruct(camera);
     184    pmFPA *mask = pmFPAConstruct(camera);
     185    pmFPA *bias = pmFPAConstruct(camera);
     186    pmFPA *dark = pmFPAConstruct(camera);
     187    pmFPA *flat = pmFPAConstruct(camera);
    183188
    184189    // Set various tasks
     
    198203
    199204    if (psMetadataLookupBool(NULL, recipe, "MASK")) {
    200         if (maskName) {
     205        if (strlen(maskName) > 0) {
    201206            doMask = true;
    202207        } else {
     
    209214    }
    210215    if (psMetadataLookupBool(NULL, recipe, "BIAS")) {
    211         if (biasName) {
     216        if (strlen(biasName) > 0) {
    212217            doBias = true;
    213218        } else {
     
    217222    }
    218223    if (psMetadataLookupBool(NULL, recipe, "DARK")) {
    219         if (darkName) {
     224        if (strlen(darkName) > 0) {
    220225            doDark = true;
    221226        } else {
     
    260265        }
    261266    }
     267
    262268    if (psMetadataLookupBool(NULL, recipe, "FLAT")) {
    263         if (flatName) {
     269        if (strlen(flatName) > 0) {
    264270            doFlat = true;
    265271        } else {
     
    269275    }
    270276
    271 #if 0
    272277    // Chip selection
    273278    if (chipNum >= 0) {
     
    282287
    283288    // Read in the input pixels
    284     if (! pmFPARead(input, inputFile)) {
     289    if (! pmFPARead(input, inputFile, header, database)) {
    285290        psErrorStackPrint(stderr, "Unable to populate camera from FITS file: %s\n", inputName);
    286291        exit(EXIT_FAILURE);
     
    292297#endif
    293298
     299    pmFPAPrint(input);
    294300
    295301    // Load the calibration frames, if required
    296     if (biasName) {
     302    if (doBias) {
     303#ifdef PRODUCTION
    297304        psFits *biasFile = psFitsOpen(biasName, "r"); // File handle for bias
     305#else
     306        psFits *biasFile = psFitsAlloc(biasName);
     307#endif
    298308        psMetadata *biasHeader = psFitsReadHeader(NULL, biasFile); // FITS header for bias
    299309        if (! pmConfigValidateCamera(camera, biasHeader)) {
    300             psError("phase2", true, "Bias (%s) does not seem to be from the same camera.\n", biasName);
     310            psError(PS_ERR_IO, true, "Bias (%s) does not seem to be from the same camera.\n", biasName);
    301311            exit(EXIT_FAILURE);
    302312        }
    303313        psFree(biasHeader);
    304         if (! pmFPARead(bias, biasFile)) {
     314        if (! pmFPARead(bias, biasFile, NULL, database)) {
    305315            psErrorStackPrint(stderr, "Unable to populate bias camera from fits FITS: %s\n", biasName);
    306316            exit(EXIT_FAILURE);
    307317        }
     318#ifdef PRODUCTION
    308319        psFitsClose(biasFile);
    309     }
    310 
    311     if (darkName) {
     320#else
     321        psFree(biasFile);
     322#endif
     323    }
     324
     325    if (doDark) {
     326#ifdef PRODUCTION
    312327        psFits *darkFile = psFitsOpen(darkName, "r"); // File handle for dark
     328#else
     329        psFits *darkFile = psFitsAlloc(darkName);
     330#endif
    313331        psMetadata *darkHeader = psFitsReadHeader(NULL, darkFile); // FITS header for dark
    314332        if (! pmConfigValidateCamera(camera, darkHeader)) {
    315             psError("phase2", true, "Dark (%s) does not seem to be from the same camera.\n", darkName);
     333            psError(PS_ERR_IO, true, "Dark (%s) does not seem to be from the same camera.\n", darkName);
    316334            exit(EXIT_FAILURE);
    317335        }
    318336        psFree(darkHeader);
    319         if (! pmFPARead(dark, darkFile)) {
     337        if (! pmFPARead(dark, darkFile, NULL, database)) {
    320338            psErrorStackPrint(stderr, "Unable to populate dark camera from fits FITS: %s\n", darkName);
    321339            exit(EXIT_FAILURE);
    322340        }
     341#ifdef PRODUCTION
    323342        psFitsClose(darkFile);
    324     }
    325 
    326     if (maskName) {
     343#else
     344        psFree(darkFile);
     345#endif
     346    }
     347
     348    if (doMask) {
     349#ifdef PRODUCTION
    327350        psFits *maskFile = psFitsOpen(maskName, "r"); // File handle for mask
     351#else
     352        psFits *maskFile = psFitsAlloc(maskName);
     353#endif
    328354        psMetadata *maskHeader = psFitsReadHeader(NULL, maskFile); // FITS header for mask
    329355        if (! pmConfigValidateCamera(camera, maskHeader)) {
    330             psError("phase2", true, "Mask (%s) does not seem to be from the same camera.\n", maskName);
     356            psError(PS_ERR_IO, true, "Mask (%s) does not seem to be from the same camera.\n", maskName);
    331357            exit(EXIT_FAILURE);
    332358        }
    333359        psFree(maskHeader);
    334         if (! pmFPARead(mask, maskFile)) {
     360        if (! pmFPARead(mask, maskFile, NULL, database)) {
    335361            psErrorStackPrint(stderr, "Unable to populate mask camera from fits FITS: %s\n", maskName);
    336362            exit(EXIT_FAILURE);
    337363        }
     364#ifdef PRODUCTION
    338365        psFitsClose(maskFile);
     366#else
     367        psFree(maskFile);
     368#endif
    339369    }
    340370   
    341     if (flatName) {
     371    if (doFlat) {
     372#ifdef PRODUCTION
    342373        psFits *flatFile = psFitsOpen(flatName, "r"); // File handle for flat
     374#else
     375        psFits *flatFile = psFitsAlloc(flatName);
     376#endif
    343377        psMetadata *flatHeader = psFitsReadHeader(NULL, flatFile); // FITS header for flat
    344378        if (! pmConfigValidateCamera(camera, flatHeader)) {
    345             psError("phase2", true, "Flat (%s) does not seem to be from the same camera.\n", flatName);
     379            psError(PS_ERR_IO, true, "Flat (%s) does not seem to be from the same camera.\n", flatName);
    346380            exit(EXIT_FAILURE);
    347381        }
    348382        psFree(flatHeader);
    349         if (! pmFPARead(flat, flatFile)) {
     383        if (! pmFPARead(flat, flatFile, NULL, database)) {
    350384            psErrorStackPrint(stderr, "Unable to populate flat camera from fits FITS: %s\n", flatName);
    351385            exit(EXIT_FAILURE);
    352386        }
     387#ifdef PRODUCTION
    353388        psFitsClose(flatFile);
     389#else
     390        psFree(flatFile);
     391#endif
    354392    }
    355393   
    356     psArray *inputChips = inputs->chips; // Array of chips in input image
     394    psArray *inputChips = input->chips; // Array of chips in input image
    357395    psArray *biasChips = bias->chips;   // Array of chips in bias image
    358396    psArray *darkChips = dark->chips;   // Array of chips in dark image
     
    377415
    378416        for (int j = 0; j < inputCells->n; j++) {
    379             pmCell *inputCell = inputCells->data[i]; // Cell of interest in input image
    380             pmCell *biasCell = biasCells->data[i]; // Cell of interest in bias image
    381             pmCell *darkCell = darkCells->data[i]; // Cell of interest in dark imag
    382             pmCell *maskCell = maskCells->data[i]; // Cell of interest in mask image
    383             pmCell *flatCell = flatCells->data[i]; // Cell of interest in flat image
     417            pmCell *inputCell = inputCells->data[j]; // Cell of interest in input image
     418            pmCell *biasCell = biasCells->data[j]; // Cell of interest in bias image
     419            pmCell *darkCell = darkCells->data[j]; // Cell of interest in dark imag
     420            pmCell *maskCell = maskCells->data[j]; // Cell of interest in mask image
     421            pmCell *flatCell = flatCells->data[j]; // Cell of interest in flat image
    384422
    385423            if (! inputCell->valid) {
     
    388426
    389427            psArray *inputReadouts = inputCell->readouts; // Array of readouts in input image
    390             if (biasCell->readouts->n != 1) {
     428            if (doBias && biasCell->readouts->n > 1) {
    391429                psLogMsg("phase2", PS_LOG_WARN, "Bias contains multiple readouts: only the first will be"
    392430                         " used.");
    393431            }
    394             if (darkCell->readouts->n != 1) {
     432            if (doDark && darkCell->readouts->n > 1) {
    395433                psLogMsg("phase2", PS_LOG_WARN, "Dark contains multiple readouts: only the first will be"
    396434                         " used.");
    397435            }
    398             if (maskCell->readouts->n != 1) {
     436            if (doMask && maskCell->readouts->n > 1) {
    399437                psLogMsg("phase2", PS_LOG_WARN, "Mask contains multiple readouts: only the first will be"
    400438                         " used.");
    401439            }
    402             if (flatCell->readouts->n != 1) {
     440            if (doFlat && flatCell->readouts->n > 1) {
    403441                psLogMsg("phase2", PS_LOG_WARN, "Flat contains multiple readouts: only the first will be"
    404442                         " used.");
    405443            }
    406444
     445            pmReadout *biasReadout = NULL; // Bias readout
     446            pmReadout *maskReadout = NULL; // Mask readout
     447            pmReadout *flatReadout = NULL; // Flat readout
    407448            psImage *biasImage = NULL;  // Bias pixels
    408449            psImage *darkImage = NULL;  // Dark pixels
    409450            float darkTime = 1.0;       // Dark time for dark image
    410 
    411             if (biasName) {
    412                 pmReadout *biasReadout = biasCell->readouts->data[0]; // Readout of interest in bias image
     451            psImage *maskImage = NULL;  // Mask pixels
     452
     453            if (doBias) {
     454                biasReadout = biasCell->readouts->data[0]; // Readout of interest in bias image
    413455                biasImage = biasReadout->image;
    414456            }
    415             if (darkName) {
     457            if (doDark) {
    416458                pmReadout *darkReadout = darkCell->readouts->data[0]; // Readout of interest in dark image
    417459                darkImage = darkReadout->image;
    418                 darkTime = pmReadoutGetDarkTime(darkReadout);
     460                darkTime = psMetadataLookupF32(NULL, darkCell->concepts, "CELL.DARKTIME");
    419461                if (darkTime <= 0.0) {
    420462                    psErrorStackPrint(stderr, "DARKTIME for dark image (%f) is non-positive.\n", darkTime);
     
    422464                }
    423465            }
    424             if (maskName) {
    425                 pmReadout *maskReadout = maskCell->readouts->data[0]; // Readout of interest in mask image
    426             }
    427             if (flatName) {
    428                 pmReadout *flatReadout = flatCell->readouts->data[0]; // Readout of interest in mask image
     466            if (doMask) {
     467                maskReadout = maskCell->readouts->data[0]; // Readout of interest in mask image
     468                maskImage = maskReadout->image;
     469            }
     470            if (doFlat) {
     471                flatReadout = flatCell->readouts->data[0]; // Readout of interest in mask image
    429472            }
    430473
     
    432475                pmReadout *inputReadout = inputReadouts->data[k]; // Readout of interest in input image
    433476                psImage *inputImage = inputReadout->image; // The actual image
    434                 psList *inputBias = inputReadout->bias; // List of overscan bias regions
     477                psList *inputOverscans = inputReadout->overscans; // List of overscan bias regions
    435478
    436479                // Mask bad pixels
    437480                if (doMask) {
     481                    float saturation = psMetadataLookupF32(NULL, inputCell->concepts, "CELL.SATURATION");
     482
    438483                    // Need to change this later to grow the mask by the size of the convolution kernel
    439                     (void)pmMaskBadPixels(inputReadout, maskReadout,
    440                                           PS_MASK_TRAP | PS_MASK_BADCOL | PS_MASK_SAT, saturation,
    441                                           PS_MASK_TRAP, 0);
     484                    (void)pmMaskBadPixels(inputReadout, maskImage,
     485                                          PM_MASK_TRAP | PM_MASK_BADCOL | PM_MASK_SAT, saturation,
     486                                          PM_MASK_TRAP, 0);
    442487                }
    443488
     
    450495                    } else {
    451496                        switch (dataItem->type) {
    452                           case PS_META_VECTOR:
    453                             // These are the polynomial coefficients
    454                             psVector *coeff = dataItem->data.V; // The coefficient vector
    455                             if (coeff->type.type != PS_TYPE_F64) {
    456                                 psVector *temp = psVectorCopy(NULL, coeff, PS_TYPE_F64); // F64 version
     497                          case PS_META_VEC:
     498                            {
     499                                // These are the polynomial coefficients
     500                                psVector *coeff = dataItem->data.V; // The coefficient vector
     501                                if (coeff->type.type != PS_TYPE_F64) {
     502                                    psVector *temp = psVectorCopy(NULL, coeff, PS_TYPE_F64); // F64 version
     503                                    psFree(coeff);
     504                                    coeff = temp;
     505                                }
     506                                psPolynomial1D *correction = psPolynomial1DAlloc(PS_POLYNOMIAL_ORD,
     507                                                                                 coeff->n + 1);
     508                                for (int i = 0; i < coeff->n; i++) {
     509                                    correction->coeff[i] = coeff->data.F64[i];
     510                                }
     511                                (void)pmNonLinearityPolynomial(inputReadout, correction);
    457512                                psFree(coeff);
    458                                 coeff = temp;
     513                                psFree(correction);
    459514                            }
    460                             psPolynomial1D *correction = psPolynomial1DAlloc(PS_POLYNOMIAL_ORD,
    461                                                                              coeff->n + 1);
    462                             for (int i = 0; i < coeff->n; i++) {
    463                                 correction->coeff[i] = coeff->data.F64[i];
    464                             }
    465                             (void)pmNonLinearityPolynomial(inputReadout, correction);
    466                             psFree(coeffCopy);
    467                             psFree(coeff);
    468                             psFree(correction);
    469515                            break;
    470516                          case PS_META_STR:
    471                             // This is a filename
    472                             psString name = dataItem->data.V;   // Filename
    473                             psLookupTable *table = psLookupTableAlloc(name, "%f %f", 0);
    474                             if (psLookupTableRead(table) <= 0) {
    475                                 psErrorStackPrint(stderr, "Unable to read non-linearity correction file %s "
    476                                                   "--- ignored\n", tableName);
    477                             } else {
    478                                 (void)pmNonLinearityLookup(inputReadout, table);
     517                            {
     518                                // This is a filename
     519                                psString name = dataItem->data.V;       // Filename
     520                                psLookupTable *table = psLookupTableAlloc(name, "%f %f", 0);
     521                                if (psLookupTableRead(table) <= 0) {
     522                                    psErrorStackPrint(stderr, "Unable to read non-linearity correction file "
     523                                                      "%s --- ignored\n", name);
     524                                } else {
     525#ifdef PRODUCTION
     526                                    (void)pmNonLinearityLookup(inputReadout, table);
     527#else
     528                                    printf("XXX: Non-linearity with lookup table not yet implemented.\n");
     529#endif
     530                                }
     531                                psFree(table);
    479532                            }
    480                             psFree(table);
    481533                            break;
    482534                          case PS_META_META:
    483                             // This is a menu
    484                             psMetadata *options = dataItem->data.V; // Options with concept values as keys
    485                             bool mdok = false; // Success of MD lookup
    486                             psString concept = psMetadataLookupStr(&mdok, recipe, "NONLIN.SOURCE");
    487                             if (! mdok || ! concept) {
    488                                 psLogMsg("phase2", PS_LOG_WARN, "Non-linearity correction desired, but "
    489                                          "unable to find NONLIN.SOURCE in recipe --- ignored.\n");
    490                             } else {
    491                                 psMetadataItem *conceptValueItem = pmCellGetConcept(inputCell, concept);
    492                                 if (! conceptValueItem) {
    493                                     psLogMsg("phase2", PS_LOG_WARN, "Unable to find value of concept %s "
    494                                              "for non-linearity correction --- ignored.\n", concept);
    495                                 } else if (conceptValueItem->type != PS_META_STR) {
    496                                     psLogMsg("phase2", PS_LOG_WARN, "Type for concept %s isn't STRING, as"
    497                                              " expected for non-linearity correction --- ignored.\n",
    498                                              concept);
     535                            {
     536                                // This is a menu
     537                                psMetadata *options = dataItem->data.V; // Options with concept values as keys
     538                                bool mdok = false; // Success of MD lookup
     539                                psString concept = psMetadataLookupString(&mdok, recipe, "NONLIN.SOURCE");
     540                                if (! mdok || ! concept) {
     541                                    psLogMsg("phase2", PS_LOG_WARN, "Non-linearity correction desired, but "
     542                                             "unable to find NONLIN.SOURCE in recipe --- ignored.\n");
    499543                                } else {
    500                                     psString conceptValue = conceptValueItem->data.V;
    501                                     // Get the value of the concept
    502                                     psMetadataItem *optionItem = psMetadataLookup(options, conceptValue);
    503                                     if (!optionItem) {
    504                                         psLogMsg("phase2", PS_LOG_WARN, "Unable to find %s in NONLIN.DATA"
    505                                                  " --- ignored.\n", conceptValue);
    506                                     } else if (optionItem->type == PS_META_VECTOR) {
    507                                         // These are the polynomial coefficients
    508                                         psVector *coeff = optionItem->data.V; // The coefficient vector
    509                                         if (coeff->type.type != PS_TYPE_F64) {
    510                                             psVector *temp = psVectorCopy(NULL, coeff, PS_TYPE_F64);
     544                                    psMetadataItem *conceptValueItem = pmCellGetConcept(inputCell, concept);
     545                                    if (! conceptValueItem) {
     546                                        psLogMsg("phase2", PS_LOG_WARN, "Unable to find value of concept %s "
     547                                                 "for non-linearity correction --- ignored.\n", concept);
     548                                    } else if (conceptValueItem->type != PS_META_STR) {
     549                                        psLogMsg("phase2", PS_LOG_WARN, "Type for concept %s isn't STRING, as"
     550                                                 " expected for non-linearity correction --- ignored.\n",
     551                                                 concept);
     552                                    } else {
     553                                        psString conceptValue = conceptValueItem->data.V;
     554                                        // Get the value of the concept
     555                                        psMetadataItem *optionItem = psMetadataLookup(options, conceptValue);
     556                                        if (!optionItem) {
     557                                            psLogMsg("phase2", PS_LOG_WARN, "Unable to find %s in NONLIN.DATA"
     558                                                     " --- ignored.\n", conceptValue);
     559                                        } else if (optionItem->type == PS_META_VEC) {
     560                                            // These are the polynomial coefficients
     561                                            psVector *coeff = optionItem->data.V; // The coefficient vector
     562                                            if (coeff->type.type != PS_TYPE_F64) {
     563                                                psVector *temp = psVectorCopy(NULL, coeff, PS_TYPE_F64);
     564                                                psFree(coeff);
     565                                                coeff = temp;
     566                                            }
     567                                            // Polynomial correction
     568                                            psPolynomial1D *correction = psPolynomial1DAlloc(PS_POLYNOMIAL_ORD,
     569                                                                                             coeff->n + 1);
     570                                            for (int i = 0; i < coeff->n; i++) {
     571                                                correction->coeff[i] = coeff->data.F64[i];
     572                                            }
     573                                            (void)pmNonLinearityPolynomial(inputReadout, correction);
    511574                                            psFree(coeff);
    512                                             coeff = temp;
     575                                            psFree(correction);
     576                                        } else if (optionItem->type == PS_META_STR) {
     577                                            // This is a filename
     578                                            psString tableName = optionItem->data.V; // The filename
     579                                            psLookupTable *table = psLookupTableAlloc(tableName, "%f %f", 0);
     580                                            int numLines = 0; // Number of lines read from table
     581                                            if ((numLines = psLookupTableRead(table)) <= 0) {
     582                                                psErrorStackPrint(stderr, "Unable to read non-linearity "
     583                                                                  "correction file %s --- ignored\n",
     584                                                                  tableName);
     585                                            } else {
     586#ifdef PRODUCTION
     587                                                (void)pmNonLinearityLookup(inputReadout, table);
     588#else
     589                                                printf("XXX: Non-linearity correction from lookup table not "
     590                                                       "yet implemented.\n");
     591#endif
     592                                            }
     593                                            psFree(table);
     594                                        } else {
     595                                            psLogMsg("phase2", PS_LOG_WARN, "Non-linearity correction "
     596                                                     "desired but unable to interpret NONLIN.DATA for %s"
     597                                                     " --- ignored\n", conceptValue);
    513598                                        }
    514                                         // Polynomial correction
    515                                         psPolynomial1D *correction = psPolynomial1DAlloc(PS_POLYNOMIAL_ORD,
    516                                                                                          coeff->n + 1);
    517                                         for (int i = 0; i < coeff->n; i++) {
    518                                             correction->coeff[i] = coeff->data.F64[i];
    519                                         }
    520                                         (void)pmNonLinearityPolynomial(inputReadout, correction);
    521                                         psFree(coeffCopy);
    522                                         psFree(coeff);
    523                                         psFree(correction);
    524                                     } else if (optionItem->type == PS_META_STR) {
    525                                         // This is a filename
    526                                         psString tableName = optionItem->data.V; // The filename
    527                                         psLookupTable *table = psLookupTableAlloc(tableName, "%f %f", 0);
    528                                         if ((int numLines = psLookupTableRead(table)) <= 0) {
    529                                             psErrorStackPrint(stderr, "Unable to read non-linearity "
    530                                                               "correction file %s --- ignored\n", tableName);
    531                                         } else {
    532                                             (void)pmNonLinearityLookup(inputReadout, table);
    533                                         }
    534                                         psFree(table);
    535                                     } else {
    536                                         psLogMsg("phase2", PS_LOG_WARN, "Non-linearity correction "
    537                                                  "desired but unable to interpret NONLIN.DATA for %s"
    538                                                  " --- ignored\n", conceptValue);
    539599                                    }
    540600                                }
     
    553613                // it's not in there yet.  Once it is, we can get rid of "pedestal".
    554614
     615#ifdef PRODUCTION
    555616                psImage *pedestal = NULL;       // Pedestal image (bias + scaled dark)
    556617                if (doDark) {
    557                     float inputTime = pmReadoutGetDarkTime(inputReadout); // Dark time for input image
     618                    // Dark time for input image
     619                    float inputTime = psMetadataLookupF32(NULL, inputCell->concepts, "CELL.DARKTIME");
    558620                    if (inputTime <= 0) {
    559621                        psErrorStackPrint(stderr, "DARKTIME for input image (%f) is non-positive.\n",
     
    562624                    }
    563625
    564                     pedestal = psBinaryOp(NULL, darkImage, "*",
    565                                           psScalarAlloc(inputTime * darkTime, PS_TYPE_F32));
     626                    pedestal = (psImage*)psBinaryOp(NULL, darkImage, "*",
     627                                                    psScalarAlloc(inputTime * darkTime, PS_TYPE_F32));
    566628                }
    567629                if (doBias) {
     
    569631                        if (biasImage->numRows != darkImage->numRows ||
    570632                            biasImage->numCols != darkImage->numCols) {
    571                             psError("phase2", true, "Bias and dark images have different dimensions: "
     633                            psError(PS_ERR_IO, true, "Bias and dark images have different dimensions: "
    572634                                    "%dx%d vs %dx%d\n", biasImage->numCols, biasImage->numRows,
    573635                                    darkImage->numCols, darkImage->numRows);
     
    579641                    }
    580642                }
    581 
     643#endif
    582644                if (overscanMode == PM_OVERSCAN_ROWS || overscanMode == PM_OVERSCAN_COLUMNS) {
    583645                    // Need to get the read direction
    584                     int readdir = pmCellGetReaddir(inputCell); // Read direction
     646                    int readdir = psMetadataLookupS32(NULL, inputCell->concepts, "CELL.READDIR");
    585647                    if (readdir == 1) {
    586648                        overscanMode = PM_OVERSCAN_ROWS;
     
    594656                }
    595657
    596                 void *overscanResult = NULL; // Result of overscan fit
    597                 (void)pmSubtractBias(inputImage, overscanResult, inputBias, overscanMode, overscanStat,
    598                                      overscanBin, overscanFit, pedestal);
    599 
    600                 // Output overscan fit results, if required
    601                 if (doOverscan) {
    602                     switch (overscanFit) {
    603                       case PM_FIT_POLYNOMIAL:
    604                         psPolynomial1D *poly = (psPolynomial1D*)overscanResult; // The polynomial
    605                         psString coeffs = NULL; // String containing the coefficients
    606                         for (int i = 0; i < poly->n; i++) {
    607                             psStringAppend(&coeffs, "%.2f ", poly->coeffs[i]);
     658                printf("mode: %d\n", overscanMode);
     659
     660                if (doBias || doOverscan || doDark) {
     661                    void *overscanResult = NULL; // Result of overscan fit
     662#ifdef PRODUCTION
     663                    (void)pmSubtractBias(inputReadout, overscanResult, inputOverscans, overscanMode,
     664                                         overscanStats, overscanBins, overscanFit, pedestal);
     665#else
     666                    (void)pmSubtractBias(inputReadout, overscanResult, inputOverscans, overscanMode,
     667                                         overscanStats, overscanBins, overscanFit, biasReadout);
     668#endif
     669                   
     670                    // Output overscan fit results, if required
     671                    if (doOverscan) {
     672                        switch (overscanFit) {
     673                          case PM_FIT_POLYNOMIAL:
     674                            {
     675                                psPolynomial1D *poly = (psPolynomial1D*)overscanResult; // The polynomial
     676                                psString coeffs = NULL; // String containing the coefficients
     677                                for (int i = 0; i < poly->n; i++) {
     678                                    psStringAppend(&coeffs, "%.2f ", poly->coeff[i]);
     679                                }
     680                                psLogMsg("phase2", PS_LOG_INFO, "Overscan polynomial coefficients:\n%s\n",
     681                                         coeffs);
     682                                psFree(coeffs);
     683                            }
     684                            break;
     685                          case PM_FIT_SPLINE:
     686                            {
     687                                psSpline1D *spline = (psSpline1D*)overscanResult; // The spline
     688                                psString coeffs = NULL; // String containing the coefficients
     689                                for (int i = 0; i < spline->n; i++) {
     690                                    psPolynomial1D *poly = spline->spline[i]; // i-th polynomial
     691                                    psStringAppend(&coeffs, "%d: ", i);
     692                                    for (int j = 0; j < poly->n; j++) {
     693                                        psStringAppend(&coeffs, "%.2f ", poly->coeff[i]);
     694                                    }
     695                                    psStringAppend(&coeffs, "\n");
     696                                }
     697                                psLogMsg("phase2", PS_LOG_INFO, "Overscan spline coefficients:\n%s\n",
     698                                         coeffs);
     699                                psFree(coeffs);
     700                            }
     701                            break;
     702                          case PM_FIT_NONE:
     703                            break;
     704                          default:
     705                            psAbort(__func__, "Should never get here!!!\n");
    608706                        }
    609                         psLogMsg("phase2", PS_LOG_INFO, "Overscan polynomial coefficients:\n%s\n",
    610                                  coeffs);
    611                         psFree(coeffs);
    612                         break;
    613                       case PM_FIT_SPLINE:
    614                         psSpline1D *spline = (psSpline1D*)overscanResult; // The spline
    615                         psString coeffs = NULL; // String containing the coefficients
    616                         for (int i = 0; i < spline->n; i++) {
    617                             psPolynomial1D *poly = spline->spline[i]; // i-th polynomial
    618                             psStringAppend(&coeffs, "%d: ", i);
    619                             for (int j = 0; j < poly->n; j++) {
    620                                 psStringAppend(&coeffs, "%.2f ", poly->coeffs[i]);
    621                             }
    622                             psStringAppend(&coeffs, "\n");
    623                         }
    624                         psLogMsg("phase2", PS_LOG_INFO, "Overscan spline coefficients:\n%s\n", coeffs);
    625                         psFree(coeffs);
    626                         break;
    627                       case PM_FIT_NONE:
    628                         break;
    629                       default:
    630                         psAbort("Should never get here!!!\n");
    631707                    }
    632708                }
     
    634710                // Flat-field correction
    635711                if (doFlat) {
    636                     (void)pmFlatField(inputReadout, maskReadout, flatReadout);
     712                    (void)pmFlatField(inputReadout, flatReadout);
    637713                }
    638714
     
    659735
    660736    // Write the output
    661     pmFPAWrite(outputFile, input);
    662     pmFPAWriteMask(outputMaskFile, input);
     737    pmFPAWrite(outputFile, input, database);
     738//    pmFPAWriteMask(outputMaskFile, input);
    663739#ifdef PRODUCTION
    664740    psFitsClose(outputFile);
    665     psFitsClose(outputMaskFile);
     741//    psFitsClose(outputMaskFile);
    666742#else
    667743    psFree(outputFile);
    668     psFree(outputMaskFile);
    669 #endif
    670 
    671 #endif
    672 
     744//    psFree(outputMaskFile);
     745#endif
     746
     747    psFree(arguments);
    673748    psFree(site);
    674749    psFree(header);
     
    681756    psFree(flat);
    682757    psFree(overscanStats);
    683     psFree(inputFile);
    684758
    685759#if 1
  • trunk/archive/scripts/src/phase2/papStuff.c

    r4820 r5104  
    66
    77#include "papStuff.h"
     8
     9
     10static void memPrint(const psPtr ptr)
     11{
     12    psMemBlock *mb = ((psMemBlock*)ptr) - 1;
     13    printf("Memory block %lld (%lld):\n"
     14           "\tFile %s, line %d, size %d\n"
     15           "\tPosts: %x %x %x\n",
     16           mb->id, mb->refCounter, mb->file, mb->lineno, mb->userMemorySize, mb->startblock, mb->endblock,
     17           *(void**)((int8_t *)(mb + 1) + mb->userMemorySize));
     18}
    819
    920// Split string on given characters
     
    3041                (void)psListAdd(values, PS_LIST_TAIL, word);
    3142                start = i + 1;
     43                psFree(word);
    3244            }
    3345        }
     
    3749        psString word = psStringNCopy(&string[start], length - start);
    3850        (void)psListAdd(values, PS_LIST_TAIL, word);
     51        psFree(word);
    3952    }
    4053
  • trunk/archive/scripts/src/phase2/pmConfig.c

    r4820 r5104  
    133133        }
    134134        psTrace(__func__, 7, "Setting log destination to STDOUT.\n");
    135         psLogSetDestination(0);
     135        psLogSetDestination(PS_LOG_TO_STDOUT);
    136136    }
    137137
     
    147147            }
    148148            psTrace(__func__, 7, "Setting trace level for %s to %d\n", traceItem->name, traceItem->data.S32);
    149             (void)psTraceSetLevel(traceItem->name, traceItem->data.S32);
     149            psTraceSetLevel(traceItem->name, traceItem->data.S32);
    150150        }
    151151        psFree(traceIter);
  • trunk/archive/scripts/src/phase2/pmFPA.c

    r4820 r5104  
    11#include <stdio.h>
    22#include <string.h>
     3#include <assert.h>
    34#include "pslib.h"
    45#include "psAdditionals.h"
     
    67#include "pmFPA.h"
    78
    8 
    99//////////////////////////////////////////////////////////////////////////////////////////////////////////////
    1010// Allocators
    1111//////////////////////////////////////////////////////////////////////////////////////////////////////////////
    1212
    13 pmFPA *pmFPAAlloc(const psMetadata *camera, // Camera configuration
    14                   psDB *db              // Database
     13p_pmHDU *p_pmHDUAlloc(const char *extname)
     14{
     15    p_pmHDU *hdu = psAlloc(sizeof(p_pmHDU));
     16    psMemSetDeallocator(hdu, (psFreeFunc)p_pmHDUFree);
     17
     18    hdu->extname = extname;
     19    hdu->pixels = NULL;
     20    hdu->header = NULL;
     21
     22    return hdu;
     23}
     24
     25void p_pmHDUFree(p_pmHDU *hdu)
     26{
     27    psFree(hdu->pixels);
     28    psFree(hdu->header);
     29}
     30
     31pmFPA *pmFPAAlloc(const psMetadata *camera // Camera configuration
    1532    )
    1633{
    1734    pmFPA *fpa = psAlloc(sizeof(pmFPA));// The FPA
    18     psMemSetDeallocator(fpa, (psFreeFcn)p_pmFPAFree);
     35    psMemSetDeallocator(fpa, (psFreeFunc)p_pmFPAFree);
    1936
    2037    // Fill in the components
     
    2340    fpa->projection = NULL;
    2441
    25     fpa->values = psMetadataAlloc();
    26 //    fpa->camera = psMemIncrRefCounter((psPtr)camera);
    27     fpa->db = db;
     42    fpa->concepts = psMetadataAlloc();
     43    fpa->camera = psMemIncrRefCounter((psPtr)camera);
    2844    fpa->chips = psArrayAlloc(0);
    2945
    30     fpa->extname = NULL;
    31     fpa->pixels = NULL;
    32     fpa->header = NULL;
     46    fpa->private = NULL;
    3347
    3448    return fpa;
     
    4155    psFree(fpa->projection);
    4256
    43     psFree(fpa->values);
     57    psFree(fpa->concepts);
    4458    psFree((psPtr)fpa->camera);
    45     psFree(fpa->db);
    4659    psFree(fpa->chips);
    4760
    48     psFree(fpa->pixels);
    49     psFree(fpa->header);
     61    psFree(fpa->private);
    5062}
    5163
     
    5567{
    5668    pmChip *chip = psAlloc(sizeof(pmChip)); // The chip
    57     psMemSetDeallocator(chip, (psFreeFcn)p_pmChipFree);
     69    psMemSetDeallocator(chip, (psFreeFunc)p_pmChipFree);
    5870
    5971    // Push onto the array of chips
     
    6779    chip->fromFPA = NULL;
    6880
    69     chip->values = psMetadataAlloc();
    70 //    chip->parent = psMemIncrRefCounter(fpa);
     81    chip->concepts = psMetadataAlloc();
    7182    chip->cells = psArrayAlloc(0);
    72 
    73     chip->extname = NULL;
    74     chip->pixels = NULL;
    75     chip->header = NULL;
    76 
    77     psMetadataAddStr(chip->values, PS_LIST_HEAD, "CHIP.NAME", "Chip name added at pmChipAlloc", name);
     83    chip->parent = fpa;                 // We don't increment the reference counter on this --- it's a
     84                                        // "hidden" link.  If we increment the reference counter, we get stuck
     85                                        // in a circle.
     86    chip->valid = true;   
     87
     88    chip->private = NULL;
     89
     90    psMetadataAddStr(chip->concepts, PS_LIST_HEAD, "CHIP.NAME", 0, "Chip name added at pmChipAlloc", name);
    7891
    7992    return chip;
     
    8598    psFree(chip->fromFPA);
    8699
    87     psFree(chip->values);
     100    psFree(chip->concepts);
    88101    psFree(chip->cells);
    89 //    psFree(chip->parent);
    90 
    91     psFree(chip->pixels);
    92     psFree(chip->header);
     102
     103    psFree(chip->private);
     104
     105    // We don't free the parent member, since that would generate a circular call.  We don't increment the
     106    // reference counter when we add it, anyway, so that's OK.
    93107}
    94108
    95109pmCell *pmCellAlloc(pmChip *chip,       // Chip to which the cell belongs
    96                     psMetadata *values, // Initial values for concepts
     110                    psMetadata *cameraData, // Camera data
    97111                    psString name       // Name of cell
    98112    )
    99113{
    100114    pmCell *cell = psAlloc(sizeof(pmCell)); // The cell
    101     psMemSetDeallocator(cell, (psFreeFcn)p_pmCellFree);
     115    psMemSetDeallocator(cell, (psFreeFunc)p_pmCellFree);
    102116
    103117    // Push onto the array of chips
     
    114128    cell->toSky = NULL;
    115129
    116     cell->values = psMemIncrRefCounter(values);
     130    cell->concepts = psMetadataAlloc();
     131    psMetadataAddStr(cell->concepts, PS_LIST_HEAD, "CELL.NAME", 0, "Cell name added at pmCellAlloc", name);
     132    cell->camera = psMemIncrRefCounter(cameraData);
     133
    117134    cell->readouts = psArrayAlloc(0);
    118 //    cell->parent = psMemIncrRefCounter(chip);
    119 
    120     cell->extname = NULL;
    121     cell->pixels = NULL;
    122     cell->header = NULL;
    123 
    124     psMetadataAddStr(cell->values, PS_LIST_HEAD, "CELL.NAME", "Cell name added at pmCellAlloc", name);
     135    cell->parent = chip;                // We don't increment the reference counter on this --- it's a
     136                                        // "hidden" link.  If we increment the reference counter, we get stuck
     137                                        // in a circle.
     138    cell->valid = true;
     139
     140    cell->private = NULL;
    125141
    126142    return cell;
     
    135151    psFree(cell->toSky);
    136152
    137     psFree(cell->values);
     153    psFree(cell->concepts);
     154    psFree(cell->camera);
    138155    psFree(cell->readouts);
    139 //    psFree(cell->parent);
    140 
    141     psFree(cell->pixels);
    142     psFree(cell->header);
     156
     157    psFree(cell->private);
     158
     159    // We don't free the parent member, since that would generate a circular call.  We don't increment the
     160    // reference counter when we add it, anyway, so that's OK.
    143161}
    144162
    145163pmReadout *pmReadoutAlloc(pmCell *cell, // Cell to which the readout belongs
    146                           int readoutNum, // Number of the readout
    147164                          psImage *image, // The pixels
    148165                          psList *overscans, // The overscan images
     
    151168{
    152169    pmReadout *readout = psAlloc(sizeof(pmReadout));
    153     psMemSetDeallocator(readout, (psFreeFcn)p_pmReadoutFree);
    154     psArray *readouts = cell->readouts;
    155     if (readoutNum >= readouts->nalloc) {
    156         readouts = psArrayRealloc(readouts, readoutNum);
    157         cell->readouts = readouts;
    158     }
    159     readouts->data[readoutNum] = readout;
     170    psMemSetDeallocator(readout, (psFreeFunc)p_pmReadoutFree);
     171    cell->readouts = psArrayAdd(cell->readouts, 0, readout);
    160172   
    161173    // Set the components
    162     readout->image = image;
    163     readout->overscans = overscans;
     174    readout->image = psMemIncrRefCounter(image);
     175    readout->mask = NULL;
     176    readout->overscans = psMemIncrRefCounter(overscans);
    164177   
    165     readout->values = psMetadataAlloc();
     178    //readout->concepts = psMetadataAlloc();
    166179   
    167180    *(int*)&readout->col0 = col0;
     
    178191{
    179192    psFree(readout->image);
     193    psFree(readout->mask);
    180194    psFree(readout->overscans);
    181     psFree(readout->values);
    182 }
     195    //psFree(readout->concepts);
     196}
     197
     198
     199//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     200// Select and Exclude chips
     201//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     202
     203bool pmFPASelectChip(pmFPA *fpa, int chipNum)
     204{
     205    assert(fpa);
     206
     207    if (chipNum < 0 || chipNum > fpa->chips->n) {
     208        return false;
     209    }
     210    psArray *chips = fpa->chips;        // Component chips
     211    for (int i = 0; i < chips->n; i++) {
     212        pmChip *chip = chips->data[i];  // The chip of interest
     213        if (i == chipNum) {
     214            psTrace(__func__, 5, "Marking chip %d valid.\n", i);
     215            chip->valid = true;
     216            psArray *cells = chip->cells; // Component cells
     217            for (int j = 0; j < cells->n; j++) {
     218                pmCell *cell = cells->data[j]; // Cell of interest
     219                cell->valid = true;
     220            }
     221        } else {
     222            psTrace(__func__, 5, "Marking chip %d invalid.\n", i);
     223            chip->valid = false;
     224            psArray *cells = chip->cells; // Component cells
     225            for (int j = 0; j < cells->n; j++) {
     226                pmCell *cell = cells->data[j]; // Cell of interest
     227                cell->valid = false;
     228            }
     229        }
     230    }
     231    return true;
     232}
     233
     234int pmFPAExcludeChip(pmFPA *fpa, int chipNum)
     235{
     236    assert(fpa);
     237
     238    if (chipNum < 0 || chipNum > fpa->chips->n) {
     239        psLogMsg(__func__, PS_LOG_WARN, "Invalid chip number: %d\n", chipNum);
     240    }
     241    int numValid = 0;                   // Number of valid chips
     242    psArray *chips = fpa->chips;        // Component chips
     243    for (int i = 0; i < chips->n; i++) {
     244        pmChip *chip = chips->data[i];  // The chip of interest
     245        if (i == chipNum) {
     246            psTrace(__func__, 5, "Marking chip %d invalid.\n", i);
     247            chip->valid = false;
     248            psArray *cells = chip->cells; // Component cells
     249            for (int j = 0; j < cells->n; j++) {
     250                pmCell *cell = cells->data[j]; // Cell of interest
     251                cell->valid = false;
     252            }
     253        } else if (chip->valid) {
     254            numValid++;
     255        }
     256    }
     257
     258    psTrace(__func__, 5, "%d valid chips in fpa.\n", numValid);
     259    return numValid;
     260}
  • trunk/archive/scripts/src/phase2/pmFPA.h

    r4820 r5104  
    1010
    1111typedef struct {
     12    const char *extname;                // Extension name, if it corresponds to this level
     13    psArray *pixels;                    // The pixel data, if it corresponds to this level
     14    psMetadata *header;                 // The FITS header, if it corresponds to this level
     15} p_pmHDU;
     16
     17typedef struct {
    1218    // Astrometric transformations
    1319    psPlaneDistort* fromTangentPlane;   // Transformation from tangent plane to focal plane
     
    1521    psProjection *projection;           // Projection from tangent plane to sky
    1622    // Information
    17     psMetadata *values;                 // Important values (cached)
    18     const psMetadata *camera;           // Camera configuration
    19     psDB *db;                           // Database
     23    psMetadata *camera;                 // Camera configuration
     24    psMetadata *concepts;               // Values for concepts
     25    psMetadata *phu;                    // Primary Header
    2026    psArray *chips;                     // The chips
    21     // FITS data
    22     const char *extname;                // Extension name, if it corresponds to this level
    23     psArray *pixels;                    // The pixel data, if it corresponds to this level
    24     psMetadata *header;                 // The FITS header, if it corresponds to this level
     27    p_pmHDU *private;                   // FITS data
    2528} pmFPA;
    2629
     
    3336    psPlaneTransform* fromFPA;          // Transformation from FPA to chip coordinates
    3437    // Information
    35     psMetadata *values;                 // Important values (cached)
     38    psMetadata *concepts;               // Values for concepts
    3639    psArray *cells;                     // The cells (referred to by name)
    3740    pmFPA *parent;                      // Parent FPA
    38     // FITS data
    39     const char *extname;                // Extension name, if it corresponds to this level
    40     psArray *pixels;                    // The pixel data, if it corresponds to this level
    41     psMetadata *header;                 // The FITS header, if it corresponds to this level
     41    bool valid;                         // Valid for reading in?
     42    p_pmHDU *private;                   // FITS data
    4243} pmChip;
    4344
     
    5354    psPlaneTransform* toSky;            // Transformations from cell to tangent plane coordinates
    5455    // Information
    55     psMetadata *values;                 // Important values (cached)
     56    psMetadata *concepts;               // Values for concepts
     57    psMetadata *camera;                 // Camera information
    5658    psArray *readouts;                  // The readouts (referred to by number)
    5759    pmChip *parent;                     // Parent chip
    58     // FITS data
    59     const char *extname;                // Extension name, if it corresponds to this level
    60     psArray *pixels;                    // The pixel data, if it corresponds to this level
    61     psMetadata *header;                 // The FITS header, if it corresponds to this level
     60    bool valid;                         // Valid for operating on?
     61    p_pmHDU *private;                   // FITS data
    6262} pmCell;
    6363
     
    7373    // Information
    7474    psImage *image;                     // The pixels
    75     psList *overscans;                  // Array of subimages containing the overscan region(s)
    76     psMetadata *values;                 // readout-level metadata
     75    psImage *mask;                      // Mask image
     76    psList *overscans;                  // List of subimages containing the overscan regions
     77    psMetadata *concepts;               // Concepts for readouts
    7778} pmReadout;
    7879
    7980// Allocators and deallocators
    80 pmFPA *pmFPAAlloc(const psMetadata *camera, // Camera configuration
    81                   psDB *db              // Database
     81p_pmHDU *p_pmHDUAlloc(const char *extname);
     82void p_pmHDUFree(p_pmHDU *hdu);
     83pmFPA *pmFPAAlloc(const psMetadata *camera // Camera configuration
    8284    );
    8385void p_pmFPAFree(pmFPA *fpa);
     
    8991
    9092pmCell *pmCellAlloc(pmChip *chip,       // Chip to which the cell belongs
    91                     psMetadata *values, // Initial values for concepts
     93                    psMetadata *cameraData, // Camera data
    9294                    psString name       // Name of cell
    9395    );
     
    9597
    9698pmReadout *pmReadoutAlloc(pmCell *cell, // Cell to which the readout belongs
    97                           int readoutNum, // Number of the readout
    9899                          psImage *image, // The pixels
    99100                          psList *overscans, // The overscan images
     
    102103void p_pmReadoutFree(pmReadout *readout);
    103104
     105// Select and exclude chips
     106bool pmFPASelectChip(pmFPA *fpa, int chipNum);
     107int pmFPAExcludeChip(pmFPA *fpa, int chipNum);
     108
    104109#endif
  • trunk/archive/scripts/src/phase2/pmFPAConstruct.c

    r4820 r5104  
    3030        psError(PS_ERR_IO, false, "Unable to find specs for cell %s: ignored\n", cellName);
    3131    }
     32
     33#if 0
     34    // Need to create a new instance, so that each cell can work with its own
     35    psMetadata *copy = psMetadataAlloc();
     36    psMetadataIterator *iter = psMetadataIteratorAlloc(cellData, PS_LIST_HEAD, NULL); // Iterator
     37    psMetadataItem *item = NULL;        // Item from iteration
     38    while (item = psMetadataGetAndIncrement(iter)) {
     39        if (item->type == PS_META_MULTI || item->type == PS_META_META) {
     40            psLogMsg(__func__, PS_LOG_WARN, "PS_META_MULTI and PS_META_META are not supported in a cell "
     41                     "definition --- %s ignored.\n", item->name);
     42            continue;
     43        }
     44        if (! psMetadataAdd(copy, PS_LIST_TAIL, item->name, item->type, item->comment, item->data.V)) {
     45            psAbort(__func__, "Should never reach here!\n");
     46        }
     47    }
     48    psFree(iter);
     49
     50    return copy;
     51#else
    3252    return cellData;
     53#endif
     54
    3355}
    3456
     
    3860//////////////////////////////////////////////////////////////////////////////////////////////////////////////
    3961
    40 pmFPA *pmFPAConstruct(const psMetadata *camera, // The camera configuration
    41                       psDB *db          // Database handle
     62pmFPA *pmFPAConstruct(const psMetadata *camera // The camera configuration
    4263    )
    4364{
    44     pmFPA *fpa = pmFPAAlloc(camera, db); // The FPA to fill out
     65    pmFPA *fpa = pmFPAAlloc(camera); // The FPA to fill out
    4566    bool mdStatus = true;               // Status from metadata lookups
    4667    const char *phuType = psMetadataLookupString(&mdStatus, camera, "PHU"); // What is the PHU?
     
    6788                psString extName = contentItem->name; // The name of the extension
    6889                pmChip *chip = pmChipAlloc(fpa, extName); // The chip
    69                 chip->extname = extName;// Mark chip to receive FITS data
     90                chip->private = p_pmHDUAlloc(extName); // Prepare chip to receive FITS data
    7091                if (contentItem->type != PS_META_STR) {
    7192                    psLogMsg(__func__, PS_LOG_WARN, "Type of content item (%x) is not string: ignored\n",
     
    83104                    psMetadata *cellData = getCellData(camera, cellName);
    84105                    pmCell *cell = pmCellAlloc(chip, cellData, cellName); // The cell
     106//                  psFree(cellData);
    85107                }
    86108                psFree(cellNamesIter);
     
    102124                const char *content = contentItem->data.V; // The content of the extension
    103125                psList *contents = papSplit(content, ": "); // Split the name from the type
    104                 if (contents->size != 2) {
     126                if (contents->n != 2) {
    105127                    psLogMsg(__func__, PS_LOG_WARN, "Unable to read contents of %s: ignored.\n", extName);
    106128                } else {
     
    123145                    psMetadata *cellData = getCellData(camera, cellType);
    124146                    pmCell *cell = pmCellAlloc(chip, cellData, extName); // The cell
    125                     cell->extname = extName; // Mark cell to receive FITS data
     147//                  psFree(cellData);
     148                    cell->private = p_pmHDUAlloc(extName); // Prepare cell to receive FITS data
    126149
    127150                    psFree(chip);
    128151                    psFree(cell);
    129                     psFree(chipName);
    130                     psFree(cellType);
    131152                }
    132153                psFree(contents);
     
    136157        } else if (strcasecmp(extType, "NONE") == 0) {
    137158            // No extensions; Content contains metadata, each entry is a chip with its component cells
    138             fpa->extname = "PHU";
     159            fpa->private = p_pmHDUAlloc("PHU"); // Prepare FPA to receive FITS data
    139160            while (contentItem = psMetadataGetAndIncrement(contentsIter)) {
    140161                psString chipName = contentItem->name; // The name of the chip
     
    155176                    psMetadata *cellData = getCellData(camera, cellName);
    156177                    pmCell *cell = pmCellAlloc(chip, cellData, cellName); // The cell
     178//                  psFree(cellData);
    157179                }
    158180                psFree(cellNamesIter);
     
    174196        if (strcasecmp(extType, "NONE") == 0) {
    175197            // There are no extensions --- only the PHU
    176             chip->extname = "PHU";
     198            chip->private = p_pmHDUAlloc("PHU");
    177199
    178200            const char *contents = psMetadataLookupString(&mdStatus, camera, "CONTENTS");
     
    188210                psMetadata *cellData = getCellData(camera, cellName);
    189211                pmCell *cell = pmCellAlloc(chip, cellData, cellName); // The cell
     212//              psFree(cellData);
    190213            }
    191214            psFree(cellIter);
     
    223246                psMetadata *cellData = getCellData(camera, cellType);
    224247                pmCell *cell = pmCellAlloc(chip, cellData, extName); // The cell
    225                 cell->extname = extName; // Mark cell to receive FITS data
     248//              psFree(cellData);
     249                cell->private = p_pmHDUAlloc(extName); // Prepare cell to receive FITS data
    226250            } // Iterating through contents
    227251            psFree(contentsIter);
     
    249273{
    250274    psTrace(__func__, 0, "FPA:\n");
    251     if (fpa->extname) {
    252         psTrace(__func__, 1, "---> FPA is extension %s.\n", fpa->extname);
    253         if (! fpa->pixels) {
    254             psTrace(__func__, 1, "---> NO PIXELS for extension %s\n", fpa->extname);
     275    if (fpa->private) {
     276        psTrace(__func__, 1, "---> FPA is extension %s.\n", fpa->private->extname);
     277        if (! fpa->private->pixels) {
     278            psTrace(__func__, 1, "---> NO PIXELS for extension %s\n", fpa->private->extname);
    255279        }
    256280    }
     
    261285        psTrace(__func__, 1, "Chip: %d\n", i);
    262286        pmChip *chip = chips->data[i]; // The chip
    263         if (chip->extname) {
    264             psTrace(__func__, 2, "---> Chip is extension %s.\n", chip->extname);
    265             if (! chip->pixels) {
    266                 psTrace(__func__, 2, "---> NO PIXELS for extension %s\n", chip->extname);
     287        if (chip->private) {
     288            psTrace(__func__, 2, "---> Chip is extension %s.\n", chip->private->extname);
     289            if (! chip->private->pixels) {
     290                psTrace(__func__, 2, "---> NO PIXELS for extension %s\n", chip->private->extname);
    267291            }
    268292        }
     
    272296            psTrace(__func__, 2, "Cell: %d\n", j);
    273297            pmCell *cell = cells->data[j]; // The cell
    274             if (cell->extname) {
    275                 psTrace(__func__, 3, "---> Cell is extension %s.\n", cell->extname);
    276                 if (! cell->pixels) {
    277                     psTrace(__func__, 3, "---> NO PIXELS for extension %s\n", cell->extname);
    278                 }
    279             }
    280             psMetadataPrint(cell->values, 3);
     298            if (cell->private) {
     299                psTrace(__func__, 3, "---> Cell is extension %s.\n", cell->private->extname);
     300                if (! cell->private->pixels) {
     301                    psTrace(__func__, 3, "---> NO PIXELS for extension %s\n", cell->private->extname);
     302                }
     303            }
     304            psMetadataPrint(cell->concepts, 3);
    281305            psTrace(__func__, 3, "Readouts:\n");
    282306            psArray *readouts = cell->readouts; // Array of readouts
  • trunk/archive/scripts/src/phase2/pmFPAConstruct.h

    r4820 r5104  
    66
    77// Read the contents of a FITS file (format specified by the camera configuration) into memory
    8 pmFPA *pmFPAConstruct(const psMetadata *camera, // The camera configuration
    9                       psDB *db          // Database handle
     8pmFPA *pmFPAConstruct(const psMetadata *camera // The camera configuration
    109    );
    1110
  • trunk/archive/scripts/src/phase2/psAdditionals.c

    r4793 r5104  
    103103            break;
    104104          default:
     105            printf("\n");
    105106            psError(PS_ERR_IO, false, "Non-printable metadata type: %x\n", item->type);
    106107        }
     
    262263                // Go through the MULTI
    263264                psList *multi = argItem->data.V; // The list of MULTI psMetadataItems
    264                 if (*argc < i + multi->size) {
     265                if (*argc < i + multi->n) {
    265266                    psError(PS_ERR_IO, true, "Not enough arguments for %s.\n", argItem->name);
    266267                    // Remove the arguments --- they will be ignored
  • trunk/archive/scripts/src/phase2/psAdditionals.h

    r4793 r5104  
    33
    44#include "pslib.h"
    5 
    6 typedef char* psString;
    75
    86// Get a value from the metadata that we believe should be metadata.
Note: See TracChangeset for help on using the changeset viewer.