File: netcdf.cc

package info (click to toggle)
python-escript 5.6-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144,304 kB
  • sloc: python: 592,074; cpp: 136,909; ansic: 18,675; javascript: 9,411; xml: 3,384; sh: 738; makefile: 207
file content (715 lines) | stat: -rw-r--r-- 14,724 bytes parent folder | download | duplicates (3)
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715

/*****************************************************************************
*
* Copyright (c) 2017-2018 by The University of Queensland
* http://www.uq.edu.au
*
* Primary Business: Queensland, Australia
* Licensed under the Apache License, version 2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Development until 2012 by Earth Systems Science Computational Center (ESSCC)
* Development 2012-2013 by School of Earth Sciences
* Development from 2014 by Centre for Geoscience Computing (GeoComp)
*
*****************************************************************************/



#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <vector>
#include <boost/lexical_cast.hpp>

/* From http://www.unidata.ucar.edu/software/netcdf/docs/file_format_specifications.html
The grammar for the header is:

header       = magic  numrecs  dim_list  gatt_list  var_list
= CDF? followed by 4 bytes(which we don't care about)
We can just search for the relevant tags:


*/

union esV
{
    unsigned char b;
    short s;
    int i;
    float f;
    double d;
};


class esncdfValues
{
public:
    char type;
    std::string svalue;
    std::vector<union esV> vec;  
};

namespace
{
    char NC_DIM_TAG=10;
    char NC_VAR_TAG=11;
    char NC_ATT_TAG=12;
    
    
    
    enum nc_type
    {
      NC_BYTE=1,
      NC_CHAR=2,
      NC_SHORT=3,
      NC_INT=4,
      NC_FLOAT=5,
      NC_DOUBLE=6
    };
    
    int offset;
    
char getChar(std::ifstream& ifs)
{
    offset++;
    return ifs.get(); 
}




typedef std::vector<std::pair<std::string, esncdfValues> >  vsu; 

}


using namespace std;
using namespace boost;








// Holds a list of name value pairs
class esncdfCollection
{
public:
    void add(const std::string& k, const esncdfValues& v)
    {
        vec.push_back(std::pair<std::string, esncdfValues>(k,v));
    }
    void add_int(const std::string& k, int i)
    {
	esncdfValues v;
	v.type=NC_INT;
	union esV es;
	es.i=i;
	v.vec.push_back(es);
        vec.push_back(std::pair<std::string, esncdfValues>(k,v));
    }
    
/*    void add(const std::string& k, const std::string& s)
    {
	esncdfValues u;
	u.type=NC_CHAR;
	u.svalue=s;
        vec.push_back(std::pair<std::string, esncdfValues>(k,u));
    }  */  
    vsu vec;
    // Here would be extra information regarding variables
    std::string name;       
    std::string type;
};


class esncdfVariables
{
public:    
    std::vector<esncdfCollection> vars;    
    void add(const esncdfCollection& c)
    {
        vars.push_back(c);
    }
};


class esncdfHeader
{
public:    
    esncdfCollection dimensions;
    esncdfCollection attributes;     // global attributes
    esncdfVariables variables;       // including specific attributes
};


bool read_list(std::ifstream& ifs, char t, bool offset32, esncdfCollection& coll);


bool gobble(ifstream& ifs, int s)
{
    for (int i=0;i<s;++i)
    {
        getChar(ifs);
    }
    return ifs.good();
}


bool read_int(std::ifstream& ifs, int& res)
{
    char buff32[4];
    for (int i=0;i<4;++i)
    {
        buff32[i]=getChar(ifs);
    }
    if (!ifs)
    {
        return false;
    }
    res=0;
    for (int i=0;i<4;++i)
    {
        res=(res << 8);
        res+=(unsigned char)buff32[i];
    }
    return true;
}

bool read_item_header(std::ifstream& ifs, std::string& iname)
{
    // format for name is 32 bit length followed by chars (padded to 4byte boundary)
    int len=0;
    if (!read_int(ifs,len))
    {
        return false;
    }
    for (int i=0;i<len;++i)
    {
        char c=getChar(ifs);
        if (c!=0)       // if anyone puts \x00 in their string
        {               // ...
            iname+=c;
        }
    }
    // now we need to skip padding
    if (len%4!=0)
    {
	for (int i=0;i<4-len%4;++i)
	{
	    char c=getChar(ifs);
	}
    }
    if (!ifs)
    {
        return false;
    }
    return true;    
}

bool read_item_dim(std::ifstream& ifs, std::string& iname, esncdfCollection& dims)
{
    if (!read_item_header(ifs, iname))
    {
        return false;
    }
    int len=0;
    if (!read_int(ifs, len))
    {
        return false;
    }
    dims.add_int(iname, len);
    return true;
}

bool read_tag(std::ifstream& ifs, char& t)
{
    for (int i=0;i<3;++i)
    {
        t=getChar(ifs); 
	if (t!=0)
	{
	    return false;
	}
    }
    t=getChar(ifs);
    return ifs.good();  
}

esncdfValues get_string_value(char tag, int len, char* bytes)
{
    esncdfValues res;
    res.type=tag;
    switch (tag)
    {
        case NC_CHAR:  res.svalue=std::string(bytes); break;
        case NC_BYTE:
	{
	    union esV e;
            for (int i=0;i<len;++i)
            {
		e.b=bytes[i];
		res.vec.push_back(e);
            }
            break;
	}
        case NC_SHORT:
        {
	    union esV e;
	    for (int i=0;i<len;++i)
	    {
		short v=(bytes[0] << 8)+bytes[1];
	        e.s=v;
		res.vec.push_back(e);
	    }
            break;
        }
        case NC_INT:
        {
	    union esV e;
	    for (int i=0;i<len;++i)
	    {
		int v=(bytes[0] << 24) +(bytes[1] << 16) + (bytes[2] << 8) +bytes[3];	      
	        e.i=v;
		res.vec.push_back(e);
	    }
            break;
        }
        case NC_FLOAT:      // here we assume the correct floating point std
        {
	    union esV e;
	    float f=-0.0;
	    if (reinterpret_cast<unsigned char*>(&f)[0]!=0)	// big endian (yes we assume IEEE floating point
	    {
		for (int i=0;i<len;++i)
		{
		    float f=0;		// assumes floats are 32bit!
		    unsigned char* v=reinterpret_cast<unsigned char*>(&f);
		    for (int j=0;j<4;++j)
		    {
			v[j]=bytes[4*i+j];
		    }
		    e.f=f;
		    res.vec.push_back(e);
		}
	    }
	    else
	    {
		for (int i=0;i<len;++i)
		{
		    float f=0;		// assumes floats are 32bit!
		    unsigned char* v=reinterpret_cast<unsigned char*>(&f);
		    for (int j=0;j<4;++j)
		    {
			v[j]=bytes[4*i+3-j];
		    }
		    e.f=f;
		    res.vec.push_back(e);
		}
	    }
	    break;
        }
        case NC_DOUBLE:
        {
	    union esV e;
	    double f=-0.0;
	    if (reinterpret_cast<unsigned char*>(&f)[0]!=0)	// big endian (yes we assume IEEE floating point
	    {
		for (int i=0;i<len;++i)
		{
		    double f=0;
		    unsigned char* v=reinterpret_cast<unsigned char*>(&f);
		    for (int j=0;j<8;++j)
		    {
			v[j]=bytes[8*i+j];
		    }
		    e.d=f;
		    res.vec.push_back(e);
		}
	    }
	    else
	    {
		for (int i=0;i<len;++i)
		{
		    double f=0;
		    unsigned char* v=reinterpret_cast<unsigned char*>(&f);
		    for (int j=0;j<8;++j)
		    {
			v[j]=bytes[8*i+7-j];
		    }
		    e.d=f;
		    res.vec.push_back(e);
		}
	    }	  
	    break;	  
        }
        
        
        default:
	    res.type=NC_CHAR;
	    res.svalue="?????";
    }    
    return res;  
}

// structure for an attribute is:
//  name  nc_type  nelems  [values ...]
bool read_item_attr(std::ifstream& ifs, std::string& iname, esncdfCollection& coll)
{
    if (!read_item_header(ifs, iname))
    {
        return false;
    }    
    int len=0;
    char tag;
    if (!read_tag(ifs, tag))
    {
        return false;
    }
    if ((tag<NC_BYTE) || (tag>NC_DOUBLE))	// invalid type
    {
        return false;
    }
    if (!read_int(ifs, len))
    {
        return false;
    }
    // for now I'm just going to skip over the values
    // to do that, I need to know how big each type is
    int size=0;
    switch (tag)
    {
        case NC_SHORT: size=2; break;
        case NC_INT: size=4; break;
        case NC_FLOAT: size=4; break;
        case NC_DOUBLE: size=8; break;
        default:
        size=1;
    }
    
    int skip=len*size;
    // now we need to skip padding
    if (len*size%4!=0)
    {
        skip+=4-len*size%4;
    }
    char buff[skip+1];
    buff[skip]=0;
    for (int i=0;i<skip;++i)
    {
        buff[i]=getChar(ifs);
    }
    if (!ifs.good())
    {
        return false;
    }
    esncdfValues vs=get_string_value(tag, len, buff);
    coll.add(iname, vs);    
    return true;
}





// structure for a variable is:
// name  nelems  [dimid ...]  vatt_list  nc_type  vsize  begin
bool read_item_var(std::ifstream& ifs, std::string& iname, bool offset32, esncdfVariables& vars)
{
    if (!read_item_header(ifs, iname))
    {
        return false;
    }    
    int dim=0;
    if (!read_int(ifs, dim))
    {
        return false;
    }
    for (int i=0;i<dim;++i)	// just disgarding these at the moment
    {
      int v;
      if (!read_int(ifs, v))
      {
        return false;
      } 
    }
    esncdfCollection current;
    current.name=iname;
    if (!read_list(ifs, NC_ATT_TAG, offset32, current))
    {
        return false;
    }
    // now determine the type
    char tag;
    if (!read_tag(ifs, tag))
    {
        return false;
    }
    if ((tag<NC_BYTE) || (tag>NC_DOUBLE))	// invalid type
    {
        return false;
    }
    switch(tag)
    {
    case NC_CHAR: current.type="char"; break;
    case NC_BYTE: current.type="byte";break;
    case NC_SHORT: current.type="short";break;
    case NC_INT: current.type="int";break;
    case NC_FLOAT: current.type="float";break;
    case NC_DOUBLE: current.type="double";break;
    default:
        current.type="????";
    }
    int len;
    if (!read_int(ifs, len))
    {
        return false;
    }
    // now we need to get the offset of the variable in the file
    // which we will promptly discard
    int gobcount=offset32?4:8;
    if (!gobble(ifs, gobcount))
    {
        return false;
    }
    vars.add(current);
    return true;
}

// lists<T> are defined as: ABSENT | NC_DIMENSION  nelems  [T ...]
// where T in {dim, attr, var}
bool read_list_vars(std::ifstream& ifs, bool offset32, esncdfVariables& coll)
{
    char b;
    // we expect either 8 zeros indicating no list or the expected tag followed
    // by list information
    for (int i=0;i<3;++i)
    {
        
        if (!ifs || ((b=getChar(ifs))!=0))
        {
            return false;
        }
    }
    b=getChar(ifs);
    if (!ifs || (b!=0 && b!=NC_VAR_TAG)) // didn't get tag we expected
    {
        return false;
    }
    if (b==0)   // expecting 4 more zeros
    {
        for (int i=0;i<4;++i)
        {
            b=getChar(ifs);
            if (!ifs || b!=0)
            {
                return false;
            }
        }
        return true;		// This list is absent
    }
    // we must have seen the correct tag
    // first thing will be number of items
    int count=0;
    if (!read_int(ifs, count))
    {
        return false;
    }
    for (int i=0;i<count;++i)
    {
        std::string itemname;
        if (!read_item_var(ifs, itemname, offset32, coll))
        {
            return false;
        }
    }
    return true;
}




// lists<T> are defined as: ABSENT | NC_DIMENSION  nelems  [T ...]
// where T in {dim, attr, var}
bool read_list(std::ifstream& ifs, char t, bool offset32, esncdfCollection& coll)
{
    char b;
    // we expect either 8 zeros indicating no list or the expected tag followed
    // by list information
    for (int i=0;i<3;++i)
    {
        
        if (!ifs || ((b=getChar(ifs))!=0))
        {
            return false;
        }
    }
    b=getChar(ifs);
    if (!ifs || (b!=0 && b!=t)) // didn't get tag we expected
    {
        return false;
    }
    if (b==0)   // expecting 4 more zeros
    {
        for (int i=0;i<4;++i)
        {
            b=getChar(ifs);
            if (!ifs || b!=0)
            {
                return false;
            }
        }
        return true;		// This list is absent
    }
    // we must have seen the correct tag
    // first thing will be number of items
    int count=0;
    if (!read_int(ifs, count))
    {
        return false;
    }
    if (t==NC_DIM_TAG) 
    {
        for (int i=0;i<count;++i)
        {
            std::string itemname;
            if (!read_item_dim(ifs, itemname, coll))
            {
                return false;
            }
        }
    }
    else if (t==NC_ATT_TAG)
    {
        for (int i=0;i<count;++i)
        {
            std::string itemname;
            if (!read_item_attr(ifs, itemname, coll))
            {
                return false;
            }
        }
    }
    else
    {
        return false;
    }
    return true;
}

bool read_header(const char* fname, esncdfHeader& header)
{
    ifstream ifs(fname);
    if (!ifs)
    {
        return false;
    }
    gobble(ifs,3);	// skip over "CDF"
    bool offset32=(getChar(ifs)==1);
    gobble(ifs, 4);
    if (!ifs)
    {
        return false;
    }
    if (!read_list(ifs, NC_DIM_TAG, offset32, header.dimensions))        
    {
        return false;
    }
    cout << "Dimensions:\n";
    cout << "Attributes:\n";    
    if (!read_list(ifs, NC_ATT_TAG, offset32, header.attributes))        
    {
        return false;
    }

    cout << "Variables:\n";    
    if (!read_list_vars(ifs, offset32, header.variables)) 
    {
        return false;
    }   
    return true;
}

void dump_Values(std::ostream& os, const esncdfValues& u)
{
    if (u.type==NC_CHAR)
    {
        os << u.svalue;
    }
    else
    {
        for (int i=0;i<u.vec.size();++i)
	{
	    switch (u.type)
	    {
	    case NC_BYTE: 
	    {
		unsigned char x=u.vec[i].b;
	        os << hex << (unsigned short)(x) << dec << " "; break;
	    }
	    case NC_SHORT: os << u.vec[i].s << " "; break;
	    case NC_INT: os << u.vec[i].i << " "; break;
	    case NC_FLOAT: os << u.vec[i].f << " "; break;
	    case NC_DOUBLE: os << u.vec[i].d << " "; break;
	    default:
	      os << "????";
	    }
	}
    }
  
}

void print_header(const esncdfHeader& h)
{
    cout << "Dimensions:\n";
    for (auto it=h.dimensions.vec.begin();it!=h.dimensions.vec.end();++it)
    {
       const esncdfValues& v=it->second;
       cout << it->first << " = ";
       dump_Values(cout, v);
       cout << endl;
    }
    cout << "Attributes:\n";
    for (auto it=h.attributes.vec.begin();it!=h.attributes.vec.end();++it)
    {
       const esncdfValues& v=it->second;
       cout << it->first << " = ";
       dump_Values(cout, v);
       cout << endl;
    }
    cout << "Variables:\n";
    for (auto it=h.variables.vars.begin();it!=h.variables.vars.end();++it)
    {
       cout << it->type << " " << it->name << ":\n";
       for (auto jt=it->vec.begin();jt!=it->vec.end();++jt)
       {
	  cout << "  " << jt->first << " = ";
	  const esncdfValues& v=jt->second;
	  dump_Values(cout, v);
	  cout << endl;
       }
    }
}

int main(int argc, char** argv)
{
    offset=-1;
    if (argc==1)
    {
        std::cerr << "Usage: " << argv[0] << " file.nc\n";
        return 1;
    }
    esncdfHeader header;
    if (!read_header(argv[1], header))
    {
	std::cerr << "Failed\n";
    }
    cout << "-------------\n";
    print_header(header);
    return 0;

}