File: header.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (473 lines) | stat: -rw-r--r-- 14,477 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
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
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
466
467
468
469
470
471
472
473
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <rpc/types.h>
#include <rpc/xdr.h>
#include "G3d.h"
#include "G3d_intern.h"
#include "g3dkeys.h"

/*---------------------------------------------------------------------------*/

char *tmpCompress = NULL;
int tmpCompressLength;
char *xdr = NULL;
int xdrLength;

/*---------------------------------------------------------------------------*/

#define G3D_HEADER_TILEX "TileDimensionX"
#define G3D_HEADER_TILEY "TileDimensionY"
#define G3D_HEADER_TILEZ "TileDimensionZ"
#define G3D_HEADER_TYPE "CellType"
#define G3D_HEADER_COMPRESSION "useCompression"
#define G3D_HEADER_USERLE "useRle"
#define G3D_HEADER_USELZW "useLzw"
#define G3D_HEADER_PRECISION "Precision"
#define G3D_HEADER_DATA_OFFSET "nofHeaderBytes"
#define G3D_HEADER_USEXDR "useXdr"
#define G3D_HEADER_HASINDEX "hasIndex"
#define G3D_HEADER_UNIT "Units"

/*---------------------------------------------------------------------------*/

static int
G3d_readWriteHeader (headerKeys, doRead,
		     proj, zone,
		     north, south, east, west, top, bottom,
		     rows, cols, depths,
		     ew_res, ns_res, tb_res,
		     tileX, tileY, tileZ,
		     type, 
		     compression, useRle, useLzw, precision,
		     dataOffset, useXdr, hasIndex, unit)

     struct Key_Value *headerKeys;
     int doRead;
     int *proj, *zone;
     double *north, *south, *east, *west, *top, *bottom;
     int *rows, *cols, *depths;
     double *ew_res, *ns_res, *tb_res;
     int *tileX, *tileY, *tileZ, *type, *compression;
     int *useRle, *useLzw, *precision, *dataOffset, *useXdr, *hasIndex;
     char **unit;

{
  char msg[1024];
  int returnVal;
  int (*headerInt) (), (*headerDouble) (), (*headerValue) ();
  int (*headerString) ();

  if (doRead) {
    headerDouble = G3d_keyGetDouble;
    headerInt = G3d_keyGetInt;
    headerString = G3d_keyGetString;
    headerValue = G3d_keyGetValue;
  } else {
    headerDouble = G3d_keySetDouble;
    headerInt = G3d_keySetInt;
    headerString = G3d_keySetString;
    headerValue = G3d_keySetValue;
  }

  returnVal = 1;
  returnVal &= headerInt (headerKeys, G3D_REGION_PROJ, proj);
  returnVal &= headerInt (headerKeys, G3D_REGION_ZONE, zone);

  returnVal &= headerDouble (headerKeys, G3D_REGION_NORTH, north);
  returnVal &= headerDouble (headerKeys, G3D_REGION_SOUTH, south);
  returnVal &= headerDouble (headerKeys, G3D_REGION_EAST, east );
  returnVal &= headerDouble (headerKeys, G3D_REGION_WEST, west);
  returnVal &= headerDouble (headerKeys, G3D_REGION_TOP, top);
  returnVal &= headerDouble (headerKeys, G3D_REGION_BOTTOM, bottom);

  returnVal &= headerInt (headerKeys, G3D_REGION_ROWS, rows);
  returnVal &= headerInt (headerKeys, G3D_REGION_COLS, cols);
  returnVal &= headerInt (headerKeys, G3D_REGION_DEPTHS, depths);

  returnVal &= headerDouble (headerKeys, G3D_REGION_NSRES, ns_res);
  returnVal &= headerDouble (headerKeys, G3D_REGION_EWRES, ew_res);
  returnVal &= headerDouble (headerKeys, G3D_REGION_TBRES, tb_res);

  returnVal &= headerInt (headerKeys, G3D_HEADER_TILEX, tileX);
  returnVal &= headerInt (headerKeys, G3D_HEADER_TILEY, tileY);
  returnVal &= headerInt (headerKeys, G3D_HEADER_TILEZ, tileZ);

  returnVal &= headerValue (headerKeys, G3D_HEADER_TYPE, 
			    "double", "float", G3D_DOUBLE, G3D_FLOAT, type);
  returnVal &= headerValue (headerKeys, G3D_HEADER_COMPRESSION, 
			    "0", "1", 0, 1, compression);
  returnVal &= headerValue (headerKeys, G3D_HEADER_USERLE, 
			    "0", "1", 0, 1, useRle);
  returnVal &= headerValue (headerKeys, G3D_HEADER_USELZW, 
			    "0", "1", 0, 1, useLzw);

  returnVal &= headerInt (headerKeys, G3D_HEADER_PRECISION, precision);
  returnVal &= headerInt (headerKeys, G3D_HEADER_DATA_OFFSET, dataOffset);

  returnVal &= headerValue (headerKeys, G3D_HEADER_USEXDR, 
			    "0", "1", 0, 1, useXdr);
  returnVal &= headerValue (headerKeys, G3D_HEADER_HASINDEX, 
			    "0", "1", 0, 1, hasIndex);
  returnVal &= headerString (headerKeys, G3D_HEADER_UNIT, unit);

  if (returnVal) return 1;

  G3d_error ("G3d_readWriteHeader: error writing header");
  return 0;
}

/*---------------------------------------------------------------------------*/

int
G3d_readHeader (map,
		proj, zone,
		north, south, east, west, top, bottom,
		rows, cols, depths,
		ew_res, ns_res, tb_res,
		tileX, tileY, tileZ,
		type, 
		compression, useRle, useLzw, precision,
		dataOffset, useXdr, hasIndex, unit)

     G3D_Map *map;
     int *proj, *zone;
     double *north, *south, *east, *west, *top, *bottom;
     int *rows, *cols, *depths;
     double *ew_res, *ns_res, *tb_res;
     int *tileX, *tileY, *tileZ, *type, *compression;
     int *useRle, *useLzw, *precision, *dataOffset, *useXdr, *hasIndex;
     char **unit;

{
  struct Key_Value *headerKeys;
  char path[1024], msg[1024];
  int status, returnVal;

  G3d_filename (path, G3D_HEADER_ELEMENT, map->fileName, map->mapset);
  if (access(path, R_OK) != 0) {
    sprintf (msg,"G3d_readHeader: unable to find [%s]", path);
    G3d_error (msg);
    return 0;
  }
  
  headerKeys = G_read_key_value_file (path, &status);
  if (status != 0) {
    sprintf (msg, "G3d_readHeader: Unable to open %s", path);
    G3d_error (msg);
    return 0;
  }

  if (! G3d_readWriteHeader (headerKeys, 1, 
			     proj, zone,
			     north, south, east, west, top, bottom,
			     rows, cols, depths, 
			     ew_res, ns_res, tb_res,
			     tileX, tileY, tileZ,
			     type, compression, useRle, useLzw, precision,
			     dataOffset, useXdr, hasIndex, unit)) {
    sprintf (msg, "G3d_readHeader: error extracting header key(s) of file %s",
	     path);
    G3d_error (msg);
    return 0;
  }

  G_free_key_value(headerKeys);
  return 1;
}

/*---------------------------------------------------------------------------*/

int
G3d_writeHeader (map,
		 proj, zone,
		 north, south, east, west, top, bottom,
		 rows, cols, depths,
		 ew_res, ns_res, tb_res,
		 tileX, tileY, tileZ,
		 type, 
		 compression, useRle, useLzw, precision,
		 dataOffset, useXdr, hasIndex, unit)

     G3D_Map *map;
     int proj, zone;
     double north, south, east, west, top, bottom;
     int rows, cols, depths;
     double ew_res, ns_res, tb_res;
     int tileX, tileY, tileZ, type, compression;
     int useRle, useLzw, precision, dataOffset, useXdr, hasIndex;
     char *unit;

{
  struct Key_Value *headerKeys;
  char path[1024], msg[1024];
  int status;

  headerKeys = G_create_key_value();

  if (! G3d_readWriteHeader (headerKeys, 0, 
			     &proj, &zone,
			     &north, &south, &east, &west, &top, &bottom,
			     &rows, &cols, &depths, 
			     &ew_res, &ns_res, &tb_res,
			     &tileX, &tileY, &tileZ,
			     &type, &compression, &useRle, &useLzw, 
			     &precision, &dataOffset, &useXdr, &hasIndex,
			     &unit)) {
    sprintf (msg, "G3d_writeHeader: error adding header key(s) for file %s", 
	     path);
    G3d_error (msg);
    return 0;
  }

  G3d_filename(path, G3D_HEADER_ELEMENT, map->fileName, map->mapset);
  G3d_makeMapsetMapDirectory (map->fileName);
  G_write_key_value_file (path, headerKeys, &status);

  G_free_key_value(headerKeys);

  if (status == 0) return 1;

  sprintf (msg, "G3d_writeHeader: error writing header file %s", path);
  G3d_error (msg);
  return 0;
}
  
/*---------------------------------------------------------------------------*/


/*!
 * \brief 
 *
 *  Returns a number
 * which encodes multiplicity <em>n</em> of <em>cacheCode</em>. This value can be used
 * to specify the size of the cache.
 * If <em>cacheCode</em> is the size (in tiles) of the cache the function returns
 * <em>cacheCode * n</em>.
 * If <em>cacheCode</em> is G3D_USE_CACHE_DEFAULT the function returns
 * G3D_USE_CACHE_DEFAULT.
 * If <em>cacheCode</em> is G3D_USE_CACHE_??? the function returns a value
 * encoding G3D_USE_CACHE_??? and <em>n</em>. Here G3D_USE_CACHE_??? is one
 * of G3D_USE_CACHE_X, G3D_USE_CACHE_Y, G3D_USE_CACHE_Z,
 * G3D_USE_CACHE_XY, G3D_USE_CACHE_XZ, G3D_USE_CACHE_YZ, or
 * G3D_USE_CACHE_XYZ, where e.g.  G3D_USE_CACHE_X specifies that the cache
 * should store as many tiles as there exist in one row along the x-axis of the
 * tile cube, and G3D_USE_CACHE_XY specifies that the cache should store as
 * many tiles as there exist in one slice of the tile cube with constant Z
 * coordinate.
 *
 *  \param cacheCode
 *  \param n
 *  \return int
 */

int
G3d_cacheSizeEncode (cacheCode, n)

     int cacheCode, n;

{
  if (cacheCode >= G3D_NO_CACHE) return cacheCode * n;
  if (cacheCode == G3D_USE_CACHE_DEFAULT) return cacheCode;

  if (cacheCode < G3D_USE_CACHE_XYZ) 
    G3d_fatalError ("G3d_cacheSizeEncode: invalid cache code");

  return n * (-10) + cacheCode;
}

/*---------------------------------------------------------------------------*/

int
G3d__computeCacheSize (map, cacheCode)

     G3D_Map *map;
     int cacheCode;

{
  int n, size;

  if (cacheCode >= G3D_NO_CACHE) return cacheCode;
  if (cacheCode == G3D_USE_CACHE_DEFAULT) 
    return G3D_MIN (g3d_cache_default, map->nTiles);

  n = - (cacheCode / 10);
  n = G3D_MAX (1, n);
  cacheCode = - ((-cacheCode) % 10);

  if      (cacheCode == G3D_USE_CACHE_X) size = map->nx * n;
  else if (cacheCode == G3D_USE_CACHE_Y) size = map->ny * n;
  else if (cacheCode == G3D_USE_CACHE_Z) size = map->nz * n;
  else if (cacheCode == G3D_USE_CACHE_XY) size = map->nxy * n;
  else if (cacheCode == G3D_USE_CACHE_XZ) size = map->nx * map->nz * n;
  else if (cacheCode == G3D_USE_CACHE_YZ) size = map->ny * map->nz * n;
  else if (cacheCode == G3D_USE_CACHE_XYZ) size = map->nTiles;
  else G3d_fatalError ("G3d__computeCacheSize: invalid cache code");

  return G3D_MIN (size, map->nTiles);
}

/*---------------------------------------------------------------------------*/

/* this function does actually more than filling the header fields of the */
/* G3D-Map structure. It also allocates memory for compression and xdr, */
/* and initializes the index and cache. This function should be taken apart. */

int
G3d_fillHeader (map, operation, compression, useRle, useLzw, type, precision, 
		cache, hasIndex, useXdr, 
		typeIntern, nofHeaderBytes, 
		tileX, tileY, tileZ, 
		proj, zone,
		north, south, east, west, top, bottom, rows, cols, depths,
		ew_res, ns_res, tb_res,
		unit)

     G3D_Map *map;
     int compression, useRle, useLzw, operation, precision, cache, hasIndex;
     int useXdr, type, nofHeaderBytes, tileX, tileY, tileZ;
     int proj, zone;
     double north, south, east, west, top, bottom;
     int rows, cols, depths, typeIntern;
     double ew_res, ns_res, tb_res;
     char *unit;

{
  if (! G3D_VALID_OPERATION (operation)) 
    G3d_fatalError ("G3d_fillHeader: operation not valid\n");

  map->operation = operation;

  map->unit = G_store (unit);

  map->region.proj = proj;
  map->region.zone = zone;

  map->region.north = north;
  map->region.south = south;
  map->region.east = east;
  map->region.west = west;
  map->region.top = top;
  map->region.bottom = bottom;

  map->region.rows = rows; 
  map->region.cols = cols; 
  map->region.depths = depths; 

  map->region.ew_res = ew_res;
  map->region.ns_res = ns_res;
  map->region.tb_res = tb_res;

  G3d_adjustRegion (&(map->region));

  map->tileX = tileX;
  map->tileY = tileY;
  map->tileZ = tileZ;
  map->tileXY = map->tileX * map->tileY;
  map->tileSize = map->tileXY * map->tileZ;

  map->nx = (map->region.cols - 1) / tileX + 1;
  map->ny = (map->region.rows - 1) / tileY + 1;
  map->nz = (map->region.depths - 1) / tileZ + 1;
  map->nxy = map->nx * map->ny;
  map->nTiles = map->nxy * map->nz;
  
  if ((map->region.cols) % map->tileX != 0) map->clipX = map->nx - 1;
  else map->clipX = -1;
  if ((map->region.rows) % map->tileY != 0) map->clipY = map->ny - 1;
  else map->clipY = -1;
  if ((map->region.depths) % map->tileZ != 0) map->clipZ = map->nz - 1;
  else map->clipZ = -1;

  if ((type != G3D_FLOAT) && (type != G3D_DOUBLE))
    G3d_fatalError ("G3d_fillHeader: invalid type");
  map->type = type;

  if ((typeIntern != G3D_FLOAT) && (typeIntern != G3D_DOUBLE))
    G3d_fatalError ("G3d_fillHeader: invalid type");
  map->typeIntern = typeIntern;

  if (! G3D_VALID_XDR_OPTION (useXdr))
    G3d_fatalError ("G3d_fillHeader: invalid xdr option");
  map->useXdr = useXdr;

  map->offset = nofHeaderBytes;

  if ((map->fileEndPtr = lseek (map->data_fd, (long) 0, SEEK_END)) == -1) {
    G3d_error ("G3d_fillHeader: can't position file");
    return 0;
  }

  map->useCache = (cache != G3D_NO_CACHE);

  map->numLengthIntern = G3d_length (map->typeIntern);
  map->numLengthExtern = G3d_externLength (map->type);

  map->compression = compression;
  map->useRle = useRle;
  map->useLzw = useLzw;
  map->precision = precision;

#define RLE_STATUS_BYTES 2

  if (map->compression != G3D_NO_COMPRESSION) {
    if (tmpCompress == NULL) {
      tmpCompressLength = map->tileSize * 
                         G3D_MAX (map->numLengthIntern, map->numLengthExtern) +
			 RLE_STATUS_BYTES;
      tmpCompress = G3d_malloc (tmpCompressLength);
      if (tmpCompress == NULL) {
	G3d_error ("G3d_fillHeader: error in G3d_malloc");
	return 0;
      }
    } else
      if (map->tileSize * G3D_MAX (map->numLengthIntern, map->numLengthExtern)
	  + RLE_STATUS_BYTES > tmpCompressLength) {
	tmpCompressLength = map->tileSize * 
	                 G3D_MAX (map->numLengthIntern, map->numLengthExtern) +
			 RLE_STATUS_BYTES;
	tmpCompress = G3d_realloc (tmpCompress, tmpCompressLength);
	if (tmpCompress == NULL) {
	  G3d_error ("G3d_fillHeader: error in G3d_realloc");
	  return 0;
	}
      }
  }

#define XDR_MISUSE_BYTES 10

  if (! G3d_initFpXdr (map, XDR_MISUSE_BYTES)) {
    G3d_error ("G3d_fillHeader: error in G3d_initFpXdr");
    return 0;
  }

  if ((! map->useCache) ||
      ((cache == G3D_USE_CACHE_DEFAULT) && (g3d_cache_default == 0))) {
    map->useCache = 0;
    map->cache = NULL;
    /* allocate one tile buffer */
    map->data = G3d_malloc (map->tileSize * map->numLengthIntern);
    if (map->data == NULL) {
      G3d_error ("G3d_fillHeader: error in G3d_malloc");
      return 0;
    }
    map->currentIndex = -1;
  } else {
    if (! G3d_initCache (map, 
			 G3D_MAX (1, 
				  G3D_MIN (G3d__computeCacheSize (map, cache),
				  g3d_cache_max / 
				  map->tileSize / map->numLengthIntern)))) {
      G3d_error ("G3d_fillHeader: error in G3d_initCache");
      return 0;
    }
  }

  if (! G3d_initIndex (map, hasIndex)) {
    G3d_error ("G3d_fillHeader: error in G3d_initIndex");
    return 0;
  }

  return 1;
}