#include <stdlib.h>
#include <M3.h>
#define STRING_LENGTH 256
int main( int argc, char **argv )
{
M3_RunConfigStruct *runConfig;
M3_DataSetLL *dataSet;
M3_IntervalLL *intervalNode;
int notDone;
M3_PointingEl *pointing = NULL;
M3_PixelClassLL *pixelClass;
M3_PointingClassLL *pointingClass;
int32_t numNZ, numClassNZ, numPixelClass, mapSize;
M3_MapEl **mapArray;
double **weightArray;
int32_t *nnzArray;
int32_t numPixel;
int64_t i, j, k, l, n;
M3_Interval interval;
double *tod = NULL;
char mapName[STRING_LENGTH];
FILE *fid;
M3_MapHeader mapHeader;
char temp = 0;
M3_RunConfigStruct_Read(&runConfig, argv[1], M3_SPRINGTIDE_RUN_TYPE );
numPixelClass = 0;
for( notDone = M3_RunConfigStruct_GetPixelClassRoot(runConfig, &pixelClass); notDone; notDone = M3_PixelClassLL_GetNextNodeInList(&pixelClass) )
numPixelClass++;
mapArray = (M3_MapEl **)calloc(numPixelClass, sizeof(M3_MapEl *));
weightArray = (double **)calloc(numPixelClass*numPixelClass, sizeof(double *));
nnzArray = (int32_t *)malloc(numPixelClass*sizeof(int32_t));
for( i = 0, notDone = M3_RunConfigStruct_GetPixelClassRoot(runConfig, &pixelClass);
notDone;
i++, notDone = M3_PixelClassLL_GetNextNodeInList(&pixelClass) )
{
M3_PixelClassLL_GetNumPixelInClass( pixelClass, &mapSize);
mapArray[i] = (M3_MapEl *)calloc(mapSize,sizeof(M3_MapEl));
for( j = 0; j < mapSize; j++ )
{
mapArray[i][j].pixel = j;
mapArray[i][j].value = nan(&temp);
}
weightArray[i] = calloc(mapSize,sizeof(double));
}
for ( notDone = M3_RunConfigStruct_GetDataSetRoot( runConfig, &dataSet );
notDone;
M3_DataSetLL_GetNextNodeInList( &dataSet ))
{
M3_DataSetLL_GetNumNZ(dataSet, &numNZ);
for( notDone = M3_DataSetLL_GetCoveredIntervalRoot( dataSet, &intervalNode);
notDone;
notDone = M3_IntervalLL_GetNextNodeInList( &intervalNode ) )
{
M3_IntervalLL_GetInterval( intervalNode, &interval );
n = interval.lastSample - interval.firstSample + 1;
if( tod && pointing )
{
tod = (double*)realloc( tod, sizeof(double)*n);
pointing = (M3_PointingEl*)realloc( pointing, numNZ*sizeof(M3_PointingEl)*n );
}
else
{
tod = (double*)malloc( sizeof(double)*n);
pointing = (M3_PointingEl*)malloc( numNZ*sizeof(M3_PointingEl)*n );
}
M3_DataSetLL_GetPointing(dataSet, interval, pointing );
M3_DataSetLL_GetTOD( dataSet, interval, tod );
l = 0;
for( i = 0; i < n; i++ )
{
j = 0;
notDone = M3_DataSetLL_GetPointingClassRoot( dataSet, &pointingClass );
while( notDone )
{
M3_PointingClassLL_GetNumNZ( pointingClass, &numClassNZ );
for( k = 0; k < numClassNZ; k++ )
{
if(!isnan(mapArray[j][pointing[l].pixel].value))
{
mapArray[j][pointing[l].pixel].value += tod[i]*pointing[l].weight;
weightArray[j][pointing[l].pixel] += pointing[l].weight*pointing[l].weight;
}
else
{
mapArray[j][pointing[l].pixel].value = tod[i]*pointing[l].weight;
weightArray[j][pointing[l].pixel] = pointing[l].weight*pointing[l].weight;
}
l++;
}
j++;
notDone = M3_PointingClassLL_GetNextNodeInList( &pointingClass );
}
}
}
M3_RunConfigStruct_GetPixelClassRoot( runConfig, &pixelClass );
for( i = 0; i < numPixelClass; i++ )
{
M3_PixelClassLL_GetNumPixelInClass( pixelClass, &numPixel );
k = 0;
for( j = 0 ; j < numPixel; j++)
{
if( !isnan(mapArray[i][j].value) && weightArray[i][j] )
{
mapArray[i][k].value = mapArray[i][j].value/sqrt(weightArray[i][j]);
mapArray[i][k].pixel = mapArray[i][j].pixel;
k++;
}
}
M3_PixelClassLL_GetMapName( pixelClass, mapName, STRING_LENGTH );
fid = fopen( mapName, "w");
mapHeader.numPixelInClass = numPixel;
mapHeader.numPixelInMap = k;
fwrite( &mapHeader, 1, sizeof(M3_MapHeader), fid);
fwrite( mapArray[i], sizeof(M3_MapEl), k, fid);
fclose(fid);
M3_PixelClassLL_GetNextNodeInList( &pixelClass );
}
}
free(tod);
free(pointing);
return 0;
}