ieglib.c 13 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 "ieg.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"


Uwe Schulzweida's avatar
Uwe Schulzweida committed
20
static int initIegLib      = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
22
23
24
25
26
27
28
static int iegDefaultDprec = 0;


/*
 * A version string.
 */

#undef  LIBVERSION
29
#define LIBVERSION      1.3.3
Uwe Schulzweida's avatar
Uwe Schulzweida committed
30
31
#define XSTRING(x)	#x
#define STRING(x)	XSTRING(x)
32
static const char ieg_libvers[] = STRING(LIBVERSION) " of " __DATE__ " " __TIME__;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46

const char *iegLibraryVersion(void)
{
  return (ieg_libvers);
}


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



void iegLibInit()
{
  char *envString;
Oliver Heidmann's avatar
Oliver Heidmann committed
47
  const char *envName = "IEG_PRECISION";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69


  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 'r':
	      {
		switch ( (int) envString[pos+1] )
		  {
		  case '4': iegDefaultDprec = SINGLE_PRECISION; break;
		  case '8': iegDefaultDprec = DOUBLE_PRECISION; break;
		  default:
70
		    Message("Invalid digit in %s: %s", envName, envString);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
71
		  }
72
		break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
73
74
	      }
	    default:
75
76
77
78
79
              {
                Message("Invalid character in %s: %s", envName, envString);
                break;
              }
            }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
80
81
82
83
84
85
86
87
88
89
90
91
92
	  pos += 2;
	}
    }

  initIegLib = 1;
}


void iegDebug(int debug)
{
  IEG_Debug = debug;

  if ( IEG_Debug )
93
    Message("debug level %d", debug);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
94
95
96
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
97
void iegInit(iegrec_t *iegp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98
99
100
101
102
103
104
105
{
  iegp->checked    = 0;
  iegp->byteswap   = 0;
  iegp->dprec      = 0;
  iegp->refval     = 0;
  iegp->datasize   = 0;
  iegp->buffersize = 0;
  iegp->buffer     = NULL;
106
107
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
108

Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
void iegInitMem(iegrec_t *iegp)
110
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
111
112
113
114
115
116
  memset(iegp->ipdb, 0, sizeof(iegp->ipdb));
  memset(iegp->igdb, 0, sizeof(iegp->igdb));
  memset(iegp->vct,  0, sizeof(iegp->vct));
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
iegrec_t *iegNew(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
  iegrec_t *iegp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
120
121
122

  if ( ! initIegLib ) iegLibInit();

Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
  iegp = (iegrec_t *) malloc(sizeof(iegrec_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
124
125

  iegInit(iegp);
126
  iegInitMem(iegp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
128
129
130
131

  return (iegp);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
void iegDelete(iegrec_t *iegp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
{
  if ( iegp )
    {
      if ( iegp->buffer ) free(iegp->buffer);
      free(iegp);
    }
}


int iegCheckFiletype(int fileID, int *swap)
{
  size_t blocklen = 0;
  size_t sblocklen = 0;
  size_t data = 0;
  size_t dimx = 0, dimy = 0;
148
  size_t fact = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
150
  unsigned char buffer[1048], *pbuf;

151
  if ( fileRead(fileID, buffer, 4) != 4 ) return (0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
153
154
155
156

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

  if ( IEG_Debug )
157
    Message("blocklen = %d sblocklen = %d", blocklen, sblocklen);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
158
159
160
161
162

  if ( blocklen == 636 || blocklen == 640 )
    {
     *swap = 0;
      fact = 4;
163
      if ( fileRead(fileID, buffer, blocklen+8) != blocklen+8 ) return (0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
164
165
166
167
168
169
170
171
      pbuf = buffer+(37+4)*4;    dimx = (size_t) get_UINT32(pbuf);
      pbuf = buffer+(37+5)*4;    dimy = (size_t) get_UINT32(pbuf);
      pbuf = buffer+blocklen+4;  data = (size_t) get_UINT32(pbuf);
    }
  else if ( blocklen == 1040 || blocklen == 1036 )
    {
     *swap = 0;
      fact = 8;
172
      if ( fileRead(fileID, buffer, blocklen+8) != blocklen+8 ) return (0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
173
174
175
176
177
178
179
180
      pbuf = buffer+(37+4)*4;    dimx = (size_t) get_UINT32(pbuf);
      pbuf = buffer+(37+5)*4;    dimy = (size_t) get_UINT32(pbuf);
      pbuf = buffer+blocklen+4;  data = (size_t) get_UINT32(pbuf);
    }
  else if ( sblocklen == 636 || sblocklen == 640 )
    {
     *swap = 1;
      fact = 4;
181
      if ( fileRead(fileID, buffer, sblocklen+8) != sblocklen+8 ) return (0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
182
183
184
185
186
187
188
189
      pbuf = buffer+(37+4)*4;     dimx = (size_t) get_SUINT32(pbuf);
      pbuf = buffer+(37+5)*4;     dimy = (size_t) get_SUINT32(pbuf);
      pbuf = buffer+sblocklen+4;  data = (size_t) get_SUINT32(pbuf);
    }
  else if ( sblocklen == 1040 || sblocklen == 1036 )
    {
     *swap = 1;
      fact = 8;
190
      if ( fileRead(fileID, buffer, sblocklen+8) != sblocklen+8 ) return (0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
192
193
194
195
196
197
      pbuf = buffer+(37+4)*4;     dimx = (size_t) get_SUINT32(pbuf);
      pbuf = buffer+(37+5)*4;     dimy = (size_t) get_SUINT32(pbuf);
      pbuf = buffer+sblocklen+4;  data = (size_t) get_SUINT32(pbuf);
    }

  fileRewind(fileID);

198
  int found = data && (dimx*dimy*fact == data || dimx*dimy*8 == data);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
199
200
201

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

  return (found);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
210
void iegCopyMeta(iegrec_t *diegp, iegrec_t *siegp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
211
212
213
214
215
216
217
218
219
220
221
{
  /*  diegp->byteswap = siegp->byteswap; */
  diegp->dprec    = siegp->dprec;
  diegp->refval   = siegp->refval;

  memcpy(diegp->ipdb, siegp->ipdb, sizeof(siegp->ipdb));
  memcpy(diegp->igdb, siegp->igdb, sizeof(siegp->igdb));
  memcpy(diegp->vct,  siegp->vct,  sizeof(siegp->vct));
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
222
int iegInqData(iegrec_t *iegp, int prec, void *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
{
  size_t datasize;
  size_t i;
  int ierr = 0;
  int dprec;
  void *buffer;
  int byteswap = iegp->byteswap;


  datasize = iegp->datasize;

  buffer = iegp->buffer;

  dprec = iegp->dprec;

  switch ( dprec )
    {
    case SINGLE_PRECISION:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
242
	if ( sizeof(FLT32) == 4 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
244
245
246
	  {
	    if ( byteswap ) swap4byte(buffer, datasize);

	    if ( dprec == prec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
247
	      memcpy(data, buffer, datasize*sizeof(FLT32));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
248
249
250
251
252
253
	    else
	      for (i = 0; i < datasize; i++)
		((double *) data)[i] = (double) ((float *) buffer)[i];
	  }
	else
	  {
254
	    Error("not implemented for %d byte float", sizeof(FLT32));
255
	  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
256
257
258
	break;
      }
    case DOUBLE_PRECISION:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
259
	if ( sizeof(FLT64) == 8 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
260
261
262
263
	  {
	    if ( byteswap ) swap8byte(buffer, datasize);

	    if ( dprec == prec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
264
	      memcpy(data, buffer, datasize*sizeof(FLT64));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
265
266
267
268
269
270
	    else
	      for (i = 0; i < datasize; i++)
		((float *) data)[i] = (float) ((double *) buffer)[i];
	  }
	else
	  {
271
	    Error("not implemented for %d byte float", sizeof(FLT64));
272
	  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
273
274
275
	break;
    default:
      {
276
	Error("unexpected data precision %d", dprec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
277
        break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
278
279
280
281
282
283
284
      }
    }

  return (ierr);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
285
int iegInqDataSP(iegrec_t *iegp, float *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
286
287
288
289
290
{
  return (iegInqData(iegp, SINGLE_PRECISION, (void *) data));
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
291
int iegInqDataDP(iegrec_t *iegp, double *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
292
293
294
295
296
{
  return (iegInqData(iegp, DOUBLE_PRECISION, (void *) data));
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
297
int iegDefData(iegrec_t *iegp, int prec, const void *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
{
  size_t datasize;
  size_t blocklen;
  size_t buffersize;
  size_t i;
  int dprec;
  void *buffer;


  if ( iegDefaultDprec ) dprec = iegDefaultDprec;
  else                   dprec = iegp->dprec;

  if ( ! dprec ) dprec = prec;

  iegp->dprec = dprec;

314
315
  datasize = (size_t)IEG_G_NumLon(iegp->igdb) * (size_t)IEG_G_NumLat(iegp->igdb);
  blocklen = datasize * (size_t)dprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336

  iegp->datasize = datasize;

  buffersize = iegp->buffersize;

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

  switch ( dprec )
    {
    case SINGLE_PRECISION:
      {
	if ( dprec == prec )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
337
	  memcpy(buffer, data, datasize*sizeof(FLT32));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
338
339
340
341
342
343
344
345
346
	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
347
	  memcpy(buffer, data, datasize*sizeof(FLT64));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
348
349
350
351
352
353
354
355
	else
	  for (i = 0; i < datasize; i++)
	    ((double *) buffer)[i] = (double) ((float *) data)[i];

	break;
      }
    default:
      {
356
	Error("unexpected data precision %d", dprec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
357
        break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
358
359
360
361
362
363
364
      }
    }

  return (0);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
365
int iegDefDataSP(iegrec_t *iegp, const float *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
366
367
368
369
370
{
  return (iegDefData(iegp, SINGLE_PRECISION, (void *) data));
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
371
int iegDefDataDP(iegrec_t *iegp, const double *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
372
373
374
375
376
{
  return (iegDefData(iegp, DOUBLE_PRECISION, (void *) data));
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
377
int iegRead(int fileID, iegrec_t *iegp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
378
379
380
381
{
  size_t datasize;
  size_t blocklen, blocklen2;
  size_t i;
382
  char tmpbuffer[800], *tmpbuf = tmpbuffer;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
383
384
385
386
387
388
389
390
  int dprec = 0;
  void *buffer;
  int byteswap;
  int status;

  if ( ! iegp->checked )
    {
      status = iegCheckFiletype(fileID, &iegp->byteswap);
391
      if ( status == 0 ) Error("Not a IEG file!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
392
393
394
395
396
397
398
399
400
401
402
      iegp->checked = 1;
    }

  byteswap = iegp->byteswap;

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

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

  if ( IEG_Debug )
403
    Message("blocklen = %lu", blocklen);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
404
405
406
407
408
409
410

  if ( blocklen == 636 || blocklen == 640 )
    dprec = 4;
  else if ( blocklen == 1040 || blocklen == 1036 )
    dprec = 8;
  else
    {
411
      Warning("unexpecteted header size %d!", (int) blocklen);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
      return (-1);
    }

  iegp->dprec = dprec;

  binReadInt32(fileID, byteswap, 37, (INT32 *) tmpbuf);
  for ( i = 0; i < 37; i++ ) iegp->ipdb[i] = (int) ((INT32 *) tmpbuf)[i];

  binReadInt32(fileID, byteswap, 18, (INT32 *) tmpbuf);
  for ( i = 0; i < 18; i++ ) iegp->igdb[i] = (int) ((INT32 *) tmpbuf)[i];

  if ( blocklen == 636 || blocklen == 1036 )
    {
      fileRead(fileID, tmpbuf, 4);
      if ( byteswap ) swap4byte(tmpbuf, 1);
      iegp->refval = (double) ((float *) tmpbuf)[0];
    }
  else
    {
      fileRead(fileID, tmpbuf, 8);
      if ( byteswap ) swap8byte(tmpbuf, 1);
      iegp->refval = (double) ((double *) tmpbuf)[0];
    }

  binReadInt32(fileID, byteswap, 3, (INT32 *) tmpbuf);
  for ( i = 0; i < 3; i++ ) iegp->igdb[18+i] = (int) ((INT32 *) tmpbuf)[i];

  if ( dprec == SINGLE_PRECISION )
    {
      fileRead(fileID, tmpbuf, 400);
      if ( byteswap ) swap4byte(tmpbuf, 100);
      for ( i = 0; i < 100; i++ )
	iegp->vct[i] = (double) ((float *) tmpbuf)[i];
    }
  else
    {
      fileRead(fileID, tmpbuf, 800);
      if ( byteswap ) swap8byte(tmpbuf, 100);
      for ( i = 0; i < 100; i++ )
	iegp->vct[i] = (double) ((double *) tmpbuf)[i];
    }
453

Uwe Schulzweida's avatar
Uwe Schulzweida committed
454
455
456
457
458
  /*
  fprintf(stderr, "refval %g\n", iegp->refval);

  for ( i = 0; i < 100; i++ )
    fprintf(stderr, "%3d %g\n", i, iegp->vct[i]);
459

Uwe Schulzweida's avatar
Uwe Schulzweida committed
460
461
462
463
464
465
466
467
468
469
470
471
  {
    int i;
    for ( i = 0; i < 37; i++ )
      fprintf(stderr, "pdb: %d %d\n", i, iegp->ipdb[i]);
    for ( i = 0; i < 22; i++ )
      fprintf(stderr, "gdb: %d %d\n", i, iegp->igdb[i]);
  }
  */
  blocklen2 = binReadF77Block(fileID, byteswap);

  if ( blocklen2 != blocklen )
    {
472
      Warning("header blocklen differ!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
473
474
475
      return (-1);
    }

476
477
  iegp->datasize = (size_t)IEG_G_NumLon(iegp->igdb)
    * (size_t)IEG_G_NumLat(iegp->igdb);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
478
479

  if ( IEG_Debug )
480
    Message("datasize = %lu", iegp->datasize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
481
482
483

  blocklen = binReadF77Block(fileID, byteswap);

484
  size_t buffersize = iegp->buffersize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485

486
  if ( buffersize < blocklen )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
    {
      buffersize = blocklen;
      buffer = iegp->buffer;
      buffer = realloc(buffer, buffersize);
      iegp->buffer = buffer;
      iegp->buffersize = buffersize;
    }
  else
    buffer = iegp->buffer;

  datasize = iegp->datasize;

  if ( dprec != (int) (blocklen/datasize) )
    {
501
      Warning("data precision differ! (h = %d; d = %d)",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
502
503
504
505
506
507
508
509
510
511
	      (int) dprec, (int) (blocklen/datasize));
      return (-1);
    }

  fileRead(fileID, buffer, blocklen);

  blocklen2 = binReadF77Block(fileID, byteswap);

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

  return (0);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
520
int iegWrite(int fileID, iegrec_t *iegp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
521
522
523
524
525
{
  size_t datasize;
  size_t blocklen;
  size_t i;
  int dprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
526
527
  float refvalf;
  double refval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
528
529
530
531
532
533
534
535
536
537
538
539
  char tmpbuf[800];
  float fvct[100];
  void *buffer;
  int byteswap = iegp->byteswap;


  dprec  = iegp->dprec;

  /* write header record */
  if ( dprec == SINGLE_PRECISION )
    blocklen = 636;
  else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
540
    blocklen = 1040;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
541
542
543
544
545
546
547
548
549
550

  binWriteF77Block(fileID, byteswap, blocklen);

  for ( i = 0; i < 37; i++ ) ((INT32 *) tmpbuf)[i] = (INT32) iegp->ipdb[i];
  binWriteInt32(fileID, byteswap, 37, (INT32 *) tmpbuf);

  for ( i = 0; i < 18; i++ ) ((INT32 *) tmpbuf)[i] = (INT32) iegp->igdb[i];
  binWriteInt32(fileID, byteswap, 18, (INT32 *) tmpbuf);

  refval = iegp->refval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
551
552
553
554
555
  refvalf = (float) refval;
  if ( dprec == SINGLE_PRECISION )
    binWriteFlt32(fileID, byteswap, 1, (FLT32 *) &refvalf);
  else
    binWriteFlt64(fileID, byteswap, 1, (FLT64 *) &refval);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
556
557
558
559
560
561
562

  for ( i = 0; i < 3; i++ ) ((INT32 *) tmpbuf)[i] = (INT32) iegp->igdb[18+i];
  binWriteInt32(fileID, byteswap, 3, (INT32 *) tmpbuf);

  if ( dprec == SINGLE_PRECISION )
    {
      for ( i = 0; i < 100; i++ ) fvct[i] = (float) iegp->vct[i];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
563
      binWriteFlt32(fileID, byteswap, 100, fvct);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
564
565
566
    }
  else
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
567
      binWriteFlt64(fileID, byteswap, 100, iegp->vct);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
568
    }
569

Uwe Schulzweida's avatar
Uwe Schulzweida committed
570
571
  binWriteF77Block(fileID, byteswap, blocklen);

572
573
  datasize = (size_t)iegp->igdb[4] * (size_t)iegp->igdb[5];
  blocklen = datasize * (size_t)dprec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
574
575
576
577
578
579
580
581
582
583
584

  binWriteF77Block(fileID, byteswap, blocklen);

  iegp->datasize = datasize;

  buffer = iegp->buffer;

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

  binWriteF77Block(fileID, byteswap, blocklen);

  return (0);
}
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:
 */