/* Convert a CDFnode var to a projection; include pseudodimensions; always whole variable. */ int dapvar2projection(CDFnode* var, DCEprojection** projectionp) { int i,j; int ncstat = NC_NOERR; NClist* path = nclistnew(); NClist* segments; DCEprojection* projection = NULL; int dimindex; /* Collect the nodes needed to construct the projection segments */ collectnodepath(var,path,!WITHDATASET); segments = nclistnew(); dimindex = 0; /* point to next subset of slices */ nclistsetalloc(segments,nclistlength(path)); for(i=0;i<nclistlength(path);i++) { DCEsegment* segment = (DCEsegment*)dcecreate(CES_SEGMENT); CDFnode* n = (CDFnode*)nclistget(path,i); int localrank; NClist* dimset; segment->annotation = (void*)n; segment->name = nulldup(n->ocname); /* We need to assign whole slices to each segment */ localrank = nclistlength(n->array.dimsetplus); segment->rank = localrank; dimset = n->array.dimsetplus; for(j=0;j<localrank;j++) { DCEslice* slice; CDFnode* dim; slice = &segment->slices[j]; dim = (CDFnode*)nclistget(dimset,j); ASSERT(dim->dim.declsize0 > 0); dcemakewholeslice(slice,dim->dim.declsize0); } segment->slicesdefined = 1; segment->slicesdeclized = 1; dimindex += localrank; nclistpush(segments,(void*)segment); } projection = (DCEprojection*)dcecreate(CES_PROJECT); projection->discrim = CES_VAR; projection->var = (DCEvar*)dcecreate(CES_VAR); projection->var->annotation = (void*)var; projection->var->segments = segments; #ifdef DEBUG1 fprintf(stderr,"dapvar2projection: projection=%s\n", dumpprojection(projection)); #endif nclistfree(path); if(ncstat) dcefree((DCEnode*)projection); else if(projectionp) *projectionp = projection; return ncstat; }
/* convert path to string using the ncname field */ char* makecdfpathstring(CDFnode* var, const char* separator) { char* spath; NClist* path = nclistnew(); collectnodepath(var,path,WITHDATASET); /* <= note */ spath = makepathstring(path,separator,PATHNC); nclistfree(path); return spath; }
char* dumppath(CDFnode* leaf) { NClist* path = nclistnew(); NCbytes* buf = ncbytesnew(); char* result; int i; if(leaf == NULL) return nulldup(""); collectnodepath(leaf,path,!WITHDATASET); for(i=0;i<nclistlength(path);i++) { CDFnode* node = (CDFnode*)nclistget(path,i); if(i > 0) ncbytescat(buf,"."); ncbytescat(buf,node->ncbasename); } result = ncbytesdup(buf); ncbytesfree(buf); nclistfree(path); return result; }
/* convert all names in projections in paths to be fully qualified by adding prefix segment objects. */ static NCerror qualifyprojectionnames(DCEprojection* proj) { NCerror ncstat = NC_NOERR; NClist* fullpath = nclistnew(); ASSERT((proj->discrim == CES_VAR && proj->var->annotation != NULL && ((CDFnode*)proj->var->annotation)->ocnode != NULL)); collectnodepath((CDFnode*)proj->var->annotation,fullpath,!WITHDATASET); #ifdef DEBUG fprintf(stderr,"qualify: %s -> ", dumpprojection(proj)); #endif /* Now add path nodes to create full path */ completesegments(fullpath,proj->var->segments); #ifdef DEBUG fprintf(stderr,"%s\n", dumpprojection(proj)); #endif nclistfree(fullpath); return ncstat; }
NCerror fixprojections(NClist* list) { int i,j,k; NCerror ncstat = NC_NOERR; NClist* tmp = nclistnew(); /* misc. uses */ #ifdef DEBUG fprintf(stderr,"fixprojection: list = %s\n",dumpprojections(list)); #endif if(nclistlength(list) == 0) goto done; /* Step 1: remove duplicates and complain about slice mismatches */ for(i=0;i<nclistlength(list);i++) { DCEprojection* p1 = (DCEprojection*)nclistget(list,i); if(p1 == NULL) continue; if(p1->discrim != CES_VAR) continue; /* dont try to unify functions */ for(j=i;j<nclistlength(list);j++) { DCEprojection* p2 = (DCEprojection*)nclistget(list,j); if(p2 == NULL) continue; if(p1 == p2) continue; if(p2->discrim != CES_VAR) continue; if(p1->var->annotation != p2->var->annotation) continue; /* check for slice mismatches */ if(!slicematch(p1->var->segments,p2->var->segments)) { /* complain */ nclog(NCLOGWARN,"Malformed projection: same variable with different slicing"); } /* remove p32 */ nclistset(list,j,(void*)NULL); dcefree((DCEnode*)p2); } } /* Step 2: remove containers when a field is also present */ for(i=0;i<nclistlength(list);i++) { DCEprojection* p1 = (DCEprojection*)nclistget(list,i); if(p1 == NULL) continue; if(p1->discrim != CES_VAR) continue; /* dont try to unify functions */ if(!iscontainer((CDFnode*)p1->var->annotation)) continue; for(j=i;j<nclistlength(list);j++) { DCEprojection* p2 = (DCEprojection*)nclistget(list,j); if(p2 == NULL) continue; if(p2->discrim != CES_VAR) continue; nclistclear(tmp); collectnodepath((CDFnode*)p2->var->annotation,tmp,WITHDATASET); for(k=0;k<nclistlength(tmp);k++) { void* candidate = (void*)nclistget(tmp,k); if(candidate == p1->var->annotation) { nclistset(list,i,(void*)NULL); dcefree((DCEnode*)p1); goto next; } } } next: continue; } /* Step 3: expand all containers recursively down to the leaf nodes */ for(;;) { nclistclear(tmp); for(i=0;i<nclistlength(list);i++) { DCEprojection* target = (DCEprojection*)nclistget(list,i); CDFnode* leaf; if(target == NULL) continue; if(target->discrim != CES_VAR) continue; /* dont try to unify functions */ leaf = (CDFnode*)target->var->annotation; ASSERT(leaf != NULL); if(iscontainer(leaf)) {/* capture container */ if(!nclistcontains(tmp,(void*)target)) nclistpush(tmp,(void*)target); nclistset(list,i,(void*)NULL); } } if(nclistlength(tmp) == 0) break; /*done*/ /* Now explode the containers */ for(i=0;i<nclistlength(tmp);i++) { DCEprojection* container = (DCEprojection*)nclistget(tmp,i); CDFnode* leaf = (CDFnode*)container->var->annotation; for(j=0;i<nclistlength(leaf->subnodes);j++) { CDFnode* field = (CDFnode*)nclistget(leaf->subnodes,j); /* Convert field node to a proper constraint */ DCEprojection* proj = projectify(field,container); nclistpush(list,(void*)proj); } /* reclaim the container */ dcefree((DCEnode*)container); } } /*for(;;)*/ /* remove all NULL elements */ for(i=nclistlength(list)-1;i>=0;i--) { DCEprojection* target = (DCEprojection*)nclistget(list,i); if(target == NULL) nclistremove(list,i); } done: #ifdef DEBUG fprintf(stderr,"fixprojection: exploded = %s\n",dumpprojections(list)); #endif nclistfree(tmp); return ncstat; }
static NCerror matchpartialname(NClist* nodes, NClist* segments, CDFnode** nodep) { int i,nsegs; NCerror ncstat = NC_NOERR; DCEsegment* lastseg = NULL; NClist* namematches = nclistnew(); NClist* matches = nclistnew(); NClist* matchpath = nclistnew(); /* Locate all nodes with the same name as the last element in the segment path */ nsegs = nclistlength(segments); lastseg = (DCEsegment*)nclistget(segments,nsegs-1); for(i=0;i<nclistlength(nodes);i++) { CDFnode* node = (CDFnode*)nclistget(nodes,i); if(node->ocname == null) continue; /* Path names come from oc space */ if(strcmp(node->ocname,lastseg->name) != 0) continue; /* Only look at selected kinds of nodes */ if(node->nctype != NC_Sequence && node->nctype != NC_Structure && node->nctype != NC_Grid && node->nctype != NC_Atomic ) continue; nclistpush(namematches,(void*)node); } if(nclistlength(namematches)==0) { nclog(NCLOGERR,"No match for projection name: %s",lastseg->name); ncstat = NC_EDDS; goto done; } /* Now, collect and compare paths of the matching nodes */ for(i=0;i<nclistlength(namematches);i++) { CDFnode* matchnode = (CDFnode*)nclistget(namematches,i); nclistclear(matchpath); collectnodepath(matchnode,matchpath,0); /* Do a suffix match */ if(matchsuffix(matchpath,segments)) { nclistpush(matches,(void*)matchnode); #ifdef DEBUG fprintf(stderr,"matchpartialname: pathmatch: %s :: %s\n", matchnode->ncfullname,dumpsegments(segments)); #endif } } /* |matches|==0 => no match; |matches|>1 => ambiguity */ switch (nclistlength(matches)) { case 0: nclog(NCLOGERR,"No match for projection name: %s",lastseg->name); ncstat = NC_EDDS; break; case 1: if(nodep) *nodep = (CDFnode*)nclistget(matches,0); break; default: { CDFnode* minnode = NULL; int minpath = 0; int nmin = 0; /* to catch multiple ones with same short path */ /* ok, see if one of the matches has a path that is shorter then all the others */ for(i=0;i<nclistlength(matches);i++) { CDFnode* candidate = (CDFnode*)nclistget(matches,i); nclistclear(matchpath); collectnodepath(candidate,matchpath,0); if(minpath == 0) { minpath = nclistlength(matchpath); minnode = candidate; } else if(nclistlength(matchpath) == minpath) { nmin++; } else if(nclistlength(matchpath) < minpath) { minpath = nclistlength(matchpath); minnode = candidate; nmin = 1; } } /*for*/ if(minnode == NULL || nmin > 1) { nclog(NCLOGERR,"Ambiguous match for projection name: %s", lastseg->name); ncstat = NC_EDDS; } else if(nodep) *nodep = minnode; } break; } #ifdef DEBUG fprintf(stderr,"matchpartialname: choice: %s %s for %s\n", (nclistlength(matches) > 1?"":"forced"), (*nodep)->ncfullname,dumpsegments(segments)); #endif done: nclistfree(namematches); nclistfree(matches); nclistfree(matchpath); return THROW(ncstat); }