IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 34848


Ignore:
Timestamp:
Dec 18, 2012, 2:12:42 PM (13 years ago)
Author:
watersc1
Message:

Added linear scaling code, which is off by default. These models do look better than with no scaling, but they're not as good as my manual models. I'm not sure what is different, but I wanted to get this into SVN.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ppStack/src/ppStackCombineAlternate.c

    r34800 r34848  
    2323    pmFPAfileClose(file,view);
    2424  }
     25
     26  // Scaling block
     27  if (!ppStackLinearScale(inputs, config)) {
     28    psFree(inputs);
     29    return(false);
     30  }
     31
    2532  if (!pmStackSimpleMedianCombine(outRO,inputs)) {
    2633    psFree(inputs);
    2734    return(false);
    2835  }
     36
    2937#if 0
    3038  if (!ppStackWriteImage("/tmp/test_forced.median.fits",
     
    7987    pmFPAfileClose(file,view);
    8088  }
     89
     90
     91  // Do combination
    8192  if (!pmStackSimpleMedianCombine(bkgRO,inputs)) {
    8293    psFree(inputs);
     
    113124  return(true);
    114125}
    115  
     126
     127bool ppStackLinearScale (psArray *inputs, pmConfig *config)  {
     128  psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSTACK_RECIPE);
     129  bool doLinearScaling = psMetadataLookupBool(NULL, recipe, "DO.LINEAR.INPUT.SCALING");
     130  if (doLinearScaling) {
     131    int minInputCols, maxInputCols, minInputRows, maxInputRows; // Smallest and largest values to combine
     132    int xSize, ySize;                   // Size of the output image
     133   
     134    if (!pmReadoutStackValidate(&minInputCols, &maxInputCols, &minInputRows, &maxInputRows, &xSize, &ySize,inputs)) {
     135      psError(psErrorCodeLast(), false, "Input stack is not valid.");
     136      psFree(inputs);
     137      return false;
     138    }
     139   
     140    // Determine best input.
     141    int ref = 0;
     142    int Nref = 0;
     143    double refScale = 0;
     144   
     145    for (int i = 0; i < inputs->n; i++) {
     146      pmReadout *ro = inputs->data[i];
     147      psImage *roImage = ro->image;
     148      int Nvalid = 0;
     149      double scale = 0;
     150
     151      for (int y = minInputRows; y < maxInputRows; y++) {
     152        for (int x = minInputCols; x < maxInputCols; x++) {
     153          if ((isfinite(roImage->data.F32[y][x]))) {
     154            Nvalid += 1;
     155            scale += roImage->data.F32[y][x];
     156          }
     157        }
     158      }
     159      if ((scale > refScale)&&(Nvalid > 0.9 * Nref)) {
     160        ref = i;
     161        refScale = scale;
     162        Nref = Nvalid;
     163      }
     164    }
     165    fprintf(stderr,"ref: %d %d %f\n",ref,Nref,refScale);
     166    // Calculate scaling factors
     167    pmReadout *refReadout = inputs->data[ref];
     168    psImage *refImage     = refReadout->image;
     169    for (int i = 0; i < inputs->n; i++) {
     170      pmReadout *ro = inputs->data[i];
     171      psImage *roImage = ro->image;
     172      double S = 0.0;
     173      double Sx = 0.0;
     174      double Sy = 0.0;
     175      double Sxx = 0.0;
     176      double Syy = 0.0;
     177      double Sxy = 0.0;
     178      double D = 0.0;
     179      double offset = 0.0;
     180      double scale  = 0.0;
     181     
     182      for (int y = minInputRows; y < maxInputRows; y++) {
     183        for (int x = minInputCols; x < maxInputCols; x++) {
     184          if ((isfinite(refImage->data.F32[y][x]))&&
     185              (isfinite(roImage->data.F32[y][x]))) {
     186            S += 1.0;
     187            Sx += roImage->data.F32[y][x];
     188            Sy += refImage->data.F32[y][x];
     189           
     190            Sxx += pow(roImage->data.F32[y][x],2);
     191            Syy += pow(refImage->data.F32[y][x],2);
     192            Sxy += roImage->data.F32[y][x] * refImage->data.F32[y][x];
     193          }
     194        }
     195      }
     196     
     197      D = S * Sxx - Sx * Sx;
     198      offset = (Sy * Sxx - Sx * Sxy) / D;
     199      scale  = (S * Sxy - Sx * Sy) / D;
     200      fprintf(stderr,"Scales: %d %g %g\n",i,offset,scale);
     201      // Apply scaling factors
     202      for (int y = minInputRows; y < maxInputRows; y++) {
     203        for (int x = minInputCols; x < maxInputCols; x++) {
     204          roImage->data.F32[y][x] = offset + scale * roImage->data.F32[y][x];
     205        }
     206      }
     207    }     
     208  }
     209  return(true);
     210}
     211
     212
Note: See TracChangeset for help on using the changeset viewer.