IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 28280 for trunk/ppBackground


Ignore:
Timestamp:
Jun 9, 2010, 6:16:39 PM (16 years ago)
Author:
Paul Price
Message:

Coding more or less done. Need to add ppStats call. Compiles.

Location:
trunk/ppBackground
Files:
10 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/ppBackground

    • Property svn:ignore set to
      configure
      Makefile.in
      ignore
      config.log
      depcomp
      config.status
      config.guess
      ltmain.sh
      config.sub
      autom4te.cache
      libtool
      missing
      Makefile
      aclocal.m4
      install-sh
  • trunk/ppBackground/configure.ac

    r28276 r28280  
    2020PKG_CHECK_MODULES([PSLIB], [pslib >= 1.0.0])
    2121PKG_CHECK_MODULES([PSMODULE], [psmodules >= 1.0.0])
    22 PKG_CHECK_MODULES([PSPHOT], [psphot >= 1.0.0])
     22PKG_CHECK_MODULES([PSPHOT], [psphot])
     23
     24AC_PATH_PROG([ERRORCODES], [psParseErrorCodes], [missing])
     25if test "$ERRORCODES" = "missing" ; then
     26  AC_MSG_ERROR([psParseErrorCodes is required])
     27fi
    2328
    2429dnl Set CFLAGS for build
  • trunk/ppBackground/src

    • Property svn:ignore set to
      ppBackground
      Makefile.in
      config.h
      ppBackgroundVersionDefinitions.h
      .deps
      Makefile
      ppBackgroundErrorCodes.c
      ppBackgroundErrorCodes.h
      stamp-h1
      config.h.in
  • trunk/ppBackground/src/Makefile.am

    r28276 r28280  
    22
    33if HAVE_SVNVERSION
    4 PPVIZPATTERN_VERSION=`$(SVNVERSION) ..`
     4PPBACKGROUND_VERSION=`$(SVNVERSION) ..`
    55else
    6 PPVIZPATTERN_VERSION="UNKNOWN"
     6PPBACKGROUND_VERSION="UNKNOWN"
    77endif
    88
    99if HAVE_SVN
    10 PPVIZPATTERN_BRANCH=`$(SVN) info .. | $(SED) -n -e '/URL:/ h' -e '/Repository Root:/ { x; H; x; s|Repository Root: \(.*\)\nURL: \1\(.*\)|\2| ; s|^/|| ; s|/[a-zA-Z]*/src.*|| ; p }'`
    11 PPVIZPATTERN_SOURCE=`$(SVN) info | $(SED) -n -e 's/Repository UUID: // p'`
     10PPBACKGROUND_BRANCH=`$(SVN) info .. | $(SED) -n -e '/URL:/ h' -e '/Repository Root:/ { x; H; x; s|Repository Root: \(.*\)\nURL: \1\(.*\)|\2| ; s|^/|| ; s|/[a-zA-Z]*/src.*|| ; p }'`
     11PPBACKGROUND_SOURCE=`$(SVN) info | $(SED) -n -e 's/Repository UUID: // p'`
    1212else
    13 PPVIZPATTERN_BRANCH="UNKNOWN"
    14 PPVIZPATTERN_SOURCE="UNKNOWN"
     13PPBACKGROUND_BRANCH="UNKNOWN"
     14PPBACKGROUND_SOURCE="UNKNOWN"
    1515endif
    1616
     
    1919ppBackgroundVersionDefinitions.h: ppBackgroundVersionDefinitions.h.in FORCE
    2020        -$(RM) ppBackgroundVersionDefinitions.h
    21         $(SED) -e "s|@PPVIZPATTERN_VERSION@|\"$(PPVIZPATTERN_VERSION)\"|" -e "s|@PPVIZPATTERN_BRANCH@|\"$(PPVIZPATTERN_BRANCH)\"|" -e "s|@PPVIZPATTERN_SOURCE@|\"$(PPVIZPATTERN_SOURCE)\"|" ppBackgroundVersionDefinitions.h.in > ppBackgroundVersionDefinitions.h
     21        $(SED) -e "s|@PPBACKGROUND_VERSION@|\"$(PPBACKGROUND_VERSION)\"|" -e "s|@PPBACKGROUND_BRANCH@|\"$(PPBACKGROUND_BRANCH)\"|" -e "s|@PPBACKGROUND_SOURCE@|\"$(PPBACKGROUND_SOURCE)\"|" ppBackgroundVersionDefinitions.h.in > ppBackgroundVersionDefinitions.h
    2222FORCE: ;
    2323
     
    3535        ppBackgroundLoop.c      \
    3636        ppBackgroundVersion.c   \
    37         ppBackgroundModel.c     \
    38         ppBackgroundErrorCodes.c
     37        ppBackgroundRestore.c   \
     38        ppBackgroundErrorCodes.c        \
     39        ppBackgroundExit.c
    3940
    4041noinst_HEADERS = \
  • trunk/ppBackground/src/ppBackground.c

    r28276 r28280  
    66#include <pslib.h>
    77#include <psmodules.h>
     8#include <psphot.h>
    89
    910#include "ppBackground.h"
     
    1213{
    1314    ppBackgroundVersionPrint();
     15
     16    pmErrorRegister();
     17    psphotErrorRegister();
     18    ppBackgroundErrorRegister();
    1419
    1520    ppBackgroundData *data = ppBackgroundDataInit(&argc, argv);
     
    4651        }
    4752        data->statsFile = NULL;
    48         pmConfigRunFilenameAddWrite(data->config, "STATS", data->statsName);
    4953        exitValue = ppBackgroundExitCode(exitValue);
    5054    }
  • trunk/ppBackground/src/ppBackground.h

    r28276 r28280  
    55#include <psmodules.h>
    66
    7 #include <ppBackgroundErrorCodes.h>
     7#include "ppBackgroundErrorCodes.h"
    88
    99#define PPBACKGROUND_RECIPE "PPBACKGROUND"      // Recipe name
     
    3737    );
    3838
     39/// Restore the background to an image
     40bool ppBackgroundRestore(
     41    pmChip *chip,                       // Input chip
     42    const pmChip *background,           // Chip with background model
     43    const pmChip *pattern,              // Chip with pattern
     44    const pmFPAview *view,              // View to data
     45    pmConfig *config                    // Configuration
     46    );
     47
    3948/// Determine exit code
    4049psExit ppBackgroundExitCode(
  • trunk/ppBackground/src/ppBackgroundArguments.c

    r28276 r28280  
    4848    psMetadataAddStr(arguments, PS_LIST_TAIL, "-pattern", 0, "Filename of pattern correction", NULL);
    4949    psMetadataAddStr(arguments, PS_LIST_TAIL, "-background", 0, "Filename of background model", NULL);
    50     psMetadataAddStr(arguments, PS_LIST_TAIL, "-image", 0, "Filename of image", NULL);
     50    psMetadataAddStr(arguments, PS_LIST_TAIL, "-image", 0, "Filename of image (required)", NULL);
    5151    psMetadataAddStr(arguments, PS_LIST_TAIL, "-mask", 0, "Filename of mask", NULL);
     52    psMetadataAddStr(arguments, PS_LIST_TAIL, "-stats", 0, "Output statistics file", NULL);
    5253    if (argc == 1 || !psArgumentParse(arguments, &argc, argv) || argc != 2) {
    5354        usage(argv[0], arguments, data);
     
    6869        return false;
    6970    }
     71    if (!data->imageName) {
     72        psError(PPBACKGROUND_ERR_CONFIG, true, "Must specify -image");
     73        return false;
     74    }
    7075
    7176    PS_ASSERT_STRING_NON_EMPTY(data->outRoot, false);
     77
     78    const char *statsName = psMetadataLookupStr(NULL, arguments, "-stats");
     79    if (statsName) {
     80        data->statsFile = fopen(statsName, "w");
     81        if (!data->statsFile) {
     82            psError(PPBACKGROUND_ERR_IO, true, "Unable to open statistics file %s", statsName);
     83            return false;
     84        }
     85        data->stats = psMetadataAlloc();
     86        pmConfigRunFilenameAddWrite(data->config, "STATS", statsName);
     87    }
    7288
    7389    return true;
  • trunk/ppBackground/src/ppBackgroundCamera.c

    r28276 r28280  
    3333
    3434    fileArguments("PATTERN", data->patternName, "Input pattern", data->config);
    35     pmFPAfile *pattern = pmFPAfileDefineFromArgs(&status, data->config, "PPBACKGROUND.PATTERN",
    36                                                  "PATTERN"); // File
     35    pmFPAfileDefineFromArgs(&status, data->config, "PPBACKGROUND.PATTERN", "PATTERN"); // File
    3736    if (!status) {
    3837        psError(psErrorCodeLast(), false, "Failed to build file from PPBACKGROUND.PATTERN");
     
    4140
    4241    fileArguments("BACKGROUND", data->backgroundName, "Input background model", data->config);
    43     pmFPAfile *background = pmFPAfileBindFromArgs(&status, data->config, "PPBACKGROUND.BACKGROUND",
    44                                                   "BACKGROUND"); // File
     42    pmFPAfileDefineFromArgs(&status, data->config, "PPBACKGROUND.BACKGROUND", "BACKGROUND"); // File
    4543    if (!status) {
    4644        psError(psErrorCodeLast(), false, "Failed to build file from PPBACKGROUND.BACKGROUND");
     
    4947
    5048    fileArguments("IMAGE", data->backgroundName, "Input uncorrected image", data->config);
    51     pmFPAfile *image = pmFPAfileBindFromArgs(&status, background, data->config, "PPBACKGROUND.IMAGE",
    52                                              "IMAGE"); // File
    53     if (!status) {
     49    pmFPAfile *image = pmFPAfileDefineFromArgs(&status, data->config, "PPBACKGROUND.IMAGE",
     50                                               "IMAGE"); // File
     51    if (!status || !image) {
    5452        psError(psErrorCodeLast(), false, "Failed to build file from PPBACKGROUND.IMAGE");
    5553        return false;
     
    5755
    5856    fileArguments("MASK", data->backgroundName, "Input uncorrected mask", data->config);
    59     pmFPAfile *mask = pmFPAfileBindFromArgs(&status, image, data->config, "PPBACKGROUND.MASK",
    60                                               "MASK"); // File
     57    pmFPAfileBindFromArgs(&status, image, data->config, "PPBACKGROUND.MASK", "MASK"); // File
    6158    if (!status) {
    6259        psError(psErrorCodeLast(), false, "Failed to build file from PPBACKGROUND.MASK");
     
    6461    }
    6562
    66     pmFPAfile *output = pmFPAfileDefineOutput(data->config, file->fpa, "PPBACKGROUND.OUTPUT");
     63    pmFPAfile *output = pmFPAfileDefineOutput(data->config, image->fpa, "PPBACKGROUND.OUTPUT");
    6764    if (!output) {
    6865        psError(psErrorCodeLast(), false, "Unable to define output.");
  • trunk/ppBackground/src/ppBackgroundLoop.c

    r28276 r28280  
    1313{
    1414    pmConfig *config = data->config;                                        // Configuration data
    15     pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PPBACKGROUND.BACKGROUND", 0);
     15    pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PPBACKGROUND.IMAGE", 0);
    1616    pmFPAfile *patternFile = pmFPAfileSelectSingle(config->files, "PPBACKGROUND.PATTERN", 0); // File with data
     17    pmFPAfile *bgFile = pmFPAfileSelectSingle(config->files, "PPBACKGROUND.BACKGROUND", 0);   // File with bg
    1718    pmFPA *patternMosaic = pmFPAConstruct(file->camera, file->cameraName); // Mosaicked FPA for pattern
    1819
     
    3435        }
    3536
    36         if (chip->cell->n > 1) {
     37        if (chip->cells->n > 1) {
    3738            psError(PPBACKGROUND_ERR_CONFIG, true,
    3839                    "Input image and background model should be chip-mosaicked");
     
    4445            pmFPAfileActivate(config->files, true, "PPBACKGROUND.PATTERN");
    4546
    46             pmCell *cell;               // Cell with pattern data
     47            pmCell *patternCell;               // Cell with pattern data
    4748            while ((patternCell = pmFPAviewNextCell(view, patternFile->fpa, 1))) {
    48                 if (!cell->process || !cell->file_exists) {
     49                if (!patternCell->process || !patternCell->file_exists) {
    4950                    continue;
    5051                }
     
    6869                    // Get size of image from concepts
    6970                    if (numCols == 0 || numRows == 0) {
    70                         numCols = psMetadataLookupS32(NULL, cell->concepts, "CELL.XSIZE");
    71                         numRows = psMetadataLookupS32(NULL, cell->concepts, "CELL.YSIZE");
     71                        numCols = psMetadataLookupS32(NULL, patternCell->concepts, "CELL.XSIZE");
     72                        numRows = psMetadataLookupS32(NULL, patternCell->concepts, "CELL.YSIZE");
    7273                    }
    7374                    // Get size of image from TRIMSEC
    7475                    if (numCols == 0 || numRows == 0) {
    75                         psRegion *trimsec = psMetadataLookupPtr(NULL, cell->concepts, "CELL.TRIMSEC");
     76                        psRegion *trimsec = psMetadataLookupPtr(NULL, patternCell->concepts, "CELL.TRIMSEC");
    7677                        numCols = trimsec->x1 - trimsec->x0;
    7778                        numRows = trimsec->y1 - trimsec->y0;
     
    9192
    9293                    // Remove bias sections to avoid warnings
    93                     psMetadataItem *biassec = psMetadataLookup(patternRO->parent->concepts, "CELL.BIASSEC");
     94                    psMetadataItem *biassec = psMetadataLookup(patternCell->concepts, "CELL.BIASSEC");
    9495                    if (psListLength(biassec->data.V)) {
    9596                        psFree(biassec->data.V);
     
    123124        }
    124125
     126        pmChip *patternChip = patternFile ? pmFPAviewThisChip(view, patternMosaic) : NULL; // Chip with pattern
     127        pmChip *bgChip = bgFile ? pmFPAviewThisChip(view, bgFile->fpa) : NULL; // Chip with background model
     128        if (!ppBackgroundRestore(chip, bgChip, patternChip, view, config)) {
     129            psError(psErrorCodeLast(), false, "Unable to replace background");
     130            return false;
     131        }
     132        pmChipFreeData(patternChip);
    125133
    126 
    127 
    128 
    129 
    130 
    131                 pmHDU *hdu = pmHDUGetLowest(file->fpa, chip, cell); // HDU for readout
    132                 if (!hdu) {
    133                     psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find HDU for data.");
    134                     return false;
    135                 }
    136                 if (!hdu->header) {
    137                     hdu->header = psMetadataAlloc();
    138                 }
    139                 ppBackgroundVersionHeader(hdu->header);
    140 
    141 
    142 
    143 
    144 
    145 
    146 
     134        pmHDU *hdu = pmHDUGetLowest(file->fpa, chip, NULL); // HDU for chip
     135        if (!hdu) {
     136            psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find HDU for data.");
     137            return false;
     138        }
     139        if (!hdu->header) {
     140            hdu->header = psMetadataAlloc();
     141        }
     142        ppBackgroundVersionHeader(hdu->header);
    147143
    148144        // Chip
     
    158154    }
    159155
     156    psFree(view);
     157    psFree(patternMosaic);
     158
    160159    return true;
    161160}
  • trunk/ppBackground/src/ppBackgroundRestore.c

    r28279 r28280  
    44#include <psphot.h>
    55
     6#include "ppBackground.h"
    67
    7 bool ppBackgroundModel(
    8         pmReadout *ro = cell->readouts->data[0]; // Readout of interest
    9         if (!ro || !ro->data_exists) {
    10             psError(PS_ERR_UNKNOWN, true, "readout has no data");
     8bool ppBackgroundRestore(pmChip *chip, const pmChip *background, const pmChip *pattern,
     9                         const pmFPAview *oldView, pmConfig *config)
     10{
     11    PS_ASSERT_PTR_NON_NULL(chip, false);
     12    PS_ASSERT_PTR_NON_NULL(oldView, false);
     13
     14    pmFPAview *view = pmFPAviewAlloc(0); // View to readout
     15    *view = *oldView;
     16    view->cell = 0;
     17    view->readout = 0;
     18
     19    pmReadout *ro = pmFPAviewThisReadout(view, chip->parent);
     20    if (!ro || !ro->data_exists) {
     21        psError(PPBACKGROUND_ERR_CONFIG, true, "Readout has no data");
     22        return false;
     23    }
     24    psImage *image = ro->image;                             // Image to correct
     25    int numCols = image->numCols, numRows = image->numRows; // Size of image
     26
     27    if (background) {
     28        pmReadout *bgRO = pmFPAviewThisReadout(view, background->parent); // Readout with background
     29        psImageBinning *binning = psphotBackgroundBinning(bgRO->image, config);
     30        if (!binning) {
     31            psError(psErrorCodeLast(), false, "Unable to find background binning");
    1132            return false;
    1233        }
    13         psImage *image = ro->image;
    14 //        psImage *mask = NULL;
    15 //        psImageMaskType maskVal = 0;
    16 
    17         pmFPAview roView = *view;
    18         roView.cell = 0;
    19         roView.readout = 0;
    20         pmReadout *modelRO = pmFPAviewThisReadout(&roView, backmdl->fpa);
    21         if (!modelRO || !modelRO->data_exists) {
    22             psError(PS_ERR_UNKNOWN, true, "model readout has no data");
     34        if (binning->nXfine != numCols || binning->nYfine != numRows) {
     35            psError(PPBACKGROUND_ERR_CONFIG, true,
     36                    "Unbinned background model and input don't match (%dx%d vs %dx%d)",
     37                    binning->nXfine, binning->nYfine, numCols, numRows);
    2338            return false;
    2439        }
    25         output->save = true;
    26         background->save = save_background;
    2740
    28         psImageBinning *binning = psphotBackgroundBinning(image, config);
    29         if (!binning) {
    30             psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find background binning");
    31             return false;
    32         }
    33         pmReadout *backgroundRO;
    34 #define USE_CELL
    35 #ifdef USE_CELL
    36         pmCell *backgroundCell = pmFPAviewThisCell(&roView, background->fpa);
    37         backgroundRO = pmReadoutAlloc(backgroundCell);
    38 #else
    39         backgroundRO = pmFPAviewThisReadout(&roView, background->fpa);
    40 //        backgroundRO = pmFPAfileDefineInternal (config->files, "PSPHOT.BACKGND", image->numCols, image->numRows, PS_TYPE_F32);
    41 #endif
    42         backgroundRO->image = psImageAlloc(image->numCols, image->numRows, PS_TYPE_F32);
    43         backgroundRO->data_exists = true;
    44         backgroundRO->parent->data_exists = true;
    45         backgroundRO->parent->parent->data_exists = true;
    46 
    47         // linear interpolation to full-scale
    48         if (!psImageUnbin (backgroundRO->image, modelRO->image, binning)) {
    49             psError (PSPHOT_ERR_PROG, true, "inconsistent sizes for unbinning");
     41        psImage *bgImage = psImageAlloc(numCols, numRows, PS_TYPE_F32); // Background
     42        if (!psImageUnbin(bgImage, bgRO->image, binning)) {
     43            psError(psErrorCodeLast(), false, "Unable to unbin background model");
    5044            psFree(binning);
    5145            return false;
    5246        }
    5347        psFree(binning);
    54         psF32 **backData = backgroundRO->image->data.F32;
    5548
    56         // Undo the background subtraction
    57         int numCols = image->numCols, numRows = image->numRows; // Size of image
    58         long nancount = 0;
    59         long naninputs = 0;
    60         long finitecount = 0;
    6149        for (int y = 0; y < numRows; y++) {
    6250            for (int x = 0; x < numCols; x++) {
    63                 float imageval =  image->data.F32[y][x] ;
    64                 float bgvalue = backData[y][x];
    65                 if (!isfinite(bgvalue)) {
    66                     // if background is NAN leave the image unchanged
    67                     // According to ppImageSubtractBackground pixels where the background is not finite should be NAN
    68                     // experimentally
    69                     // for breakpoint
    70                     nancount++;
    71                     if (isfinite(imageval)) {
    72                          finitecount++;
    73                          //   psWarning("unexpected finite image value %f found at %d %d background is infinite", imageval, x, y);
    74                     }
    75                     //      Doing this is causes a lot of new NAN pixels to appear.
    76                     //      NANS are appearing in the model for some reason.
    77                     //      image->data.F32[y][x] = NAN;
    78                 } else if (isfinite(imageval)) {
    79                     // XXX: TODO:following ppImage we should not modify pixels that are masked.
    80                     image->data.F32[y][x] = imageval + bgvalue;
    81                 } else {
    82                     naninputs++;
    83                 }
     51                image->data.F32[y][x] += bgImage->data.F32[y][x];
    8452            }
    8553        }
    86         if (naninputs) {
    87             printf("%ld infinite pixels found in input image\n", naninputs);
     54        psFree(bgImage);
     55    }
     56
     57    if (pattern) {
     58        pmReadout *patternRO = pmFPAviewThisReadout(view, pattern->parent); // Readout with pattern
     59        psImage *patternImage = patternRO->image; // Image with pattern
     60        for (int y = 0; y < numRows; y++) {
     61            for (int x = 0; x < numCols; x++) {
     62                image->data.F32[y][x] += patternImage->data.F32[y][x];
     63            }
    8864        }
    89         if (nancount) {
    90             psWarning("%ld infinite pixels found in background %ld pixels were finite\n", nancount, finitecount);
    91         }
    92 //        pmFPAfileDropInternal(config->files, "PSPHOT.BACKGND");
     65    }
     66
     67    return true;
     68}
     69
     70
     71
  • trunk/ppBackground/src/ppBackgroundVersionDefinitions.h.in

    r28276 r28280  
    1 #ifndef PPVIZPATTERN_VERSION_DEFINITIONS_H
    2 #define PPVIZPATTERN_VERSION_DEFINITIONS_H
     1#ifndef PPBACKGROUND_VERSION_DEFINITIONS_H
     2#define PPBACKGROUND_VERSION_DEFINITIONS_H
    33
    4 #define PPVIZPATTERN_VERSION @PPVIZPATTERN_VERSION@ // SVN version
    5 #define PPVIZPATTERN_BRANCH  @PPVIZPATTERN_BRANCH@  // SVN branch
    6 #define PPVIZPATTERN_SOURCE  @PPVIZPATTERN_SOURCE@  // SVN source
     4#define PPBACKGROUND_VERSION @PPBACKGROUND_VERSION@ // SVN version
     5#define PPBACKGROUND_BRANCH  @PPBACKGROUND_BRANCH@  // SVN branch
     6#define PPBACKGROUND_SOURCE  @PPBACKGROUND_SOURCE@  // SVN source
    77
    88#endif
Note: See TracChangeset for help on using the changeset viewer.