extralib.c 12.3 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
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
#define LIBVERSION      1.2.0
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
  static const char *func = "extDebug";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
52
53
54
55
56
57
58
59
60
61

  EXT_Debug = debug;

  if ( EXT_Debug )
    Message(func, "debug level %d", debug);
}


void extLibInit()
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
62
  static const char *func = "extLibInit";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
63
64
65
66
67
68
69
  char *envString;
  char *envName = "EXT_PRECISION";


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

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

  initExtLib = 1;
}

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


Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
void *extNew(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
124
  static const char *func = "extNew";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
125
  extrec_t *extp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
127
128

  if ( ! initExtLib ) extLibInit();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
  extp = (extrec_t *) malloc(sizeof(extrec_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
130
131
132

  extInit(extp);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
133
  return ((void*)extp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
135
136
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
void extDelete(void *ext)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
138
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
139
  static const char *func = "extDelete";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
  extrec_t *extp = (extrec_t *) ext;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
142
143
144
145
146
147
148
149
150
151

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


int extCheckFiletype(int fileID, int *swap)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
  static const char *func = "extCheckFiletype";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
153
154
155
156
157
158
159
160
161
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
198
199
200
201
202
  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 )
    Message(func, "blocklen = %d sblocklen = %d", blocklen, sblocklen);

  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
203
204
  if      ( data && dimxy*fact   == data ) found = 1;
  else if ( data && dimxy*fact*2 == data ) found = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
205
206
207
208
209
210
211
212
213
214
215

  if ( EXT_Debug )
    {
      Message(func, "swap = %d fact = %d", *swap, fact);
      Message(func, "dimxy = %lu data = %lu", dimxy, data);
    }

  return (found);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
216
int extInqHeader(void *ext, int *header)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
217
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
218
  static const char *func = "extInqHeader";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
219
  extrec_t *extp = (extrec_t *) ext;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
220
221
222
223
224
  size_t i;

  for ( i = 0; i < EXT_HEADER_LEN; i++ )
    header[i] = extp->header[i];
  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
225
  if ( EXT_Debug ) Message(func, "datasize = %lu", extp->datasize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
226
227
228
229
230

  return (0);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
231
int extDefHeader(void *ext, const int *header)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
233
  static const char *func = "extDefHeader";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
234
  extrec_t *extp = (extrec_t *) ext;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
235
236
237
238
239
240
  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
241
  if ( extp->number == EXT_COMP ) extp->datasize *= 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
242

Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
  if ( EXT_Debug ) Message(func, "datasize = %lu", extp->datasize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
244
245
246
247
248

  return (0);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
249
int extInqData(void *ext, int prec, void *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
250
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
251
  static const char *func = "extInqData";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
252
  extrec_t *extp = (extrec_t *) ext;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
253
254
255
  size_t datasize;
  size_t i;
  int ierr = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
256
  int rprec, number;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
257
258
259
260
  void *buffer;
  int byteswap = extp->byteswap;

  datasize = extp->datasize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
261
262
263
  buffer   = extp->buffer;
  rprec    = extp->prec;
  number   = extp->number;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
264

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

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

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

  return (ierr);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
311
int extInqDataSP(void *ext, float *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
312
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
313
  return (extInqData(ext, SINGLE_PRECISION, (void *) data));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
314
315
316
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
317
int extInqDataDP(void *ext, double *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
319
  return (extInqData(ext, DOUBLE_PRECISION, (void *) data));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
320
321
322
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
323
int extDefData(void *ext, int prec, const void *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
324
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
325
  static const char *func = "extDefData";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
326
  extrec_t *extp = (extrec_t *) ext;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
327
328
329
330
  size_t datasize;
  size_t blocklen;
  size_t buffersize;
  size_t i;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
331
  int rprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
332
333
334
  int *header;
  void *buffer;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
335
336
  if ( extDefaultPrec ) rprec = extDefaultPrec;
  else                  rprec = extp->prec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
337

Uwe Schulzweida's avatar
Uwe Schulzweida committed
338
  if ( ! rprec ) rprec = prec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
339

Uwe Schulzweida's avatar
Uwe Schulzweida committed
340
  extp->prec = rprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
341
342
343
344

  header = extp->header;

  datasize = header[3];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
345
346
  if ( extp->number == EXT_COMP ) datasize *= 2;
  blocklen = datasize * rprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362

  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
363
  switch ( rprec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
364
365
366
    {
    case SINGLE_PRECISION:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
367
	if ( rprec == prec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368
	  memcpy(buffer, data, datasize*sizeof(FLT32));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
369
370
371
372
373
374
375
376
	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
377
	if ( rprec == prec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
378
	  memcpy(buffer, data, datasize*sizeof(FLT64));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
379
380
381
382
383
384
385
386
	else
	  for (i = 0; i < datasize; i++)
	    ((double *) buffer)[i] = (double) ((float *) data)[i];

	break;
      }
    default:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
387
	Error(func, "unexpected data precision %d", rprec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
388
389
390
391
392
393
394
      }
    }

  return (0);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
int extDefDataSP(void *ext, const float *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
396
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
397
  return (extDefData(ext, SINGLE_PRECISION, (void *) data));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
398
399
400
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
401
int extDefDataDP(void *ext, const double *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
402
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
403
  return (extDefData(ext, DOUBLE_PRECISION, (void *) data));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
404
405
406
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
407
int extRead(int fileID, void *ext)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
408
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
409
  static const char *func = "extRead";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
410
  extrec_t *extp = (extrec_t *) ext;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
  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);
      if ( status == 0 ) Error(func, "Not a EXTRA file!");
      extp->checked = 1;
    }

  byteswap = extp->byteswap;

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

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

  if ( EXT_Debug )
    Message(func, "blocklen = %lu", blocklen);

  hprec = blocklen / EXT_HEADER_LEN;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
439
  extp->prec = hprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
466
467
468
469
470
471
472
473
474
475
476

  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:
      {
	Error(func, "unexpected header precision %d", hprec);
      }
    }

  blocklen2 = binReadF77Block(fileID, byteswap);

  if ( blocklen2 != blocklen )
    {
      Warning(func, "header blocklen differ!");
      return (-1);
    }

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
477
  if ( EXT_Debug ) Message(func, "datasize = %lu", extp->datasize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493

  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
494
  dprec = blocklen / extp->datasize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
495

Uwe Schulzweida's avatar
Uwe Schulzweida committed
496
497
498
499
500
501
502
503
504
505
  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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526

  if ( dprec != SINGLE_PRECISION && dprec != DOUBLE_PRECISION )
    {
      Warning(func, "unexpected data precision %d", dprec);
      return (-1);
    }

  fileRead(fileID, buffer, blocklen);

  blocklen2 = binReadF77Block(fileID, byteswap);

  if ( blocklen2 != blocklen )
    {
      Warning(func, "data blocklen differ!");
      return (-1);
    }

  return (0);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
527
int extWrite(int fileID, void *ext)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
528
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
529
  static const char *func = "extWrite";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
530
  extrec_t *extp = (extrec_t *) ext;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
531
532
533
  size_t datasize;
  size_t blocklen;
  size_t i;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
534
  int rprec, number;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
535
536
537
538
539
540
  char tempheader[32];
  int *header;
  void *buffer;
  int byteswap = extp->byteswap;


Uwe Schulzweida's avatar
Uwe Schulzweida committed
541
542
  rprec  = extp->prec;
  number = extp->number;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
543
544
545
  header = extp->header;

  /* write header record */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
546
  blocklen = EXT_HEADER_LEN * rprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
547
548
549

  binWriteF77Block(fileID, byteswap, blocklen);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
550
  switch ( rprec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
    {
    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:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
572
	Error(func, "unexpected header precision %d", rprec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
573
574
575
576
577
578
      }
    }
  
  binWriteF77Block(fileID, byteswap, blocklen);

  datasize = header[3];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
579
580
  if ( number == EXT_COMP ) datasize *= 2;
  blocklen = datasize * rprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
581
582
583
584
585
586
587

  binWriteF77Block(fileID, byteswap, blocklen);

  extp->datasize = datasize;

  buffer = extp->buffer;

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

  binWriteF77Block(fileID, byteswap, blocklen);

  return (0);
}