IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 27, 2014, 4:21:00 PM (12 years ago)
Author:
watersc1
Message:

Working with metadata structures is far easier than pmFPAfiles. First cleanly running-to-completion version. Now to sort out the resolution issues.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ppBackground/src/ppBackgroundStackLoop.c

    r36582 r36615  
    2020      // If we didn't load the OTA solution from an external source, we need to build one.
    2121      if (data->fit_OTAS) {
     22        printf("Fitting OTAs!\n");
    2223        if (!ppBackgroundStackModelFitOTASolution(data)) {
    2324          // Currently can't fail.
     
    2526          return(false);
    2627        }
    27       }
     28
     29        if (data->OTApath) {
     30          // This will write the solutions out.
     31          psMetadataIterator *iter = psMetadataIteratorAlloc(data->OTA_solutions, PS_LIST_HEAD, NULL); // Iterator
     32          psMetadataItem *item; // Item from iteration
     33          int i = 0;
     34          while ((item = psMetadataGetAndIncrement(iter))) {
     35            i++;
     36            if (item->type != PS_DATA_IMAGE) {
     37              psString chipName = item->name;
     38              psImage  *image    = item->data.V;
     39              psMetadata *header = psMetadataAlloc();
     40              psString solutionFileName = psStringCopy(data->OTApath);
     41              psStringAppend(&solutionFileName, ".%s.fits",chipName);
     42              psString resolved = pmConfigConvertFilename(solutionFileName,config,true,true);
     43              psFits *solutionFits = psFitsOpen(resolved,"w");
     44              if (!solutionFits) {
     45                psError(2, false, "Unable to open FITS file %s to write model %s.", resolved, chipName);
     46                psFitsClose(solutionFits);
     47                psFree(resolved);
     48                return(false);
     49              }
     50              if (!psFitsWriteImage(solutionFits, header, image, 0, NULL)) {
     51                psError(2, false, "Unable to write FITS image %s.", resolved);
     52                psFitsClose(solutionFits);
     53                psFree(resolved);
     54                return false;
     55              }
     56              if (!psFitsClose(solutionFits)) {
     57                psError(2, false, "Unable to close FITS image %s.", resolved);
     58                psFree(resolved);
     59                return false;
     60              }
     61              psFree(resolved);
     62              psFree(solutionFileName);
     63            }
     64          }
     65          psFree(item);
     66          psFree(iter);
     67        }
     68       
     69      }
     70     
    2871     
    2972      // This seems wrong, but I need a blank modelMap object, so we fit the zero-data we've stored in the calib objects
     73      printf("Determining blank modelMap!\n");
    3074      if (!ppBackgroundStackModelFit(data)) {
    3175        psError(psErrorCodeLast(), false, "Error determining the blank modelMap object.");
     
    3478     
    3579      // Apply OTA solution
     80      printf("Calib apply!\n");
    3681      if (!ppBackgroundStackCalibApply(data)) {
    3782        psError(psErrorCodeLast(), false, "Error applying the calibration models.");
     
    4388    {
    4489      // Construct the offset information
     90      printf("Model fit!\n");
    4591      if (!ppBackgroundStackDataModelFit(data)) {
    4692        psError(psErrorCodeLast(), false, "Error determining the exposure/OTA scaling.");
     
    4995     
    5096      // Apply full correction
     97      printf("Calib apply!\n");
    5198      if (!ppBackgroundStackCalibApply(data)) {
    5299        psError(psErrorCodeLast(), false, "Error applying the calibration models.");
     
    55102     
    56103      // Fit the new model
     104      printf("Determining model!\n");
    57105      if (!ppBackgroundStackModelFit(data)) {
    58106        psError(psErrorCodeLast(), false, "Error determining the modelMap object.");
     
    66114      pmFPAfile *stack = data->stack_data->data[i];
    67115      pmFPAview *view = pmFPAviewAlloc(0);
    68       // Define output image
    69       pmFPAfile *stack_model = pmFPAfileDefineOutput(config,stack->fpa,"PPBACKGROUND.STACK.MODEL");
    70 
     116
     117      // Define output image.  Why is this always so hard to do?
     118      pmFPA *tmp_fpa1,*tmp_fpa2;
     119      tmp_fpa1 = pmFPAConstruct(config->camera,config->cameraName);
     120      tmp_fpa2 = pmFPAConstruct(config->camera,config->cameraName);
     121
     122      pmFPAfile *stack_model = pmFPAfileDefineOutput(config,tmp_fpa1,"PPBACKGROUND.STACK.MODEL");
     123     
    71124      if (!stack_model) {
    72125        psError(psErrorCodeLast(), false, "Unable to generate output model");
     
    74127      }
    75128     
    76       pmFPAfile *stack_corr  = pmFPAfileDefineOutput(config,stack->fpa,"PPBACKGROUND.STACK.OUTPUT");
     129      pmFPAfile *stack_corr  = pmFPAfileDefineOutput(config,tmp_fpa2,"PPBACKGROUND.STACK.OUTPUT");
    77130      if (!stack_corr) {
    78131        psError(psErrorCodeLast(), false, "Unable to generate output result");
     
    82135      stack_corr->save = true;
    83136
     137      printf("I'm about to loop over the parts of this stack: %d\n",i);
    84138      // Iterate over the images.
     139      pmFPAfileActivate(config->files,true,"PPBACKGROUND.STACK");
     140      pmFPAfileActivate(config->files,true,"PPBACKGROUND.STACK.MODEL");
     141      pmFPAfileActivate(config->files,true,"PPBACKGROUND.STACK.OUTPUT");
     142      if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
     143        psError(psErrorCodeLast(), false, "load failure for Chip");
     144        return(false);
     145      }
     146
     147     
    85148      pmChip *chip;
    86149      while ((chip = pmFPAviewNextChip(view, stack->fpa, 1))) {
     
    93156          return(false);
    94157        }
    95 
     158        printf("  I'm in a chip\n");
    96159        pmCell *cell;
    97160       
     
    109172            continue;
    110173          }
     174          printf("    I'm in a cell\n");
     175
    111176
    112177          // process each of the readouts
     
    120185              continue;
    121186            }
    122 
    123             pmReadout *model = pmFPAfileThisReadout(config->files,view,"PPBACKGROUND.STACK.MODEL");
    124             pmReadout *corr  = pmFPAfileThisReadout(config->files,view,"PPBACKGROUND.STACK.OUTPUT");
    125 
     187            printf("      I'm in a readout\n");
     188
     189            // Futz with things to get an acceptable output product.
     190            pmCell *model_cell = pmFPAviewThisCell(view,stack_model->fpa);
     191            pmCell *corr_cell  = pmFPAviewThisCell(view,stack_corr->fpa);
     192           
     193            pmReadout *model = pmReadoutAlloc(model_cell);
     194            pmReadout *corr  = pmReadoutAlloc(corr_cell);
     195            model->data_exists = true;
     196            corr->data_exists = true;
     197            model->parent->data_exists = true;
     198            corr->parent->data_exists = true;
     199            model->parent->parent->data_exists = true;
     200            corr->parent->parent->data_exists = true;
     201            model->image = psImageAlloc(readout->image->numCols,readout->image->numRows,PS_TYPE_F32);
     202            corr->image = psImageAlloc(readout->image->numCols,readout->image->numRows,PS_TYPE_F32);
     203
     204            model_cell->concepts = psMemIncrRefCounter(cell->concepts);
     205            model_cell->conceptsRead = cell->conceptsRead;
     206            corr_cell->concepts = psMemIncrRefCounter(cell->concepts);
     207            corr_cell->conceptsRead = cell->conceptsRead;
     208           
    126209            psPlane *pix = psPlaneAlloc();   // Pixel coordinates on chip
    127210            psPlane *fp = psPlaneAlloc();    // Focal plane coordinates
     
    130213            int x,y;
    131214            for (y = 0; y < readout->image->numRows; y++) {
     215              pix->y = y;
    132216              for (x = 0; x < readout->image->numCols; x++) {
     217                pix->x = x;
    133218                // Calculate model for each pixel of output
    134                 psPlaneTransformApply(fp, chip->toFPA, pix);
    135                 psPlaneTransformApply(tp, stack->fpa->toTPA, fp);
     219                //              psPlaneTransformApply(fp, chip->toFPA, pix);
     220                psPlaneTransformApply(tp, stack->fpa->toTPA, pix);
    136221               
    137222                model->image->data.F32[y][x] = psImageMapEval(data->modelMap,tp->x,tp->y);
    138223                corr->image->data.F32[y][x] = readout->image->data.F32[y][x] + model->image->data.F32[y][x];
     224               
    139225              }
    140226            }
     
    143229            psFree(tp);
    144230          } // Close readout
    145 
     231          printf("    I'm done with that readout\n");
    146232          // Close output image
    147           // Close cells (XXX shouldn't pmFPAfileClose iterate down as needed?)
    148           view->cell = -1;
    149           while ((cell = pmFPAviewNextCell(view, stack->fpa, 1)) != NULL) {
    150             if (!cell->process || !cell->file_exists) {
    151               continue;
    152             }
    153             if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
    154                 psError(psErrorCodeLast(), false, "save failure for Cell");
    155                 return(false);
    156             }
    157           }
    158233        } // Close Cell
    159        
     234        // Close cells (XXX shouldn't pmFPAfileClose iterate down as needed?)
     235        view->cell = -1;
     236        while ((cell = pmFPAviewNextCell(view, stack->fpa, 1)) != NULL) {
     237          if (!cell->process || !cell->file_exists) {
     238            continue;
     239          }
     240          if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
     241            psError(psErrorCodeLast(), false, "save failure for Cell");
     242            return(false);
     243          }
     244        }
     245
    160246        // Close chip
    161247        if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
Note: See TracChangeset for help on using the changeset viewer.