extralib.c 12 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
16
17
18
#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 "extra.h"
#include "error.h"
#include "file.h"
#include "binary.h"
#include "swap.h"


Uwe Schulzweida's avatar
Uwe Schulzweida committed
19
20
#define  SINGLE_PRECISION    4
#define  DOUBLE_PRECISION    8
Uwe Schulzweida's avatar
Uwe Schulzweida committed
21

Uwe Schulzweida's avatar
Uwe Schulzweida committed
22
#define  EXT_HEADER_LEN      4
Uwe Schulzweida's avatar
Uwe Schulzweida committed
23
24


Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
26
27
static int initExtLib       = 0;
static int extDefaultPrec   = 0;
static int extDefaultNumber = EXT_REAL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
28
29
30
31
32
33
34


/*
 * A version string.
 */

#undef  LIBVERSION
35
#define LIBVERSION      1.3.1
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 ext_libvers[] = STRING(LIBVERSION) " of "__DATE__" "__TIME__;

const char *extLibraryVersion(void)
{
  return (ext_libvers);
}


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


void extDebug(int debug)
{
  EXT_Debug = debug;

  if ( EXT_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
}


void extLibInit()
{
  char *envString;
  char *envName = "EXT_PRECISION";


  envString = getenv(envName);
  if ( envString )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
67
      int pos = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
68

Uwe Schulzweida's avatar
Uwe Schulzweida committed
69
      if ( strlen(envString) == 2  )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
70
71
72
	{
	  switch ( tolower((int) envString[pos]) )
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
73
	    case 'r':
Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
	      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
		extDefaultNumber = EXT_REAL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
76
77
		switch ( (int) envString[pos+1] )
		  {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
79
		  case '4': extDefaultPrec = SINGLE_PRECISION; break;
		  case '8': extDefaultPrec = DOUBLE_PRECISION; break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
80
		  default:
81
		    Message("Invalid digit in %s: %s", envName, envString);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
82
		  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
83
		break;		
Uwe Schulzweida's avatar
Uwe Schulzweida committed
84
	      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
85
	    case 'c':
Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
	      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
		extDefaultNumber = EXT_COMP;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
88
89
		switch ( (int) envString[pos+1] )
		  {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
90
91
		  case '4': extDefaultPrec = SINGLE_PRECISION; break;
		  case '8': extDefaultPrec = DOUBLE_PRECISION; break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
92
		  default:
93
		    Message("Invalid digit in %s: %s", envName, envString);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
94
95
96
97
		  }
		break;		
	      }
	    default:
98
	      Message("Invalid character in %s: %s", envName, envString);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
99
	      break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
101
102
103
104
105
106
	    }
	}
    }

  initExtLib = 1;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
107
108
static
void extInit(extrec_t *extp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
110
111
{
  extp->checked    = 0;
  extp->byteswap   = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
112
113
  extp->prec       = 0;
  extp->number     = extDefaultNumber;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
115
116
117
118
119
  extp->datasize   = 0;
  extp->buffersize = 0;
  extp->buffer     = NULL;
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
120
void *extNew(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
  extrec_t *extp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
124
125

  if ( ! initExtLib ) extLibInit();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
  extp = (extrec_t *) malloc(sizeof(extrec_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
128
129

  extInit(extp);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
130
  return ((void*)extp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
132
133
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
void extDelete(void *ext)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
135
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
136
  extrec_t *extp = (extrec_t *) ext;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

  if ( extp )
    {
      if ( extp->buffer ) free(extp->buffer);
      free(extp);
    }
}


int extCheckFiletype(int fileID, int *swap)
{
  size_t blocklen = 0;
  size_t sblocklen = 0;
  size_t data =  0;
  size_t dimxy = 0;
  int fact = 0, found = 0;
  unsigned char buffer[40], *pbuf;

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

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

  if ( EXT_Debug )
161
    Message("blocklen = %d sblocklen = %d", blocklen, sblocklen);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
162
163
164
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

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

  fileRewind(fileID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
198
199
  if      ( data && dimxy*fact   == data ) found = 1;
  else if ( data && dimxy*fact*2 == data ) found = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
200
201
202

  if ( EXT_Debug )
    {
203
204
      Message("swap = %d fact = %d", *swap, fact);
      Message("dimxy = %lu data = %lu", dimxy, data);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
205
206
207
208
209
210
    }

  return (found);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
211
int extInqHeader(void *ext, int *header)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
212
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
213
  extrec_t *extp = (extrec_t *) ext;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
214
215
216
217
218
  size_t i;

  for ( i = 0; i < EXT_HEADER_LEN; i++ )
    header[i] = extp->header[i];
  
219
  if ( EXT_Debug ) Message("datasize = %lu", extp->datasize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
220
221
222
223
224

  return (0);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
225
int extDefHeader(void *ext, const int *header)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
226
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
227
  extrec_t *extp = (extrec_t *) ext;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
228
229
230
231
232
233
  size_t i;

  for ( i = 0; i < EXT_HEADER_LEN; i++ )
    extp->header[i] = header[i];
  
  extp->datasize = header[3];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
234
  if ( extp->number == EXT_COMP ) extp->datasize *= 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
235

236
  if ( EXT_Debug ) Message("datasize = %lu", extp->datasize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
237
238
239
240
241

  return (0);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
242
int extInqData(void *ext, int prec, void *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
244
  extrec_t *extp = (extrec_t *) ext;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
245
246
247
  size_t datasize;
  size_t i;
  int ierr = 0;
248
  int rprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
249
250
251
252
  void *buffer;
  int byteswap = extp->byteswap;

  datasize = extp->datasize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
253
254
  buffer   = extp->buffer;
  rprec    = extp->prec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255

Uwe Schulzweida's avatar
Uwe Schulzweida committed
256
  switch ( rprec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
257
258
259
    {
    case SINGLE_PRECISION:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
260
	if ( sizeof(FLT32) == 4 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
261
262
263
	  {
	    if ( byteswap ) swap4byte(buffer, datasize);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
264
	    if ( rprec == prec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
265
	      memcpy(data, buffer, datasize*sizeof(FLT32));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
266
	    else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
267
	      for ( i = 0; i < datasize; ++i )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
268
269
270
271
		((double *) data)[i] = (double) ((float *) buffer)[i];
	  }
	else
	  {
272
	    Error("not implemented for %d byte float", sizeof(FLT32));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
273
274
275
276
	  }	
	break;
      }
    case DOUBLE_PRECISION:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
277
	if ( sizeof(FLT64) == 8 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
278
279
280
	  {
	    if ( byteswap ) swap8byte(buffer, datasize);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
281
	    if ( rprec == prec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
282
	      memcpy(data, buffer, datasize*sizeof(FLT64));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
283
	    else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
284
	      for ( i = 0; i < datasize; ++i )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
285
286
287
288
		((float *) data)[i] = (float) ((double *) buffer)[i];
	  }
	else
	  {
289
	    Error("not implemented for %d byte float", sizeof(FLT64));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
290
291
292
293
	  }	
	break;
    default:
      {
294
	Error("unexpected data precision %d", rprec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
295
        break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
296
297
298
299
300
301
302
      }
    }

  return (ierr);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
303
int extInqDataSP(void *ext, float *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
304
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
305
  return (extInqData(ext, SINGLE_PRECISION, (void *) data));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
307
308
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
309
int extInqDataDP(void *ext, double *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
310
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
311
  return (extInqData(ext, DOUBLE_PRECISION, (void *) data));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
312
313
314
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
315
int extDefData(void *ext, int prec, const void *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
316
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
317
  extrec_t *extp = (extrec_t *) ext;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
319
320
321
  size_t datasize;
  size_t blocklen;
  size_t buffersize;
  size_t i;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
322
  int rprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
323
324
325
  int *header;
  void *buffer;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
326
327
  if ( extDefaultPrec ) rprec = extDefaultPrec;
  else                  rprec = extp->prec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
328

Uwe Schulzweida's avatar
Uwe Schulzweida committed
329
  if ( ! rprec ) rprec = prec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
330

Uwe Schulzweida's avatar
Uwe Schulzweida committed
331
  extp->prec = rprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
332
333
334
335

  header = extp->header;

  datasize = header[3];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
336
337
  if ( extp->number == EXT_COMP ) datasize *= 2;
  blocklen = datasize * rprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353

  extp->datasize = datasize;

  buffersize = extp->buffersize;

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
354
  switch ( rprec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
355
356
357
    {
    case SINGLE_PRECISION:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
358
	if ( rprec == prec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
359
	  memcpy(buffer, data, datasize*sizeof(FLT32));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
360
361
362
363
364
365
366
367
	else
	  for (i = 0; i < datasize; i++)
	    ((float *) buffer)[i] = (float) ((double *) data)[i];

	break;
      }
    case DOUBLE_PRECISION:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368
	if ( rprec == prec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
369
	  memcpy(buffer, data, datasize*sizeof(FLT64));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
370
371
372
373
374
375
376
377
	else
	  for (i = 0; i < datasize; i++)
	    ((double *) buffer)[i] = (double) ((float *) data)[i];

	break;
      }
    default:
      {
378
	Error("unexpected data precision %d", rprec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
379
        break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
380
381
382
383
384
385
386
      }
    }

  return (0);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
387
int extDefDataSP(void *ext, const float *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
388
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
389
  return (extDefData(ext, SINGLE_PRECISION, (void *) data));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
390
391
392
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
393
int extDefDataDP(void *ext, const double *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
394
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
  return (extDefData(ext, DOUBLE_PRECISION, (void *) data));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
396
397
398
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
399
int extRead(int fileID, void *ext)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
400
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
401
  extrec_t *extp = (extrec_t *) ext;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
402
403
404
405
406
407
408
409
410
411
412
413
  size_t blocklen, blocklen2;
  size_t i;
  char tempheader[32];
  int hprec, dprec;
  void *buffer;
  int buffersize;
  int byteswap;
  int status;

  if ( ! extp->checked )
    {
      status = extCheckFiletype(fileID, &extp->byteswap);
414
      if ( status == 0 ) Error("Not a EXTRA file!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
415
416
417
418
419
420
421
422
423
424
425
      extp->checked = 1;
    }

  byteswap = extp->byteswap;

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

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

  if ( EXT_Debug )
426
    Message("blocklen = %lu", blocklen);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
427
428
429

  hprec = blocklen / EXT_HEADER_LEN;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
430
  extp->prec = hprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453

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

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

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

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

	break;
      }
    default:
      {
454
	Error("unexpected header precision %d", hprec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
455
        break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
456
457
458
459
460
461
462
      }
    }

  blocklen2 = binReadF77Block(fileID, byteswap);

  if ( blocklen2 != blocklen )
    {
463
      Warning("header blocklen differ!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
464
465
466
467
468
      return (-1);
    }

  extp->datasize = extp->header[3];

469
  if ( EXT_Debug ) Message("datasize = %lu", extp->datasize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485

  blocklen = binReadF77Block(fileID, byteswap);

  buffersize = extp->buffersize;

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
486
  dprec = blocklen / extp->datasize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
487

Uwe Schulzweida's avatar
Uwe Schulzweida committed
488
489
490
491
492
493
494
495
496
497
  if ( dprec == hprec )
    {
      extp->number = EXT_REAL;
    }
  else if ( dprec == 2*hprec )
    {
      dprec /= 2;
      extp->datasize *= 2;
      extp->number = EXT_COMP;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
498
499
500

  if ( dprec != SINGLE_PRECISION && dprec != DOUBLE_PRECISION )
    {
501
      Warning("unexpected data precision %d", dprec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
502
503
504
505
506
507
508
509
510
      return (-1);
    }

  fileRead(fileID, buffer, blocklen);

  blocklen2 = binReadF77Block(fileID, byteswap);

  if ( blocklen2 != blocklen )
    {
511
      Warning("data blocklen differ!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
512
513
514
515
516
517
518
      return (-1);
    }

  return (0);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
519
int extWrite(int fileID, void *ext)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
520
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
521
  extrec_t *extp = (extrec_t *) ext;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
522
523
524
  size_t datasize;
  size_t blocklen;
  size_t i;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
525
  int rprec, number;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
526
527
528
529
530
531
  char tempheader[32];
  int *header;
  void *buffer;
  int byteswap = extp->byteswap;


Uwe Schulzweida's avatar
Uwe Schulzweida committed
532
533
  rprec  = extp->prec;
  number = extp->number;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
534
535
536
  header = extp->header;

  /* write header record */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
537
  blocklen = EXT_HEADER_LEN * rprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
538
539
540

  binWriteF77Block(fileID, byteswap, blocklen);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
541
  switch ( rprec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
    {
    case SINGLE_PRECISION:
      {
	for (i = 0; i < EXT_HEADER_LEN; i++)
          ((INT32 *) tempheader)[i] = (INT32) header[i];

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

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

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

	break;
      }
    default:
      {
563
	Error("unexpected header precision %d", rprec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
564
        break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
565
566
567
568
569
570
      }
    }
  
  binWriteF77Block(fileID, byteswap, blocklen);

  datasize = header[3];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
571
572
  if ( number == EXT_COMP ) datasize *= 2;
  blocklen = datasize * rprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
573
574
575
576
577
578
579

  binWriteF77Block(fileID, byteswap, blocklen);

  extp->datasize = datasize;

  buffer = extp->buffer;

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

  binWriteF77Block(fileID, byteswap, blocklen);

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