/********************************************************************* * ms_readtracelist_selection: * * This routine will open and read all Mini-SEED records in specified * file and populate a trace list. This routine is thread safe. * * If reclen is <= 0 the length of every record is automatically * detected. * * If a Selections list is supplied it will be used to limit which * records are added to the trace list. * * Returns MS_NOERROR and populates an MSTraceList struct at *ppmstl * on successful read, otherwise returns a libmseed error code (listed * in libmseed.h). *********************************************************************/ int ms_readtracelist_selection (MSTraceList **ppmstl, const char *msfile, int reclen, double timetol, double sampratetol, Selections *selections, flag dataquality, flag skipnotdata, flag dataflag, flag verbose) { MSRecord *msr = 0; MSFileParam *msfp = 0; int retcode; if (!ppmstl) return MS_GENERROR; /* Initialize MSTraceList if needed */ if (!*ppmstl) { *ppmstl = mstl_init (*ppmstl); if (!*ppmstl) return MS_GENERROR; } /* Loop over the input file */ while ((retcode = ms_readmsr_main (&msfp, &msr, msfile, reclen, NULL, NULL, skipnotdata, dataflag, NULL, verbose)) == MS_NOERROR) { /* Test against selections if supplied */ if (selections) { char srcname[50]; hptime_t endtime; msr_srcname (msr, srcname, 1); endtime = msr_endtime (msr); if (ms_matchselect (selections, srcname, msr->starttime, endtime, NULL) == NULL) { continue; } } /* Add to trace list */ mstl_addmsr (*ppmstl, msr, dataquality, 1, timetol, sampratetol); } /* Reset return code to MS_NOERROR on successful read by ms_readmsr() */ if (retcode == MS_ENDOFFILE) retcode = MS_NOERROR; ms_readmsr_main (&msfp, &msr, NULL, 0, NULL, NULL, 0, 0, NULL, 0); return retcode; } /* End of ms_readtracelist_selection() */
int main (int argc, char **argv) { struct filelink *flp; MSRecord *msr = 0; MSTraceList *mstl = 0; FILE *bfp = 0; FILE *ofp = 0; int retcode = MS_NOERROR; char envvariable[100]; int dataflag = 0; long long int totalrecs = 0; long long int totalsamps = 0; long long int totalfiles = 0; off_t filepos = 0; char srcname[50]; char stime[30]; /* Set default error message prefix */ ms_loginit (NULL, NULL, NULL, "ERROR: "); /* Process given parameters (command line and parameter file) */ if ( processparam (argc, argv) < 0 ) return 1; /* Setup encoding environment variable if specified, ugly kludge */ if ( encodingstr ) { snprintf (envvariable, sizeof(envvariable), "UNPACK_DATA_FORMAT=%s", encodingstr); if ( putenv (envvariable) ) { ms_log (2, "Cannot set environment variable UNPACK_DATA_FORMAT\n"); return 1; } } /* Open the integer output file if specified */ if ( binfile ) { if ( strcmp (binfile, "-") == 0 ) { bfp = stdout; } else if ( (bfp = fopen (binfile, "wb")) == NULL ) { ms_log (2, "Cannot open binary data output file: %s (%s)\n", binfile, strerror(errno)); return 1; } } /* Open the output file if specified */ if ( outfile ) { if ( strcmp (outfile, "-") == 0 ) { ofp = stdout; } else if ( (ofp = fopen (outfile, "wb")) == NULL ) { ms_log (2, "Cannot open output file: %s (%s)\n", outfile, strerror(errno)); return 1; } } if ( printdata || binfile ) dataflag = 1; if ( tracegapsum || tracegaponly ) mstl = mstl_init (NULL); flp = filelist; while ( flp != 0 ) { if ( verbose >= 2 ) { if ( flp->offset ) ms_log (1, "Processing: %s (starting at byte %lld)\n", flp->filename, flp->offset); else ms_log (1, "Processing: %s\n", flp->filename); } /* Set starting byte offset if supplied as negative file position */ filepos = - flp->offset; /* Loop over the input file */ while ( reccntdown != 0 ) { if ( (retcode = ms_readmsr (&msr, flp->filename, reclen, &filepos, NULL, skipnotdata, 0, verbose)) != MS_NOERROR ) break; /* Check if record matches start/end time criteria */ if ( starttime != HPTERROR || endtime != HPTERROR ) { hptime_t recendtime = msr_endtime (msr); if ( starttime != HPTERROR && (msr->starttime < starttime && ! (msr->starttime <= starttime && recendtime >= starttime)) ) { if ( verbose >= 3 ) { msr_srcname (msr, srcname, 1); ms_hptime2seedtimestr (msr->starttime, stime, 1); ms_log (1, "Skipping (starttime) %s, %s\n", srcname, stime); } continue; } if ( endtime != HPTERROR && (recendtime > endtime && ! (msr->starttime <= endtime && recendtime >= endtime)) ) { if ( verbose >= 3 ) { msr_srcname (msr, srcname, 1); ms_hptime2seedtimestr (msr->starttime, stime, 1); ms_log (1, "Skipping (starttime) %s, %s\n", srcname, stime); } continue; } } if ( match || reject ) { /* Generate the srcname with the quality code */ msr_srcname (msr, srcname, 1); /* Check if record is matched by the match regex */ if ( match ) { if ( regexec ( match, srcname, 0, 0, 0) != 0 ) { if ( verbose >= 3 ) { ms_hptime2seedtimestr (msr->starttime, stime, 1); ms_log (1, "Skipping (match) %s, %s\n", srcname, stime); } continue; } } /* Check if record is rejected by the reject regex */ if ( reject ) { if ( regexec ( reject, srcname, 0, 0, 0) == 0 ) { if ( verbose >= 3 ) { ms_hptime2seedtimestr (msr->starttime, stime, 1); ms_log (1, "Skipping (reject) %s, %s\n", srcname, stime); } continue; } } } if ( reccntdown > 0 ) reccntdown--; totalrecs++; totalsamps += msr->samplecnt; if ( ! tracegaponly ) { if ( printoffset ) ms_log (0, "%-10lld", (long long) filepos); if ( printlatency ) ms_log (0, "%-10.6g secs ", msr_host_latency(msr)); if ( printraw ) ms_parse_raw (msr->record, msr->reclen, ppackets, -1); else msr_print (msr, ppackets); } if ( tracegapsum || tracegaponly ) mstl_addmsr (mstl, msr, dataquality, 1, timetol, sampratetol); if ( dataflag ) { /* Parse the record (again) and unpack the data */ int rv = msr_unpack (msr->record, msr->reclen, &msr, 1, verbose); if ( rv == MS_NOERROR && printdata && ! tracegaponly ) { int line, col, cnt, samplesize; int lines = (msr->numsamples / 6) + 1; void *sptr; if ( (samplesize = ms_samplesize(msr->sampletype)) == 0 ) { ms_log (2, "Unrecognized sample type: %c\n", msr->sampletype); } if ( msr->sampletype == 'a' ) { char *ascii = (char *)msr->datasamples; int length = msr->numsamples; ms_log (0, "ASCII Data:\n"); /* Print maximum log message segments */ while ( length > (MAX_LOG_MSG_LENGTH-1) ) { ms_log (0, "%.*s", (MAX_LOG_MSG_LENGTH-1), ascii); ascii += MAX_LOG_MSG_LENGTH-1; length -= MAX_LOG_MSG_LENGTH-1; } /* Print any remaining ASCII and add a newline */ if ( length > 0 ) { ms_log (0, "%.*s\n", length, ascii); } else { ms_log (0, "\n"); } } else for ( cnt = 0, line = 0; line < lines; line++ ) { for ( col = 0; col < 6 ; col ++ ) { if ( cnt < msr->numsamples ) { sptr = (char*)msr->datasamples + (cnt * samplesize); if ( msr->sampletype == 'i' ) ms_log (0, "%10d ", *(int32_t *)sptr); else if ( msr->sampletype == 'f' ) ms_log (0, "%10.8g ", *(float *)sptr); else if ( msr->sampletype == 'd' ) ms_log (0, "%10.10g ", *(double *)sptr); cnt++; } } ms_log (0, "\n"); /* If only printing the first 6 samples break out here */ if ( printdata == 1 ) break; } } if ( binfile ) { uint8_t samplesize = ms_samplesize (msr->sampletype); if ( samplesize ) { fwrite (msr->datasamples, samplesize, msr->numsamples, bfp); } else { ms_log (1, "Cannot write to binary file, unknown sample type: %c\n", msr->sampletype); } } } if ( outfile ) { fwrite (msr->record, 1, msr->reclen, ofp); } } /* Print error if not EOF and not counting down records */ if ( retcode != MS_ENDOFFILE && reccntdown != 0 ) { ms_log (2, "Cannot read %s: %s\n", flp->filename, ms_errorstr(retcode)); ms_readmsr (&msr, NULL, 0, NULL, NULL, 0, 0, 0); exit (1); } /* Make sure everything is cleaned up */ ms_readmsr (&msr, NULL, 0, NULL, NULL, 0, 0, 0); totalfiles++; flp = flp->next; } /* End of looping over file list */ if ( binfile ) fclose (bfp); if ( outfile ) fclose (ofp); if ( basicsum ) ms_log (0, "Files: %lld, Records: %lld, Samples: %lld\n", totalfiles, totalrecs, totalsamps); if ( tracegapsum || tracegaponly ) { if ( tracegapsum == 1 || tracegaponly == 1 ) { mstl_printtracelist (mstl, timeformat, 1, tracegaps); } if ( tracegapsum == 2 || tracegaponly == 2 ) { mstl_printgaplist (mstl, timeformat, mingapptr, maxgapptr); } if ( tracegaponly == 3 ) { mstl_printsynclist (mstl, NULL, 1); } } if ( mstl ) mstl_free (&mstl, 0); return 0; } /* End of main() */