File: file.c

package info (click to toggle)
paraview 4.0.1-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 526,572 kB
  • sloc: cpp: 2,284,430; ansic: 816,374; python: 239,936; xml: 70,162; tcl: 48,295; fortran: 39,116; yacc: 5,466; java: 3,518; perl: 3,107; lex: 1,620; sh: 1,555; makefile: 932; asm: 471; pascal: 228
file content (486 lines) | stat: -rw-r--r-- 11,708 bytes parent folder | download | duplicates (4)
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
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
  Copyright 2010 University Corporation for Atmospheric
  Research/Unidata. See COPYRIGHT file for more info.

  This file defines the file create and open functions.
*/

#include "ncdispatch.h"

static int nc_initialized = 0;

static int
NC_check_file_type(const char *path, int use_parallel, void *mpi_info,
		   int *cdf, int *hdf)
{
   char magic[MAGIC_NUMBER_LEN];
    
   *hdf = 0; *cdf = 0;

   /* Get the 4-byte magic from the beginning of the file. Don't use posix
    * for parallel, use the MPI functions instead. */
#ifdef USE_PARALLEL_MPIO
   if (use_parallel) 
   {
      MPI_File fh;
      MPI_Status status;
      int retval;
      MPI_Comm comm = 0;
      MPI_Info info = 0;

      if(mpi_info != NULL) {
	 comm = ((NC_MPI_INFO*)mpi_info)->comm;
	 info = ((NC_MPI_INFO*)mpi_info)->info;
      }
      if((retval = MPI_File_open(comm, (char *)path, MPI_MODE_RDONLY,info, 
				 &fh)) != MPI_SUCCESS)
	 return NC_EPARINIT;
      if((retval = MPI_File_read(fh, magic, MAGIC_NUMBER_LEN, MPI_CHAR,
				 &status)) != MPI_SUCCESS)
	 return NC_EPARINIT;
      if((retval = MPI_File_close(&fh)) != MPI_SUCCESS)
	 return NC_EPARINIT;
   } else
#endif /* USE_PARALLEL */
   {
      FILE *fp;
      int i;

      if (!path)
	 return 0;
      if (!(fp = fopen(path, "r")))
	 return errno;
      i = fread(magic, MAGIC_NUMBER_LEN, 1, fp);
      fclose(fp);
      if(i != 1)
	 return errno;
   }
    
   /* Ignore the first byte for HDF */
   if(magic[1] == 'H' && magic[2] == 'D' && magic[3] == 'F')
      *hdf = 5;
   else if(magic[0] == '\016' && magic[1] == '\003'
	   && magic[2] == '\023' && magic[3] == '\001')
      *hdf = 4;
   else if(magic[0] == 'C' && magic[1] == 'D' && magic[2] == 'F') 
   {
      if(magic[3] == '\001') 
	 *cdf = 1;
      else if(magic[3] == '\002') 
	 *cdf = 2;
   }
    
   return NC_NOERR;
}

int
nc_create(const char *path, int cmode, int *ncidp)
{
   return NC_create(path, cmode, 0, 0, NULL, 0, NULL, ncidp);
}

int
nc__create(const char *path, int cmode, size_t initialsz,
	   size_t *chunksizehintp, int *ncidp)
{
   return NC_create(path, cmode, initialsz, 0, 
		    chunksizehintp, 0, NULL, ncidp);

}

int
nc__create_mp(const char *path, int cmode, size_t initialsz, 
	      int basepe, size_t *chunksizehintp, int *ncidp)
{
   return NC_create(path, cmode, initialsz, basepe, 
		    chunksizehintp, 0, NULL, ncidp);
}

/*
  For create, we have the following pieces of information
  to use to determine the dispatch table.
  1. table specified by override
  2. path
  3. cmode
*/
int
NC_create(const char *path, int cmode, size_t initialsz, 
	  int basepe, size_t *chunksizehintp, int useparallel, 
	  void* mpi_info, int *ncidp)
{
   int stat = NC_NOERR;
   NC* ncp = NULL;
   NC_Dispatch* dispatcher = NULL;
   /* Need three pieces of information for now */
   int model = 0; /* one of the NC_DISPATCH_XXX values */
   int isurl = 0;   /* dap or cdmremote or neither */
   int xcmode = 0; /* for implied cmode flags */
   extern int default_create_format;

   /* Initialize the dispatch table. The function pointers in the
    * dispatch table will depend on how netCDF was built
    * (with/without netCDF-4, DAP, CDMREMOTE). */
   if(!nc_initialized)
   {
      if ((stat = NC_initialize()))
	 return stat; 
      nc_initialized = 1;
   }

   if((isurl = NC_testurl(path)))
	model = NC_urlmodel(path);

   /* Look to the incoming cmode for hints */
   if(model == 0) {
      if(cmode & NC_NETCDF4 || cmode & NC_PNETCDF)
	model = NC_DISPATCH_NC4;
   }

   if(model == 0) {
      /* Check default format */
      int format = default_create_format;
      switch (format) {
#ifdef USE_NETCDF4
	 case NC_FORMAT_NETCDF4:
	    xcmode |= NC_NETCDF4;
	    model = NC_DISPATCH_NC4;
	    break;
	 case NC_FORMAT_NETCDF4_CLASSIC:
	    xcmode |= NC_CLASSIC_MODEL;
	    model = NC_DISPATCH_NC4;
	    break;
#endif
	 case NC_FORMAT_64BIT:
	    xcmode |= NC_64BIT_OFFSET;
	    /* fall thru */
	 case NC_FORMAT_CLASSIC:
	 default:
	    model = NC_DISPATCH_NC3;
	    break;
      }
   }
   
   /* Add inferred flags */
   cmode |= xcmode;

#ifdef USE_NETCDF4
   if((cmode & NC_MPIIO && cmode & NC_MPIPOSIX))
      return  NC_EINVAL;
#endif

   dispatcher = NC_get_dispatch_override();
   if(dispatcher != NULL) goto havetable;

   /* Figure out what dispatcher to use */
#ifdef USE_NETCDF4
#ifdef USE_CDMREMOTE
   if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
	dispatcher = NCCR_dispatch_table;
   else
#endif
#ifdef USE_DAP
   if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCD))
	dispatcher = NCD4_dispatch_table;
   else
#endif
   if(model == (NC_DISPATCH_NC4))
	dispatcher = NC4_dispatch_table;
   else
#endif /*USE_NETCDF4*/
#ifdef USE_DAP
   if(model == (NC_DISPATCH_NC3 | NC_DISPATCH_NCD))
	dispatcher = NCD3_dispatch_table;
   else
#endif
   if(model == (NC_DISPATCH_NC3))
	dispatcher = NC3_dispatch_table;
   else
      return  NC_ENOTNC;

  havetable:
   stat = dispatcher->create(path,cmode,initialsz,basepe,chunksizehintp,
			     useparallel,mpi_info,dispatcher,&ncp);
   if(stat == NC_NOERR) {
      ncp->dispatch = dispatcher;
      if(ncidp) *ncidp = ncp->ext_ncid;
      ncp->path = strdup(path);
      if(path == NULL) stat = NC_ENOMEM;	
   }
   return stat;
}

int
nc_open(const char *path, int mode, int *ncidp)
{
   return NC_open(path, mode, 0, NULL, 0, NULL, ncidp);
}

int
nc__open(const char *path, int cmode,
	 size_t *chunksizehintp, int *ncidp)
{
   return NC_open(path, cmode, 0, chunksizehintp, 0, 
		  NULL, ncidp);
}

int
nc__open_mp(const char *path, int cmode, int basepe, 
	    size_t *chunksizehintp, int *ncidp)
{
   return NC_open(path, cmode, basepe, chunksizehintp,
		  0, NULL, ncidp);
}

/*
  For create, we have the following pieces of information
  to use to determine the dispatch table.
  1. table specified by override
  2. path
  3. cmode
  4. the contents of the file (if it exists);
  basically checking its magic number
*/

int
NC_open(const char *path, int cmode,
	int basepe, size_t *chunksizehintp,
        int useparallel, void* mpi_info,
        int *ncidp)
{
   int stat = NC_NOERR;
   NC* ncp = NULL;
   NC_Dispatch* dispatcher = NULL;
   /* Need two pieces of information for now */
   int model = 0;
   int isurl = 0; 
   int cdfversion = 0;
   int hdfversion = 0;
   extern int default_create_format;

   if(!nc_initialized)
   {stat = NC_initialize(); if(stat) return stat; nc_initialized = 1;}

   if((isurl = NC_testurl(path)))
      model = NC_urlmodel(path);

   if(isurl == 0) {
      /* Look at the file if it exists */
      stat = NC_check_file_type(path,useparallel,mpi_info,&cdfversion,&hdfversion);
      if(stat == NC_NOERR) {
	 if(hdfversion != 0) {
	    model = NC_DISPATCH_NC4;
	 } else if(cdfversion != 0) {
	    model = NC_DISPATCH_NC3;
	 }
      } 
      /* else ignore the file */
   }

   /* Look to the incoming cmode for hints */
   if(model == 0) {
      if(cmode & NC_NETCDF4 || cmode & NC_PNETCDF) model = NC_DISPATCH_NC4;
   }

   if(model == 0) model = NC_DISPATCH_NC3; /* final default */

   /* Force flag consistentcy */
   if(model & NC_DISPATCH_NC4)
      cmode |= NC_NETCDF4;
   else if(model & NC_DISPATCH_NC3) {
      cmode &= ~NC_NETCDF4; /* must be netcdf-3 */
      if(cdfversion == 2) cmode |= NC_64BIT_OFFSET;
   }

   if((cmode & NC_MPIIO && cmode & NC_MPIPOSIX))
      return  NC_EINVAL;

   /* override overrides any other table choice */
   dispatcher = NC_get_dispatch_override();
   if(dispatcher != NULL) goto havetable;

   /* Figure out what dispatcher to use */
#if  defined(USE_CDMREMOTE)
   if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCR))
	dispatcher = NCCR_dispatch_table;
   else
#endif
#if defined(USE_NETCDF4) && defined(USE_DAP)
   if(model == (NC_DISPATCH_NC4 | NC_DISPATCH_NCD))
	dispatcher = NCD4_dispatch_table;
   else
#endif
#if defined(USE_DAP)
   if(model == (NC_DISPATCH_NC3 | NC_DISPATCH_NCD))
	dispatcher = NCD3_dispatch_table;
   else
#endif
#if defined(USE_NETCDF4)
   if(model == (NC_DISPATCH_NC4))
	dispatcher = NC4_dispatch_table;
   else
#endif
   if(model == (NC_DISPATCH_NC3))
	dispatcher = NC3_dispatch_table;
   else
      return  NC_ENOTNC;

  havetable:
   stat = dispatcher->open(path, cmode, basepe, chunksizehintp,
			   useparallel, mpi_info, dispatcher, &ncp);
   if(stat == NC_NOERR) {
      ncp->dispatch = dispatcher;
      if(ncidp) *ncidp = ncp->ext_ncid;
      ncp->path = strdup(path);
      if(path == NULL) stat = NC_ENOMEM;	
   }
   return stat;
}

/* This function returns the file pathname (or the opendap URL) which
 * was used to open/create the ncid's file. */
int 
nc_inq_path(int ncid, size_t *pathlen, char *path)
{
   NC* ncp;
   int stat = NC_NOERR;
   if ((stat = NC_check_id(ncid, &ncp)))
      return stat;
   if(ncp->path == NULL) {
	if(pathlen) *pathlen = 0;
	if(path) path[0] = '\0';
   } else {
       if (pathlen) *pathlen = strlen(ncp->path);
       if (path) strcpy(path, ncp->path);
   }
   return stat;
}

int
nc_redef(int ncid)
{
   NC* ncp;
   int stat = NC_check_id(ncid, &ncp);
   if(stat != NC_NOERR) return stat;
   return ncp->dispatch->redef(ncid);
}

int
nc__enddef(int ncid, size_t h_minfree, size_t v_align, size_t v_minfree, size_t r_align)
{
   NC* ncp;
   int stat = NC_check_id(ncid, &ncp);
   if(stat != NC_NOERR) return stat;
   return ncp->dispatch->_enddef(ncid,h_minfree,v_align,v_minfree,r_align);
}

int
nc_enddef(int ncid)
{
   int status;
   NC *ncp;
   status = NC_check_id(ncid, &ncp); 
   if(status != NC_NOERR) return status;
   return ncp->dispatch->_enddef(ncid,0,1,0,1);
}

int
nc_sync(int ncid)
{
   NC* ncp;
   int stat = NC_check_id(ncid, &ncp);
   if(stat != NC_NOERR) return stat;
   return ncp->dispatch->sync(ncid);
}

static void
NC_reclaim(NC* ncp)
{
   /* reclaim the path */
   if(ncp->path != NULL) free(ncp->path);
   ncp->path = NULL;   
}

int
nc_abort(int ncid)
{
   NC* ncp;
   int stat = NC_check_id(ncid, &ncp);
   if(stat != NC_NOERR) return stat;
   NC_reclaim(ncp);
   return ncp->dispatch->abort(ncid);
}

int
nc_close(int ncid)
{
   NC* ncp;
   int stat = NC_check_id(ncid, &ncp);
   if(stat != NC_NOERR) return stat;
   return ncp->dispatch->close(ncid);
}

int
nc_set_fill(int ncid, int fillmode, int *old_modep)
{
   NC* ncp;
   int stat = NC_check_id(ncid, &ncp);
   if(stat != NC_NOERR) return stat;
   return ncp->dispatch->set_fill(ncid,fillmode,old_modep);
}

int
nc_inq_base_pe(int ncid, int *pe)
{
   NC* ncp;
   int stat = NC_check_id(ncid, &ncp);
   if(stat != NC_NOERR) return stat;
   return ncp->dispatch->inq_base_pe(ncid,pe);
}

int
nc_set_base_pe(int ncid, int pe)
{
   NC* ncp;
   int stat = NC_check_id(ncid, &ncp);
   if(stat != NC_NOERR) return stat;
   return ncp->dispatch->set_base_pe(ncid,pe);
}


int
nc_inq_format(int ncid, int *formatp)
{
   NC* ncp;
   int stat = NC_check_id(ncid, &ncp);
   if(stat != NC_NOERR) return stat;
   return ncp->dispatch->inq_format(ncid,formatp);
}

int
nc_inq(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *unlimdimidp)
{
   NC* ncp;
   int stat = NC_check_id(ncid, &ncp);
   if(stat != NC_NOERR) return stat;
   return ncp->dispatch->inq(ncid,ndimsp,nvarsp,nattsp,unlimdimidp);
}

int
nc_inq_type(int ncid, nc_type xtype, char *name, size_t *size)
{
   NC* ncp;
   /* For compatibility, we need to allow inq about
      atomic types, even if ncid is ill-defined */
   if(xtype <= ATOMICTYPEMAX) {
      if(xtype <= NC_NAT) return NC_EBADTYPE;
      if(name) strncpy(name,NC_atomictypename(xtype),NC_MAX_NAME);
      if(size) *size = NC_atomictypelen(xtype);
      return NC_NOERR;
   } else {
      int stat = NC_check_id(ncid, &ncp);
      if(stat != NC_NOERR) return NC_EBADTYPE; /* compatibility */
      return ncp->dispatch->inq_type(ncid,xtype,name,size);
   }
}