00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdio.h>
00029 #include <ctype.h>
00030 #include <stdlib.h>
00031 #include <unistd.h>
00032 #include <time.h>
00033 #include <string.h>
00034 #include <assert.h>
00035 #include <sys/types.h>
00036
00037 #include "common.h"
00038
00039 FILE *seg2ChannelFile[MAX_CHANNEL] = {
00040 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
00041 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
00042 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
00043 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
00044 NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
00045 };
00046
00047 char seg2ChannelFileName[MAX_CHANNEL][128];
00048
00049 int KFULLSEG = 1;
00050
00051
00052
00053 FILE *seg2File = NULL;
00054 char seg2FileName[128];
00055 int NbrChan=0;
00056 int CurChan=0;
00057
00058 int AddString(FILE *f, char *str)
00059 {
00060 int16_t u16;
00061 if (str==NULL)
00062 {
00063 u16=0;
00064 fwrite(&u16,2,1,f);
00065 return(0);
00066 }
00067
00068 u16=strlen(str)+3;
00069 fwrite(&u16,2,1,f);
00070
00071
00072 fwrite(str,1,u16-3,f);
00073
00074
00075 u16=0;
00076 fwrite(&u16,1,1,f);
00077 return(0);
00078 }
00079
00080
00083 int WriteFileDescriptorBlock(FILE *f)
00084 {
00085 int16_t u16;
00086 int32_t u32;
00087 unsigned char u8;
00088 int i;
00089 int32_t firstTraceOffset;
00090 char string[512];
00091 struct tm tm;
00092 time_t T;
00093 int channel;
00094
00095 channel =0;
00096
00097 u16=0x3a55;
00098 fwrite(&u16,2,1,f);
00099
00100 u16=1;
00101 fwrite(&u16,2,1,f);
00102
00103 u16=(int16_t) 4*NbrChan;
00104 fwrite(&u16,2,1,f);
00105
00106 u16=(int16_t) NbrChan;
00107 fwrite(&u16,2,1,f);
00108
00109 u8=1;
00110 fwrite(&u8,1,1,f);
00111
00112 u8=0;
00113 fwrite(&u8,1,1,f);
00114
00115 u8=0;
00116 fwrite(&u8,1,1,f);
00117
00118 u8=1;
00119 fwrite(&u8,1,1,f);
00120
00121 u8='\n';
00122 fwrite(&u8,1,1,f);
00123
00124 u8=0;
00125 fwrite(&u8,1,1,f);
00126
00127 u8=0;
00128 for (i=14; i<32; i++)
00129 {
00130 fwrite(&u8,1,1,f);
00131 }
00132
00133 firstTraceOffset=ftell(f);
00134
00135 u32=0;
00136 for (i=1; i<=NbrChan; i++)
00137 {
00138 fwrite(&u32, 4, 1, f);
00139 }
00140
00141
00142
00143 T=(time_t)infoHeader[channel].correctedTime;
00144 strcpy(string,"ACQUISITION_DATE ");
00145 memcpy(&tm,gmtime(&T),sizeof(struct tm));
00146 strftime(string+strlen(string),64,"%d/%m/%Y",&tm);
00147 AddString(f, string);
00148
00149 strcpy(string,"ACQUISITION_TIME ");
00150 strftime(string+strlen(string),64,"%H:%M:%S",&tm);
00151 AddString(f, string);
00152 sprintf(string,"ACQUISITION_TIME_US %6d",
00153 (int)(1.e6*(infoHeader[channel].correctedTime-(double)T)));
00154 AddString(f, string);
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 sprintf(string,"TRACE_SORT AS_ACQUIRED");
00165 AddString(f, string);
00166
00167
00168 sprintf(string,"UNITS METERS");
00169 AddString(f, string);
00170
00171 AddString(f, NULL);
00172
00173
00174 u32=ftell(f);
00175 while (u32%4!=0)
00176 {
00177 u8=0;
00178 fwrite(&u8,1,1,f);
00179 u32=ftell(f);
00180 }
00181
00182 fseek(f,firstTraceOffset,SEEK_SET);
00183
00184 fwrite(&u32, 4, 1, f);
00185
00186 fseek(f,u32,SEEK_SET);
00187 return(0);
00188 }
00189
00194 int UpdateFileDescriptorBlock(FILE *f,int channelS0)
00195 {
00196
00197
00198
00199
00200
00201 int32_t u32;
00202
00203 u32 = ftell(f);
00204
00205
00206 fseek(f,32+4*channelS0,SEEK_SET);
00207 fwrite(&u32,4,1,f);
00208
00209 fseek(f,u32,SEEK_SET);
00210 return(0);
00211 }
00212
00213
00214
00219
00220
00221
00222 int WriteTraceDescriptorBlock(FILE *f, int channel)
00223 {
00224 int16_t u16;
00225 int32_t u32;
00226 unsigned char u8;
00227 int i;
00228 int32_t blockSizeOffset;
00229 char string[512];
00230
00231
00232
00233 blockSizeOffset=ftell(f);
00234
00235 u16=0x4422;
00236 fwrite(&u16,2,1,f);
00237
00238 u16=0;
00239 fwrite(&u16,2,1,f);
00240
00241 u32=infoHeader[channel].npts*4;
00242 fwrite(&u32,4,1,f);
00243
00244 u32=infoHeader[channel].npts;
00245 fwrite(&u32,4,1,f);
00246
00247 u8=2;
00248 fwrite(&u8,1,1,f);
00249
00250 u8=0;
00251 for (i=13; i<32; i++)
00252 {
00253 fwrite(&u8,1,1,f);
00254 }
00255
00256 sprintf(string,"CHANNEL_NUMBER %2d",channel);
00257 AddString(f, string);
00258
00259
00260
00261
00262
00263 sprintf(string,"DELAY %6f",0.0);
00264 AddString(f, string);
00265
00266
00267 sprintf(string,"DESCALING_FACTOR %6f",
00268 HWConfig.LSB/(float)HWConfig.gain[HWConfig.selectedGain[channel]]/1000000.);
00269 AddString(f, string);
00270
00271
00272 sprintf(string,"RAW_RECORD %s",currentFile);
00273 AddString(f, string);
00274
00275
00276
00277
00278
00279 sprintf(string,"RECEIVER_LOCATION %f %f %f",0.0,0.0,0.0);
00280 AddString(f, string);
00281
00282 sprintf(string,"RECEIVER_NAME %s",infoHeader[channel].channel);
00283 AddString(f, string);
00284
00285
00286 sprintf(string,"SAMPLE_INTERVAL %6f",(double)infoHeader[channel].sr.div/(double)infoHeader[channel].sr.base);
00287 AddString(f, string);
00288
00289
00290 AddString(f, NULL);
00291
00292
00293 u32=ftell(f)-blockSizeOffset;
00294 while (u32%4!=0)
00295 {
00296 u8=0;
00297 fwrite(&u8,1,1,f);
00298 u32=ftell(f)-blockSizeOffset;
00299 }
00300
00301 fseek(f,blockSizeOffset+2,SEEK_SET);
00302
00303
00304 u16 = u32;
00305 fwrite(&u16, 2, 1, f);
00306
00307 fseek(f,blockSizeOffset+u32,SEEK_SET);
00308 return(0);
00309 }
00310
00311 FILE* OpenFullSeg2(char* path,int channel)
00312 {
00313 if (seg2File==NULL)
00314 {
00315
00316 if (KFULLSEG)
00317 {
00318 int fno=fileNameOptions;
00319
00320 fno&=~ADD_CHANNEL_CODE;
00321 fno&=~ADD_NETWORK_CODE;
00322 strcpy(path,FileName("", ".SEG", channel,fno));
00323 PrintLog("OUTPUT: %s\n",path);
00324 }
00325 else
00326 {
00327 strcpy(path,FileName("SINGLE", ".SEG", channel, fileNameOptions));
00328
00329 }
00330 assert( (seg2File = fopen(path,"wb")) !=NULL);
00331 WriteFileDescriptorBlock(seg2File);
00332 }
00333 return(seg2File);
00334 }
00338 FILE* OpenSeg2Channel(int channel)
00339 {
00340 if (seg2ChannelFile[channel]==NULL)
00341 {
00342 int fd;
00343 strcpy(seg2ChannelFileName[channel],"titanfileXXXXXX");
00344 fd=OpenTempFile(seg2ChannelFileName[channel]);
00345 assert((seg2ChannelFile[channel]=fdopen(fd,"wb"))!=NULL);
00346 setvbuf(seg2ChannelFile[channel],NULL,_IOFBF,FILE_BUFFER_SIZE);
00347
00348 if (seg2ChannelFile[channel])
00349 {
00350
00351 NbrChan++;
00352
00353 InitInfoHeader(channel);
00354 }
00355 else
00356 {
00357 PrintError("unable to open %s: %m\n",seg2ChannelFileName[channel]);
00358 }
00359 }
00360 return(seg2ChannelFile[channel]);
00361 }
00362
00366
00367
00368 void AppendChannelToSeg2File(int channel,FILE* fid)
00369 {
00370 int32_t *u32;
00371 assert((u32 = malloc(sizeof(int32_t)*infoHeader[channel].npts))!=NULL);
00372 fread(u32,sizeof(int32_t),infoHeader[channel].npts,fid);
00373 fwrite(u32,sizeof(int32_t),infoHeader[channel].npts,seg2File);
00374 free(u32);
00375
00376 }
00377
00378
00379 void CloseFullSeg2File()
00380 {
00381 if (seg2File==NULL)
00382 {
00383 return;
00384 }
00385 fclose(seg2File);
00386 seg2File = NULL;
00387
00388 NbrChan = 0;
00389 CurChan = 0;
00390 }
00391
00392
00396 void CloseSeg2File(int channel)
00397 {
00398 char line[128];
00399 char fulline[132];
00400 FILE* fid;
00401
00402
00403 if (seg2ChannelFile[channel]==NULL)
00404 {
00405 return;
00406 }
00407 if (KFULLSEG == 0)
00408 {
00409 NbrChan = 1;
00410 }
00411
00412 UpdateInfoHeader(channel);
00413
00414
00415 fclose(seg2ChannelFile[channel]);
00416 seg2ChannelFile[channel]=NULL;
00417
00418 strcpy(line,FileName("", ".SEG2", channel, fileNameOptions));
00419 if (noWrite==0)
00420 {
00421 Rename(seg2ChannelFileName[channel],line);
00422 }
00423
00424
00425 OpenFullSeg2(fulline,channel);
00426 UpdateFileDescriptorBlock(seg2File,CurChan);
00427 WriteTraceDescriptorBlock(seg2File,channel);
00428
00429
00430 fid = fopen(line,"rb");
00431 if (fid==NULL)
00432 {
00433 PrintError("%s: %m\n",line);
00434 }
00435 assert( fid !=NULL );
00436 AppendChannelToSeg2File(channel,fid);
00437 fclose(fid);
00438
00439
00440 unlink(line);
00441 unlink(seg2ChannelFileName[channel]);
00442 CurChan++;
00443 if (KFULLSEG == 0)
00444 {
00445 fclose(seg2File);
00446 seg2File = NULL;
00447 CurChan = 0;
00448 }
00449 }
00450
00453 void AbortSeg2File(int channel)
00454 {
00455 PrintDebug("abort for %s\n",seg2ChannelFileName[channel]);
00456 if (seg2ChannelFile[channel])
00457 {
00458 fclose(seg2ChannelFile[channel]);
00459 seg2ChannelFile[channel]=NULL;
00460 unlink(seg2ChannelFileName[channel]);
00461 }
00462 }
00463
00464
00466 void AddSeg2Data(int channel)
00467 {
00468 if ((HWConfig.srObs[channel].npts==0)||(HWConfig.srExp[channel].npts==0))
00469 {
00470 return;
00471 }
00472
00473 if (OpenSeg2Channel(channel))
00474 {
00475 int d;
00476 d=HWConfig.srObs[channel].npts;
00477 if (d!=fwrite(HWConfig.data[channel],4,d,seg2ChannelFile[channel]))
00478 {
00479 PrintError("unable to write to %s: %m\n",
00480 seg2ChannelFileName[channel]);
00481 AbortSeg2File(channel);
00482 }
00483 infoHeader[channel].npts+=d;
00484
00485 }
00486 }