source: SH_SHM/trunk/source/seed_io/seed512to4096.c @ 16

Revision 16, 7.7 KB checked in by marcus, 15 years ago (diff)

r1 | svn | 2007-12-13 11:10:29 +0100 (Do, 13 Dez 2007) | 2 lines

Initial import

Line 
1
2/* file seed512to4096.c
3 *      ===============
4 *
5 * version 5, 2-Feb-2007
6 *
7 * Reformats 512 byte records to 4096 byte records by reading 8 512-records
8 * and writing one 4096-record.  Some space in the output record remains
9 * unused.
10 * K. Stammler, 17-Sep-98
11 *
12 * v  5, K.S.  changed MAXSAMPLE from 5000 to 1000
13 */
14
15
16#include <stdio.h>
17#include <string.h>
18#include "basecnst.h"
19#ifdef BC_INC_STDLIB
20#include BC_INC_STDLIB
21#endif
22#include "sysbase.h"
23#include "erusrdef.h"
24#include "tcusrdef.h"
25#include "cpar.h"
26#include "seedcfg.h"
27#include "seed_lib.h"
28
29
30#define INPRECSIZE 512
31        /* input record length */
32#define OUTRECSIZE 4096
33        /* output record length */
34#define MERGEREC 8
35        /* this number of 512 records is merged to one 4096 */
36#define MAXSAMPLE 10000
37        /* array size */
38#define MAXSMPPERREC 3772
39        /* maximum number of samples in one 4096 block */
40
41
42
43/* global variables */
44static SeedSbyteT    cv_seedrec[INPRECSIZE];  /* SEED input record */
45static SeedSbyteT    cv_wrk[OUTRECSIZE*2];    /* SEED workspace */
46static INT32         cv_smp[MAXSAMPLE];       /* decoded samples */
47
48
49int main( int argc, char *argv[] )
50{
51        /* local variables */
52        SeedDataHeaderT hdrproto;            /* header prototype */
53        TSyStatus status;                    /* status value */
54        char     fname[cBcFileLth+1];        /* name of input file */
55        FILE     *fp;                        /* pointer to input file */
56        int      read_ret;                   /* read return value */
57        int      mergecnt;                   /* merge counter */
58        float    dt;                         /* sample distance in sec */
59        float    tdiff;                      /* time difference */
60        NTIME    lasttime;                   /* end time of previous record */
61        NTIME    bt, et;                     /* begin and end time of record */
62        NTIME    wt;                         /* start time of sample array */
63        TSyBoolean gap_found;                /* gap found */
64        TSyBoolean swap;                     /* swap data */
65        INT32    smpnum;                     /* current number of samples */
66        INT32    new_smpnum;                 /* number of samples in current rec. */
67        INT32    prev_smp;                   /* previos sample */
68        int      av_timequal;                /* average time quality over 4096 rec */
69        int      cur_timequal;               /* time qulaity of current 512 rec */
70        int      tq_cnt;                     /* time quality counter */
71        UBYTE    activity, ioflags, quality; /* data flags in header */
72        TSyBoolean flushbuf;                 /* flush buffer */
73
74        /* executable code */
75
76        status = cBcNoError;
77
78        /* initialize seed routines */
79        SeedLibInitialize( &status );
80        if  (Severe(&status))  err_writemsg( status, "", TRUE );
81        SeedSetOutputReclth( OUTRECSIZE );
82
83        /* get parameters */
84        pa_init( argc, argv );
85        if  (pa_pnumber() != 1)  {
86                fprintf( stderr, "Usage: %s <file512>\n", pa_progname() );
87                fprintf( stderr, "   qualifiers:\n" );
88                fprintf( stderr, "   -swap      swap data\n" );
89                fprintf( stderr, "   -noswap    don't swap data\n" );
90                return 1;
91        } /*endif*/
92        strcpy( fname, pa_pvalue(1) );
93        swap = TRUE;
94        if  (pa_qspecified("-swap"))  swap = TRUE;
95        if  (pa_qspecified("-noswap"))  swap = FALSE;
96
97        /* open input file */
98        fp = fopen( fname, "r" );
99        if  (fp == NULL)  {
100                fprintf( stderr, "%s: input file %s not found\n", pa_progname(), fname );
101                return 1;
102        } /*endif*/
103
104        mergecnt = 0;
105        lasttime.month = 0;
106        gap_found = FALSE;
107        smpnum = 0;
108        prev_smp = 0;
109        av_timequal = cur_timequal = tq_cnt = 0;
110        activity = ioflags = quality = (UBYTE)0;
111
112        FOREVER {
113
114                /* read next record */
115                read_ret = fread( cv_seedrec, 1, INPRECSIZE, fp );
116                if  (read_ret == 0)  break;
117                if  (read_ret < INPRECSIZE)  {
118                        fprintf( stderr, "%s: read error on %s, abort.\n", pa_progname(),
119                                fname );
120                        break;
121                } /*endif*/
122                /* use only data records */
123                if  (cv_seedrec[6] != 'D')  continue;
124                SeedStoreReclth( (SeedDataHeaderT *)cv_seedrec, INPRECSIZE );
125                SeedRecordTime( cv_seedrec, &bt, &et, &status );
126                if  (SySevere(&status))  {
127                        fprintf( stderr, "%s: decode error on %s\n", pa_progname(), fname );
128                        status = cBcNoError;
129                        continue;
130                } /*endif*/
131
132                /* check for gaps */
133                if  (lasttime.month == 0)  {
134                        dt = SeedGetSampleDist( (SeedDataHeaderT *)cv_seedrec );
135                        hdrproto = *(SeedDataHeaderT *)cv_seedrec ;
136                        SeedSetHeaderPrototype( &hdrproto );
137                        wt = bt;
138                } else {
139                        tdiff = tc_ndiff( &bt, &lasttime, &status );
140                        gap_found = (Abs(tdiff) > dt/2.0);
141                } /*endif*/
142                lasttime = et;
143
144                /* store data flags */
145                activity |= ((SeedDataHeaderT *)cv_seedrec)->activity;
146                ioflags |= ((SeedDataHeaderT *)cv_seedrec)->ioflags;
147                quality |= ((SeedDataHeaderT *)cv_seedrec)->quality;
148
149                /* increment record read counter */
150                mergecnt++;
151                if  (mergecnt == 1)  {
152                        wt = bt;
153                        if  (smpnum > 0)  tc_nadd( &wt, -smpnum*dt, &wt, &status );
154                        if  (SySevere(&status))  {
155                                fprintf( stderr, "%s: error computing times.  Abort.\n",
156                                        pa_progname() );
157                                exit( 1 );
158                        } /*endif*/
159                } /*endif*/
160
161                if  (gap_found)  {
162                        /* set time quality if found */
163                        if  (tq_cnt > 0)  {
164                                av_timequal = Nint( (float)av_timequal/(float)tq_cnt );
165                                SeedSetNextTimequal( av_timequal );
166                                av_timequal = tq_cnt = 0;
167                        } /*endif*/
168                        hdrproto.activity = activity;
169                        hdrproto.ioflags = ioflags;
170                        hdrproto.quality = quality;
171                        SeedSetHeaderPrototype( &hdrproto );
172                        /* flush output, then decode new record */
173                        SeedEncodeSteim1( fileno(stdout), cv_smp, &prev_smp, &smpnum, &wt,
174                                cv_wrk, 2, TRUE, &status );
175                        if  (SySevere(&status))  {
176                                fprintf( stderr, "%s: error writing SEED output.  Abort.\n",
177                                        pa_progname() );
178                                exit( 1 );
179                        } /*endif*/
180                        activity = ioflags = quality = (UBYTE)0;
181                        /* decode record */
182                        SeedDecodeSteim1( cv_seedrec, swap, MAXSAMPLE, cv_smp, &smpnum );
183                        mergecnt = 1;
184                        wt = bt;
185                        prev_smp = 0;
186                        /* get time quality */
187                        cur_timequal = SeedGetTimequal( (SeedDataHeaderT *)cv_seedrec );
188                        if  (cur_timequal >= 0)  {
189                                av_timequal += cur_timequal;
190                                tq_cnt++;
191                        } /*endif*/
192                } else {
193                        /* decode new record, if 8 input records -> write output record */
194                        SeedDecodeSteim1( cv_seedrec, swap, MAXSAMPLE-smpnum,
195                                cv_smp+smpnum, &new_smpnum );
196                        smpnum += new_smpnum;
197                        /* get time quality */
198                        cur_timequal = SeedGetTimequal( (SeedDataHeaderT *)cv_seedrec );
199                        if  (cur_timequal >= 0)  {
200                                av_timequal += cur_timequal;
201                                tq_cnt++;
202                        } /*endif*/
203                        if  (mergecnt == MERGEREC || smpnum >= MAXSMPPERREC)  {
204                                /* set time quality if found */
205                                if  (tq_cnt > 0)  {
206                                        av_timequal = Nint( (float)av_timequal/(float)tq_cnt );
207                                        SeedSetNextTimequal( av_timequal );
208                                        av_timequal = tq_cnt = 0;
209                                } /*endif*/
210                                hdrproto.activity = activity;
211                                hdrproto.ioflags = ioflags;
212                                hdrproto.quality = quality;
213                                hdrproto.timecorr = ((SeedDataHeaderT *)cv_seedrec)->timecorr;
214                                SeedSetHeaderPrototype( &hdrproto );
215                                flushbuf = (smpnum <= MAXSMPPERREC);
216                                /* write output record */
217                                SeedEncodeSteim1( fileno(stdout), cv_smp, &prev_smp, &smpnum, &wt,
218                                        cv_wrk, 2, flushbuf, &status );
219                                if  (SySevere(&status))  {
220                                        fprintf( stderr, "%s: error writing SEED output.  Abort.\n",
221                                                pa_progname() );
222                                        exit( 1 );
223                                } /*endif*/
224                                /* reset merge and sample counter */
225                                mergecnt = 0;
226                                if  (flushbuf)  smpnum = 0;
227                                activity = ioflags = quality = (UBYTE)0;
228                        } /*endif*/
229                } /*endif*/
230
231        } /*endfor*/
232
233        /* flush output */
234        if  (smpnum > 0)  {
235                /* set time quality if found */
236                if  (tq_cnt > 0)  {
237                        av_timequal = Nint( (float)av_timequal/(float)tq_cnt );
238                        SeedSetNextTimequal( av_timequal );
239                        av_timequal = tq_cnt = 0;
240                } /*endif*/
241                SeedEncodeSteim1( fileno(stdout), cv_smp, &prev_smp, &smpnum, &wt,
242                        cv_wrk, 2, TRUE, &status );
243                if  (SySevere(&status))  {
244                        fprintf( stderr, "%s: error writing SEED output.  Abort.\n",
245                                pa_progname() );
246                        exit( 1 );
247                } /*endif*/
248        } /*endif*/
249
250        fclose( fp );
251        return 0;
252
253} /* end of main */
Note: See TracBrowser for help on using the repository browser.