IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 28, 2009, 1:36:37 PM (17 years ago)
Author:
beaumont
Message:

Added QA plots to ppSub. Migrated psastro QA plots to psModules/extras.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/cnb_branch_20090113/psModules/src/astrom/pmAstrometryVisual.c

    r20801 r21208  
    1 /** Diagnostic plots called from within pmAstrometry routines.
    2  * @author      Chris Beaumont
    3  * @date        October 2008
    4  */
    5 
    6 /* Include Files   */
    71#ifdef HAVE_CONFIG_H
    82#include <config.h>
     
    1610#include <pslib.h>
    1711
    18 #include "pmKapaPlots.h"
    19 
    2012#include "pmHDU.h"
    2113#include "pmFPA.h"
     14#include "pmFPAfile.h"
    2215#include "pmAstrometryObjects.h"
     16#include "pmAstrometryVisual.h"
     17#include "pmFPAExtent.h"
    2318
    2419# if (HAVE_KAPA)
    2520# include <kapa.h>
    26 
    27 # define KAPAX 700
    28 # define KAPAY 700
    29 
     21# include "pmKapaPlots.h"
     22#include "pmVisual.h"
    3023
    3124//variables to determine when things are plotted
     
    3326static bool plotGridMatch        = true;
    3427static bool plotTweak            = true;
     28static bool plotRawStars         = true;
     29static bool plotRefStars         = false;
     30static bool plotLumFunc          = true;
     31static bool plotRemoveClumps     = true;
     32static bool plotOneChipFit       = true;
     33static bool plotFixChips         = true;
     34static bool plotAstromGuessCheck = true;
     35static bool plotMosaicMatches    = true;
     36static bool plotCommonScale      = true;
     37static bool plotMosaicOneChip    = true;
    3538
    3639// variables to store plotting window indices
    3740static int kapa = -1;
    38 
    39 
    40 /* Utility Routine Prototypes */
    41 bool pmAstromVisualScaleGraphdata(Graphdata *graphdata, psVector *xVec, psVector *yVec, bool clip);
     41static int kapa2 = -1;
    4242
    4343
    4444/* Initialization Routines  */
    4545
    46 
    47 /** start or stop plotting */
    4846bool pmAstromSetVisual (bool mode) {
    4947    isVisual = mode;
     
    5250
    5351
    54 /** open name, and resize a window if necessary */
    55 bool pmAstromVisualInitWindow (int *kapid, char *name) {
    56     if (*kapid == -1) {
    57         *kapid = KapaOpenNamedSocket("kapa", name);
    58         if (*kapid == -1) {
    59             fprintf (stderr, "failure to open kapa; visual mode disabled for pmAstrom\n");
    60             isVisual = false;
    61             return false;
    62         }
    63         KapaResize (*kapid, KAPAX, KAPAY);
    64     }
    65     return true;
    66 }
    67 
    68 
    69 /** ask the user how to proceed */
    70 bool pmAstromVisualAskUser(bool *plotflag)
    71 {
    72     char key[10];
    73     fprintf (stdout, "[c]ontinue? [s]kip the rest of these plots? [a]bort all visual plots?");
    74     if (!fgets(key, 8, stdin)) {
    75         psWarning("Unable to read option");
    76     }
    77     if (key[0] == 's') {
    78         *plotflag = false;
    79     }
    80     if (key[0] == 'a') {
    81         pmAstromSetVisual(false);
    82     }
    83     return true;
    84 }
    85 
    86 
    87 /** destroy windows at the end of a run*/
    8852bool pmAstromVisualClose()
    8953{
    9054    if(kapa != -1)
    9155        KiiClose(kapa);
     56    if(kapa2 != -1)
     57        KiiClose(kapa2);
    9258    return true;
    9359}
    9460
    9561
    96 /* plotting routines */
    97 
    98 /**
    99  * Plot the offset between every pair of reference and raw source locations. The peak of this
    100  * distribution nominally gives the offset, scale difference, and rotation of the two catalogs.
    101  * Overplots the location of this peak as determined by pmAstromGridAngle, as well as some profiles
    102  * along horizontal and vertical cuts through this peak.
    103  */
    104 bool pmAstromVisualPlotGridMatch (const psArray *raw, ///< raw stars
    105                                   const psArray *ref, ///< reference stars
    106                                   psImage *gridNP,    ///< a 2D histogram of raw-ref star distances
    107                                   double offsetX,     ///< The X location (FP coordinates) of the peak of gridNP
    108                                   double offsetY,     ///< the Y location (FP coordinates) of the peak of gridNP
    109                                   double maxOffpix,   ///< The half-width of gridNP in FP coordinates
    110                                   double Scale,       ///< The pixel size of gridNP in histogram-bin-coordinates
    111                                   double Offset       ///< The (x,y) location (histogram-bin coordinates) of the FP point (0,0) in gridNP
     62/* Plotting Routines */
     63bool pmAstromVisualPlotRawStars (psArray *rawstars, pmFPA *fpa, pmChip *chip, psMetadata *recipe)
     64{
     65    // make sure we want to plot this
     66    if (!plotRawStars || !isVisual) return true;
     67
     68    //set up plot region
     69    if (!pmVisualInitWindow (&kapa, "psastro:plots"))
     70        return false;
     71    Graphdata graphdata;
     72    KapaSection section;
     73
     74    KapaInitGraph (&graphdata);
     75    KapaClearPlots (kapa);
     76
     77    graphdata.color = KapaColorByName ("black");
     78    graphdata.ptype = 7;
     79    graphdata.size = 0.5;
     80    graphdata.style = 2;
     81
     82    section.dx = 0.4;
     83    section.dy = 0.4;
     84
     85    //initialize and populate plotting vectors
     86    bool status = false;
     87    float iMagMin = psMetadataLookupF32 (&status, recipe, "PSASTRO.PLOT.INST.MAG.MIN");
     88    float iMagMax = psMetadataLookupF32 (&status, recipe, "PSASTRO.PLOT.INST.MAG.MAX");
     89
     90    psVector *xVec = psVectorAlloc (rawstars->n, PS_TYPE_F32);
     91    psVector *yVec = psVectorAlloc (rawstars->n, PS_TYPE_F32);
     92    psVector *zVec = psVectorAlloc (rawstars->n, PS_TYPE_F32);
     93
     94    section.x = 0.0;
     95    section.y = 0.5;
     96    section.name = NULL;
     97    psStringAppend (&section.name, "a0");
     98    KapaSetSection (kapa, &section);
     99    psFree (section.name);
     100
     101    //Chip coordinates
     102    int n = 0;
     103    for (int i = 0; i < rawstars->n; i++) {
     104        pmAstromObj *raw = rawstars->data[i];
     105        if (!isfinite(raw->Mag)) continue;
     106        if (raw->Mag < iMagMin) continue;
     107        if (raw->Mag > iMagMax) continue;
     108
     109        xVec->data.F32[n] = raw->chip->x;
     110        yVec->data.F32[n] = raw->chip->y;
     111        zVec->data.F32[n] = raw->Mag;
     112        n++;
     113    }
     114    xVec->n = yVec->n = zVec->n = n;
     115
     116    KapaSendLabel(kapa, "Chip", KAPA_LABEL_XP);
     117    KapaSendLabel(kapa, "X", KAPA_LABEL_XM);
     118    KapaSendLabel(kapa, "Y", KAPA_LABEL_YM);
     119    pmVisualTriplePlot (kapa, &graphdata, xVec, yVec, zVec, false);
     120
     121    //Focal Plane Coordinates
     122    section.x = 0.5;
     123    section.y = 0.5;
     124    section.name = NULL;
     125    psStringAppend (&section.name, "a1");
     126    KapaSetSection (kapa, &section);
     127    psFree (section.name);
     128
     129    n = 0;
     130    for (int i = 0; i < rawstars->n; i++) {
     131        pmAstromObj *raw = rawstars->data[i];
     132        if (!isfinite(raw->Mag)) continue;
     133        if (raw->Mag < iMagMin) continue;
     134        if (raw->Mag > iMagMax) continue;
     135
     136        xVec->data.F32[n] = raw->FP->x;
     137        yVec->data.F32[n] = raw->FP->y;
     138        zVec->data.F32[n] = raw->Mag;
     139        n++;
     140    }
     141    xVec->n = yVec->n = zVec->n = n;
     142
     143    KapaSendLabel (kapa, "Focal Plane", KAPA_LABEL_XP);
     144    KapaSendLabel (kapa, "L", KAPA_LABEL_XM);
     145    KapaSendLabel (kapa, "M", KAPA_LABEL_YM);
     146    pmVisualTriplePlot (kapa, &graphdata, xVec, yVec, zVec, false);
     147
     148    // Tangent Plane Coordinates
     149    section.x = 0.0;
     150    section.y = 0.0;
     151    section.name = NULL;
     152    psStringAppend (&section.name, "a2");
     153    KapaSetSection (kapa, &section);
     154    psFree (section.name);
     155
     156    n = 0;
     157    for (int i = 0; i < rawstars->n; i++) {
     158        pmAstromObj *raw = rawstars->data[i];
     159        if (!isfinite(raw->Mag)) continue;
     160        if (raw->Mag < iMagMin) continue;
     161        if (raw->Mag > iMagMax) continue;
     162
     163        xVec->data.F32[n] = raw->TP->x;
     164        yVec->data.F32[n] = raw->TP->y;
     165        zVec->data.F32[n] = raw->Mag;
     166        n++;
     167    }
     168    xVec->n = yVec->n = zVec->n = n;
     169
     170    KapaSendLabel (kapa, "Tangential Plane", KAPA_LABEL_XP);
     171    KapaSendLabel (kapa, "P", KAPA_LABEL_XM);
     172    KapaSendLabel (kapa, "Q", KAPA_LABEL_YM);
     173    pmVisualTriplePlot (kapa, &graphdata, xVec, yVec, zVec, false);
     174
     175    //sky coordinates
     176    section.x = 0.5;
     177    section.y = 0.0;
     178    section.name = NULL;
     179    psStringAppend (&section.name, "a3");
     180    KapaSetSection (kapa, &section);
     181    psFree (section.name);
     182
     183    n = 0;
     184    for (int i = 0; i < rawstars->n; i++) {
     185        pmAstromObj *raw = rawstars->data[i];
     186        if (!isfinite(raw->Mag)) continue;
     187        if (raw->Mag < iMagMin) continue;
     188        if (raw->Mag > iMagMax) continue;
     189
     190        xVec->data.F32[n] = DEG_RAD*raw->sky->r;
     191        yVec->data.F32[n] = DEG_RAD*raw->sky->d;
     192        zVec->data.F32[n] = raw->Mag;
     193        n++;
     194    }
     195    xVec->n = yVec->n = zVec->n = n;
     196
     197    KapaSendLabel (kapa, "Sky", KAPA_LABEL_XP);
     198    KapaSendLabel(kapa, "RA", KAPA_LABEL_XM);
     199    KapaSendLabel(kapa, "Dec", KAPA_LABEL_YM);
     200    pmVisualTriplePlot (kapa, &graphdata, xVec, yVec, zVec, false);
     201
     202    // flip x (East increase to left)
     203    SWAP (graphdata.xmin, graphdata.xmax);
     204    KapaSetLimits (kapa, &graphdata);
     205
     206    // plot label
     207    section.x = 0.0;
     208    section.y = 0.0;
     209    section.dx = .95;
     210    section.dy = .95;
     211    section.name = NULL;
     212    psStringAppend (&section.name, "a5");
     213    KapaSetSection (kapa, &section);
     214    KapaSendLabel (kapa, "Raw Star Selection - Initial Astrometry", KAPA_LABEL_XP);
     215    psFree (section.name);
     216
     217    // pause and wait for user input:
     218    pmVisualAskUser(&plotRawStars, &isVisual);
     219
     220    psFree (xVec);
     221    psFree (yVec);
     222    psFree (zVec);
     223    return true;
     224}
     225
     226
     227bool pmAstromVisualPlotRefStars (psArray *refstars, psMetadata *recipe)
     228{
     229    //make sure we want to plot this
     230    if (!isVisual || !plotRefStars) return true;
     231
     232    //set up plotting variables
     233    if (!pmVisualInitWindow (&kapa, "psastro:plots"))
     234        return false;
     235
     236    Graphdata graphdata;
     237    KapaInitGraph (&graphdata);
     238    KapaClearSections (kapa);
     239
     240    graphdata.color = KapaColorByName ("black");
     241    graphdata.ptype = 7;
     242    graphdata.size = 0.5;
     243    graphdata.style = 2;
     244
     245    //initialize and populate plot vectors
     246    bool status = false;
     247    float rMagMin = psMetadataLookupF32 (&status, recipe, "PSASTRO.PLOT.REF.MAG.MIN");
     248    float rMagMax = psMetadataLookupF32 (&status, recipe, "PSASTRO.PLOT.REF.MAG.MAX");
     249
     250    psVector *xVec = psVectorAlloc (refstars->n, PS_TYPE_F32);
     251    psVector *yVec = psVectorAlloc (refstars->n, PS_TYPE_F32);
     252    psVector *zVec = psVectorAlloc (refstars->n, PS_TYPE_F32);
     253
     254    int n = 0;
     255    for (int i = 0; i < refstars->n; i++) {
     256        pmAstromObj *ref = refstars->data[i];
     257        if (!isfinite(ref->Mag)) continue;
     258        if (ref->Mag > rMagMax) continue;
     259        if (ref->Mag < rMagMin) continue;
     260
     261        xVec->data.F32[n] = DEG_RAD*ref->sky->r;
     262        yVec->data.F32[n] = DEG_RAD*ref->sky->d;
     263        zVec->data.F32[n] = ref->Mag;
     264        n++;
     265    }
     266    xVec->n = yVec->n = zVec->n = n;
     267
     268    KapaSendLabel (kapa, "RA", KAPA_LABEL_XM);
     269    KapaSendLabel (kapa, "Dec", KAPA_LABEL_YM);
     270    KapaSendLabel (kapa, "Reference Stars", KAPA_LABEL_XP);
     271    pmVisualTriplePlot(kapa, &graphdata, xVec, yVec, zVec, false);
     272
     273    // flip x (East increase to left)
     274    SWAP (graphdata.xmin, graphdata.xmax);
     275    KapaSetLimits (kapa, &graphdata);
     276
     277    // pause and wait for user input:
     278    pmVisualAskUser(&plotRefStars, &isVisual);
     279
     280    psFree (xVec);
     281    psFree (yVec);
     282    psFree (zVec);
     283    return true;
     284}
     285
     286
     287bool pmAstromVisualPlotLuminosityFunction (psVector *lnMag,   // Log(n) for each magnitude bin
     288                                          psVector *Mag,     // magnitude bins
     289                                          pmLumFunc *lumFunc,// Fit to the reference star luminosity function
     290                                          pmLumFunc *rawFunc // Fit to the raw star luminoisty function
     291                                          )
     292{
     293
     294    // make sure we want to plot this
     295    if ( !isVisual || !plotLumFunc ) return true;
     296
     297    //set up plotting variables
     298    if ( !pmVisualInitWindow (&kapa, "psastro:plots"))
     299        return false;
     300
     301    Graphdata graphdata;
     302    KapaSection section1 = {"s1", .1, .1, .85, .35};
     303    KapaSection section2 =  {"s2", .1, .5, .85, .35};
     304    KapaSection section;
     305    if(rawFunc == NULL)
     306        section = section1;
     307    else
     308        section = section2;
     309    if (rawFunc == NULL)
     310        KapaClearPlots(kapa);
     311    KapaInitGraph(&graphdata);
     312
     313    //Determine Plot Limits
     314    pmVisualScaleGraphdata(&graphdata, Mag, lnMag, false);
     315
     316    //Make a line for the fit
     317    float x[2] = {graphdata.xmin, graphdata.xmax};
     318    float y[2] = {lumFunc->offset + x[0] * lumFunc->slope,
     319                 lumFunc->offset + x[1] * lumFunc->slope};
     320
     321    //Plot Data
     322    KapaSetSection(kapa, &section);
     323    KapaSetLimits(kapa, &graphdata);
     324
     325    KapaSetFont (kapa, "helvetica", 14);
     326    KapaBox (kapa, &graphdata);
     327    KapaSendLabel (kapa, "Magnitude", KAPA_LABEL_XM);
     328    KapaSendLabel (kapa, "Log(N)", KAPA_LABEL_YM);
     329    if (rawFunc == NULL)
     330        KapaSendLabel (kapa, "Raw Star Luminosity Function", KAPA_LABEL_XP);
     331    else
     332        KapaSendLabel (kapa,
     333                       "Reference Star Luminosity Function, Shifted Raw Fit, and Cutoff",
     334                       KAPA_LABEL_XP);
     335    graphdata.color=KapaColorByName("black");
     336    graphdata.style = 1;
     337    KapaPrepPlot (kapa, lnMag->n, &graphdata);
     338    KapaPlotVector(kapa, lnMag->n,   Mag->data.F32, "x");
     339    KapaPlotVector(kapa, lnMag->n, lnMag->data.F32, "y");
     340
     341    //Overplot fit
     342    graphdata.style=0;
     343    KapaPrepPlot(kapa,2,&graphdata);
     344    KapaPlotVector(kapa, 2, x, "x");
     345    KapaPlotVector(kapa, 2, y, "y");
     346
     347    //If rawFunc was supplied, overplot the raw star fit + cutoff
     348    if( rawFunc != NULL) {
     349        double mRef = 0.5*(lumFunc->mMin + lumFunc->mMax);
     350        double logRho = mRef * lumFunc->slope + lumFunc->offset;
     351        double mRaw = (logRho - rawFunc->offset) / rawFunc->slope;
     352        double deltaM = mRef - mRaw;
     353        double mRefMax = rawFunc->mMax + deltaM;
     354
     355        float xraw[2] = {rawFunc->mMin + deltaM, rawFunc->mMax + deltaM};
     356        float yraw[2] = {rawFunc->offset + (rawFunc->slope) * rawFunc->mMin,
     357                        rawFunc->offset + (rawFunc->slope) * rawFunc->mMax};
     358        float x[2] = {mRefMax, mRefMax};
     359        float y[2] = {graphdata.ymin, graphdata.ymax};
     360        graphdata.color= KapaColorByName("red");
     361        KapaPrepPlot(kapa, 2, &graphdata);
     362        KapaPlotVector(kapa, 2, x, "x");
     363        KapaPlotVector(kapa, 2, y, "y");
     364        KapaPrepPlot (kapa, 2, &graphdata);
     365        KapaPlotVector (kapa, 2, xraw, "x");
     366        KapaPlotVector (kapa, 2, yraw, "y");
     367
     368        // pause and wait for user input:
     369        pmVisualAskUser (&plotLumFunc, &isVisual);
     370    }
     371    return true;
     372} // end of pmAstromVisualPlotLuminosityFunction
     373
     374
     375bool pmAstromVisualPlotRemoveClumps (psArray *input, // Array containing the field stars
     376                                    psImage *count, // A 2D histogram of the field star distribution
     377                                    int scale,      // The pixel size of the histogram
     378                                    float limit     // The minimum numuber of stars in a bin flagged as a clump
     379                                    )
     380{
     381
     382    //make sure we want to plot this
     383    if (!isVisual || !plotRemoveClumps) return true;
     384
     385    //set up plot variables
     386    if ( !pmVisualInitWindow (&kapa, "psastro:plots")) return false;
     387
     388    KapaSection section;
     389    Graphdata graphdata;
     390    section.x = 0;
     391    section.dx = .9;
     392    section.y = 0;
     393    section.dy = .9;
     394    section.name = NULL;
     395    psStringAppend( &section.name, "a0");
     396    KapaInitGraph(&graphdata);
     397    KapaSetSection(kapa, &section);
     398    psFree(section.name);
     399
     400    graphdata.ptype = 7;
     401    graphdata.size = 0.5;
     402    graphdata.style = 2;
     403    graphdata.color = KapaColorByName ("black");
     404    KapaClearPlots(kapa);
     405
     406    //set up plot vectors
     407    float Xmin = +FLT_MAX;
     408    float Xmax = -FLT_MAX;
     409    float Ymin = +FLT_MAX;
     410    float Ymax = -FLT_MAX;
     411    psVector *xVec = psVectorAlloc (input->n, PS_TYPE_F32);
     412    psVector *yVec = psVectorAlloc (input->n, PS_TYPE_F32);
     413
     414    //determine boundaries for histogram bin calculation
     415    int n = 0;
     416    for (int i=0; i< input->n; i++) {
     417        pmAstromObj *obj = (pmAstromObj *)input->data[i];
     418        if (!isfinite(obj->FP->x)) continue;
     419        if (!isfinite(obj->FP->y)) continue;
     420        xVec->data.F32[n] = obj->FP->x;
     421        yVec->data.F32[n] = obj->FP->y;
     422        Xmin = PS_MIN (Xmin, xVec->data.F32[n]);
     423        Xmax = PS_MAX (Xmax, xVec->data.F32[n]);
     424        Ymin = PS_MIN (Ymin, yVec->data.F32[n]);
     425        Ymax = PS_MAX (Ymax, yVec->data.F32[n]);
     426        n++;
     427    }
     428    xVec->n = yVec->n = n;
     429
     430    //plot stars
     431    graphdata.xmax = Xmax;
     432    graphdata.xmin = Xmin;
     433    graphdata.ymax = Ymax;
     434    graphdata.ymin = Ymin;
     435    KapaSetLimits (kapa, &graphdata);
     436    KapaSetFont (kapa, "helvetica", 14);
     437
     438    KapaBox (kapa, &graphdata);
     439
     440    KapaSendLabel (kapa, "L (pixels)", KAPA_LABEL_XM);
     441    KapaSendLabel (kapa, "M (pixels)", KAPA_LABEL_YM);
     442    KapaSendLabel (kapa, "Regions Flagged as Clumps (Red Boxes)",
     443                   KAPA_LABEL_XP);
     444
     445    KapaPrepPlot (kapa, xVec->n, &graphdata);
     446    KapaPlotVector (kapa, xVec->n, xVec->data.F32, "x");
     447    KapaPlotVector (kapa, yVec->n, yVec->data.F32, "y");
     448
     449    graphdata.color = KapaColorByName ("red");
     450    graphdata.style = 1;
     451
     452    //overplot clumpy regions excluded from analysis
     453    for(int i = 0; i < count->numCols; i++) {
     454        for (int j = 0; j < count->numRows; j++) {
     455            if(count->data.U32[j][i] <= limit) continue; //not a clump
     456            float Xbot = (i - 5) * scale + Xmin;
     457            float Ybot = (j - 5) * scale + Ymin;
     458            if(Xbot < graphdata.xmin || Xbot > graphdata.xmax ||
     459               Ybot < graphdata.ymin || Ybot > graphdata.ymax) continue;
     460            float x[5] = {Xbot, Xbot + scale, Xbot + scale, Xbot, Xbot};
     461            float y[5] = {Ybot, Ybot, Ybot + scale, Ybot + scale, Ybot};
     462            KapaPrepPlot (kapa, 5, &graphdata);
     463            KapaPlotVector (kapa, 5, x, "x");
     464            KapaPlotVector (kapa, 5, y, "y");
     465        }
     466    }
     467
     468    //ask for user input and finish
     469    pmVisualAskUser (&plotRemoveClumps, &isVisual);
     470    psFree (xVec);
     471    psFree (yVec);
     472
     473    return true;
     474}
     475
     476
     477bool pmAstromVisualPlotOneChipFit (psArray *rawstars, // stars detected in the image
     478                                  psArray *refstars, // reference stars over the same region
     479                                  psArray *match,    // contains which rawstars match to which refstars
     480                                  psMetadata *recipe // data reduction recipe
    112481                                  )
     482{
     483
     484    //make sure we want to plot this
     485    if (!isVisual || !plotOneChipFit)
     486        return true;
     487
     488    //plot the residuals
     489    if (!pmVisualResidPlot(rawstars, refstars, match, recipe, "Single Chip Fit Residuals (Chip Coordinates)", &kapa, &kapa2))
     490        return false;
     491
     492    //ask for user input and finish
     493    pmVisualAskUser(&plotOneChipFit, &isVisual);
     494    return true;
     495}
     496
     497
     498bool pmAstromVisualPlotFixChips (pmFPAfile *input, // focal plane array file
     499                                psVector *xOld, // old X location of chip cornerss
     500                                psVector *yOld // old Y location of chip corners
     501                                )
     502{
     503    //make sure we want to plot this
     504    if(!isVisual || !plotFixChips) return true;
     505
     506    if(!pmVisualInitWindow(&kapa, "psastro:plots")) return false;
     507
     508    KapaSection section = {"s1", .05, .05, .9, .9};
     509    Graphdata graphdata;
     510    KapaInitGraph( &graphdata);
     511    KapaClearPlots (kapa);
     512    graphdata.ptype = 2;
     513    graphdata.style = 2;
     514
     515    psVector *xNew = psVectorAllocEmpty (xOld->n, PS_TYPE_F32);
     516    psVector *yNew = psVectorAllocEmpty (yOld->n, PS_TYPE_F32);
     517
     518    // copy of the code in psastroFixChips that generated xOld, yOld, but for xNew, yNew
     519    pmFPAview *view = pmFPAviewAlloc (0);
     520
     521    pmChip *obsChip = NULL;
     522    while ((obsChip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) {
     523        if (!obsChip->process || !obsChip->file_exists || !obsChip->data_exists) { continue; }
     524
     525        psRegion *region = pmChipPixels(obsChip);
     526        psPlane ptCP, ptFP;
     527
     528        ptCP.x = region->x0; ptCP.y = region->y0;
     529        psPlaneTransformApply (&ptFP, obsChip->toFPA, &ptCP);
     530        psVectorAppend (xNew, ptFP.x);
     531        psVectorAppend (yNew, ptFP.y);
     532
     533        ptCP.x = region->x0; ptCP.y = region->y1;
     534        psPlaneTransformApply (&ptFP, obsChip->toFPA, &ptCP);
     535        psVectorAppend (xNew, ptFP.x);
     536        psVectorAppend (yNew, ptFP.y);
     537
     538        ptCP.x = region->x1; ptCP.y = region->y1;
     539        psPlaneTransformApply (&ptFP, obsChip->toFPA, &ptCP);
     540        psVectorAppend (xNew, ptFP.x);
     541        psVectorAppend (yNew, ptFP.y);
     542
     543        ptCP.x = region->x1; ptCP.y = region->y0;
     544        psPlaneTransformApply (&ptFP, obsChip->toFPA, &ptCP);
     545        psVectorAppend (xNew, ptFP.x);
     546        psVectorAppend (yNew, ptFP.y);
     547
     548        psFree (region);
     549    }
     550
     551    //set up graph
     552    pmVisualScaleGraphdata(&graphdata, xOld, yOld, true);
     553    pmVisualInitGraph(kapa, &section, &graphdata);
     554    KapaSendLabel (kapa, "L (FP)", KAPA_LABEL_XM);
     555    KapaSendLabel (kapa, "M (FP)", KAPA_LABEL_YM);
     556    KapaSendLabel (kapa, "Chip corners before (black) and after (red) FixChips", KAPA_LABEL_XP);
     557    KapaPrepPlot (kapa, xOld->n, &graphdata);
     558    KapaPlotVector (kapa, xOld->n, xOld->data.F32, "x");
     559    KapaPlotVector (kapa, xOld->n, yOld->data.F32, "y");
     560
     561    graphdata.ptype = 1;
     562    graphdata.color = KapaColorByName("red");
     563    KapaPrepPlot (kapa, xNew->n, &graphdata);
     564    KapaPlotVector (kapa, xNew->n, xNew->data.F32, "x");
     565    KapaPlotVector (kapa, xNew->n, yNew->data.F32, "y");
     566
     567    pmVisualAskUser(&plotFixChips, &isVisual);
     568    psFree(xNew);
     569    psFree(yNew);
     570    psFree(view);
     571    return true;
     572}
     573
     574
     575bool pmAstromVisualPlotAstromGuessCheck (psVector *cornerPo, // P coordinates of chip corners before fitting
     576                                        psVector *cornerQo, // Q coordinates of chip corners before fitting
     577                                        psVector *cornerPn, // P coordinates of chip corners after fitting
     578                                        psVector *cornerQn, // Q coordinates of chip corners after fitting
     579                                        psVector *cornerPd, // P coordinate residuals of fit from old to new coordinates
     580                                        psVector *cornerQd  // Q coordinate residuals of fit from old to new coordinates
     581                                        )
     582{
     583
     584    //make sure we want to plot this
     585    if (!isVisual || !plotAstromGuessCheck) return true;
     586
     587    //set up graph window
     588    if ( !pmVisualInitWindow (&kapa, "psastro:plots"))
     589        return false;
     590    Graphdata graphdata;
     591    KapaSection section;
     592    KapaInitGraph(&graphdata);
     593    KapaClearPlots (kapa);
     594
     595    graphdata.color = KapaColorByName ("black");
     596    graphdata.ptype = 2;
     597    graphdata.size = 0.5;
     598    graphdata.style = 2;
     599
     600    section.dx = 0.4;
     601    section.dy = 0.4;
     602
     603    //Old Corners
     604    section.x = 0.30;
     605    section.y = 0.50;
     606    section.name = NULL;
     607    psStringAppend (&section.name, "a0");
     608    KapaSetSection (kapa, &section);
     609    psFree(section.name);
     610
     611    pmVisualScaleGraphdata (&graphdata, cornerPo, cornerPo, true);
     612    KapaSetLimits (kapa, &graphdata);
     613    KapaBox (kapa, &graphdata);
     614    KapaSendLabel (kapa, "P (Pixels)", KAPA_LABEL_XM);
     615    KapaSendLabel (kapa, "Q (Pixels)", KAPA_LABEL_YM);
     616    KapaSendLabel (kapa,
     617                   "Fiducial Points in the Tangent Plane. Black: Initial Astrometry. Red: Final Astrometry",
     618                   KAPA_LABEL_XP);
     619    KapaPrepPlot (kapa, cornerPo->n, &graphdata);
     620    KapaPlotVector (kapa, cornerPo->n, cornerPo->data.F32, "x");
     621    KapaPlotVector (kapa, cornerQo->n, cornerQo->data.F32, "y");
     622
     623    // New Corners
     624    graphdata.color = KapaColorByName("red");
     625    graphdata.ptype = 7;
     626    graphdata.size = 1.5;
     627    KapaPrepPlot (kapa, cornerPn->n, &graphdata);
     628    KapaPlotVector (kapa, cornerPn->n, cornerPn->data.F32, "x");
     629    KapaPlotVector (kapa, cornerQn->n, cornerQn->data.F32, "y");
     630
     631    // Residuals
     632    psVector *xResid = psVectorAlloc(cornerPn->n, PS_DATA_F32);
     633    psVector *yResid = psVectorAlloc(cornerQn->n, PS_DATA_F32);
     634    for(int i=0; i < cornerPn->n; i++) {
     635        xResid->data.F32[i] = (cornerPd->data.F32[i]);
     636        yResid->data.F32[i] = (cornerQd->data.F32[i]);
     637    }
     638
     639    graphdata.color = KapaColorByName("black");
     640    graphdata.size=0.5;
     641    section.x = 0.3;
     642    section.y = 0.0;
     643    section.name = NULL;
     644    psStringAppend (&section.name, "a1");
     645    KapaSetSection (kapa, &section);
     646    psFree(section.name);
     647
     648    pmVisualScaleGraphdata (&graphdata, xResid, yResid, true);
     649    KapaSetLimits (kapa, &graphdata);
     650    KapaBox (kapa, &graphdata);
     651    KapaSendLabel (kapa, "dP", KAPA_LABEL_XM);
     652    KapaSendLabel (kapa, "dQ", KAPA_LABEL_YM);
     653    KapaSendLabel (kapa,
     654                   "Residual of the Fit from the Initial Astrometry to the Final Astrometry",
     655                   KAPA_LABEL_XP);
     656    KapaPrepPlot (kapa, cornerPd->n, &graphdata);
     657    KapaPlotVector (kapa, cornerPd->n, xResid->data.F32, "x");
     658    KapaPlotVector (kapa, cornerQd->n, yResid->data.F32, "y");
     659
     660    psFree(xResid);
     661    psFree(yResid);
     662    pmVisualAskUser (&plotAstromGuessCheck, &isVisual);
     663    return true;
     664}
     665
     666
     667bool pmAstromVisualPlotCommonScale (pmFPA *fpa,         // the fpa
     668                                   psVector *oldScale  // the old pixel scale of each chip in the fpa
     669                                   )
     670{
     671    //make sure we want to plot this
     672    if (!isVisual || !plotCommonScale) return false;
     673
     674    if (!pmVisualInitWindow(&kapa, "psastro:plots")) return false;
     675
     676    KapaSection section = {"s1", .05, .05, .9, .9};
     677    Graphdata graphdata;
     678    psPlane ptCH, ptFP;
     679    ptCH.x = 0;
     680    ptCH.y = 0;
     681    psVector *xVec = psVectorAlloc (oldScale->n, PS_TYPE_F32);
     682    psVector *yVec = psVectorAlloc (oldScale->n, PS_TYPE_F32);
     683
     684    int nobj = 0;
     685
     686    //project each chip corner to the Focal Plane
     687    for(int i = 0; i < fpa->chips->n; i++) {
     688        pmChip *chip = fpa->chips->data[i];
     689        if (!chip->process || !chip->file_exists) { continue; }
     690        if (!chip->toFPA) { continue; }
     691
     692        psPlaneTransformApply (&ptFP, chip->toFPA, &ptCH);
     693        xVec->data.F32[nobj] = ptFP.x;
     694        yVec->data.F32[nobj] = ptFP.y;
     695        nobj++;
     696    }
     697
     698    //set up plot window
     699    KapaInitGraph (&graphdata);
     700    KapaClearPlots (kapa);
     701    KapaSetSection (kapa, &section);
     702    KapaSetFont (kapa, "helvetica", 14);
     703    pmVisualTriplePlot (kapa, &graphdata, xVec, yVec, oldScale, false);
     704    KapaSendLabel (kapa, "L (FP)", KAPA_LABEL_XM);
     705    KapaSendLabel (kapa, "M (FP)", KAPA_LABEL_YM);
     706    KapaSendLabel (kapa, "Old Pixel Scale of FPA Chips (Not to Scale)", KAPA_LABEL_XP);
     707
     708    pmVisualAskUser (&plotCommonScale, &isVisual);
     709    return true;
     710}
     711
     712
     713bool pmAstromVisualPlotMosaicOneChip (psArray *rawstars, psArray *refstars,
     714                                     psArray *match, psMetadata *recipe)
     715{
     716
     717    //make sure we want to plot this
     718    if (!isVisual || !plotMosaicOneChip) return false;
     719
     720    //plot the residuals
     721    if (!pmVisualResidPlot(rawstars, refstars, match, recipe, "Single Chip Fit Residuals - Mosaic Mode", &kapa, &kapa2))
     722        return false;
     723
     724    //ask for user input and finish
     725    pmVisualAskUser(&plotMosaicOneChip, &isVisual);
     726
     727    return true;
     728}
     729
     730
     731bool pmAstromVisualPlotMosaicMatches( psArray *rawstars, psArray *refstars,
     732                                    psArray *match, int iteration,
     733                                    psMetadata *recipe)
     734{
     735    //make sure we want to plot this
     736    if (!isVisual || !plotMosaicMatches) return true;
     737
     738    char title[60];
     739    sprintf(title, "Matches found during psastroMosaicSetMatch iteration %d", iteration);
     740
     741    if (!pmVisualResidPlot(rawstars, refstars, match, recipe, title, &kapa, &kapa2))
     742        return false;
     743
     744    //ask for user input
     745    pmVisualAskUser (&plotMosaicMatches, &isVisual);
     746    return true;
     747}
     748
     749
     750bool pmAstromVisualPlotGridMatch (const psArray *raw,
     751                                  const psArray *ref,
     752                                  psImage *gridNP,
     753                                  double offsetX,
     754                                  double offsetY,
     755                                  double maxOffpix,
     756                                  double Scale,
     757                                  double Offset)
    113758{
    114759    //make sure we want to plot this
    115760    if (!isVisual || !plotGridMatch) return true;
    116     if (!pmAstromVisualInitWindow(&kapa, "pmAstrom:plots"))
     761    if (!pmVisualInitWindow(&kapa, "pmAstrom:plots"))
    117762        return false;
    118763
     
    254899    KapaPlotVector (kapa, 2, yslice, "y");
    255900
    256     pmAstromVisualAskUser(&plotGridMatch);
     901    pmVisualAskUser(&plotGridMatch, &isVisual);
    257902    return true;
    258903} // end of pmAstromVisualPlotGridMatch
    259904
    260905
    261 /** Plot the refinements made within pmAstromGridTweak.
    262  * After pmAstromGridMatch finds the best rotaion/scale/offset between raw and reference stars
    263  * within a coarse grid of rotations/scales, pmAstromGridTweak computes a higher precision
    264  * estimate of the offset. It computes the 2 point correlation function between raw and ref
    265  * stars along horizontal and vertical cuts through the first-guess offset. It finds the peak
    266  * of these two profiles and adjusts the offset accordingly. This procedure plots the profiles.
    267  */
    268 bool pmAstromVisualPlotTweak (psVector *xHist, ///< Smoothed Horizontal cut through the histogram
    269                               psVector *yHist, ///< Smoothed Vertical cut throug the histogram
    270                               int xBin,        ///< X Bin index of the histogram peak
    271                               int yBin         ///< Y bin index of the histogram peak
     906bool pmAstromVisualPlotTweak (psVector *xHist, // Smoothed Horizontal cut through the histogram
     907                              psVector *yHist, // Smoothed Vertical cut throug the histogram
     908                              int xBin,        // X Bin index of the histogram peak
     909                              int yBin         // Y bin index of the histogram peak
    272910    )
    273911{
    274912    //make sure we want to plot this
    275913    if (!isVisual || !plotTweak) return true;
    276     if (!pmAstromVisualInitWindow(&kapa, "pmAstrom:plots")) {
     914    if (!pmVisualInitWindow(&kapa, "pmAstrom:plots")) {
    277915        return false;
    278916    }
     
    298936
    299937    // plot the X histogram
    300     pmAstromVisualScaleGraphdata(&graphdata, xIndices, xHist, false);
     938    pmVisualScaleGraphdata(&graphdata, xIndices, xHist, false);
    301939    KapaSetSection(kapa, &section1);
    302940    KapaSetLimits (kapa, &graphdata);
     
    325963
    326964    //plot the Y histogram
    327     pmAstromVisualScaleGraphdata(&graphdata, yIndices, yHist, false);
     965    pmVisualScaleGraphdata(&graphdata, yIndices, yHist, false);
    328966    KapaSetSection(kapa, &section2);
    329967    KapaSetLimits (kapa, &graphdata);
     
    357995                   KAPA_LABEL_XP);
    358996
    359     pmAstromVisualAskUser(&plotTweak);
     997    pmVisualAskUser(&plotTweak, &isVisual);
    360998
    361999    psFree(xIndices);
     
    3641002} //end of pmAstromPlotTweak
    3651003
    366 
    367 /** pmAstromScaleGraphdata
    368  * Scale the graphdata structure based on x and y coordinates. Use sigma clipping to
    369  * prevent outliers from making te plot region too big.
    370  */
    371 bool pmAstromVisualScaleGraphdata(Graphdata *graphdata, psVector *xVec, psVector *yVec, bool clip) {
    372 
    373     graphdata->xmin = +FLT_MAX;
    374     graphdata->xmax = -FLT_MAX;
    375     graphdata->ymin = +FLT_MAX;
    376     graphdata->ymax = -FLT_MAX;
    377 
    378     //determine standard deviation of xVec and yVec
    379     psStats *statsX = psStatsAlloc(PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
    380     psStats *statsY = psStatsAlloc(PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
    381     psVectorStats (statsX, xVec, NULL, NULL, 0);
    382     psVectorStats (statsY, yVec, NULL, NULL, 0);
    383 
    384     float xhi  = statsX->sampleMedian + 3 *statsX->sampleStdev;
    385     float xlo = statsX->sampleMedian - 3 *statsX->sampleStdev;
    386     float yhi = statsY->sampleMedian + 3 *statsY->sampleStdev;
    387     float ylo = statsY->sampleMedian - 3 *statsY->sampleStdev;
    388 
    389     // don't sigma clip
    390     if (!clip) {
    391         xhi = +FLT_MAX;
    392         xlo = -FLT_MAX;
    393         yhi = +FLT_MAX;
    394         ylo = -FLT_MAX;
    395     }
    396 
    397     // abort if there is no good data
    398     if (!isfinite(xhi) || !isfinite(xlo) || !isfinite(yhi) || !isfinite(ylo)) {
    399         graphdata->xmin = -1;
    400         graphdata->ymin  = -1;
    401         graphdata->xmax = 1;
    402         graphdata->ymax = 1;
    403         psFree(statsX);
    404         psFree(statsY);
    405         return false;
    406     }
    407 
    408     for(int i = 0; i < xVec->n; i++) {
    409         if (!isfinite(xVec->data.F32[i])) continue;
    410         if (xVec->data.F32[i] > xhi || xVec->data.F32[i] < xlo) continue;
    411         graphdata->xmin = PS_MIN (graphdata->xmin, xVec->data.F32[i]);
    412         graphdata->xmax = PS_MAX (graphdata->xmax, xVec->data.F32[i]);
    413     }
    414 
    415     for (int i = 0; i < yVec->n; i++) {
    416         if (!isfinite(xVec->data.F32[i])) continue;
    417         if (yVec->data.F32[i] > yhi || yVec->data.F32[i] < ylo) continue;
    418         graphdata->ymin = PS_MIN (graphdata->ymin, yVec->data.F32[i]);
    419         graphdata->ymax = PS_MAX (graphdata->ymax, yVec->data.F32[i]);
    420     }
    421 
    422     // add a whitespace border
    423     float range = graphdata->xmax - graphdata->xmin;
    424     if (range == 0) range = 1;
    425     graphdata->xmin -= .05 * range;
    426     graphdata->xmax += .05 * range;
    427 
    428     range = graphdata->ymax - graphdata->ymin;
    429     if (range == 0) range = 1;
    430     graphdata->ymin -= .05 * range;
    431     graphdata->ymax += .05 * range;
    432 
    433     psFree (statsX);
    434     psFree (statsY);
    435     return true;
    436 }
    437 
    438 
    4391004# else
    4401005
    4411006bool pmAstromSetVisual(bool mode) { return true; }
    442 bool pmAstromVisualInitWindow (int *kapid, char *name) { return true; }
    443 bool pmAstromVisualAskUser (bool *plotflag) { return true; }
    4441007bool pmAstromVisualClose() { return true; }
    4451008bool pmAstromVisualPlotGridMatch (const psArray *raw, const psArray *ref, psImage *gridNP, double offsetX, double offsetY, double maxOffpix, double Scale, double Offset) { return true; }
    4461009bool pmAstromVisualPlotTweak (psVector *xHist, psVector *yHist, int xBin, int yBin) {return true;}
     1010bool pmAstromVisualPlotLuminosityFunction (psVector *lnMag, psVector *Mag, pmLumFunc *lumFunc, pmLumFunc *rawFunc) {return true;}
     1011bool pmAstromVisualPlotRawStars (psArray *rawstars, pmFPA *fpa, pmChip *chip, psMetadata *recipe) {return true;}
     1012bool pmAstromVisualPlotRefStars (psArray *refstars, psMetadata *recipe) {return true;}
     1013bool pmAstromVisualPlotRemoveClumps (psArray *input, psImage *count, int scale, float limit) {return true;}
     1014bool pmAstromVisualPlotFixChips (pmFPAfile *input, psVector *xOld, psVector *yOld) {return true;}
     1015bool pmAstromVisualPlotOneChipFit (psArray *rawstars, psArray *refstars, psArray *match, psMetadata *recipe) {return true;}
     1016bool pmAstromVisualPlotAstromGuessCheck (psVector *cornerPo, psVector *cornerQo, psVector *cornerPn, psVector *cornerQn, psVector *cornerPd, psVector *cornerQd) {return true;}
     1017bool pmAstromVisualPlotMosaicOneChip (psArray *rawstars, psArray *refstars, psArray *match, psMetadata *recipe) {return true;}
     1018bool pmAstromVisualPlotCommonScale (pmFPA *fpa, psVector *oldScale) {return true;}
     1019bool pmAstromVisualPlotMosaicMatches (psArray *rawstars, psArray *refstars, psArray *match, int iteration, psMetadata *recipe) {return true;}
    4471020
    4481021# endif
Note: See TracChangeset for help on using the changeset viewer.