IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 24, 2009, 6:00:00 PM (17 years ago)
Author:
Paul Price
Message:

Wrote functions to merge detections from multiple skycells, and to write detections out. Compiles, not yet tested.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/pap_mops/ppMops/src/ppMopsMerge.c

    r25162 r25181  
    55#include <stdio.h>
    66#include <pslib.h>
     7#include <string.h>
    78
    89#include "ppMops.h"
    910
     11#define LEAF_SIZE 4                     // Size of leaf
     12#define MATCH_RADIUS 1.0/3600           // Matching radius
     13
     14// Get distance from detection to centre of image
     15static float mergeDistance(const ppMopsDetections *detections, // Detections of interest
     16                           long index                          // Index for source of interest
     17    )
     18{
     19    float dx = detections->x->data.F32[index] - detections->naxis1->data.S32[index] / 2.0;
     20    float dy = detections->y->data.F32[index] - detections->naxis2->data.S32[index] / 2.0;
     21    return PS_SQR(dx) + PS_SQR(dy);
     22}
     23
     24
    1025ppMopsDetections *ppMopsMerge(const psArray *detections)
    1126{
     27    PS_ASSERT_ARRAY_NON_NULL(detections, NULL);
    1228
    13     return NULL;
     29    ppMopsDetections *merged = psMemIncrRefCounter(detections->data[0]); // Merged list
     30    int num = 1;                                                         // Number of merged files
     31    for (int i = 1; i < detections->n; i++) {
     32        ppMopsDetections *det = detections->data[i]; // Detections of interest
     33        if (!det) {
     34            continue;
     35        }
     36        num++;
     37
     38        // XXX compare exposure properties
     39        if (strcmp(merged->raBoresight, det->raBoresight) != 0) {
     40            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Exposure RA values differ: %s vs %s",
     41                    merged->raBoresight, det->raBoresight);
     42            return NULL;
     43        }
     44        if (strcmp(merged->decBoresight, det->decBoresight) != 0) {
     45            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Exposure Dec values differ: %s vs %s",
     46                    merged->decBoresight, det->decBoresight);
     47            return NULL;
     48        }
     49        if (strcmp(merged->filter, det->filter) != 0) {
     50            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Exposure filter values differ: %s vs %s",
     51                    merged->filter, det->filter);
     52            return NULL;
     53        }
     54
     55        if (merged->airmass != det->airmass) {
     56            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Exposure airmass values differ: %f vs %f",
     57                    merged->airmass, det->airmass);
     58            return NULL;
     59        }
     60        if (merged->exptime != det->exptime) {
     61            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Exposure exposure time values differ: %f vs %f",
     62                    merged->exptime, det->exptime);
     63            return NULL;
     64        }
     65        if (merged->posangle != det->posangle) {
     66            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Exposure position angle values differ: %f vs %f",
     67                    merged->posangle, det->posangle);
     68            return NULL;
     69        }
     70        if (merged->alt != det->alt) {
     71            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Exposure altitude values differ: %lf vs %lf",
     72                    merged->alt, det->alt);
     73            return NULL;
     74        }
     75        if (merged->az != det->az) {
     76            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Exposure azimuth values differ: %lf vs %lf",
     77                    merged->az, det->az);
     78            return NULL;
     79        }
     80        if (merged->mjd != det->mjd) {
     81            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Exposure MJD values differ: %lf vs %lf",
     82                    merged->mjd, det->mjd);
     83            return NULL;
     84        }
     85
     86        merged->seeing += det->seeing;  // Taking average
     87
     88        psTree *tree = psTreePlant(2, LEAF_SIZE, PS_TREE_SPHERICAL, merged->ra, merged->dec); // kd tree
     89        if (!tree) {
     90            psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to generate kd tree");
     91            psFree(merged);
     92            return NULL;
     93        }
     94
     95        psVector *coords = psVectorAlloc(2, PS_TYPE_F64); // Coordinates of interest
     96        for (int j = 0; j < det->num; j++) {
     97            coords->data.F64[0] = det->ra->data.F64[j];
     98            coords->data.F64[1] = det->dec->data.F64[j];
     99            psVector *indices = psTreeAllWithin(tree, coords, MATCH_RADIUS); // Indices for matching sources
     100            if (!indices) {
     101                psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to search for matches");
     102                psFree(coords);
     103                psFree(tree);
     104                psFree(merged);
     105                return NULL;
     106            }
     107            if (indices->n == 0) {
     108                psFree(indices);
     109                continue;
     110            }
     111
     112            // Which one do we keep?
     113            float bestDistance = INFINITY; // Best distance to centre
     114            long bestIndex = -1;           // Index with best distance
     115            for (int k = 0; k < indices->n; k++) {
     116                long index = indices->data.S64[k]; // Index of point
     117                float distance = mergeDistance(merged, index); // Distance to centre of image
     118                if (distance < bestDistance) {
     119                    bestDistance = distance;
     120                    bestIndex = index;
     121                }
     122            }
     123
     124            float distance = mergeDistance(det, j); // Distance to centre of image
     125            if (distance < bestDistance) {
     126                // Blow away existing sources
     127                for (int k = 0; k < indices->n; k++) {
     128                    long index = indices->data.S64[k]; // Index of point
     129                    merged->mask->data.U8[index] = 0xFF;
     130                }
     131
     132                ppMopsDetectionsCopySingle(merged, det, j);
     133            }
     134            psFree(indices);
     135        }
     136
     137        psFree(tree);
     138        ppMopsDetectionsPurge(merged);
     139    }
     140
     141    merged->seeing /= num;
     142
     143    return merged;
    14144}
     145
Note: See TracChangeset for help on using the changeset viewer.