forked from Unidata/netcdf-c
/
ncd2dispatch.c
2800 lines (2464 loc) · 79.4 KB
/
ncd2dispatch.c
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 1993, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*********************************************************************/
#include "dapincludes.h"
#include "ncd2dispatch.h"
#include "ncoffsets.h"
#include "netcdf_filter.h"
#ifdef _MSC_VER
#include <crtdbg.h>
#endif
#define NCRCFILE "NCRCFILE"
#ifdef HAVE_GETRLIMIT
# ifdef HAVE_SYS_RESOURCE_H
# include <sys/time.h>
# endif
# ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
# endif
#endif
/* Define the set of protocols known to be constrainable */
static char* constrainableprotocols[] = {"http", "https",NULL};
static int ncd2initialized = 0;
/* Forward */
static NCerror buildncstructures(NCDAPCOMMON*);
static NCerror builddims(NCDAPCOMMON*);
static char* getdefinename(CDFnode* node);
static NCerror buildvars(NCDAPCOMMON*);
static NCerror buildglobalattrs(NCDAPCOMMON*, CDFnode* root);
static NCerror buildattribute(NCDAPCOMMON*, NCattribute*, nc_type, int);
static void computedimindexanon(CDFnode* dim, CDFnode* var);
static void replacedims(NClist* dims);
static int equivalentdim(CDFnode* basedim, CDFnode* dupdim);
static NCerror addstringdims(NCDAPCOMMON*);
static NCerror defrecorddim(NCDAPCOMMON*);
static NCerror defseqdims(NCDAPCOMMON*);
static NCerror showprojection(NCDAPCOMMON*, CDFnode* var);
static NCerror getseqdimsize(NCDAPCOMMON*, CDFnode* seq, size_t* sizep);
static NCerror makeseqdim(NCDAPCOMMON*, CDFnode* seq, size_t count, CDFnode** sqdimp);
static NCerror countsequence(NCDAPCOMMON*, CDFnode* xseq, size_t* sizep);
static NCerror freeNCDAPCOMMON(NCDAPCOMMON*);
static NCerror fetchpatternmetadata(NCDAPCOMMON*);
static size_t fieldindex(CDFnode* parent, CDFnode* child);
static NCerror computeseqcountconstraints(NCDAPCOMMON*, CDFnode*, NCbytes*);
static void computeseqcountconstraintsr(NCDAPCOMMON*, CDFnode*, CDFnode**);
static void estimatevarsizes(NCDAPCOMMON*);
static NCerror fetchconstrainedmetadata(NCDAPCOMMON*);
static NCerror suppressunusablevars(NCDAPCOMMON*);
static NCerror fixzerodims(NCDAPCOMMON*);
static void applyclientparamcontrols(NCDAPCOMMON*);
static NCerror applyclientparams(NCDAPCOMMON*);
/**************************************************/
static int
NCD2_create(const char *path, int cmode,
size_t initialsz, int basepe, size_t *chunksizehintp,
int use_parallel, void* mpidata,
NC_Dispatch*,NC* ncp);
static int NCD2_redef(int ncid);
static int NCD2__enddef(int ncid, size_t h_minfree, size_t v_align, size_t v_minfree, size_t r_align);
static int NCD2_sync(int ncid);
static int NCD2_abort(int ncid);
static int NCD2_put_vara(int ncid, int varid,
const size_t *start, const size_t *edges0,
const void *value0,
nc_type memtype);
static int NCD2_get_vara(int ncid, int varid,
const size_t *start, const size_t *edges,
void *value,
nc_type memtype);
static int NCD2_put_vars(int ncid, int varid,
const size_t *start, const size_t *edges, const ptrdiff_t* stride,
const void *value0, nc_type memtype);
static int NCD2_get_vars(int ncid, int varid,
const size_t *start, const size_t *edges, const ptrdiff_t* stride,
void *value, nc_type memtype);
static NC_Dispatch NCD2_dispatch_base = {
NC_FORMATX_DAP2,
NCD2_create,
NCD2_open,
NCD2_redef,
NCD2__enddef,
NCD2_sync,
NCD2_abort,
NCD2_close,
NCD2_set_fill,
NCD2_inq_base_pe,
NCD2_set_base_pe,
NCD2_inq_format,
NCD2_inq_format_extended, /*inq_format_extended*/
NCD2_inq,
NCD2_inq_type,
NCD2_def_dim,
NCD2_inq_dimid,
NCD2_inq_dim,
NCD2_inq_unlimdim,
NCD2_rename_dim,
NCD2_inq_att,
NCD2_inq_attid,
NCD2_inq_attname,
NCD2_rename_att,
NCD2_del_att,
NCD2_get_att,
NCD2_put_att,
NCD2_def_var,
NCD2_inq_varid,
NCD2_rename_var,
NCD2_get_vara,
NCD2_put_vara,
NCD2_get_vars,
NCD2_put_vars,
NCDEFAULT_get_varm,
NCDEFAULT_put_varm,
NCD2_inq_var_all,
NCD2_var_par_access,
#ifdef USE_NETCDF4
NCD2_show_metadata,
NCD2_inq_unlimdims,
NCD2_inq_ncid,
NCD2_inq_grps,
NCD2_inq_grpname,
NCD2_inq_grpname_full,
NCD2_inq_grp_parent,
NCD2_inq_grp_full_ncid,
NCD2_inq_varids,
NCD2_inq_dimids,
NCD2_inq_typeids,
NCD2_inq_type_equal,
NCD2_def_grp,
NCD2_rename_grp,
NCD2_inq_user_type,
NCD2_inq_typeid,
NCD2_def_compound,
NCD2_insert_compound,
NCD2_insert_array_compound,
NCD2_inq_compound_field,
NCD2_inq_compound_fieldindex,
NCD2_def_vlen,
NCD2_put_vlen_element,
NCD2_get_vlen_element,
NCD2_def_enum,
NCD2_insert_enum,
NCD2_inq_enum_member,
NCD2_inq_enum_ident,
NCD2_def_opaque,
NCD2_def_var_deflate,
NCD2_def_var_fletcher32,
NCD2_def_var_chunking,
NCD2_def_var_fill,
NCD2_def_var_endian,
NCD2_def_var_filter,
NCD2_set_var_chunk_cache,
NCD2_get_var_chunk_cache,
#endif /*USE_NETCDF4*/
};
NC_Dispatch* NCD2_dispatch_table = NULL; /* moved here from ddispatch.c */
static NC_Dispatch NCD2_dispatcher;
int
NCD2_initialize(void)
{
NCD2_dispatch_table = &NCD2_dispatch_base;
ncd2initialized = 1;
#ifdef DEBUG
/* force logging to go to stderr */
nclogclose();
if(nclogopen(NULL))
ncsetlogging(1); /* turn it on */
#endif
/* Look at env vars for rc file location */
if(getenv(NCRCFILE) != NULL) {
const char* ncrcfile = getenv(NCRCFILE);
if(oc_set_rcfile(ncrcfile) != OC_NOERR)
return NC_EAUTH;
}
return NC_NOERR;
}
int
NCD2_finalize(void)
{
return NC_NOERR;
}
static int
NCD2_redef(int ncid)
{
return (NC_EPERM);
}
static int
NCD2__enddef(int ncid, size_t h_minfree, size_t v_align, size_t v_minfree, size_t r_align)
{
return (NC_EPERM);
}
static int
NCD2_sync(int ncid)
{
return (NC_EINVAL);
}
static int
NCD2_abort(int ncid)
{
return NCD2_close(ncid);
}
static int
NCD2_create(const char *path, int cmode,
size_t initialsz, int basepe, size_t *chunksizehintp,
int use_parallel, void* mpidata,
NC_Dispatch* dispatch, NC* ncp)
{
return NC_EPERM;
}
static int
NCD2_put_vara(int ncid, int varid,
const size_t *start, const size_t *edges,
const void *value,
nc_type memtype)
{
return NC_EPERM;
}
static int
NCD2_get_vara(int ncid, int varid,
const size_t *start, const size_t *edges,
void *value,
nc_type memtype)
{
int stat = nc3d_getvarx(ncid, varid, start, edges, nc_ptrdiffvector1, value,memtype);
return stat;
}
static int
NCD2_put_vars(int ncid, int varid,
const size_t *start, const size_t *edges, const ptrdiff_t* stride,
const void *value0, nc_type memtype)
{
return NC_EPERM;
}
static int
NCD2_get_vars(int ncid, int varid,
const size_t *start, const size_t *edges, const ptrdiff_t* stride,
void *value, nc_type memtype)
{
int stat = nc3d_getvarx(ncid, varid, start, edges, stride, value, memtype);
return stat;
}
/* See ncd2dispatch.c for other version */
int
NCD2_open(const char * path, int mode,
int basepe, size_t *chunksizehintp,
int useparallel, void* mpidata,
NC_Dispatch* dispatch, NC* drno)
{
NCerror ncstat = NC_NOERR;
OCerror ocstat = OC_NOERR;
NCDAPCOMMON* dapcomm = NULL;
const char* value;
int nc3id = -1;
if(path == NULL)
return NC_EDAPURL;
if(dispatch == NULL) PANIC("NCD3_open: no dispatch table");
/* Setup our NC and NCDAPCOMMON state*/
dapcomm = (NCDAPCOMMON*)calloc(1,sizeof(NCDAPCOMMON));
if(dapcomm == NULL) {ncstat = NC_ENOMEM; goto done;}
NCD2_DATA_SET(drno,dapcomm);
drno->int_ncid = nc__pseudofd(); /* create a unique id */
dapcomm->controller = (NC*)drno;
dapcomm->cdf.separator = ".";
dapcomm->cdf.smallsizelimit = DFALTSMALLLIMIT;
dapcomm->cdf.cache = createnccache();
#ifdef HAVE_GETRLIMIT
{ struct rlimit rl;
if(getrlimit(RLIMIT_NOFILE, &rl) >= 0) {
dapcomm->cdf.cache->cachecount = (size_t)(rl.rlim_cur / 2);
}
}
#endif
#ifdef OCCOMPILEBYDEFAULT
/* set the compile flag by default */
dapcomm->oc.rawurltext = (char*)emalloc(strlen(path)+strlen("[compile]")+1);
strcpy(dapcomm->oc.rawurltext,"[compile]");
strcat(dapcomm->oc.rawurltext, path);
#else
dapcomm->oc.rawurltext = strdup(path);
#endif
if(ncuriparse(dapcomm->oc.rawurltext,&dapcomm->oc.url) != NCU_OK)
{ncstat = NC_EURL; goto done;}
if(!constrainable(dapcomm->oc.url))
SETFLAG(dapcomm->controls,NCF_UNCONSTRAINABLE);
#ifdef COLUMBIA_HACK
{
const char* p;
/* Does this url look like it is from columbia? */
if(dapcomm->oc.url->host != NULL) {
for(p=dapcomm->oc.url->host;*p;p++) {
if(strncmp(p,COLUMBIA_HACK,strlen(COLUMBIA_HACK))==0)
SETFLAG(dapcomm->controls,NCF_COLUMBIA);
}
}
}
#endif
/* fail if we are unconstrainable but have constraints */
if(FLAGSET(dapcomm->controls,NCF_UNCONSTRAINABLE)) {
if(dapcomm->oc.url->query != NULL) {
nclog(NCLOGWARN,"Attempt to constrain an unconstrainable data source: %s",
dapcomm->oc.url->query);
ncstat = THROW(NC_EDAPCONSTRAINT);
goto done;
}
}
/* Use libsrc code (netcdf-3) for storing metadata */
{
char tmpname[32];
/* Create fake file name: exact name must be unique,
but is otherwise irrelevant because we are using NC_DISKLESS
*/
snprintf(tmpname,sizeof(tmpname),"%d",drno->int_ncid);
/* Now, use the file to create the hidden, in-memory netcdf file.
We want this hidden file to always be NC_CLASSIC, so we need to
force default format temporarily in case user changed it.
*/
{
int new = 0; /* format netcdf-3 */
int old = 0;
nc_set_default_format(new,&old); /* save and change */
ncstat = nc_create(tmpname,NC_DISKLESS|NC_CLASSIC_MODEL,&nc3id);
nc_set_default_format(old,&new); /* restore */
}
if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}
dapcomm->nc3id = nc3id;
/* Avoid fill */
nc_set_fill(nc3id,NC_NOFILL,NULL);
}
dapcomm->oc.dapconstraint = (DCEconstraint*)dcecreate(CES_CONSTRAINT);
dapcomm->oc.dapconstraint->projections = nclistnew();
dapcomm->oc.dapconstraint->selections = nclistnew();
/* Parse constraints to make sure they are syntactically correct */
ncstat = dapparsedapconstraints(dapcomm,dapcomm->oc.url->query,dapcomm->oc.dapconstraint);
if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}
/* Construct a url for oc minus any constraint and params*/
dapcomm->oc.urltext = ncuribuild(dapcomm->oc.url,NULL,NULL,NCURIBASE);
/* Pass to OC */
ocstat = oc_open(dapcomm->oc.urltext,&dapcomm->oc.conn);
if(ocstat != OC_NOERR) {THROWCHK(ocstat); goto done;}
#ifdef DEBUG1
(void)oc_trace_curl(dapcomm->oc.conn);
#endif
nullfree(dapcomm->oc.urltext); /* clean up */
dapcomm->oc.urltext = NULL;
/* process control client parameters */
applyclientparamcontrols(dapcomm);
/* Turn on logging; only do this after oc_open*/
if((value = dapparamvalue(dapcomm,"log")) != NULL) {
ncloginit();
if(nclogopen(value))
ncsetlogging(1);
ncloginit();
if(nclogopen(value))
ncsetlogging(1);
}
/* fetch and build the unconstrained DDS for use as
pattern */
ncstat = fetchpatternmetadata(dapcomm);
if(ncstat != NC_NOERR) goto done;
/* Operations on the pattern tree */
/* Accumulate useful nodes sets */
ncstat = computecdfnodesets(dapcomm,dapcomm->cdf.fullddsroot->tree);
if(ncstat) {THROWCHK(ncstat); goto done;}
/* Define the dimsettrans list */
ncstat = definedimsettrans(dapcomm,dapcomm->cdf.fullddsroot->tree);
if(ncstat) {THROWCHK(ncstat); goto done;}
/* Mark the nodes of the pattern that are eligible for prefetch */
ncstat = markprefetch(dapcomm);
/* fetch and build the constrained DDS */
ncstat = fetchconstrainedmetadata(dapcomm);
if(ncstat != NC_NOERR) goto done;
#ifdef DEBUG2
fprintf(stderr,"constrained dds: %s\n",dumptree(dapcomm->cdf.ddsroot));
#endif
/* Operations on the constrained tree */
/* Accumulate useful nodes sets */
ncstat = computecdfnodesets(dapcomm,dapcomm->cdf.ddsroot->tree);
if(ncstat) {THROWCHK(ncstat); goto done;}
/* Fix grids */
ncstat = fixgrids(dapcomm);
if(ncstat) {THROWCHK(ncstat); goto done;}
/* Locate and mark usable sequences */
ncstat = sequencecheck(dapcomm);
if(ncstat) {THROWCHK(ncstat); goto done;}
/* suppress variables not in usable sequences */
ncstat = suppressunusablevars(dapcomm);
if(ncstat) {THROWCHK(ncstat); goto done;}
/* apply client parameters */
ncstat = applyclientparams(dapcomm);
if(ncstat) {THROWCHK(ncstat); goto done;}
/* Add (as needed) string dimensions*/
ncstat = addstringdims(dapcomm);
if(ncstat) {THROWCHK(ncstat); goto done;}
if(nclistlength(dapcomm->cdf.ddsroot->tree->seqnodes) > 0) {
/* Build the sequence related dimensions */
ncstat = defseqdims(dapcomm);
if(ncstat) {THROWCHK(ncstat); goto done;}
}
/* Define the dimsetplus and dimsetall lists */
ncstat = definedimsets(dapcomm,dapcomm->cdf.ddsroot->tree);
if(ncstat) {THROWCHK(ncstat); goto done;}
/* Re-compute the dimension names*/
ncstat = computecdfdimnames(dapcomm);
if(ncstat) {THROWCHK(ncstat); goto done;}
/* Deal with zero size dimensions */
ncstat = fixzerodims(dapcomm);
if(ncstat) {THROWCHK(ncstat); goto done;}
/* Attempt to use the DODS_EXTRA info to turn
one of the dimensions into unlimited.
Assume computecdfdimnames34 has already been called.
*/
ncstat = defrecorddim(dapcomm);
if(ncstat) {THROWCHK(ncstat); goto done;}
if(dapcomm->cdf.recorddimname != NULL
&& nclistlength(dapcomm->cdf.ddsroot->tree->seqnodes) > 0) {
/*nclog(NCLOGWARN,"unlimited dimension specified, but sequences exist in DDS");*/
PANIC("unlimited dimension specified, but sequences exist in DDS");
}
/* Re-compute the var names*/
ncstat = computecdfvarnames(dapcomm,
dapcomm->cdf.ddsroot,
dapcomm->cdf.ddsroot->tree->varnodes);
if(ncstat) {THROWCHK(ncstat); goto done;}
/* Transfer data from the unconstrained DDS data to the unconstrained DDS */
ncstat = dimimprint(dapcomm);
if(ncstat) goto done;
/* Process the constraints to map to the constrained CDF tree */
/* (must follow fixgrids3) */
ncstat = dapmapconstraints(dapcomm->oc.dapconstraint,dapcomm->cdf.ddsroot);
if(ncstat != NC_NOERR) goto done;
/* Canonicalize the constraint */
ncstat = dapfixprojections(dapcomm->oc.dapconstraint->projections);
if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}
/* Fill in segment information */
ncstat = dapqualifyconstraints(dapcomm->oc.dapconstraint);
if(ncstat != NC_NOERR) goto done;
/* Accumulate set of variables in the constraint's projections */
ncstat = dapcomputeprojectedvars(dapcomm,dapcomm->oc.dapconstraint);
if(ncstat) {THROWCHK(ncstat); goto done;}
/* using the modified constraint, rebuild the constraint string */
if(FLAGSET(dapcomm->controls,NCF_UNCONSTRAINABLE)) {
/* ignore all constraints */
dapcomm->oc.urltext = ncuribuild(dapcomm->oc.url,NULL,NULL,NCURIBASE);
} else {
char* constraintstring = dcebuildconstraintstring(dapcomm->oc.dapconstraint);
ncurisetquery(dapcomm->oc.url,constraintstring);
nullfree(constraintstring);
dapcomm->oc.urltext = ncuribuild(dapcomm->oc.url,NULL,NULL,NCURISVC);
}
#ifdef DEBUG
fprintf(stderr,"ncdap3: final constraint: %s\n",dapcomm->oc.url->query);
#endif
/* Estimate the variable sizes */
estimatevarsizes(dapcomm);
/* Build the meta data */
ncstat = buildncstructures(dapcomm);
if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}
/* Explicitly do not call enddef because it will complain
about variables that are too large.
*/
#if 0
ncstat = nc_endef(nc3id,NC_NOFILL,NULL);
if(ncstat != NC_NOERR && ncstat != NC_EVARSIZE)
{THROWCHK(ncstat); goto done;}
#endif
{ /* (for now) break abstractions*/
NC* ncsub;
NC3_INFO* nc3i;
CDFnode* unlimited = dapcomm->cdf.recorddim;
/* get the dispatch data for the substrate */
ncstat = NC_check_id(nc3id,&ncsub);
if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}
nc3i = (NC3_INFO*)ncsub->dispatchdata;
/* This must be checked after all dds and data processing
so we can figure out the value of numrecs.
*/
if(unlimited != NULL) { /* Set the effective size of UNLIMITED */
NC_set_numrecs(nc3i,unlimited->dim.declsize);
}
/* Pretend the substrate is read-only */
NC_set_readonly(nc3i);
}
/* Do any necessary data prefetch */
if(FLAGSET(dapcomm->controls,NCF_PREFETCH)
&& FLAGSET(dapcomm->controls,NCF_PREFETCH_EAGER)) {
ncstat = prefetchdata(dapcomm);
if(ncstat != NC_NOERR) {
del_from_NCList((NC*)drno); /* undefine here */
{THROWCHK(ncstat); goto done;}
}
}
return ncstat;
done:
if(drno != NULL) NCD2_close(drno->ext_ncid);
if(ocstat != OC_NOERR) ncstat = ocerrtoncerr(ocstat);
return THROW(ncstat);
}
int
NCD2_close(int ncid)
{
NC* drno;
NCDAPCOMMON* dapcomm;
int ncstatus = NC_NOERR;
ncstatus = NC_check_id(ncid, (NC**)&drno);
if(ncstatus != NC_NOERR) return THROW(ncstatus);
dapcomm = (NCDAPCOMMON*)drno->dispatchdata;
/* We call abort rather than close to avoid
trying to write anything or try to pad file length
*/
ncstatus = nc_abort(getnc3id(drno));
/* clean NC* */
freeNCDAPCOMMON(dapcomm);
return THROW(ncstatus);
}
/**************************************************/
static NCerror
buildncstructures(NCDAPCOMMON* dapcomm)
{
NCerror ncstat = NC_NOERR;
CDFnode* dds = dapcomm->cdf.ddsroot;
ncstat = buildglobalattrs(dapcomm,dds);
if(ncstat != NC_NOERR) goto done;
ncstat = builddims(dapcomm);
if(ncstat != NC_NOERR) goto done;
ncstat = buildvars(dapcomm);
if(ncstat != NC_NOERR) goto done;
done:
return THROW(ncstat);
}
static NCerror
builddims(NCDAPCOMMON* dapcomm)
{
int i;
NCerror ncstat = NC_NOERR;
int dimid;
NClist* dimset = NULL;
NC* drno = dapcomm->controller;
NC* ncsub;
char* definename;
/* collect all dimensions from variables */
dimset = dapcomm->cdf.ddsroot->tree->dimnodes;
/* Sort by fullname just for the fun of it */
for(;;) {
int last = nclistlength(dimset) - 1;
int swap = 0;
for(i=0;i<last;i++) {
CDFnode* dim1 = (CDFnode*)nclistget(dimset,i);
CDFnode* dim2 = (CDFnode*)nclistget(dimset,i+1);
if(strcmp(dim1->ncfullname,dim2->ncfullname) > 0) {
nclistset(dimset,i,(void*)dim2);
nclistset(dimset,i+1,(void*)dim1);
swap = 1;
break;
}
}
if(!swap) break;
}
/* Define unlimited only if needed */
if(dapcomm->cdf.recorddim != NULL) {
CDFnode* unlimited = dapcomm->cdf.recorddim;
definename = getdefinename(unlimited);
ncstat = nc_def_dim(dapcomm->nc3id,
definename,
NC_UNLIMITED,
&unlimited->ncid);
nullfree(definename);
if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}
/* get the id for the substrate */
ncstat = NC_check_id(dapcomm->nc3id,&ncsub);
if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}
#if 0
nc3sub = (NC3_INFO*)&ncsub->dispatchdata;
/* Set the effective size of UNLIMITED;
note that this cannot easily be done thru the normal API.*/
NC_set_numrecs(nc3sub,unlimited->dim.declsize);
#endif
}
for(i=0;i<nclistlength(dimset);i++) {
CDFnode* dim = (CDFnode*)nclistget(dimset,i);
if(dim->dim.basedim != NULL) continue; /* handle below */
if(DIMFLAG(dim,CDFDIMRECORD)) continue; /* defined above */
#ifdef DEBUG1
fprintf(stderr,"define: dim: %s=%ld\n",dim->ncfullname,(long)dim->dim.declsize);
#endif
definename = getdefinename(dim);
ncstat = nc_def_dim(dapcomm->nc3id,definename,dim->dim.declsize,&dimid);
if(ncstat != NC_NOERR) {
THROWCHK(ncstat); nullfree(definename); goto done;
}
nullfree(definename);
dim->ncid = dimid;
}
/* Make all duplicate dims have same dimid as basedim*/
/* (see computecdfdimnames)*/
for(i=0;i<nclistlength(dimset);i++) {
CDFnode* dim = (CDFnode*)nclistget(dimset,i);
if(dim->dim.basedim != NULL) {
dim->ncid = dim->dim.basedim->ncid;
}
}
done:
nclistfree(dimset);
return THROW(ncstat);
}
/* Simultaneously build any associated attributes*/
/* and any necessary pseudo-dimensions for string types*/
static NCerror
buildvars(NCDAPCOMMON* dapcomm)
{
int i,j;
NCerror ncstat = NC_NOERR;
int varid;
NClist* varnodes = dapcomm->cdf.ddsroot->tree->varnodes;
NC* drno = dapcomm->controller;
char* definename;
ASSERT((varnodes != NULL));
for(i=0;i<nclistlength(varnodes);i++) {
CDFnode* var = (CDFnode*)nclistget(varnodes,i);
int dimids[NC_MAX_VAR_DIMS];
unsigned int ncrank;
NClist* vardims = NULL;
if(var->invisible) continue;
if(var->array.basevar != NULL) continue;
#ifdef DEBUG1
fprintf(stderr,"buildvars.candidate=|%s|\n",var->ncfullname);
#endif
vardims = var->array.dimsetall;
ncrank = nclistlength(vardims);
if(ncrank > 0) {
for(j=0;j<ncrank;j++) {
CDFnode* dim = (CDFnode*)nclistget(vardims,j);
dimids[j] = dim->ncid;
}
}
definename = getdefinename(var);
#ifdef DEBUG1
fprintf(stderr,"define: var: %s/%s",
definename,var->ocname);
if(ncrank > 0) {
int k;
for(k=0;k<ncrank;k++) {
CDFnode* dim = (CDFnode*)nclistget(vardims,k);
fprintf(stderr,"[%ld]",dim->dim.declsize);
}
}
fprintf(stderr,"\n");
#endif
ncstat = nc_def_var(dapcomm->nc3id,
definename,
var->externaltype,
ncrank,
(ncrank==0?NULL:dimids),
&varid);
nullfree(definename);
if(ncstat != NC_NOERR) {
THROWCHK(ncstat);
goto done;
}
var->ncid = varid;
if(var->attributes != NULL) {
for(j=0;j<nclistlength(var->attributes);j++) {
NCattribute* att = (NCattribute*)nclistget(var->attributes,j);
ncstat = buildattribute(dapcomm,att,var->etype,varid);
if(ncstat != NC_NOERR) goto done;
}
}
/* Tag the variable with its DAP path */
if(dapparamcheck(dapcomm,"show","projection"))
showprojection(dapcomm,var);
}
done:
return THROW(ncstat);
}
static NCerror
buildglobalattrs(NCDAPCOMMON* dapcomm, CDFnode* root)
{
int i;
NCerror ncstat = NC_NOERR;
const char* txt;
char *nltxt, *p;
NCbytes* buf = NULL;
NClist* cdfnodes;
NC* drno = dapcomm->controller;
if(root->attributes != NULL) {
for(i=0;i<nclistlength(root->attributes);i++) {
NCattribute* att = (NCattribute*)nclistget(root->attributes,i);
ncstat = buildattribute(dapcomm,att,NC_NAT,NC_GLOBAL);
if(ncstat != NC_NOERR) goto done;
}
}
/* Add global attribute identifying the sequence dimensions */
if(dapparamcheck(dapcomm,"show","seqdims")) {
buf = ncbytesnew();
cdfnodes = dapcomm->cdf.ddsroot->tree->nodes;
for(i=0;i<nclistlength(cdfnodes);i++) {
CDFnode* dim = (CDFnode*)nclistget(cdfnodes,i);
if(dim->nctype != NC_Dimension) continue;
if(DIMFLAG(dim,CDFDIMSEQ)) {
char* cname = cdflegalname(dim->ocname);
if(ncbyteslength(buf) > 0) ncbytescat(buf,", ");
ncbytescat(buf,cname);
nullfree(cname);
}
}
if(ncbyteslength(buf) > 0) {
ncstat = nc_put_att_text(dapcomm->nc3id,NC_GLOBAL,"_sequence_dimensions",
ncbyteslength(buf),ncbytescontents(buf));
}
}
/* Define some additional system global attributes
depending on show= clientparams*/
/* Ignore failures*/
if(dapparamcheck(dapcomm,"show","translate")) {
/* Add a global attribute to show the translation */
ncstat = nc_put_att_text(dapcomm->nc3id,NC_GLOBAL,"_translate",
strlen("netcdf-3"),"netcdf-3");
}
if(dapparamcheck(dapcomm,"show","url")) {
if(dapcomm->oc.rawurltext != NULL)
ncstat = nc_put_att_text(dapcomm->nc3id,NC_GLOBAL,"_url",
strlen(dapcomm->oc.rawurltext),dapcomm->oc.rawurltext);
}
if(dapparamcheck(dapcomm,"show","dds")) {
txt = NULL;
if(dapcomm->cdf.ddsroot != NULL)
txt = oc_tree_text(dapcomm->oc.conn,dapcomm->cdf.ddsroot->ocnode);
if(txt != NULL) {
/* replace newlines with spaces*/
nltxt = nulldup(txt);
for(p=nltxt;*p;p++) {if(*p == '\n' || *p == '\r' || *p == '\t') {*p = ' ';}};
ncstat = nc_put_att_text(dapcomm->nc3id,NC_GLOBAL,"_dds",strlen(nltxt),nltxt);
nullfree(nltxt);
}
}
if(dapparamcheck(dapcomm,"show","das")) {
txt = NULL;
if(dapcomm->oc.ocdasroot != NULL)
txt = oc_tree_text(dapcomm->oc.conn,dapcomm->oc.ocdasroot);
if(txt != NULL) {
nltxt = nulldup(txt);
for(p=nltxt;*p;p++) {if(*p == '\n' || *p == '\r' || *p == '\t') {*p = ' ';}};
ncstat = nc_put_att_text(dapcomm->nc3id,NC_GLOBAL,"_das",strlen(nltxt),nltxt);
nullfree(nltxt);
}
}
done:
ncbytesfree(buf);
return THROW(ncstat);
}
static NCerror
buildattribute(NCDAPCOMMON* dapcomm, NCattribute* att, nc_type vartype, int varid)
{
int i;
NCerror ncstat = NC_NOERR;
unsigned int nvalues = nclistlength(att->values);
NC* drno = dapcomm->controller;
/* If the type of the attribute is string, then we need*/
/* to convert to a single character string by concatenation.
modified: 10/23/09 to insert newlines.
modified: 10/28/09 to interpret escapes
*/
if(att->etype == NC_STRING || att->etype == NC_URL) {
char* newstring = NULL;
size_t newlen = 0;
for(i=0;i<nvalues;i++) {
char* s = (char*)nclistget(att->values,i);
newlen += (1+strlen(s));
}
if(newlen > 0)
newstring = (char*)malloc(newlen);
MEMCHECK(newstring,NC_ENOMEM);
newstring[0] = '\0';
for(i=0;i<nvalues;i++) {
char* s = (char*)nclistget(att->values,i);
if(i > 0) strcat(newstring,"\n");
strcat(newstring,s);
}
dapexpandescapes(newstring);
if(newstring[0]=='\0')
ncstat = nc_put_att_text(dapcomm->nc3id,varid,att->name,1,newstring);
else
ncstat = nc_put_att_text(dapcomm->nc3id,varid,att->name,strlen(newstring),newstring);
free(newstring);
if(ncstat) goto done;
} else {
nc_type atype;
unsigned int typesize;
void* mem = NULL;
/* It turns out that some servers upgrade the type
of _FillValue in order to correctly preserve the
original value. However, since the type of the
underlying variable is not changes, we get a type
mismatch. So, make sure the type of the fillvalue
is the same as that of the controlling variable.
*/
if(varid != NC_GLOBAL && strcmp(att->name,"_FillValue")==0)
atype = nctypeconvert(dapcomm,vartype);
else
atype = nctypeconvert(dapcomm,att->etype);
typesize = nctypesizeof(atype);
if (nvalues > 0) {
mem = malloc(typesize * nvalues);
#ifdef _MSC_VER
_ASSERTE(_CrtCheckMemory());
#endif
}
ncstat = dapcvtattrval(atype,mem,att->values);
#ifdef _MSC_VER
_ASSERTE(_CrtCheckMemory());
#endif
if(ncstat) {nullfree(mem); goto done;}
ncstat = nc_put_att(dapcomm->nc3id,varid,att->name,atype,nvalues,mem);
#ifdef _MSC_VER
_ASSERTE(_CrtCheckMemory());
#endif
if(ncstat) {nullfree(mem); goto done;}
}
done:
return THROW(ncstat);
}
static char*
getdefinename(CDFnode* node)
{
char* spath = NULL;
NClist* path = NULL;
switch (node->nctype) {
case NC_Atomic:
/* The define name is same as the fullname with elided nodes */
path = nclistnew();
collectnodepath(node,path,!WITHDATASET);
spath = makepathstring(path,".",PATHNC|PATHELIDE);
nclistfree(path);
break;
case NC_Dimension:
/* Return just the node's ncname */
spath = nulldup(node->ncbasename);
break;
default:
PANIC("unexpected nctype");
}
return spath;
}
int
NCDAP2_ping(const char* url)
{
OCerror ocstat = OC_NOERR;
ocstat = oc_ping(url);
return ocerrtoncerr(ocstat);
}
int
NCD2_inq_format_extended(int ncid, int* formatp, int* modep)
{
NC* nc;
int ncstatus = NC_check_id(ncid, (NC**)&nc);
if(ncstatus != NC_NOERR) return THROW(ncstatus);
if(modep) *modep = nc->mode;
if(formatp) *formatp = NC_FORMATX_DAP2;
return NC_NOERR;
}