-
Notifications
You must be signed in to change notification settings - Fork 68
/
CDMDSP.java
1159 lines (1063 loc) · 39.3 KB
/
CDMDSP.java
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2012-2018 University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/
package dap4.cdm.dsp;
import dap4.cdm.CDMTypeFcns;
import dap4.cdm.CDMUtil;
import dap4.cdm.NodeMap;
import dap4.core.data.DataCursor;
import dap4.core.dmr.*;
import dap4.core.util.Convert;
import dap4.core.util.DapContext;
import dap4.core.util.DapException;
import dap4.core.util.DapUtil;
import dap4.dap4lib.AbstractDSP;
import ucar.ma2.Array;
import ucar.ma2.DataType;
import ucar.ma2.IndexIterator;
import ucar.nc2.*;
import ucar.nc2.constants.CDM;
import ucar.nc2.dataset.*;
import ucar.nc2.util.CancelTask;
import java.io.IOException;
import java.util.*;
/**
* Wrap CDM source (NetcdfDataset) to be a DSP
* This basically means wrapping various (CDM)Array
* object to look like DataVariable objects.
* Currently only used on server side
*/
public class CDMDSP extends AbstractDSP
{
//////////////////////////////////////////////////
// Constants
static protected final boolean DEBUG = false;
static protected final boolean DUMPCDL = false;
// NetcdfDataset enhancement to use: need only coord systems
static protected Set<NetcdfDataset.Enhance> ENHANCEMENT = EnumSet.of(NetcdfDataset.Enhance.CoordSystems);
static final protected String FILLVALUE = "_FillValue";
//////////////////////////////////////////////////
// Class variables
static protected boolean nc4loaded = false;
//////////////////////////////////////////////////
// Class methods
//////////////////////////////////////////////////
// Instance variables
protected NetcdfDataset ncdfile = null;
protected boolean closed = false;
protected DMRFactory dmrfactory = null;
// Keep various context-dependent maps between
// CDMNode instances and DapNode instances
// package access
// Variable map
protected NodeMap<Variable, DapVariable> varmap = new NodeMap<>();
// Variable <-> DapStructure (basetype) map; compound types only
protected NodeMap<Variable, DapStructure> compoundmap = new NodeMap<>();
// Map Variable <-> DapSequence vlen type
protected NodeMap<Variable, DapSequence> vlenmap = new NodeMap<>();
// Map All other kinds of CDMNode <-> DapNode
protected NodeMap<CDMNode, DapNode> nodemap = new NodeMap<>();
//////////////////////////////////////////////////
// Constructor(s)
public CDMDSP()
{
}
public CDMDSP(String path)
throws DapException
{
super();
setLocation(path);
}
//////////////////////////////////////////////////
// DSP Interface
// This is intended to be the last DSP checked
@Override
public boolean dspMatch(String path, DapContext context)
{
return true;
}
/**
* @param filepath - absolute path to a file
* @return CDMDSP instance
* @throws DapException
*/
@Override
public CDMDSP
open(String filepath)
throws DapException
{
try {
NetcdfFile ncfile = createNetcdfFile(filepath, null);
NetcdfDataset ncd = new NetcdfDataset(ncfile, ENHANCEMENT);
return open(ncd);
} catch (IOException ioe) {
throw new DapException("CDMDSP: cannot process: " + filepath, ioe);
}
}
/**
* Provide an extra API for use in testing
*
* @param ncd
* @return
* @throws DapException
*/
public CDMDSP open(NetcdfDataset ncd)
throws DapException
{
assert this.context != null;
this.dmrfactory = new DMRFactory();
this.ncdfile = ncd;
setLocation(this.ncdfile.getLocation());
buildDMR();
return this;
}
@Override
public void close()
throws IOException
{
if (this.ncdfile != null)
this.ncdfile.close();
}
@Override
public DataCursor
getVariableData(DapVariable var)
throws DapException
{
Variable cdmvar = this.varmap.get(var);
if (cdmvar == null)
throw new DapException("Unknown variable: " + var);
CDMCursor vardata = (CDMCursor) super.getVariableData(var);
if (vardata == null) {
DataCursor.Scheme scheme = CDMCursor.schemeFor(var);
try {
vardata = new CDMCursor(scheme, this, var, null);
vardata.setArray(cdmvar.read());
} catch (IOException e) {
throw new DapException(e);
}
super.addVariableData(var, vardata);
}
return vardata;
}
//////////////////////////////////////////////////
// Abstract DSP Abstract methods
//////////////////////////////////////////////////
// CDMDSP Specific Accessors
public NetcdfDataset getNetcdfDataset()
{
return this.ncdfile;
}
//////////////////////////////////////////////////
// Nodemap Management
/* Make sure that we use the proper
* object in order to avoid identity
* problems.
*/
/**
* Track generic CDMNode <-> DapNode
*/
protected void
recordNode(CDMNode cdm, DapNode dap)
{
assert this.nodemap.get(cdm) == null && this.nodemap.get(dap) == null;
this.nodemap.put(cdm, dap);
}
/**
* Track Variable <-> DapVariable
*/
protected void
recordVar(Variable cdm, DapVariable dap)
{
cdm = CDMUtil.unwrap(cdm);
assert varmap.get(cdm) == null && varmap.get(dap) == null;
varmap.put(cdm, dap);
}
/**
* Track Variable <-> DapStructure
*/
protected void
recordStruct(Variable cdm, DapStructure dap)
{
cdm = CDMUtil.unwrap(cdm);
assert this.nodemap.get(cdm) == null && this.nodemap.get(dap) == null;
compoundmap.put(cdm, dap);
}
/**
* Track Variable <-> DapSequence
*/
protected void
recordSeq(Variable cdm, DapSequence dap)
{
cdm = CDMUtil.unwrap(cdm);
assert this.vlenmap.get(cdm) == null && this.vlenmap.get(dap) == null;
vlenmap.put(cdm, dap);
}
/**
* Lookup a cdmnode in the cdmnodemap.
* Make sure that we use the proper
* object in order to avoid identity
* problems.
*
* @param cdmnode The CMDNode (variable, dimension, etc) to record
* @return The DapNode to which the cdmnode is to be mapped or null.
*/
/*
protected DapNode
lookupNode(CDMNode cdmnode)
{
CDMSort sort = cdmnode.getSort();
if(sort == CDMSort.VARIABLE || sort == CDMSort.STRUCTURE) {
Variable basev = CDMUtil.unwrap((Variable) cdmnode);
assert (basev != null) : "Unwrap() failed";
cdmnode = (CDMNode) basev;
}
return this.nodemap.get(cdmnode);
}
*/
//////////////////////////////////////////////////
/**
* Extract the metadata from the NetcdfDataset
* and build the DMR.
*/
public void
buildDMR()
throws DapException
{
if (getDMR() != null)
return;
try {
if (DUMPCDL) {
System.out.println("writecdl:");
this.ncdfile.writeCDL(System.out, false);
System.out.flush();
}
// Use the file path to define the dataset name
String name = this.ncdfile.getLocation();
// Normalize the name
name = DapUtil.canonicalpath(name);
// Remove any path prefix
int index = name.lastIndexOf('/');
if (index >= 0)
name = name.substring(index + 1, name.length());
// Initialize the root dataset node
setDMR((DapDataset) dmrfactory.newDataset(name).annotate(NetcdfDataset.class, this.ncdfile));
// Map the CDM root group to this group
recordNode(this.ncdfile.getRootGroup(), getDMR());
getDMR().setBase(DapUtil.canonicalpath(this.ncdfile.getLocation()));
// Now recursively build the tree. Start by
// Filling the dataset with the contents of the ncfile
// root group.
fillgroup(getDMR(), this.ncdfile.getRootGroup());
// Add an order index to the tree
getDMR().sort();
// Now locate the coordinate variables for maps
/* Walk looking for VariableDS instances */
processmappedvariables(this.ncdfile.getRootGroup());
// Now set the view
getDMR().finish();
} catch (DapException e) {
setDMR(null);
throw new DapException(e);
}
}
//////////////////////////////////////////////////
// Actions
protected void
fillgroup(DapGroup dapgroup, Group cdmgroup)
throws DapException
{
// Create decls in dap group for Dimensions
for (Dimension cdmdim : cdmgroup.getDimensions()) {
DapDimension dapdim = builddim(cdmdim);
}
// Create decls in dap group for Enumerations
for (EnumTypedef cdmenum : cdmgroup.getEnumTypedefs()) {
String name = cdmenum.getShortName();
DapEnumeration dapenum = buildenum(cdmenum);
dapenum.setShortName(name);
dapgroup.addDecl(dapenum);
}
// Create decls in dap group for vlen induced Sequences
// Do this before building compound types
for (Variable cdmvar0 : cdmgroup.getVariables()) {
Variable cdmvar = CDMUtil.unwrap(cdmvar0);
buildseqtypes(cdmvar);
}
// Create decls in dap group for Compound Types
for (Variable cdmvar0 : cdmgroup.getVariables()) {
Variable cdmvar = CDMUtil.unwrap(cdmvar0);
if (cdmvar.getDataType() != DataType.STRUCTURE
&& cdmvar.getDataType() != DataType.SEQUENCE)
continue;
DapStructure struct = buildcompoundtype(cdmvar, dapgroup);
}
// Create decls in dap group for Variables
for (Variable cdmvar0 : cdmgroup.getVariables()) {
Variable cdmvar = CDMUtil.unwrap(cdmvar0);
DapNode newvar = buildvariable(cdmvar, dapgroup, cdmvar.getDimensions());
}
// Create decls in dap group for subgroups
for (Group subgroup : cdmgroup.getGroups()) {
DapGroup newgroup = buildgroup(subgroup);
dapgroup.addDecl(newgroup);
}
// Create decls in dap group for group-level attributes
buildattributes(dapgroup, cdmgroup.getAttributes());
}
//////////////////////////////////////////////////
// Declaration Builders
protected DapDimension
builddim(Dimension cdmdim)
throws DapException
{
if (cdmdim.isVariableLength())
throw new DapException("* dimensions not supported");
DapDimension dapdim = null;
long cdmsize = dapsize(cdmdim);
String name = cdmdim.getShortName();
if (name != null && name.length() == 0)
name = null;
boolean shared = cdmdim.isShared();
if (!shared) {
// Unlike the parser, since we are working
// from a NetcdfDataset instance, there might
// be multiple anonymous dimension objects
// the same size. So, just go ahead and create
// multiple instances.
dapdim = (DapDimension) dmrfactory.newDimension(null, cdmsize);
getDMR().addDecl(dapdim);
} else { // Non anonymous; create in current group
dapdim = (DapDimension) dmrfactory.newDimension(name, cdmsize);
dapdim.setShared(true);
if (cdmdim.isUnlimited()) {
dapdim.setUnlimited(true);
}
Group cdmparent = cdmdim.getGroup();
DapGroup dapparent = (DapGroup) this.nodemap.get(cdmparent);
assert dapparent != null;
assert (dapparent != null);
dapparent.addDecl(dapdim);
}
recordNode(cdmdim, dapdim);
return dapdim;
}
protected DapEnumeration
buildenum(EnumTypedef cdmenum)
throws DapException
{
// Set the enum's basetype
DapType base = null;
switch (cdmenum.getBaseType()) {
case ENUM1:
base = DapType.INT8;
break;
case ENUM2:
base = DapType.INT16;
break;
case ENUM4:
default:
base = DapType.INT32;
break;
}
DapEnumeration dapenum = (DapEnumeration) dmrfactory.newEnumeration(cdmenum.getShortName(), base);
recordNode(cdmenum, dapenum);
// Create the enum constants
Map<Integer, String> ecvalues = cdmenum.getMap();
for (Map.Entry<Integer, String> entry : ecvalues.entrySet()) {
String name = entry.getValue();
assert (name != null);
int value = (int) entry.getKey();
dapenum.addEnumConst(dmrfactory.newEnumConst(name, new Long(value)));
}
return dapenum;
}
protected DapStructure
buildcompoundtype(Variable cdmvar, DapNode parent)
throws DapException
{
cdmvar = CDMUtil.unwrap(cdmvar);
DapStructure struct;
if (cdmvar.getDataType() == DataType.STRUCTURE)
struct = (DapStructure) dmrfactory.newStructure(cdmvar.getShortName());
else if (cdmvar.getDataType() == DataType.SEQUENCE)
struct = (DapStructure) dmrfactory.newSequence(cdmvar.getShortName());
else
throw new DapException("Internal error");
struct.setParent(parent);
recordStruct(cdmvar, struct);
Structure cdmstruct = (Structure) cdmvar;
List<Variable> fields = cdmstruct.getVariables();
// recurse to create compound types of any fields c
for (CDMNode node : fields) {
Variable cdmfield = (Variable) node;
List<Dimension> dimset = cdmfield.getDimensions();
// Recurse on any nested compound types
if (cdmfield.getDataType() == DataType.STRUCTURE
|| cdmfield.getDataType() == DataType.SEQUENCE) {
DapStructure fieldstruct = buildcompoundtype(cdmfield, struct);
}
}
// Add the fields to this compound type
for (CDMNode node : fields) {
Variable cdmfield = (Variable) node;
DapType basetype = null;
switch (cdmfield.getDataType()) {
default:
basetype = CDMTypeFcns.cdmtype2daptype(cdmfield.getDataType());
break;
case STRUCTURE:
case SEQUENCE:
basetype = compoundmap.get(cdmfield);// since no forward references
break;
}
// build the field variable
List<Dimension> fielddims = cdmfield.getDimensions();
DapVariable dapfield;
if (CDMUtil.hasVLEN(cdmfield)) {
// Get the fake sequence associated with this vlen
DapSequence seq = vlenmap.get(cdmfield);
List<Dimension> coredims = getCoreDimset(fielddims);
// We need to build a variable whose basetype is the
// fake sequence rather than the cdmfield type
// First, build a variable using the existing cdmfield
dapfield = buildvariable(cdmfield, struct, coredims);
// Now modify it to use the fake sequence
dapfield.setBaseType(seq);
} else {
dapfield = buildvariable(cdmfield, struct, fielddims);
}
struct.addField(dapfield);
}
return struct;
}
protected DapVariable
buildvariable(Variable cdmbasevar, DapNode parent, List<Dimension> cdmdims)
throws DapException
{
DapVariable dapvar = null;
CDMSort sort = cdmbasevar.getSort();
switch (sort) {
case VARIABLE:
switch (cdmbasevar.getDataType()) {
default:
dapvar = buildatomicvar(cdmbasevar, parent);
break;
case ENUM1:
case ENUM2:
case ENUM4:
dapvar = buildenumvar(cdmbasevar);
break;
case OPAQUE:
dapvar = buildopaquevar(cdmbasevar);
break;
case STRING:
dapvar = buildstringvar(cdmbasevar);
break;
case STRUCTURE:
case SEQUENCE:
assert false : "Internal error"; // How could this ever happen?
break;
}
builddimrefs(dapvar, cdmdims);
break;
case STRUCTURE:
dapvar = buildstructvar(cdmbasevar);
builddimrefs(dapvar, cdmdims);
break;
case SEQUENCE:
default:
assert false : "Internal Error";
}
if (parent != null)
addToParent(parent, dapvar);
return dapvar;
}
protected DapVariable
buildatomicvar(Variable cdmvar, DapNode parent)
throws DapException
{
// Atomic => not opaque and not enum
DapType basetype = CDMTypeFcns.cdmtype2daptype(cdmvar.getDataType());
if (basetype == null)
throw new DapException("DapFile: illegal CDM variable base type: " + cdmvar.getDataType());
DapVariable dapvar = (DapVariable) dmrfactory.newVariable(cdmvar.getShortName(), basetype);
recordVar(cdmvar, dapvar);
buildattributes(dapvar, cdmvar.getAttributes());
if (CDMUtil.hasVLEN(cdmvar)) {
// Get the fake sequence associated with this vlen
DapSequence seq = vlenmap.get(cdmvar);
List<Dimension> coredims = getCoreDimset(cdmvar.getDimensions());
// We need to build a variable whose basetype is the
// fake sequence rather than the cdmfield type
// First, build a variable using the existing cdmfield
// Now modify it to use the fake sequence
dapvar.setBaseType(seq);
}
return dapvar;
}
protected DapVariable
buildopaquevar(Variable cdmvar)
throws DapException
{
assert (cdmvar.getDataType() == DataType.OPAQUE) : "Internal error";
DapVariable dapvar = (DapVariable) dmrfactory.newVariable(cdmvar.getShortName(), DapType.OPAQUE);
recordVar(cdmvar, dapvar);
buildattributes(dapvar, cdmvar.getAttributes());
Object osize = cdmvar.annotation(UCARTAGOPAQUE);
if (osize != null) {
dapvar.addXMLAttribute(UCARTAGOPAQUE, osize.toString());
}
return dapvar;
}
protected DapVariable
buildstringvar(Variable cdmvar)
throws DapException
{
assert (cdmvar.getDataType() == DataType.STRING) : "Internal error";
DapVariable dapvar = (DapVariable) dmrfactory.newVariable(cdmvar.getShortName(), DapType.STRING);
recordVar(cdmvar, dapvar);
buildattributes(dapvar, cdmvar.getAttributes());
return dapvar;
}
protected DapVariable
buildenumvar(Variable cdmvar)
throws DapException
{
assert (
cdmvar.getDataType() == DataType.ENUM1
|| cdmvar.getDataType() == DataType.ENUM2
|| cdmvar.getDataType() == DataType.ENUM4
) : "Internal error";
// Now, we need to locate the actual enumeration decl
EnumTypedef enumdef = cdmvar.getEnumTypedef();
EnumTypedef trueenumdef = findMatchingEnum(enumdef);
// Modify the cdmvar
cdmvar.setEnumTypedef(trueenumdef);
// Now, map to a DapEnumeration
DapEnumeration dapenum = (DapEnumeration) this.nodemap.get(trueenumdef);
assert (dapenum != null);
DapVariable dapvar = (DapVariable) dmrfactory.newVariable(cdmvar.getShortName(), dapenum);
recordVar(cdmvar, dapvar);
buildattributes(dapvar, cdmvar.getAttributes());
return dapvar;
}
protected DapVariable
buildstructvar(Variable cdmvar)
throws DapException
{
assert (cdmvar.getDataType() == DataType.STRUCTURE) : "Internal error";
// Find the DapStructure that is the basetype for this var
DapStructure struct = compoundmap.get(cdmvar);
assert struct != null : "Internal Error";
DapVariable dapvar = (DapVariable) dmrfactory.newVariable(cdmvar.getShortName(), struct);
recordVar(cdmvar, dapvar);
buildattributes(dapvar, cdmvar.getAttributes());
return dapvar;
}
/* Create a sequence from a variable with a
variable length last dimension.
Suppose we have cdm equivalent to this:
T var[d1]...[dn]][*]
We convert to the following
<Sequence name="var">
<T name="var"/>
<Dim name=d1/>
...
<Dim name=dn/>
</Sequence>
*/
protected DapSequence
buildseqtype(Variable cdmvar)
throws DapException
{
cdmvar = CDMUtil.unwrap(cdmvar);
assert (CDMUtil.hasVLEN(cdmvar));
DataType dt = cdmvar.getDataType();
DapType daptype = CDMTypeFcns.cdmtype2daptype(dt);
DapSequence seq = (DapSequence) dmrfactory.newSequence(cdmvar.getShortName());
// fill DapSequence with a single field; note that the dimensions
// are elided because they will attach to the sequence variable,
// not the field
DapVariable field = dmrfactory.newVariable(cdmvar.getShortName(), daptype);
seq.addField(field);
field.setParent(seq);
recordSeq(cdmvar, seq);
return seq;
}
/**
* Walk this variable, including fields, to construct sequence types
* for any contained vlen dimensions
*
* @param cdmvar variable to walk
*/
protected void
buildseqtypes(Variable cdmvar)
throws DapException
{
if (CDMUtil.hasVLEN(cdmvar)) {
buildseqtype(cdmvar);
}
if (cdmvar.getDataType() == DataType.STRUCTURE
|| cdmvar.getDataType() == DataType.SEQUENCE) {
Structure struct = (Structure) cdmvar;
List<Variable> fields = struct.getVariables();
for (int i = 0; i < fields.size(); i++) {
Variable field = fields.get(i);
buildseqtypes(field); // recurse for inner vlen dims
}
}
}
protected void
buildattributes(DapNode node, List<Attribute> attributes)
throws DapException
{
for (Attribute attr : attributes) {
if (!suppress(attr.getShortName())) {
DapAttribute dapattr = buildattribute(attr);
node.addAttribute(dapattr);
}
}
}
protected DapAttribute
buildattribute(Attribute cdmattr)
throws DapException
{
DapType attrtype = CDMTypeFcns.cdmtype2daptype(cdmattr.getDataType());
EnumTypedef cdmenum = cdmattr.getEnumType();
boolean enumfillvalue = (cdmattr.getShortName().equals(FILLVALUE)
&& cdmenum != null);
DapEnumeration dapenum = null;
// We need to handle _FillValue specially if the
// the variable is enum typed.
if (enumfillvalue) {
cdmenum = findMatchingEnum(cdmenum);
// Make sure the cdm attribute has type enumx
if (!cdmenum.getBaseType().isEnum())
throw new DapException("CDM _FillValue attribute type is not enumX");
// Modify the attr
cdmattr.setEnumType(cdmenum);
// Now, map to a DapEnumeration
dapenum = (DapEnumeration) this.nodemap.get(cdmenum);
if (dapenum == null)
throw new DapException("Illegal CDM variable attribute type: " + cdmenum);
attrtype = dapenum;
}
if (attrtype == null)
throw new DapException("DapFile: illegal CDM variable attribute type: " + cdmattr.getDataType());
DapAttribute dapattr = (DapAttribute) dmrfactory.newAttribute(cdmattr.getShortName(), attrtype);
recordNode(cdmattr, dapattr);
// Transfer the values
Array values = cdmattr.getValues();
if (!validatecdmtype(cdmattr.getDataType(), values.getElementType()))
throw new DapException("Attr type versus attribute data mismatch: " + values.getElementType());
IndexIterator iter = values.getIndexIterator();
String[] valuelist = null;
Object vec = CDMTypeFcns.createVector(cdmattr.getDataType(), values.getSize());
for (int i = 0; iter.hasNext(); i++) {
java.lang.reflect.Array.set(vec, i, iter.next());
}
valuelist = (String[]) Convert.convert(DapType.STRING, attrtype, vec);
dapattr.setValues(valuelist);
return dapattr;
}
/**
* Assign dimensions to a variable
*
* @param dapvar The variable to which we wish to assign dimensions
* @param cdmdims The cdm dimensions from which we will find the dimension info
*/
protected void
builddimrefs(DapVariable dapvar, List<Dimension> cdmdims)
throws DapException
{
if (cdmdims == null || cdmdims.size() == 0)
return;
// It is unfortunately the case that the dimensions
// associated with the variable are not
// necessarily the same object as those dimensions
// as declared, so we need to use a non-trivial
// matching algorithm.
for (Dimension cdmdim : cdmdims) {
DapDimension dapdim = null;
if (cdmdim.isShared()) {
Dimension declareddim = finddimdecl(cdmdim);
if (declareddim == null)
throw new DapException("Unprocessed cdm dimension: " + cdmdim);
dapdim = (DapDimension) this.nodemap.get(declareddim);
assert dapdim != null;
} else if (cdmdim.isVariableLength()) {// ignore
continue;
} else {//anonymous
dapdim = builddim(cdmdim);
}
assert (dapdim != null) : "Internal error";
dapvar.addDimension(dapdim);
}
}
protected void
processmappedvariables(Group g)
throws DapException
{
for (Variable v0 : g.getVariables()) {
Variable cdmvar = CDMUtil.unwrap(v0);
if (cdmvar == null)
throw new DapException("NetcdfDataset synthetic variable: " + v0);
DapNode dapvar = this.varmap.get(cdmvar);
if (dapvar == null)
throw new DapException("Unknown variable: " + cdmvar);
if (!(dapvar instanceof DapVariable))
throw new DapException("CDMVariable not mapping to dap variable: " + cdmvar);
buildmaps((DapVariable) dapvar, v0);
}
}
/**
* @param dapvar The variable to which we wish to assign maps
* @param var The NetcdfDataset variable from which to extract coord system
*/
protected void
buildmaps(DapVariable dapvar, Variable var)
throws DapException
{
// See if this cdm variable has one (or more) coordinate system
List<CoordinateSystem> css = null;
if (var.getSort() == CDMSort.VARIABLE) {
VariableDS vds = (VariableDS) var;
css = vds.getCoordinateSystems();
} else {
StructureDS sds = (StructureDS) var;
css = sds.getCoordinateSystems();
}
if (css != null && css.size() > 0) {
// Not sure what to do with multiple coordinate systems
// For now, only use the first
CoordinateSystem coordsystems = css.get(0);
for (CoordinateAxis axis : coordsystems.getCoordinateAxes()) {
// First step is to find the dap variable
// corresponding to the map
VariableDS vds = (VariableDS) axis.getOriginalVariable();
if (vds != null) {
Variable v = CDMUtil.unwrap(vds);
if (v != null) {
DapVariable mapvar = varmap.get(v);
if (mapvar == null)
throw new DapException("Illegal map variable:" + v.toString());
if (!mapvar.isAtomic())
throw new DapException("Non-atomic map variable:" + v.toString());
// Ignore maps where the map variable is inside this scope
/*
if(!mapvar.isTopLevel()) {
DapNode parent = mapvar.getContainer();
switch (parent.getSort()) {
case SEQUENCE:
case STRUCTURE:
if(dapvar.getBaseType() == parent) // Do we need to do transitive closure?
throw new DapException("Map var cannot be in same structure as map");
break;
default:
assert false : "Unexpected container type";
}
*/
DapMap map = (DapMap) dmrfactory.newMap(mapvar);
dapvar.addMap(map);
}
}
}
}
}
protected DapGroup
buildgroup(Group cdmgroup)
throws DapException
{
DapGroup dapgroup = (DapGroup) dmrfactory.newGroup(cdmgroup.getShortName());
recordNode(cdmgroup, dapgroup);
dapgroup.setShortName(cdmgroup.getShortName());
fillgroup(dapgroup, cdmgroup);
return dapgroup;
}
//////////////////////////////////////////////////
// Utilities
/**
* Unfortunately, the CDM Iosp does not
* actually use the declared enums. Rather,
* for every enum type'd variable, a new
* enum decl is defined. So, we need
* to find the original enum decl that matches
* the variable's enum.
*/
protected EnumTypedef
findMatchingEnum(EnumTypedef varenum)
throws DapException
{
List<EnumTypedef> candidates = new ArrayList<>();
for (Map.Entry<DapNode, CDMNode> entry : this.nodemap.getCDMMap().entrySet()) {
CDMNode cdmnode = entry.getValue();
if (cdmnode.getSort() != CDMSort.ENUMERATION)
continue;
// Compare the enumeration (note names will differ)
EnumTypedef target = (EnumTypedef) cdmnode;
/* Ideally, we should test the types of the enums,
but, unfortunately, the var enum is always enum4.
if(target.getBaseType() != varenum.getBaseType())
continue;
*/
Map<Integer, String> targetmap = target.getMap();
Map<Integer, String> varmap = varenum.getMap();
if (targetmap.size() != varmap.size())
continue;
boolean match = true; // until otherwise shown
for (Map.Entry<Integer, String> tpair : targetmap.entrySet()) {
String tname = tpair.getValue();
int value = (int) tpair.getKey();
boolean found = false;
for (Map.Entry<Integer, String> vpair : varmap.entrySet()) {
if (tname.equals(vpair.getValue()) && value == (int) vpair.getKey()) {
found = true;
break;
}
}
if (!found) {
match = false;
break;
}
}
if (!match)
continue;
// Save it unless it is shadowed by a closer enum
boolean shadowed = false;
for (EnumTypedef etd : candidates) {
if (shadows(etd.getGroup(), target.getGroup())) {
shadowed = true;
break;
}
}
if (!shadowed)
candidates.add(target);
}
switch (candidates.size()) {
case 0:
throw new DapException("CDMDSP: No matching enum type decl: " + varenum.getShortName());
case 1:
break;
default:
throw new DapException("CDMDSP: Multiple matching enum type decls: " + varenum.getShortName());
}
return candidates.get(0);
}
protected boolean
shadows(Group parent, Group child)
{
if (child == parent)
return true;
Group candidate = child;
do {
candidate = candidate.getGroup();
} while (candidate != null && candidate != parent);
return (candidate == parent);
}
// Convert cdm size to DapDimension size
protected long
dapsize(Dimension cdmdim)
{
assert (!cdmdim.isVariableLength());
return (long) cdmdim.getLength();
}
protected boolean
validatecdmtype(DataType datatype, Class typeclass)
{
switch (datatype) {
case CHAR:
return typeclass == char.class;
case BYTE:
case UBYTE:
return typeclass == byte.class;
case SHORT:
case USHORT:
return typeclass == short.class;
case INT:
case UINT:
return typeclass == int.class;
case LONG:
case ULONG:
return typeclass == long.class;
case FLOAT:
return typeclass == float.class;
case DOUBLE:
return typeclass == double.class;
case STRING:
return typeclass == String.class;
case OPAQUE:
return typeclass == Byte[].class;
// For these, return the integer basetype
case ENUM1:
return typeclass == byte.class;
case ENUM2:
return typeclass == short.class;
case ENUM4:
return typeclass == int.class;
// Undefined
case SEQUENCE:
case STRUCTURE:
default:
break;
}
return false;
}
protected Dimension
finddimdecl(Dimension dimref)
{
// Search on the full name, but be careful,
// the rule is that the declared dimension's fqn
// must be a prefix of the dimension reference.
for (Map.Entry<DapNode, CDMNode> entry : this.nodemap.getCDMMap().entrySet()) {
if (entry.getValue().getSort() != CDMSort.DIMENSION)