binMap.c

Go to the documentation of this file.
00001 /*******************************************************************************
00002 *   M3:  binMap.c                                                              *
00003 *                                                                              *
00004 *   Version 2.0.2 May 2007                                                     *
00005 *                                                                              *
00006 *   Copyright (C) 2004-2007  C.M. Cantalupo                                    *
00007 *                                                                              *
00008 *   This program is free software; you can redistribute it and/or modify       *
00009 *   it under the terms of the GNU General Public License as published by       *
00010 *   the Free Software Foundation; either version 2 of the License, or          *
00011 *   (at your option) any later version.                                        *
00012 *                                                                              *
00013 *   This program is distributed in the hope that it will be useful,            *
00014 *   but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00015 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
00016 *   GNU General Public License for more details.                               *
00017 *                                                                              *
00018 *   You should have received a copy of the GNU General Public License          *
00019 *   along with this program; if not, write to the Free Software                *
00020 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  *
00021 *                                                                              *
00022 *******************************************************************************/
00023 
00024 
00058 /*  Christopher M. Cantalupo November 2007 */
00059 
00060 #include <stdlib.h>
00061 #include <M3.h>
00062 
00063 #define STRING_LENGTH 256
00064 
00065 int main( int argc, char **argv )
00066 {
00067   M3_RunConfigStruct *runConfig;
00068   M3_DataSetLL *dataSet;
00069   M3_IntervalLL *intervalNode;
00070   int notDone;
00071   M3_PointingEl *pointing = NULL;
00072   M3_PixelClassLL *pixelClass;
00073   M3_PointingClassLL *pointingClass;
00074   int32_t numNZ, numClassNZ, numPixelClass, mapSize;
00075   M3_MapEl **mapArray;
00076   double **weightArray;
00077   int32_t *nnzArray;
00078   int32_t numPixel;
00079   int64_t i, j, k, l, n;
00080   M3_Interval interval;
00081   double *tod = NULL;
00082   char mapName[STRING_LENGTH];
00083   FILE *fid;
00084   M3_MapHeader mapHeader;
00085   char temp = 0;
00086 
00087   /* Read run config from disk */
00088   M3_RunConfigStruct_Read(&runConfig, argv[1], M3_SPRINGTIDE_RUN_TYPE );
00089 
00090   /* Count the number of pixel classes */
00091   numPixelClass = 0;
00092   for( notDone = M3_RunConfigStruct_GetPixelClassRoot(runConfig, &pixelClass); notDone; notDone = M3_PixelClassLL_GetNextNodeInList(&pixelClass) )
00093     numPixelClass++;
00094 
00095   /* Allocate some space */
00096   mapArray = (M3_MapEl **)calloc(numPixelClass, sizeof(M3_MapEl *));
00097   weightArray = (double **)calloc(numPixelClass*numPixelClass, sizeof(double *));
00098   nnzArray = (int32_t *)malloc(numPixelClass*sizeof(int32_t));
00099 
00100   /* Allocate maps and weights for each pixel class */
00101   for( i = 0, notDone = M3_RunConfigStruct_GetPixelClassRoot(runConfig, &pixelClass); 
00102        notDone; 
00103        i++, notDone = M3_PixelClassLL_GetNextNodeInList(&pixelClass) )
00104   {
00105     M3_PixelClassLL_GetNumPixelInClass( pixelClass, &mapSize);
00106     mapArray[i] = (M3_MapEl *)calloc(mapSize,sizeof(M3_MapEl));
00107     /* Initialize map */
00108     for( j = 0; j < mapSize; j++ )
00109     {  
00110       mapArray[i][j].pixel = j;
00111       mapArray[i][j].value = nan(&temp);
00112     }
00113     weightArray[i] = calloc(mapSize,sizeof(double));
00114   }
00115 
00116 
00117   /* Go through all of the dataSets (detectors) */
00118   for ( notDone = M3_RunConfigStruct_GetDataSetRoot( runConfig, &dataSet );
00119         notDone;
00120         M3_DataSetLL_GetNextNodeInList( &dataSet ))
00121   {
00122     /* Figure out how many non-zeros per row of the pointing matrix */
00123     M3_DataSetLL_GetNumNZ(dataSet, &numNZ);
00124 
00125     /* Go through the covered intervals */
00126     for( notDone = M3_DataSetLL_GetCoveredIntervalRoot( dataSet, &intervalNode);
00127          notDone;
00128          notDone = M3_IntervalLL_GetNextNodeInList( &intervalNode ) )
00129     {
00130       /* Retrieve the interval structure from the list node */
00131       M3_IntervalLL_GetInterval( intervalNode, &interval );
00132       /* Count the number of samples in the interval*/
00133       n = interval.lastSample - interval.firstSample + 1;
00134 
00135       /* If we have alread allocated tod and pointing, call realloc */
00136       if( tod && pointing )
00137       {
00138         tod = (double*)realloc( tod, sizeof(double)*n);
00139         pointing = (M3_PointingEl*)realloc( pointing, numNZ*sizeof(M3_PointingEl)*n );
00140       }
00141       /* Otherwise allocate memory with malloc */
00142       else
00143       {
00144         tod = (double*)malloc( sizeof(double)*n);
00145         pointing = (M3_PointingEl*)malloc( numNZ*sizeof(M3_PointingEl)*n );
00146       }
00147 
00148       /* Get the pointing data and the time ordered data*/
00149       M3_DataSetLL_GetPointing(dataSet, interval, pointing );
00150       M3_DataSetLL_GetTOD( dataSet, interval, tod );
00151 
00152       /* Cycle over all of the samples in the interval.   
00153          l keeps track of where you are in the pointing array */
00154       l = 0;
00155       /* i keeps track of the time index from the beginning of the interval */
00156       for( i = 0; i < n; i++ )
00157       {
00158         /* j keeps track of the pixel class index */
00159         j = 0;
00160         /* Get the root pointing class for this data set */
00161         notDone = M3_DataSetLL_GetPointingClassRoot( dataSet, &pointingClass );
00162         /* Go over the poiting classes */
00163         while( notDone )
00164         {
00165           /* Find out how many non-zeros per time sample this poining class contributes */
00166           M3_PointingClassLL_GetNumNZ( pointingClass, &numClassNZ );
00167           /* Loop over the non-zeros */
00168           for( k = 0; k < numClassNZ; k++ )
00169           {
00170             /* If the map element has already been set once, we can simply add to the current value */
00171             if(!isnan(mapArray[j][pointing[l].pixel].value))
00172             {
00173               /* Sum the weighted TOD into map */
00174               mapArray[j][pointing[l].pixel].value += tod[i]*pointing[l].weight;
00175               /* Sum the square of the weights */
00176               weightArray[j][pointing[l].pixel] += pointing[l].weight*pointing[l].weight;
00177             }
00178             /* Otherwise we need to set the value.  */
00179             else
00180             {
00181               /* Set the map to the weighted TOD value */
00182               mapArray[j][pointing[l].pixel].value = tod[i]*pointing[l].weight;
00183               /* Set the weight to the square of the weights */
00184               weightArray[j][pointing[l].pixel] = pointing[l].weight*pointing[l].weight;
00185             }
00186             l++;
00187           }
00188           j++;
00189           /* Go to the next pointing class */
00190           notDone = M3_PointingClassLL_GetNextNodeInList( &pointingClass ); 
00191         }
00192       }
00193     }
00194 
00195     /* Go over the pixel classes */
00196     M3_RunConfigStruct_GetPixelClassRoot( runConfig, &pixelClass );
00197     for( i = 0; i < numPixelClass; i++ )
00198     {
00199       /* Now divide the map by the sum of the squares of the weights 
00200          note that we have not actually solved for Q and U, but are 
00201          mearly finding the average weighted value.  */
00202       M3_PixelClassLL_GetNumPixelInClass( pixelClass, &numPixel );
00203       k = 0;
00204       for( j = 0 ; j < numPixel; j++)
00205       {
00206         if( !isnan(mapArray[i][j].value) && weightArray[i][j] )
00207         {
00208           mapArray[i][k].value = mapArray[i][j].value/sqrt(weightArray[i][j]);
00209           mapArray[i][k].pixel = mapArray[i][j].pixel;
00210           k++;
00211         }
00212       }
00213       /* Get the name of the map for output */
00214       M3_PixelClassLL_GetMapName( pixelClass, mapName, STRING_LENGTH );
00215       /* Write the map to disk in MADCAP3 format */
00216       fid = fopen( mapName, "w");
00217       mapHeader.numPixelInClass = numPixel;
00218       mapHeader.numPixelInMap = k;
00219       fwrite( &mapHeader, 1, sizeof(M3_MapHeader), fid);
00220       fwrite( mapArray[i], sizeof(M3_MapEl), k, fid);
00221       fclose(fid);
00222       /* Go to the next pixel class */
00223       M3_PixelClassLL_GetNextNodeInList( &pixelClass );
00224     }
00225   }
00226 
00227   free(tod);
00228   free(pointing);
00229   return 0;
00230 }
00231 

Generated on Mon Nov 24 10:05:11 2008 for M3 by  doxygen 1.5.3-20071008