Changeset 5654
- Timestamp:
- Dec 1, 2005, 9:55:16 AM (20 years ago)
- Location:
- trunk/psphot/src
- Files:
-
- 1 added
- 4 edited
-
psSparse.c (modified) (16 diffs)
-
psSparse.h (added)
-
psphot.h (modified) (1 diff)
-
psphotSparseMatrix.c (modified) (4 diffs)
-
psphotTest.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psphot/src/psSparse.c
r5653 r5654 1 1 # include "psphot.h" 2 3 typedef struct {4 psVector *Aij;5 psVector *Bfj;6 psVector *Qii;7 psVector *Si;8 psVector *Sj;9 int Nelem;10 int Nrows;11 } psSparse;12 2 13 3 void psSparseMatrixTest () { … … 16 6 psSparse *sparse = psSparseAlloc (3, 9); 17 7 18 psSparseMatrixElement (sparse, 0, 0, 1.0);19 psSparseMatrixElement (sparse, 1, 1, 1.0);8 psSparseMatrixElement (sparse, 0, 0, 3.0); 9 psSparseMatrixElement (sparse, 1, 1, 2.0); 20 10 psSparseMatrixElement (sparse, 2, 2, 1.0); 21 11 22 psSparseMatrixElement (sparse, 0, 1, 0.1);23 psSparseMatrixElement (sparse, 0, 2,0.1);24 25 psSparse MatrixResort (sparse);12 psSparseMatrixElement (sparse, 1, 0, 0.1); 13 psSparseMatrixElement (sparse, 2, 0, -0.1); 14 15 psSparseResort (sparse); 26 16 for (int i = 0; i < sparse->Nelem; i++) { 27 17 fprintf (stderr, "%d %d %f\n", … … 33 23 psVector *x = psVectorAlloc (3, PS_DATA_F32); 34 24 x->data.F32[0] = 3; 35 x->data.F32[0] = 5; 36 x->data.F32[0] = 7; 37 38 psVector B = psSparseMatrixTimesVector (NULL, sparse, x); 25 x->data.F32[1] = 5; 26 x->data.F32[2] = 7; 27 fprintf (stderr, "x: %f %f %f\n", x->data.F32[0], x->data.F32[1], x->data.F32[2]); 28 29 psVector *B = psSparseMatrixTimesVector (NULL, sparse, x); 39 30 fprintf (stderr, "B: %f %f %f\n", B->data.F32[0], B->data.F32[1], B->data.F32[2]); 40 31 … … 43 34 sparse->Bfj->data.F32[2] = B->data.F32[2]; 44 35 45 x = psSparseSolve (x, sparse); 36 x = psSparseSolve (x, sparse, 0); 37 fprintf (stderr, "x: %f %f %f\n", x->data.F32[0], x->data.F32[1], x->data.F32[2]); 38 39 x = psSparseSolve (x, sparse, 1); 40 fprintf (stderr, "x: %f %f %f\n", x->data.F32[0], x->data.F32[1], x->data.F32[2]); 41 42 x = psSparseSolve (x, sparse, 2); 43 fprintf (stderr, "x: %f %f %f\n", x->data.F32[0], x->data.F32[1], x->data.F32[2]); 44 45 x = psSparseSolve (x, sparse, 3); 46 fprintf (stderr, "x: %f %f %f\n", x->data.F32[0], x->data.F32[1], x->data.F32[2]); 47 48 x = psSparseSolve (x, sparse, 4); 46 49 fprintf (stderr, "x: %f %f %f\n", x->data.F32[0], x->data.F32[1], x->data.F32[2]); 47 50 return; … … 50 53 void psSparseResort (psSparse *sparse) { 51 54 52 Nelem = sparse->Nelem;55 int Nelem = sparse->Nelem; 53 56 54 57 psVector *index = psVectorSortIndex (NULL, sparse->Sj); 55 psVector Aij = sparse->Aij;56 psVector Si = sparse->Si;57 psVector Sj = sparse->Sj;58 psVector *Aij = sparse->Aij; 59 psVector *Si = sparse->Si; 60 psVector *Sj = sparse->Sj; 58 61 59 62 // allocate new temporary vectors … … 61 64 psVector *tSi = psVectorAlloc (Nelem, PS_DATA_S32); 62 65 psVector *tSj = psVectorAlloc (Nelem, PS_DATA_S32); 63 for (i = 0; i < Nelem; i++) {64 j = index->data.U32[i];66 for (int i = 0; i < Nelem; i++) { 67 int j = index->data.U32[i]; 65 68 tAij->data.F32[i] = Aij->data.F32[j]; 66 69 tSi->data.S32[i] = Si->data.S32[j]; … … 80 83 void psSparseMatrixElement (psSparse *sparse, int i, int j, float value) { 81 84 85 int k; 86 82 87 if (i < j) { 83 88 fprintf (stderr, "*** error: subdiagonal element ***\n"); … … 91 96 // check vectors lengths and extend if needed 92 97 if (sparse->Nelem >= sparse->Aij->nalloc) { 93 sparse->Aij->nalloc += 100; 94 psVectorRealloc (sparse->Aij, sparse->Aij->nalloc); 95 sparse->Si->nalloc += 100; 96 psVectorRealloc (sparse->Si, sparse->Si->nalloc); 97 sparse->Sj->nalloc += 100; 98 psVectorRealloc (sparse->Sj, sparse->Sj->nalloc); 98 psVectorRealloc (sparse->Aij, sparse->Aij->nalloc + 100); 99 psVectorRealloc (sparse->Si, sparse->Si->nalloc + 100); 100 psVectorRealloc (sparse->Sj, sparse->Sj->nalloc + 100); 99 101 } 100 102 … … 111 113 // check vectors lengths and extend if needed 112 114 if (sparse->Nelem >= sparse->Aij->nalloc - 1) { 113 sparse->Aij->nalloc += 100; 114 psVectorRealloc (sparse->Aij, sparse->Aij->nalloc); 115 sparse->Si->nalloc += 100; 116 psVectorRealloc (sparse->Si, sparse->Si->nalloc); 117 sparse->Sj->nalloc += 100; 118 psVectorRealloc (sparse->Sj, sparse->Sj->nalloc); 115 psVectorRealloc (sparse->Aij, sparse->Aij->nalloc + 100); 116 psVectorRealloc (sparse->Si, sparse->Si->nalloc + 100); 117 psVectorRealloc (sparse->Sj, sparse->Sj->nalloc + 100); 119 118 } 120 119 … … 139 138 void psSparseVectorElement (psSparse *sparse, int i, float value) { 140 139 141 sparse->B jf->data.F32[i] = value;140 sparse->Bfj->data.F32[i] = value; 142 141 return; 143 142 } … … 146 145 psVector *psSparseMatrixTimesVector (psVector *output, psSparse *matrix, psVector *vector) { 147 146 147 int i, Nelem; 148 float F; 149 148 150 if (output == NULL) { 149 psVector *output = psVectorAlloc (vector->n, PS_DATA_F32);151 output = psVectorAlloc (vector->n, PS_DATA_F32); 150 152 } 151 153 … … 154 156 F = 0; 155 157 while (matrix->Sj->data.S32[Nelem] == j) { 156 i = matrix->Sj->data.S32[Nelem]; 157 F += vector->data.F32[i] * matrix->Aij->data.F32[i]; 158 i = matrix->Si->data.S32[Nelem]; 159 F += vector->data.F32[i] * matrix->Aij->data.F32[Nelem]; 160 Nelem++; 158 161 } 159 162 output->data.F32[j] = F; … … 162 165 } 163 166 164 psVector *psSparseSolve (psVector *guess, psSparse *sparse ) {167 psVector *psSparseSolve (psVector *guess, psSparse *sparse, int Niter) { 165 168 166 169 psF32 dG; … … 169 172 psVector *Bfj = sparse->Bfj; 170 173 171 guess = psVectorCopy (guess, Bfj );174 guess = psVectorCopy (guess, Bfj, PS_DATA_F32); 172 175 173 176 // temporary storage for intermediate results 174 177 psVector *dQ = psVectorAlloc (guess->n, PS_DATA_F32); 175 178 176 for (int j = 0; j < 2; j++) {179 for (int j = 0; j < Niter; j++) { 177 180 dQ = psSparseMatrixTimesVector (dQ, sparse, guess); 178 for (int i = 0; i < d G->n; i++) {181 for (int i = 0; i < dQ->n; i++) { 179 182 dG = (dQ->data.F32[i] - Bfj->data.F32[i]) / Qii->data.F32[i]; 180 183 guess->data.F32[i] -= dG; … … 187 190 static void psSparseFree (psSparse *sparse) { 188 191 if (sparse == NULL) return; 189 psFree (sparse .Aij);190 psFree (sparse .Bfj);191 psFree (sparse .Qii);192 psFree (sparse .Si);193 psFree (sparse .Sj);192 psFree (sparse->Aij); 193 psFree (sparse->Bfj); 194 psFree (sparse->Qii); 195 psFree (sparse->Si); 196 psFree (sparse->Sj); 194 197 return; 195 198 } … … 212 215 213 216 sparse->Nrows = Nrows; 217 psMemSetDeallocator(sparse, (psFreeFunc) psSparseFree); 214 218 return (sparse); 215 219 } -
trunk/psphot/src/psphot.h
r5643 r5654 9 9 # include "pmPSFtry.h" 10 10 # include "pmModelGroup.h" 11 # include "psSparse.h" 11 12 12 13 typedef struct { -
trunk/psphot/src/psphotSparseMatrix.c
r5653 r5654 1 1 # include "psphot.h" 2 2 3 psSparse *psphotStarOverlaps (psArray *sources) { 3 bool psphotEnsemblePSF (eamReadout *imdata, psMetadata *config, psArray *sources, pmPSF *psf, psStats *sky) { 4 4 5 pmModelFunc modelFunc = pmModelFunc_GetFunction (model->type); 6 psVector *x = psVectorAlloc(2, PS_TYPE_F32); 7 psVector *params = model->params; 5 bool status; 6 float x; 7 float y; 8 float Sky; 9 int Nfit = 0; 8 10 9 // source analysis is done in S/N order (brightest first) 11 psTimerStart ("psphot"); 12 13 // source analysis is done in spatial order 10 14 sources = psArraySort (sources, psphotSortByY); 11 15 12 // fill out the models for each object 13 // XXX need to assign the model parameters using the PSF function, with unit peak 16 float OUTER_RADIUS = psMetadataLookupF32 (&status, config, "SKY_OUTER_RADIUS"); 17 float PSF_FIT_NSIGMA = psMetadataLookupF32 (&status, config, "PSF_FIT_NSIGMA"); 18 float PSF_FIT_PADDING = psMetadataLookupF32 (&status, config, "PSF_FIT_PADDING"); 19 20 // set the object surface-brightness limit for fitted pixels 21 float FLUX_LIMIT = PSF_FIT_NSIGMA * sky->sampleStdev; 22 psLogMsg ("psphot.apply_psf_model", 4, "fitting pixels with at least %f object counts\n", FLUX_LIMIT); 23 24 // vector for model input coordinates 25 psVector *x = psVectorAlloc(2, PS_TYPE_F32); 26 27 // this function specifies the radius at this the model hits the given flux 28 pmModelRadius modelRadius = pmModelRadius_GetFunction (psf->type); 29 30 // this is the function to get the model function 31 pmModelFunc modelFunc = pmModelFunc_GetFunction (model->type); 32 33 // pre-calculate all model pixels 14 34 psArray *pixlist = psArrayAlloc (sources->n); 15 35 for (int k = 0; k < sources->n; k++) { 16 src = sources->data[k]; 36 source = sources->data[k]; 37 38 // use the source moments, etc to guess basic model parameters 39 pmModel *modelFLT = pmSourceModelGuess (source, psf->type); 40 41 // set PSF parameters for this model 42 pmModel *model = pmModelFromPSF (modelFLT, psf); 43 // model->params->data.F32[2] = source->peak->x; 44 // model->params->data.F32[3] = source->peak->y; 45 model->params->data.F32[1] = 1.0; 46 x = model->params->data.F32[2]; 47 y = model->params->data.F32[3]; 48 psFree (modelFLT); 49 50 // set the fit radius based on the object flux limit and the model 51 // XXX EAM : FLUX_LIMIT should be set based on local sky model (not global median) 52 model->radius = modelRadius (model->params, FLUX_LIMIT) + PSF_FIT_PADDING; 53 if (isnan(model->radius)) { 54 psAbort ("apply_psf_model", "error in radius"); 55 } 56 57 // build the model image 17 58 nx = src->pixels->numCols; 18 59 ny = src->pixels->numRows; 19 60 im = psImageAlloc (nx, ny, PS_DATA_F32); 61 mk = psImageAlloc (nx, ny, PS_DATA_U8); 62 63 // fill in the model pixel values 64 psImageKeepCircle (mk, x, y, model->radius, "OR", PSPHOT_MASK_MARKED); 65 psVector *params = model->params; 20 66 for (int i = 0; i < nx; i++) { 21 67 for (int j = 0; j < ny; j++) { 68 if (mk->data.U8[j][i]) continue; 22 69 x->data.F32[0] = (float) i + pix->col0; 23 70 x->data.F32[1] = (float) j + pix->row0; … … 28 75 } 29 76 30 psVector *Aij = psVectorAlloc (100, PS_DATA_F32); 31 psVector *Bfj = psVectorAlloc (100, PS_DATA_F32); 32 psVector *Si = psVectorAlloc (100, PS_DATA_S32); 33 psVector *Sj = psVectorAlloc (100, PS_DATA_S32); 77 // fill out the sparse matrix 78 psSparse *sparse = psSparseAlloc (sources->n, 100); 79 psVector *Aij = sparse->Aij; 80 psVector *Bfj = sparse->Bfj; 81 psVector *Si = sparse->Si; 82 psVector *Sj = sparse->Sj; 34 83 35 84 int Ri, Rj; … … 42 91 // diagonal element (auto-cross-product) 43 92 f = psphotCrossProduct (pi, pi); 44 Aij->data.F32[Nelem] = f; 45 Si->data.S32[Nelem] = i; 46 Sj->data.S32[Nelem] = j; 47 Nelem ++; 48 49 Qii->data.F32[i] = f; 93 psSparseMatrixElement (sparse, i, i, f); 50 94 51 95 // find the image x model value 52 96 f = psphotCrossProduct (pi, pf); 53 Bfj->data.F32[i] = f;97 psSparseVectorElement (sparse, i, f); 54 98 55 99 // loop over all other stars following this one … … 64 108 // got an overlap; calculate cross-product and add to output array 65 109 f = psphotCrossProduct (pi, pj); 66 67 Aij->data.F32[Nelem] = f; 68 Si->data.S32[Nelem] = i; 69 Sj->data.S32[Nelem] = j; 70 Nelem ++; 71 72 Aij->data.F32[Nelem] = f; 73 Si->data.S32[Nelem] = j; 74 Sj->data.S32[Nelem] = i; 75 Nelem ++; 110 psSparseMatrixElement (sparse, j, i, f); 76 111 } 77 112 } 78 79 index = psVectorSortIndex (NULL, Sj); 80 81 psVector *tAij = psVectorAlloc (Nelem, PS_DATA_F32); 82 psVector *tSi = psVectorAlloc (Nelem, PS_DATA_S32); 83 psVector *tSj = psVectorAlloc (Nelem, PS_DATA_S32); 84 for (i = 0; i < Nelem; i++) { 85 j = index->data.U32[i]; 86 tAij->data.F32[i] = Aij->data.F32[j]; 87 tSi->data.S32[i] = Si->data.S32[j]; 88 tSj->data.S32[i] = Sj->data.S32[j]; 89 } 90 psFree (Aij); 91 psFree (Si); 92 psFree (Sj); 93 94 sparse->Aij = Aij; 95 sparse->Bfj = Bfj; 96 sparse->Qii = Qii; 97 sparse->Si = Si; 98 sparse->Sj = Sj; 99 sparse->Nelem = Nelem; 100 sparse->Nrows = sources->n; 101 113 114 psSparseResort (sparse); 102 115 return (match); 103 116 } -
trunk/psphot/src/psphotTest.c
r5652 r5654 3 3 int main (int argc, char **argv) { 4 4 5 ps photSparseMatrixTest ();5 psSparseMatrixTest (); 6 6 7 7 }
Note:
See TracChangeset
for help on using the changeset viewer.
