00001 #include "M3.h"
00002 #include "M3_gcp.h"
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005 #include <sys/types.h>
00006 #include <sys/stat.h>
00007 #include "M3_format_ebex.h"
00008 #include "ccFitsTools.h"
00009
00010 #define M3_CONTENT_LENGTH 256
00011
00012 void M3_File_readHeader_ebex( M3_File *theFile, void *data )
00013 {
00014
00015 switch( theFile->fileType )
00016 {
00017 case M3_GCPOINTING_FILE_TYPE:
00018 M3_File_readHeader_ebex_gcpointing( theFile, (M3_GCPointingHeader *)data);
00019 break;
00020 case M3_AUX_FILE_TYPE:
00021 M3_File_readHeader_ebex_aux(theFile, data );
00022 break;
00023 default:
00024 M3_ErrorCheck(-1, "M3 can only read ebex files for the gcpointing and aux file types", 0, M3_EFLAG_DEFAULT );
00025 break;
00026 }
00027 }
00028
00029 void M3_File_readData_ebex( M3_File *theFile, void *readArg, void *data )
00030 {
00031
00032 switch( theFile->fileType )
00033 {
00034 case M3_GCPOINTING_FILE_TYPE:
00035 M3_File_readData_ebex_gcpointing(theFile, (M3_Interval*)readArg, (double*)data );
00036 break;
00037 case M3_AUX_FILE_TYPE:
00038 M3_File_readData_ebex_aux(theFile, data );
00039 break;
00040 default:
00041 M3_ErrorCheck(-1, "M3 can only read ebex files for the gcpointing and aux file types", 0, M3_EFLAG_DEFAULT );
00042 break;
00043 }
00044 }
00045
00046 void M3_File_parseFormatString_ebex( M3_File *theFile, M3_FormatStruct_ebex *out )
00047 {
00048 M3_FormatStruct_ebex formatStruct;
00049 char *s1, *s2;
00050 char errorString[M3_CONTENT_LENGTH];
00051 static char lastFormatString[M3_CONTENT_LENGTH] = {0};
00052 static M3_FormatStruct_ebex lastFormatStruct;
00053
00054
00055 if( strcmp( theFile->format, lastFormatString ) == 0 )
00056 {
00057 *out = lastFormatStruct;
00058 return;
00059 }
00060
00061
00062 out->sampleRate = 0;
00063 out->polarSpinRate = 0;
00064 out->polarSpinStart.sec = 0;
00065 out->polarSpinStart.nsec = 0;
00066 out->firstTime.sec = 0;
00067 out->firstTime.nsec = 0;
00068 out->isBigEndian = testEndian();
00069
00070 sprintf( errorString, "Error interpreting format Xstring: \"%s\"", theFile->format );
00071
00072
00073 s1 = strchr( theFile->format, '_');
00074
00075
00076 while( s1 )
00077 {
00078
00079 s1++;
00080
00081
00082 if( strncmp( s1, "firstTimeSec", 12 ) == 0 )
00083 {
00084 s1 += 12;
00085 M3_ErrorCheck( -1, errorString, *s1 == '=', M3_EFLAG_DEFAULT);
00086 s1++;
00087 out->firstTime.sec = strtoll(s1, NULL, 10);
00088 }
00089 else if( strncmp( s1, "firstTimeNsec", 13 ) == 0 )
00090 {
00091 s1 += 13;
00092 M3_ErrorCheck( -1, errorString, *s1 == '=', M3_EFLAG_DEFAULT);
00093 s1++;
00094 out->firstTime.nsec = atof(s1);
00095 }
00096 else if( strncmp( s1, "sampleRate", 10 ) == 0 )
00097 {
00098 s1 += 10;
00099 M3_ErrorCheck( -1, errorString, *s1 == '=', M3_EFLAG_DEFAULT);
00100 s1++;
00101 out->sampleRate = atof(s1);
00102 }
00103 else if( strncmp( s1, "polarSpinRate", 13 ) == 0 )
00104 {
00105 s1 += 13;
00106 M3_ErrorCheck( -1, errorString, *s1 == '=', M3_EFLAG_DEFAULT);
00107 s1++;
00108 out->polarSpinRate = atof(s1);
00109 }
00110 else if( strncmp( s1, "polarSpinStartSec", 17 ) == 0 )
00111 {
00112 s1 += 17;
00113 M3_ErrorCheck( -1, errorString, *s1 == '=', M3_EFLAG_DEFAULT);
00114 s1++;
00115 out->polarSpinStart.sec = atof(s1);
00116 }
00117 else if( strncmp( s1, "polarSpinStartNsec", 18 ) == 0 )
00118 {
00119 s1 += 18;
00120 M3_ErrorCheck( -1, errorString, *s1 == '=', M3_EFLAG_DEFAULT);
00121 s1++;
00122 out->polarSpinStart.nsec = atof(s1);
00123 }
00124 else if( strncmp( s1, "endian", 6) == 0 )
00125 {
00126 s1 += 6;
00127 M3_ErrorCheck( -1, errorString, *s1 == '=', M3_EFLAG_DEFAULT);
00128 s1++;
00129 if( strncmp( s1, "big", 3) == 0 )
00130 out->isBigEndian = 1;
00131 else if( strncmp( s1, "little", 6) == 0)
00132 out->isBigEndian = 0;
00133 else
00134 M3_ErrorCheck(-1, errorString, 0, M3_EFLAG_DEFAULT);
00135 }
00136 s1 = strchr( s1, '_');
00137 }
00138
00139
00140
00141 strcpy( lastFormatString, theFile->format );
00142 lastFormatStruct = *out;
00143
00144 }
00145
00146
00147 void M3_File_readHeader_ebex_gcpointing( M3_File *theFile, M3_GCPointingHeader *data )
00148 {
00149 M3_FormatStruct_ebex formatStruct;
00150 M3_TimeEl firstTime;
00151 struct stat statStruct;
00152
00153 M3_File_parseFormatString_ebex( theFile, &formatStruct );
00154
00155 data->firstTime_sec = formatStruct.firstTime.sec;
00156 data->firstTime_nsec = formatStruct.firstTime.nsec;
00157 stat(theFile->name, &statStruct);
00158 data->numSample = statStruct.st_size/(3*sizeof(double));
00159 data->sampleRate = formatStruct.sampleRate;
00160 data->numDataPerSample = 3;
00161 }
00162
00163 void M3_File_readData_ebex_gcpointing( M3_File *theFile, M3_Interval *readInterval, double *data )
00164 {
00165 FILE *fid;
00166 M3_FormatStruct_ebex formatStruct;
00167 int64_t nread;
00168
00169
00170 M3_File_parseFormatString_ebex( theFile, &formatStruct );
00171
00172
00173 fid = fopen(theFile->name, "r");
00174 M3_ErrorCheck(-1, theFile->name, (fid?1:0), M3_EFLAG_FOPEN_READ );
00175 fseek(fid, (readInterval->firstSample)*3*sizeof(double), SEEK_SET );
00176
00177
00178 nread = fread( data, 3*sizeof(double), readInterval->lastSample - readInterval->firstSample + 1, fid );
00179 M3_ErrorCheck(-1, theFile->name, nread == readInterval->lastSample - readInterval->firstSample + 1, M3_EFLAG_FREAD_COUNT );
00180
00181 if( formatStruct.isBigEndian != testEndian() )
00182 flipByteOrder( (void*)data, 3*(readInterval->lastSample - readInterval->firstSample + 1), sizeof(double));
00183
00184 fclose(fid);
00185 }
00186
00187
00188 void M3_File_readHeader_ebex_aux( M3_File *theFile, M3_AuxHeader *data )
00189 {
00190 char line[M3_CONTENT_LENGTH] = {0};
00191 FILE *fid;
00192 int j;
00193
00194
00195 fid = fopen(theFile->name, "r");
00196 M3_ErrorCheck(-1, theFile->name, (fid?1:0), M3_EFLAG_FOPEN_READ );
00197
00198
00199 j = 0;
00200 while( fgets( line, M3_CONTENT_LENGTH, fid ) )
00201 {
00202 if( line[0] != '#' && line[0] != '\0' )
00203 j++;
00204 }
00205
00206 data->size = sizeof(M3_ebexAux) + j*sizeof(M3_ebexDetector);
00207
00208 fclose(fid);
00209
00210 }
00211
00212
00213 void M3_File_readData_ebex_aux( M3_File *theFile, void *data )
00214 {
00215 char line[M3_CONTENT_LENGTH] = {0};
00216 FILE *fid;
00217 M3_ebexAux *ebexAux;
00218 M3_FormatStruct_ebex formatStruct;
00219 int numLines,j;
00220
00221 M3_File_parseFormatString_ebex( theFile, &formatStruct );
00222
00223 fid = fopen(theFile->name, "r");
00224 M3_ErrorCheck(-1, theFile->name, (fid?1:0), M3_EFLAG_FOPEN_READ );
00225
00226 numLines = 0;
00227 while( fgets( line, M3_CONTENT_LENGTH, fid ) )
00228 {
00229 if( line[0] != '#' && line[0] != '\0' )
00230 numLines++;
00231 }
00232
00233 fseek(fid, 0, SEEK_SET );
00234
00235 ebexAux = data;
00236 data = ((char*)data+sizeof(M3_ebexAux));
00237 ebexAux->polarSpinStart = formatStruct.polarSpinStart;
00238 ebexAux->polarSpinRate = formatStruct.polarSpinRate;
00239 ebexAux->numDetector = numLines;
00240 ebexAux->detectorInfo = data;
00241
00242 j = 0;
00243 while( fgets( line, M3_CONTENT_LENGTH, fid ) )
00244 {
00245 if( line[0] != '#' && line[0] != '\0' )
00246 {
00247 sscanf(line, "%d %*d %*d %*d %lf %lf %*d %*d", &(ebexAux->detectorInfo[j].detectorID), &(ebexAux->detectorInfo[j].offset[0]), &(ebexAux->detectorInfo[j].offset[1]) );
00248 j++;
00249 }
00250 }
00251 fclose(fid);
00252 }