servicelib.c 12.2 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>

#include "dmemory.h"

#include "service.h"
#include "error.h"
#include "file.h"
#include "binary.h"
16
#include "stream_fcommon.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
17
18
19
#include "swap.h"


20
21
22
enum {
  SRV_HEADER_LEN = 8,
};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
23
24


Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
static int initSrvLib      = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
26
27
28
29
30
31
32
33
34
static int srvDefaultHprec = 0;
static int srvDefaultDprec = 0;


/*
 * A version string.
 */

#undef  LIBVERSION
35
#define LIBVERSION      1.3.2
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#define XSTRING(x)	#x
#define STRING(x)	XSTRING(x)
static const char srv_libvers[] = STRING(LIBVERSION) " of "__DATE__" "__TIME__;

const char *srvLibraryVersion(void)
{
  return (srv_libvers);
}


int SRV_Debug = 0;    /* If set to 1, debugging */


void srvDebug(int debug)
{
  SRV_Debug = debug;

  if ( SRV_Debug )
54
    Message("debug level %d", debug);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
}


void srvLibInit()
{
  char *envString;
  char *envName = "SRV_PRECISION";


  envString = getenv(envName);
  if ( envString )
    {
      int pos;
      int nrun;
      if ( strlen(envString) == 2 ) nrun = 1;
      else                          nrun = 2;

      pos = 0;
      while ( nrun-- )
	{
	  switch ( tolower((int) envString[pos]) )
	    {
	    case 'i':
	      {
		switch ( (int) envString[pos+1] )
		  {
		  case '4': srvDefaultHprec = SINGLE_PRECISION; break;
		  case '8': srvDefaultHprec = DOUBLE_PRECISION; break;
		  default:
84
		    Message("Invalid digit in %s: %s", envName, envString);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
85
86
87
88
89
90
91
92
93
94
		  }
		break;
	      }
	    case 'r':
	      {
		switch ( (int) envString[pos+1] )
		  {
		  case '4': srvDefaultDprec = SINGLE_PRECISION; break;
		  case '8': srvDefaultDprec = DOUBLE_PRECISION; break;
		  default:
95
		    Message("Invalid digit in %s: %s", envName, envString);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
96
		  }
97
		break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98
99
	      }
	    default:
100
101
102
103
104
              {
                Message("Invalid character in %s: %s", envName, envString);
                break;
              }
            }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
105
106
107
108
109
110
111
112
	  pos += 2;
	}
    }

  initSrvLib = 1;
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
113
void srvInit(srvrec_t *srvp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
115
116
117
118
119
120
121
122
123
124
{
  srvp->checked    = 0;
  srvp->byteswap   = 0;
  srvp->hprec      = 0;
  srvp->dprec      = 0;
  srvp->datasize   = 0;
  srvp->buffersize = 0;
  srvp->buffer     = NULL;
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
125
srvrec_t *srvNew(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
  srvrec_t *srvp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
128
129
130

  if ( ! initSrvLib ) srvLibInit();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
  srvp = (srvrec_t *) malloc(sizeof(srvrec_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
133
134
135
136
137
138

  srvInit(srvp);

  return (srvp);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
139
void srvDelete(srvrec_t *srvp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
{
  if ( srvp )
    {
      if ( srvp->buffer ) free(srvp->buffer);
      free(srvp);
    }
}


int srvCheckFiletype(int fileID, int *swap)
{
  size_t blocklen = 0;
  size_t sblocklen = 0;
  size_t data = 0;
  size_t dimx = 0, dimy = 0;
  int fact = 0, found = 0;
  unsigned char buffer[72], *pbuf;

  if ( fileRead(fileID, buffer, 4) != 4 ) return (found);

  blocklen  = (size_t) get_UINT32(buffer);
  sblocklen = (size_t) get_SUINT32(buffer);

  if ( SRV_Debug )
164
    Message("blocklen = %d sblocklen = %d", blocklen, sblocklen);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

  if ( blocklen == 32 )
    {
     *swap = 0;
      fact = blocklen>>3;
      if ( fileRead(fileID, buffer, blocklen+8) != blocklen+8 ) return (found);
      pbuf = buffer+4*fact;      dimx = (size_t) get_UINT32(pbuf);
      pbuf = buffer+5*fact;      dimy = (size_t) get_UINT32(pbuf);
      pbuf = buffer+blocklen+4;  data = (size_t) get_UINT32(pbuf);
    }
  else if ( blocklen == 64 )
    {
     *swap = 0;
      fact = blocklen>>3;
      if ( fileRead(fileID, buffer, blocklen+8) != blocklen+8 ) return (found);
      pbuf = buffer+4*fact;      dimx = (size_t) get_UINT64(pbuf);
      pbuf = buffer+5*fact;      dimy = (size_t) get_UINT64(pbuf);
      pbuf = buffer+blocklen+4;  data = (size_t) get_UINT32(pbuf);
    }
  else if ( sblocklen == 32 )
    {
     *swap = 1;
      fact = sblocklen>>3;
      if ( fileRead(fileID, buffer, sblocklen+8) != sblocklen+8 ) return (found);
      pbuf = buffer+4*fact;       dimx = (size_t) get_SUINT32(pbuf);
      pbuf = buffer+5*fact;       dimy = (size_t) get_SUINT32(pbuf);
      pbuf = buffer+sblocklen+4;  data = (size_t) get_SUINT32(pbuf);
    }
  else if ( sblocklen == 64 )
    {
     *swap = 1;
      fact = sblocklen>>3;
      if ( fileRead(fileID, buffer, sblocklen+8) != sblocklen+8 ) return (found);
      pbuf = buffer+4*fact;       dimx = (size_t) get_SUINT64(pbuf);
      pbuf = buffer+5*fact;       dimy = (size_t) get_SUINT64(pbuf);
      pbuf = buffer+sblocklen+4;  data = (size_t) get_SUINT32(pbuf);
    }

  fileRewind(fileID);

  if      ( data && dimx*dimy*fact == data ) found = 1;
  else if ( data && dimx*dimy*8    == data ) found = 1;

  if ( SRV_Debug )
    {
210
211
      Message("swap = %d fact = %d", *swap, fact);
      Message("dimx = %lu dimy = %lu data = %lu", dimx, dimy, data);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
212
213
214
215
216
217
    }

  return (found);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
218
int srvInqHeader(srvrec_t *srvp, int *header)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
219
220
221
222
223
224
225
{
  size_t i;

  for ( i = 0; i < SRV_HEADER_LEN; i++ )
    header[i] = srvp->header[i];
  
  if ( SRV_Debug )
226
    Message("datasize = %lu", srvp->datasize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
227
228
229
230
231

  return (0);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
int srvDefHeader(srvrec_t *srvp, const int *header)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
233
234
235
236
237
238
239
240
241
{
  size_t i;

  for ( i = 0; i < SRV_HEADER_LEN; i++ )
    srvp->header[i] = header[i];
  
  srvp->datasize = header[4]*header[5];

  if ( SRV_Debug )
242
    Message("datasize = %lu", srvp->datasize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
244
245
246
247

  return (0);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
248
int srvInqData(srvrec_t *srvp, int prec, void *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
{
  size_t datasize;
  size_t i;
  int ierr = 0;
  int dprec;
  void *buffer;
  int byteswap = srvp->byteswap;

  datasize = srvp->datasize;

  buffer = srvp->buffer;

  dprec = srvp->dprec;

  switch ( dprec )
    {
    case SINGLE_PRECISION:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
267
	if ( sizeof(FLT32) == 4 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
268
269
270
271
	  {
	    if ( byteswap ) swap4byte(buffer, datasize);

	    if ( dprec == prec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
272
	      memcpy(data, buffer, datasize*sizeof(FLT32));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
273
274
275
276
277
278
	    else
	      for (i = 0; i < datasize; i++)
		((double *) data)[i] = (double) ((float *) buffer)[i];
	  }
	else
	  {
279
	    Error("not implemented for %d byte float", sizeof(FLT32));
280
	  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
281
282
283
	break;
      }
    case DOUBLE_PRECISION:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
284
	if ( sizeof(FLT64) == 8 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
285
286
287
288
	  {
	    if ( byteswap ) swap8byte(buffer, datasize);

	    if ( dprec == prec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
289
	      memcpy(data, buffer, datasize*sizeof(FLT64));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
290
291
292
293
294
295
	    else
	      for (i = 0; i < datasize; i++)
		((float *) data)[i] = (float) ((double *) buffer)[i];
	  }
	else
	  {
296
	    Error("not implemented for %d byte float", sizeof(FLT64));
297
	  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
298
299
300
	break;
    default:
      {
301
	Error("unexpected data precision %d", dprec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
302
        break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
303
304
305
306
307
308
309
      }
    }

  return (ierr);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
310
int srvInqDataSP(srvrec_t *srvp, float *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
311
312
313
314
315
{
  return (srvInqData(srvp, SINGLE_PRECISION, (void *) data));
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
316
int srvInqDataDP(srvrec_t *srvp, double *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
317
318
319
320
321
{
  return (srvInqData(srvp, DOUBLE_PRECISION, (void *) data));
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
322
int srvDefData(srvrec_t *srvp, int prec, const void *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
{
  size_t datasize;
  size_t blocklen;
  size_t buffersize;
  size_t i;
  int dprec, hprec;
  int *header;
  void *buffer;

  if ( srvDefaultDprec ) dprec = srvDefaultDprec;
  else                   dprec = srvp->dprec;

  if ( ! dprec ) dprec = prec;

  srvp->dprec = dprec;

  if ( srvDefaultHprec ) hprec = srvDefaultHprec;
  else                   hprec = srvp->hprec;

  if ( ! hprec ) hprec = dprec;
343

Uwe Schulzweida's avatar
Uwe Schulzweida committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  srvp->hprec = hprec;

  header = srvp->header;

  datasize = header[4]*header[5];
  blocklen = datasize * dprec;

  srvp->datasize = datasize;

  buffersize = srvp->buffersize;

  if ( buffersize != blocklen )
    {
      buffersize = blocklen;
      buffer = srvp->buffer;
      buffer = realloc(buffer, buffersize);
      srvp->buffer = buffer;
      srvp->buffersize = buffersize;
    }
  else
    buffer = srvp->buffer;

  switch ( dprec )
    {
    case SINGLE_PRECISION:
      {
	if ( dprec == prec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
371
	  memcpy(buffer, data, datasize*sizeof(FLT32));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
372
373
374
375
376
377
378
379
380
	else
	  for (i = 0; i < datasize; i++)
	    ((float *) buffer)[i] = (float) ((double *) data)[i];

	break;
      }
    case DOUBLE_PRECISION:
      {
	if ( dprec == prec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
381
	  memcpy(buffer, data, datasize*sizeof(FLT64));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
382
383
384
385
386
387
388
389
	else
	  for (i = 0; i < datasize; i++)
	    ((double *) buffer)[i] = (double) ((float *) data)[i];

	break;
      }
    default:
      {
390
	Error("unexpected data precision %d", dprec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
391
        break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
392
393
394
395
396
397
398
      }
    }

  return (0);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
399
int srvDefDataSP(srvrec_t *srvp, const float *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
400
401
402
403
404
{
  return (srvDefData(srvp, SINGLE_PRECISION, (void *) data));
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
405
int srvDefDataDP(srvrec_t *srvp, const double *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
406
407
408
409
410
{
  return (srvDefData(srvp, DOUBLE_PRECISION, (void *) data));
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
411
int srvRead(int fileID, srvrec_t *srvp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
412
413
414
415
416
417
418
419
420
421
422
423
424
425
{
  size_t datasize;
  size_t blocklen, blocklen2;
  size_t i;
  char tempheader[64];
  int hprec, dprec;
  void *buffer;
  int buffersize;
  int byteswap;
  int status;

  if ( ! srvp->checked )
    {
      status = srvCheckFiletype(fileID, &srvp->byteswap);
426
      if ( status == 0 ) Error("Not a SERVICE file!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
427
428
429
430
431
432
433
434
435
436
437
      srvp->checked = 1;
    }

  byteswap = srvp->byteswap;

  /* read header record */
  blocklen = binReadF77Block(fileID, byteswap);

  if ( fileEOF(fileID) ) return (-1);

  if ( SRV_Debug )
438
    Message("blocklen = %lu", blocklen);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465

  hprec = blocklen / SRV_HEADER_LEN;

  srvp->hprec = hprec;

  switch ( hprec )
    {
    case SINGLE_PRECISION:
      {
	binReadInt32(fileID, byteswap, SRV_HEADER_LEN, (INT32 *) tempheader);

	for ( i = 0; i < SRV_HEADER_LEN; i++ )
          srvp->header[i] = (int) ((INT32 *) tempheader)[i];

	break;
      }
    case DOUBLE_PRECISION:
      {
	binReadInt64(fileID, byteswap, SRV_HEADER_LEN, (INT64 *) tempheader);

	for ( i = 0; i < SRV_HEADER_LEN; i++ )
          srvp->header[i] = (int) ((INT64 *) tempheader)[i];

	break;
      }
    default:
      {
466
	Error("Unexpected header precision %d", hprec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
467
        break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
468
469
470
471
472
473
474
      }
    }

  blocklen2 = binReadF77Block(fileID, byteswap);

  if ( blocklen2 != blocklen )
    {
475
476
      Warning("Header blocklen differ (blocklen1=%d; blocklen2=%d)!", blocklen, blocklen2);
      if ( blocklen2 != 0 ) return (-1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
477
478
479
480
481
    }

  srvp->datasize = srvp->header[4]*srvp->header[5];

  if ( SRV_Debug )
482
    Message("datasize = %lu", srvp->datasize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506

  blocklen = binReadF77Block(fileID, byteswap);

  buffersize = srvp->buffersize;

  if ( buffersize < (int) blocklen )
    {
      buffersize = blocklen;
      buffer = srvp->buffer;
      buffer = realloc(buffer, buffersize);
      srvp->buffer = buffer;
      srvp->buffersize = buffersize;
    }
  else
    buffer = srvp->buffer;

  datasize = srvp->datasize;

  dprec = blocklen / datasize;

  srvp->dprec = dprec;

  if ( dprec != SINGLE_PRECISION && dprec != DOUBLE_PRECISION )
    {
507
      Warning("Unexpected data precision %d", dprec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
508
509
510
511
512
513
514
515
516
      return (-1);
    }

  fileRead(fileID, buffer, blocklen);

  blocklen2 = binReadF77Block(fileID, byteswap);

  if ( blocklen2 != blocklen )
    {
517
518
      Warning("Data blocklen differ (blocklen1=%d; blocklen2=%d)!", blocklen, blocklen2);
      if ( blocklen2 != 0 ) return (-1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
519
520
521
522
523
524
    }

  return (0);
}


525
void srvWrite(int fileID, srvrec_t *srvp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
{
  size_t datasize;
  size_t blocklen;
  size_t i;
  int dprec, hprec;
  char tempheader[64];
  int *header;
  void *buffer;
  int byteswap = srvp->byteswap;

  dprec  = srvp->dprec;
  hprec  = srvp->hprec;
  header = srvp->header;

  /* write header record */
  blocklen = SRV_HEADER_LEN * hprec;

  binWriteF77Block(fileID, byteswap, blocklen);

  switch ( hprec )
    {
    case SINGLE_PRECISION:
      {
	for (i = 0; i < SRV_HEADER_LEN; i++)
          ((INT32 *) tempheader)[i] = (INT32) header[i];

	binWriteInt32(fileID, byteswap, SRV_HEADER_LEN, (INT32 *) tempheader);

	break;
      }
    case DOUBLE_PRECISION:
      {
	for (i = 0; i < SRV_HEADER_LEN; i++)
          ((INT64 *) tempheader)[i] = (INT64) header[i];

	binWriteInt64(fileID, byteswap, SRV_HEADER_LEN, (INT64 *) tempheader);

	break;
      }
    default:
      {
567
	Error("unexpected header precision %d", hprec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
568
        break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
569
570
      }
    }
571

Uwe Schulzweida's avatar
Uwe Schulzweida committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
  binWriteF77Block(fileID, byteswap, blocklen);

  datasize = header[4]*header[5];
  blocklen = datasize * dprec;

  binWriteF77Block(fileID, byteswap, blocklen);

  srvp->datasize = datasize;

  buffer = srvp->buffer;

  switch ( dprec )
    {
    case SINGLE_PRECISION:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
587
	binWriteFlt32(fileID, byteswap, datasize, (FLT32 *) buffer);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
588
589
590
591
	break;
      }
    case DOUBLE_PRECISION:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
592
	binWriteFlt64(fileID, byteswap, datasize, (FLT64 *) buffer);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
593
594
595
596
	break;
      }
    default:
      {
597
	Error("unexpected data precision %d", dprec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
598
        break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
599
600
601
602
603
      }
    }

  binWriteF77Block(fileID, byteswap, blocklen);
}
604
605
606
607
608
609
610
611
612
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */