createSetIntervals.c

00001 #include "M3.c"
00002 #include "fitsio.h"
00003 #include "M3_format_LevelS.h"
00004 
00005 #define M3_CONTENT_LENGTH 256
00006 #define M3_CREATESETINTERVALS_TIME_MODE 1
00007 #define M3_CREATESETINTERVALS_SAMPLE_MODE 2
00008 #define M3_CREATESETINTERVALS_POINTING_MODE 3
00009 
00010 
00011 void M3_createSetIntervals(M3_RunConfigStruct *runConfig, int mode, double timePeriod, int64_t samplePeriod, double maxTime, int64_t maxSample, int64_t periods )
00012 {
00013   fitsfile *thisFitsFile;
00014   M3_DataSetLL *thisDataSet;
00015   M3_IntervalLL *inIntervalList = NULL;
00016   M3_IntervalLL *newIntervalList = NULL;
00017   M3_IntervalLL *thisInterval = NULL;
00018   int64_t lastSample;
00019   double tempd;
00020   M3_FormatStruct_LevelS formatStruct;
00021   int status = 0;
00022   M3_File *theFile;
00023   char errorString[M3_CONTENT_LENGTH];
00024   char strBuffer[M3_CONTENT_LENGTH];
00025   int64_t numRow, i;
00026   M3_TimeEl firstTime, lastTime;
00027 
00028   
00029   for( thisDataSet = runConfig->dataSetList; 
00030        thisDataSet; 
00031        thisDataSet = thisDataSet->next  )
00032   {
00033     if( thisDataSet->coveredIntervalList )
00034     {
00035       for( thisInterval = thisDataSet->coveredIntervalList; thisInterval->next; thisInterval = thisInterval->next );
00036       lastSample = thisInterval->interval.lastSample;
00037       
00038       switch( mode )
00039       {
00040         case M3_CREATESETINTERVALS_TIME_MODE:
00041           samplePeriod = timePeriod*thisDataSet->sampleRate;
00042           maxSample = maxTime*thisDataSet->sampleRate;
00043           break;
00044         case M3_CREATESETINTERVALS_SAMPLE_MODE:
00045           if( maxSample && maxSample - 1 < lastSample )
00046             lastSample = maxSample - 1;
00047           newIntervalList = (M3_IntervalLL *)calloc(sizeof(M3_IntervalLL), 1);
00048           M3_ErrorCheck(-1, "M3_createSetIntervals()", (newIntervalList ? 1 : 0), M3_EFLAG_MALLOC_FSTRING );
00049           thisInterval = newIntervalList;
00050           thisInterval->interval.firstSample = 0;
00051           thisInterval->interval.lastSample = samplePeriod-1;
00052           while(thisInterval->interval.lastSample < lastSample )
00053           {
00054             thisInterval->next = (M3_IntervalLL *)calloc(sizeof(M3_IntervalLL), 1);
00055             M3_ErrorCheck(-1, "M3_createSetIntervals()", (thisInterval->next ? 1 : 0), M3_EFLAG_MALLOC_FSTRING );
00056             thisInterval->next->interval.firstSample = thisInterval->interval.lastSample + 1;
00057             thisInterval->next->interval.lastSample = thisInterval->interval.lastSample + samplePeriod;
00058             thisInterval = thisInterval->next; 
00059           }
00060           break;
00061         case M3_CREATESETINTERVALS_POINTING_MODE:
00062           M3_ErrorCheck(-1, "No GCPointingGroup in list", (thisDataSet->pointingClassList->subclassList->GCPointingGroupNode ? 1:0), M3_EFLAG_DEFAULT);
00063           M3_ErrorCheck(-1, "No simmission files", (thisDataSet->pointingClassList->subclassList->GCPointingGroupNode->GCPointingFileList ? 1:0), M3_EFLAG_DEFAULT );
00064           theFile = &(thisDataSet->pointingClassList->subclassList->GCPointingGroupNode->GCPointingFileList->file);
00065           M3_File_parseFormatString_LevelS( theFile, &formatStruct);
00066           M3_ErrorCheck(-1, "Simmission must be simmission2006 format", formatStruct.simmission2006, M3_EFLAG_DEFAULT);;
00067           M3_ErrorCheck(-1, "Multiple simmission files", thisDataSet->pointingClassList->subclassList->GCPointingGroupNode->GCPointingFileList->next == NULL, M3_EFLAG_DEFAULT );
00068           
00069           fits_open_file( &thisFitsFile, theFile->name , READONLY, &status);
00070           M3_ErrorCheck(-1, theFile->name, status == 0, M3_EFLAG_FOPEN_READ );
00071          
00072           fits_movabs_hdu( thisFitsFile, 2, NULL, &status );
00073           sprintf( errorString, "Could not go to second fits header in file %s", theFile->name );
00074           M3_ErrorCheck( -1, errorString, status == 0, M3_EFLAG_DEFAULT );
00075 
00076           fits_read_key_str( thisFitsFile, "NAXIS2", strBuffer, NULL, &status );
00077           sprintf( errorString, "Could not read NAXIS2 flag from second header unit of file: %s", theFile->name );
00078           M3_ErrorCheck(-1, errorString, status == 0, M3_EFLAG_DEFAULT );
00079           numRow = strtoll(strBuffer, NULL, 10);
00080           
00081           newIntervalList = (M3_IntervalLL *)calloc(sizeof(M3_IntervalLL), 1);
00082           M3_ErrorCheck(-1, "M3_createSetIntervals()", (newIntervalList ? 1 : 0), M3_EFLAG_MALLOC_FSTRING );
00083           thisInterval = newIntervalList;
00084           
00085           
00086           fits_read_col( thisFitsFile, TDOUBLE, 1, 1, 1, 1, NULL, &tempd, NULL, &status );
00087           firstTime.sec = tempd;
00088           fits_read_col( thisFitsFile, TDOUBLE, 2, 1, 1, 1, NULL, &tempd, NULL, &status );
00089           firstTime.nsec = tempd*1e9;
00090           fits_read_col( thisFitsFile, TDOUBLE, 13, periods, 1, 1, NULL, &tempd, NULL, &status );
00091           lastTime.sec = tempd;
00092           fits_read_col( thisFitsFile, TDOUBLE, 14, periods, 1, 1, NULL, &tempd, NULL, &status );
00093           lastTime.nsec = tempd*1e9;
00094           
00095           thisInterval->interval.firstSample = M3_myRound(M3_TimeEl_Difference(firstTime, thisDataSet->firstTime)*thisDataSet->sampleRate);
00096           thisInterval->interval.lastSample = M3_myRound(M3_TimeEl_Difference(lastTime, thisDataSet->firstTime)*thisDataSet->sampleRate);
00097           
00098           for(i = 1+periods; i <= numRow && thisInterval->interval.lastSample < lastSample; i += periods )
00099           {
00100             thisInterval->next = (M3_IntervalLL *)calloc(sizeof(M3_IntervalLL), 1);
00101             M3_ErrorCheck(-1, "M3_createSetIntervals()", (thisInterval->next ? 1 : 0), M3_EFLAG_MALLOC_FSTRING );
00102             thisInterval = thisInterval->next; 
00103             
00104             fits_read_col( thisFitsFile, TDOUBLE, 1, i, 1, 1, NULL, &tempd, NULL, &status );
00105             firstTime.sec = tempd;
00106             fits_read_col( thisFitsFile, TDOUBLE, 2, i, 1, 1, NULL, &tempd, NULL, &status );
00107             firstTime.nsec = tempd*1e9;
00108             fits_read_col( thisFitsFile, TDOUBLE, 13, i + periods - 1, 1, 1, NULL, &tempd, NULL, &status );
00109             lastTime.sec = tempd;
00110             fits_read_col( thisFitsFile, TDOUBLE, 14, i + periods - 1, 1, 1, NULL, &tempd, NULL, &status );
00111             lastTime.nsec = tempd*1e9;
00112             
00113             thisInterval->interval.firstSample = M3_myRound(M3_TimeEl_Difference(firstTime, thisDataSet->firstTime)*thisDataSet->sampleRate);
00114             thisInterval->interval.lastSample = M3_myRound(M3_TimeEl_Difference(lastTime, thisDataSet->firstTime)*thisDataSet->sampleRate) - 1;
00115           }
00116           break;
00117       }
00118     }
00119 
00120     inIntervalList = thisDataSet->coveredIntervalList;
00121     thisDataSet->coveredIntervalList = M3_intersectIntervalLists( inIntervalList, newIntervalList );
00122   
00123     M3_IntervalLL_destroyList( inIntervalList );
00124     M3_IntervalLL_destroyList( newIntervalList );
00125   }
00126 }
00127 
00128 
00129 
00130 int main( int argc, char **argv )
00131 {
00132   double timePeriod = 0;
00133   int64_t samplePeriod = 0;
00134   int mode;
00135   char runConfigName[M3_CONTENT_LENGTH];
00136   char runConfigNameOut[M3_CONTENT_LENGTH];
00137   double maxTime = 0;
00138   int64_t maxSample = 0;
00139   int64_t periods = 1;
00140 
00141   M3_RunConfigStruct *runConfig;
00142 
00143   /* Usage:  createSetIntervals { -t time [maxTime] | -n samples [maxSample] | -p [periods] } runConfig*/
00144 
00145 
00146   if( strcmp( argv[1], "-t") == 0 )
00147   {
00148     M3_ErrorCheck(-1, "Usage:  createSetIntervals { -t time [maxTime] | -s samples [maxSample] | -p [periods]} runConfig", argc >= 4, M3_EFLAG_DEFAULT );
00149     mode = M3_CREATESETINTERVALS_TIME_MODE;
00150     timePeriod = atof(argv[2]);
00151     if( argc == 4 )
00152       strcpy( runConfigName, argv[3]);
00153     else
00154     {
00155       maxTime = atof(argv[3]);
00156       strcpy( runConfigName, argv[4] );
00157     }
00158         
00159   }
00160   else if( strcmp(argv[1], "-s") == 0 )
00161   {
00162     M3_ErrorCheck(-1, "Usage:  createSetIntervals { -t time [maxTime] | -s samples [maxSample] | -p [periods]} runConfig", argc >= 4, M3_EFLAG_DEFAULT );
00163     mode = M3_CREATESETINTERVALS_SAMPLE_MODE;
00164     samplePeriod = strtoll(argv[2], NULL, 10);
00165     if( argc == 4 )
00166       strcpy( runConfigName, argv[3]);
00167     else
00168     {
00169       maxSample = strtoll(argv[3], NULL, 10);
00170       strcpy( runConfigName, argv[4]);
00171     }
00172   }
00173   else if( strcmp(argv[1], "-p") == 0 )
00174   {
00175     M3_ErrorCheck(-1, "Usage:  createSetIntervals { -t time [maxTime] | -s samples [maxSample] | -p [periods] } runConfig", argc >= 3, M3_EFLAG_DEFAULT );
00176     if( argc >= 4 )
00177     {
00178       periods = atoi( argv[2] );
00179       strcpy( runConfigName, argv[3]);      
00180     }
00181     else
00182     {
00183       periods = 1;
00184       strcpy( runConfigName, argv[2]);
00185     }      
00186     mode = M3_CREATESETINTERVALS_POINTING_MODE;
00187 
00188   }
00189   else
00190     M3_ErrorCheck(-1, "Usage:  createSetIntervals { -t time [maxTime] | -s samples [maxSample] | -p [periods] } runConfig", 0, M3_EFLAG_DEFAULT );
00191   
00192 
00193   M3_RunConfigStruct_Read(&runConfig, runConfigName, M3_SPRINGTIDE_RUN_TYPE);
00194 
00195   M3_createSetIntervals(runConfig, mode, timePeriod, samplePeriod, maxTime, maxSample, periods);
00196 
00197   sprintf( runConfigNameOut, "%s.csi", runConfigName);
00198 
00199   M3_RunConfigStruct_Write(runConfig, runConfigNameOut );
00200 
00201   M3_RunConfigStruct_Destroy( runConfig );
00202 
00203   return(0);
00204 }
00205 

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