getvara3.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. /*********************************************************************
  2. * Copyright 1993, UCAR/Unidata
  3. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  4. * $Header: /upc/share/CVS/netcdf-3/libncdap3/getvara3.c,v 1.44 2010/05/27 21:34:08 dmh Exp $
  5. *********************************************************************/
  6. #include "ncdap3.h"
  7. #include "dapodom.h"
  8. #include "dapdump.h"
  9. #include "ncd3dispatch.h"
  10. #define NEWVARM
  11. /* Define a tracker for memory to support*/
  12. /* the concatenation*/
  13. struct NCMEMORY {
  14. void* memory;
  15. char* next; /* where to store the next chunk of data*/
  16. };
  17. /* Forward:*/
  18. static NCerror moveto(NCDAPCOMMON*, Getvara*, CDFnode* dataroot, void* memory);
  19. static NCerror movetor(NCDAPCOMMON*, OCdata currentcontent,
  20. NClist* path, int depth,
  21. Getvara*, int dimindex,
  22. struct NCMEMORY*, NClist* segments);
  23. static int findfield(CDFnode* node, CDFnode* subnode);
  24. static int wholeslicepoint(Dapodometer* odom);
  25. static NCerror removepseudodims(DCEprojection* proj);
  26. static int extract(NCDAPCOMMON*, Getvara*, CDFnode*, DCEsegment*, OClink, OCdata, struct NCMEMORY*);
  27. static int extractstring(NCDAPCOMMON*, Getvara*, CDFnode*, DCEsegment*, OClink, OCdata, struct NCMEMORY*);
  28. /**
  29. 1. We build the projection to be sent to the server aka
  30. the fetch constraint. We want the server do do as much work
  31. as possible, so we send it a url with a fetch constraint
  32. that is the merge of the url constraint with the vara
  33. constraint.
  34. The url constraint, if any, is the one that was provided
  35. in the url specified in nc_open().
  36. The vara constraint is the one formed from the arguments
  37. (start, count, stride) provided to the call to nc_get_vara().
  38. There are some exceptions to the formation of the fetch constraint.
  39. In all cases, the fetch constraint will use any URL selections,
  40. but will use different fetch projections.
  41. a. URL is unconstrainable (e.g. file://...):
  42. fetchprojection = null => fetch whole dataset
  43. b. The target variable (as specified in nc_get_vara())
  44. is already in the cache and is whole variable.
  45. fetchprojection = N.A. since variable is in the cache
  46. c. Vara is requesting part of a variable but NCF_WHOLEVAR flag is set.
  47. fetchprojection = unsliced vara variable => fetch whole variable
  48. d. Vara is requesting part of a variable and NCF_WHOLEVAR flag is not set.
  49. fetchprojection = sliced vara variable => fetch part variable
  50. 2. At this point, all or part of the target variable is available in the cache.
  51. 3. We build a projection to walk (guide) the use of the oc
  52. data procedures in extract the required data from the cache.
  53. For cases a,b,c:
  54. walkprojection = merge(urlprojection,varaprojection)
  55. For case d:
  56. walkprojection = varaprojection without slicing.
  57. This means we need only extract the complete contents of the cache.
  58. Notice that this will not necessarily be a direct memory to
  59. memory copy because the dap encoding still needs to be
  60. interpreted. For this case, we derive a walk projection
  61. from the vara projection that will properly access the cached data.
  62. This walk projection shifts the merged projection so all slices
  63. start at 0 and have a stride of 1.
  64. */
  65. NCerror
  66. nc3d_getvarx(int ncid, int varid,
  67. const size_t *startp,
  68. const size_t *countp,
  69. const ptrdiff_t* stridep,
  70. void *data,
  71. nc_type dsttype0)
  72. {
  73. NCerror ncstat = NC_NOERR;
  74. OCerror ocstat = OC_NOERR;
  75. int i;
  76. NC* drno;
  77. NC* substrate;
  78. NCDAPCOMMON* dapcomm;
  79. CDFnode* cdfvar; /* cdf node mapping to var*/
  80. NClist* varnodes;
  81. nc_type dsttype;
  82. Getvara* varainfo = NULL;
  83. CDFnode* xtarget = NULL; /* target in DATADDS */
  84. CDFnode* target = NULL; /* target in constrained DDS */
  85. DCEprojection* varaprojection = NULL;
  86. NCcachenode* cachenode = NULL;
  87. size_t localcount[NC_MAX_VAR_DIMS];
  88. NClist* ncdimsall;
  89. size_t ncrank;
  90. NClist* vars = NULL;
  91. DCEconstraint* fetchconstraint = NULL;
  92. DCEprojection* fetchprojection = NULL;
  93. DCEprojection* walkprojection = NULL;
  94. int state;
  95. #define FETCHWHOLE 1 /* fetch whole data set */
  96. #define FETCHVAR 2 /* fetch whole variable */
  97. #define FETCHPART 4 /* fetch constrained variable */
  98. #define CACHED 8 /* whole variable is already in the cache */
  99. ncstat = NC_check_id(ncid, (NC**)&drno);
  100. if(ncstat != NC_NOERR) goto fail;
  101. dapcomm = (NCDAPCOMMON*)drno->dispatchdata;
  102. ncstat = NC_check_id(drno->substrate, (NC**)&substrate);
  103. if(ncstat != NC_NOERR) goto fail;
  104. /* Locate var node via varid */
  105. varnodes = dapcomm->cdf.varnodes;
  106. for(i=0;i<nclistlength(varnodes);i++) {
  107. CDFnode* node = (CDFnode*)nclistget(varnodes,i);
  108. if(node->array.basevar == NULL
  109. && node->nctype == NC_Primitive
  110. && node->ncid == varid) {
  111. cdfvar = node;
  112. break;
  113. }
  114. }
  115. ASSERT((cdfvar != NULL));
  116. /* Get the dimension info */
  117. ncdimsall = cdfvar->array.dimsetall;
  118. ncrank = nclistlength(ncdimsall);
  119. #ifdef DEBUG
  120. {
  121. int i;
  122. fprintf(stderr,"getvarx: %s",cdfvar->ncfullname);
  123. for(i=0;i<ncrank;i++)
  124. fprintf(stderr,"[%ld:%ld:%ld]",
  125. (long)startp[i],
  126. (long)countp[i],
  127. (long)stridep[i]
  128. );
  129. fprintf(stderr,"\n");
  130. }
  131. #endif
  132. /* Fill in missing arguments */
  133. if(startp == NULL)
  134. startp = nc_sizevector0;
  135. if(countp == NULL) {
  136. /* Accumulate the dimension sizes */
  137. for(i=0;i<ncrank;i++) {
  138. CDFnode* dim = (CDFnode*)nclistget(ncdimsall,i);
  139. localcount[i] = dim->dim.declsize;
  140. }
  141. countp = localcount;
  142. }
  143. if(stridep == NULL)
  144. stridep = nc_ptrdiffvector1;
  145. /* Validate the dimension sizes */
  146. for(i=0;i<ncrank;i++) {
  147. CDFnode* dim = (CDFnode*)nclistget(ncdimsall,i);
  148. if(startp[i] > dim->dim.declsize
  149. || startp[i]+countp[i] > dim->dim.declsize) {
  150. ncstat = NC_EINVALCOORDS;
  151. goto fail;
  152. }
  153. }
  154. #ifdef DEBUG
  155. {
  156. NClist* dims = cdfvar->array.dimsetall;
  157. fprintf(stderr,"getvarx: %s",cdfvar->ncfullname);
  158. if(nclistlength(dims) > 0) {int i;
  159. for(i=0;i<nclistlength(dims);i++)
  160. fprintf(stderr,"[%lu:%lu:%lu]",(unsigned long)startp[i],(unsigned long)countp[i],(unsigned long)stridep[i]);
  161. fprintf(stderr," -> ");
  162. for(i=0;i<nclistlength(dims);i++)
  163. if(stridep[i]==1)
  164. fprintf(stderr,"[%lu:%lu]",(unsigned long)startp[i],(unsigned long)((startp[i]+countp[i])-1));
  165. else
  166. fprintf(stderr,"[%lu:%lu:%lu]",
  167. (unsigned long)startp[i],
  168. (unsigned long)stridep[i],
  169. (unsigned long)(((startp[i]+countp[i])*stridep[i])-1));
  170. }
  171. fprintf(stderr,"\n");
  172. }
  173. #endif
  174. dsttype = (dsttype0);
  175. /* Default to using the inquiry type for this var*/
  176. if(dsttype == NC_NAT) dsttype = cdfvar->externaltype;
  177. /* Validate any implied type conversion*/
  178. if(cdfvar->etype != dsttype && dsttype == NC_CHAR) {
  179. /* The only disallowed conversion is to/from char and non-byte
  180. numeric types*/
  181. switch (cdfvar->etype) {
  182. case NC_STRING: case NC_URL:
  183. case NC_CHAR: case NC_BYTE: case NC_UBYTE:
  184. break;
  185. default:
  186. return THROW(NC_ECHAR);
  187. }
  188. }
  189. ncstat = makegetvar34(dapcomm,cdfvar,data,dsttype,&varainfo);
  190. if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto fail;}
  191. state = 0;
  192. if(iscached(dapcomm,cdfvar,&cachenode)) {
  193. state = CACHED;
  194. ASSERT((cachenode != NULL));
  195. #ifdef DEBUG
  196. fprintf(stderr,"var is in cache\n");
  197. #endif
  198. /* If it is cached, then it is a whole variable but may still
  199. need to apply constraints during the walk */
  200. ASSERT(cachenode->wholevariable); /* by construction */
  201. } else if(FLAGSET(dapcomm->controls,NCF_UNCONSTRAINABLE)) {
  202. state = FETCHWHOLE;
  203. } else {/* load using constraints */
  204. if(FLAGSET(dapcomm->controls,NCF_WHOLEVAR))
  205. state = FETCHVAR;
  206. else
  207. state = FETCHPART;
  208. }
  209. ASSERT(state != 0);
  210. /* Convert the start/stop/stride info into a projection */
  211. ncstat = buildvaraprojection3(varainfo,
  212. startp,countp,stridep,
  213. &varaprojection);
  214. if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto fail;}
  215. fetchprojection = NULL;
  216. walkprojection = NULL;
  217. /* Create walkprojection as the merge of the url projections
  218. and the vara projection; may change in FETCHPART case below*/
  219. ncstat = daprestrictprojection(dapcomm->oc.dapconstraint->projections,
  220. varaprojection,&walkprojection);
  221. if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto fail;}
  222. #ifdef DEBUG
  223. fprintf(stderr,"getvarx: walkprojection: |%s|\n",dumpprojection(walkprojection));
  224. #endif
  225. /* define the var list of interest */
  226. vars = nclistnew();
  227. nclistpush(vars,(ncelem)varainfo->target);
  228. switch (state) {
  229. case FETCHWHOLE: {
  230. /* buildcachenode3 will create a new cachenode and
  231. will also fetch the whole corresponding datadds.
  232. */
  233. /* Build the complete constraint to use in the fetch */
  234. fetchconstraint = (DCEconstraint*)dcecreate(CES_CONSTRAINT);
  235. /* Use no projections or selections */
  236. fetchconstraint->projections = nclistnew();
  237. fetchconstraint->selections = nclistnew();
  238. #ifdef DEBUG
  239. fprintf(stderr,"getvarx: FETCHWHOLE: fetchconstraint: %s\n",dumpconstraint(fetchconstraint));
  240. #endif
  241. ncstat = buildcachenode34(dapcomm,fetchconstraint,vars,&cachenode,0);
  242. fetchconstraint = NULL; /*buildcachenode34 takes control of fetchconstraint.*/
  243. if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto fail;}
  244. } break;
  245. case CACHED: {
  246. } break;
  247. case FETCHVAR: { /* Fetch a complete single variable */
  248. /* Create fetch projection as the merge of the url projections
  249. and the vara projection */
  250. ncstat = daprestrictprojection(dapcomm->oc.dapconstraint->projections,
  251. varaprojection,&fetchprojection);
  252. /* elide any sequence and string dimensions (dap servers do not allow such). */
  253. ncstat = removepseudodims(fetchprojection);
  254. if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto fail;}
  255. /* Convert to a whole variable projection */
  256. dcemakewholeprojection(fetchprojection);
  257. #ifdef DEBUG
  258. fprintf(stderr,"getvarx: FETCHVAR: fetchprojection: |%s|\n",dumpprojection(fetchprojection));
  259. #endif
  260. /* Build the complete constraint to use in the fetch */
  261. fetchconstraint = (DCEconstraint*)dcecreate(CES_CONSTRAINT);
  262. /* merged constraint just uses the url constraint selection */
  263. fetchconstraint->selections = dceclonelist(dapcomm->oc.dapconstraint->selections);
  264. /* and the created fetch projection */
  265. fetchconstraint->projections = nclistnew();
  266. nclistpush(fetchconstraint->projections,(ncelem)fetchprojection);
  267. #ifdef DEBUG
  268. fprintf(stderr,"getvarx: FETCHVAR: fetchconstraint: %s\n",dumpconstraint(fetchconstraint));
  269. #endif
  270. /* buildcachenode3 will create a new cachenode and
  271. will also fetch the corresponding datadds.
  272. */
  273. ncstat = buildcachenode34(dapcomm,fetchconstraint,vars,&cachenode,0);
  274. fetchconstraint = NULL; /*buildcachenode34 takes control of fetchconstraint.*/
  275. if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto fail;}
  276. } break;
  277. case FETCHPART: {
  278. /* Create fetch projection as the merge of the url projections
  279. and the vara projection */
  280. ncstat = daprestrictprojection(dapcomm->oc.dapconstraint->projections,
  281. varaprojection,&fetchprojection);
  282. /* elide any sequence and string dimensions (dap servers do not allow such). */
  283. ncstat = removepseudodims(fetchprojection);
  284. if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto fail;}
  285. /* Shift the varaprojection for simple walk */
  286. dcefree((DCEnode*)walkprojection) ; /* reclaim any existing walkprojection */
  287. walkprojection = (DCEprojection*)dceclone((DCEnode*)varaprojection);
  288. dapshiftprojection(walkprojection);
  289. #ifdef DEBUG
  290. fprintf(stderr,"getvarx: FETCHPART: fetchprojection: |%s|\n",dumpprojection(fetchprojection));
  291. #endif
  292. /* Build the complete constraint to use in the fetch */
  293. fetchconstraint = (DCEconstraint*)dcecreate(CES_CONSTRAINT);
  294. /* merged constraint just uses the url constraint selection */
  295. fetchconstraint->selections = dceclonelist(dapcomm->oc.dapconstraint->selections);
  296. /* and the created fetch projection */
  297. fetchconstraint->projections = nclistnew();
  298. nclistpush(fetchconstraint->projections,(ncelem)fetchprojection);
  299. #ifdef DEBUG
  300. fprintf(stderr,"getvarx: FETCHPART: fetchconstraint: %s\n",dumpconstraint(fetchconstraint));
  301. #endif
  302. /* buildcachenode3 will create a new cachenode and
  303. will also fetch the corresponding datadds.
  304. */
  305. ncstat = buildcachenode34(dapcomm,fetchconstraint,vars,&cachenode,0);
  306. fetchconstraint = NULL; /*buildcachenode34 takes control of fetchconstraint.*/
  307. if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto fail;}
  308. } break;
  309. default: PANIC1("unknown fetch state: %d\n",state);
  310. }
  311. ASSERT(cachenode != NULL);
  312. #ifdef DEBUG
  313. fprintf(stderr,"cache.datadds=%s\n",dumptree(cachenode->datadds));
  314. #endif
  315. /* attach DATADDS to (constrained) DDS */
  316. unattach34(dapcomm->cdf.ddsroot);
  317. ncstat = attachsubset34(cachenode->datadds,dapcomm->cdf.ddsroot);
  318. if(ncstat) goto fail;
  319. /* Fix up varainfo to use the cache */
  320. varainfo->cache = cachenode;
  321. cachenode = NULL;
  322. varainfo->varaprojection = walkprojection;
  323. walkprojection = NULL;
  324. /* Get the var correlate from the datadds */
  325. target = varainfo->target;
  326. xtarget = target->attachment;
  327. if(xtarget == NULL)
  328. {THROWCHK(ncstat=NC_ENODATA); goto fail;}
  329. /* Switch to datadds tree space*/
  330. varainfo->target = xtarget;
  331. ncstat = moveto(dapcomm,varainfo,varainfo->cache->datadds,data);
  332. if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto fail;}
  333. goto ok;
  334. fail:
  335. if(ocstat != OC_NOERR) ncstat = ocerrtoncerr(ocstat);
  336. ok:
  337. nclistfree(vars);
  338. dcefree((DCEnode*)varaprojection);
  339. dcefree((DCEnode*)fetchconstraint);
  340. freegetvara(varainfo);
  341. return THROW(ncstat);
  342. }
  343. /* Remove any pseudodimensions (sequence and string)*/
  344. static NCerror
  345. removepseudodims(DCEprojection* proj)
  346. {
  347. int i;
  348. #ifdef DEBUG1
  349. fprintf(stderr,"removesequencedims.before: %s\n",dumpprojection(proj));
  350. #endif
  351. for(i=0;i<nclistlength(proj->var->segments);i++) {
  352. DCEsegment* seg = (DCEsegment*)nclistget(proj->var->segments,i);
  353. CDFnode* cdfnode = (CDFnode*)seg->annotation;
  354. if(cdfnode->array.seqdim != NULL)
  355. seg->rank = 0;
  356. else if(cdfnode->array.stringdim != NULL)
  357. seg->rank--;
  358. }
  359. #ifdef DEBUG1
  360. fprintf(stderr,"removepseudodims.after: %s\n",dumpprojection(proj));
  361. #endif
  362. return NC_NOERR;
  363. }
  364. static NCerror
  365. moveto(NCDAPCOMMON* nccomm, Getvara* xgetvar, CDFnode* xrootnode, void* memory)
  366. {
  367. OCerror ocstat = OC_NOERR;
  368. NCerror ncstat = NC_NOERR;
  369. OCconnection conn = nccomm->oc.conn;
  370. OCdata xrootcontent;
  371. OCobject ocroot;
  372. NClist* path = nclistnew();
  373. struct NCMEMORY memstate;
  374. memstate.next = (memstate.memory = memory);
  375. /* Get the root content*/
  376. ocroot = xrootnode->tree->ocroot;
  377. xrootcontent = oc_data_new(conn);
  378. ocstat = oc_data_root(conn,ocroot,xrootcontent);
  379. if(ocstat != OC_NOERR) {THROWCHK(ocstat); goto done;}
  380. /* Remember: xgetvar->target is in DATADDS tree */
  381. collectnodepath3(xgetvar->target,path,WITHDATASET);
  382. ncstat = movetor(nccomm,xrootcontent,
  383. path,0,xgetvar,0,&memstate,
  384. xgetvar->varaprojection->var->segments);
  385. done:
  386. nclistfree(path);
  387. oc_data_free(conn,xrootcontent);
  388. if(ocstat != OC_NOERR) ncstat = ocerrtoncerr(ocstat);
  389. return THROW(ncstat);
  390. }
  391. static NCerror
  392. movetor(NCDAPCOMMON* nccomm,
  393. OCdata currentcontent,
  394. NClist* path,
  395. int depth, /* depth is position in segment list*/
  396. Getvara* xgetvar,
  397. int dimindex, /* dimindex is position in xgetvar->slices*/
  398. struct NCMEMORY* memory,
  399. NClist* segments)
  400. {
  401. int i;
  402. OCerror ocstat = OC_NOERR;
  403. NCerror ncstat = NC_NOERR;
  404. size_t fieldindex,gridindex,rank;
  405. OCconnection conn = nccomm->oc.conn;
  406. CDFnode* xnode = (CDFnode*)nclistget(path,depth);
  407. OCdata reccontent = OCNULL;
  408. OCdata dimcontent = OCNULL;
  409. OCdata fieldcontent = OCNULL;
  410. Dapodometer* odom = OCNULL;
  411. OCmode currentmode = OCNULLMODE;
  412. CDFnode* xnext;
  413. int hasstringdim = 0;
  414. size_t dimoffset;
  415. DCEsegment* segment;
  416. int newdepth;
  417. int caching = FLAGSET(nccomm->controls,NCF_CACHE);
  418. int unconstrainable = FLAGSET(nccomm->controls,NCF_UNCONSTRAINABLE);
  419. /* Note that we use depth-1 because the path contains the DATASET
  420. but the segment list does not */
  421. segment = (DCEsegment*)nclistget(segments,depth-1); /*may be NULL*/
  422. if(xnode->etype == NC_STRING || xnode->etype == NC_URL) hasstringdim = 1;
  423. ocstat = oc_data_mode(conn,currentcontent,&currentmode);
  424. #ifdef DEBUG2
  425. fprintf(stderr,"moveto: nctype=%d currentmode=%d depth=%d dimindex=%d",
  426. xnode->nctype, currentmode, depth,dimindex);
  427. fprintf(stderr," segment=%s hasstringdim=%d\n",
  428. dcetostring((DCEnode*)segment),hasstringdim);
  429. #endif
  430. /* Switch on the combination of nctype and mode */
  431. #define CASE(nc1,nc2) (nc1*1024+nc2)
  432. /* This must be consistent with the oc mode transition function */
  433. switch (CASE(xnode->nctype,currentmode)) {
  434. default:
  435. PANIC2("Illegal combination: nctype=%d mode=%d",
  436. (int)xnode->nctype,(int)currentmode);
  437. break;
  438. case CASE(NC_Sequence,OCFIELDMODE):
  439. case CASE(NC_Dataset,OCFIELDMODE):
  440. case CASE(NC_Grid,OCFIELDMODE):
  441. case CASE(NC_Structure,OCFIELDMODE):
  442. /* currentcontent points to the grid/dataset/structure instance */
  443. xnext = (CDFnode*)nclistget(path,depth+1);
  444. ASSERT((xnext != NULL));
  445. fieldindex = findfield(xnode,xnext);
  446. /* If the next node is a virtual node, then
  447. we need to effectively
  448. ignore it and use the appropriate subnode.
  449. If the next node is a structuregrid node, then
  450. use it as is.
  451. */
  452. if(xnext->virtual) {
  453. CDFnode* xgrid = xnext;
  454. xnext = (CDFnode*)nclistget(path,depth+2); /* real node */
  455. gridindex = fieldindex;
  456. fieldindex = findfield(xgrid,xnext);
  457. fieldindex += gridindex;
  458. newdepth = depth+2;
  459. } else {
  460. newdepth = depth+1;
  461. }
  462. fieldcontent = oc_data_new(conn);
  463. ocstat = oc_data_ith(conn,currentcontent,fieldindex,fieldcontent);
  464. if(ocstat != OC_NOERR) {THROWCHK(ocstat); goto fail;}
  465. ncstat = movetor(nccomm,fieldcontent,
  466. path,newdepth,xgetvar,dimindex,memory,
  467. segments);
  468. break;
  469. case CASE(NC_Sequence,OCARRAYMODE): /* will actually always be scalar, but will have
  470. rank == 1 to account for the sequence dim */
  471. case CASE(NC_Grid,OCARRAYMODE): /* will actually always be scalar */
  472. case CASE(NC_Structure,OCARRAYMODE):
  473. /* figure out which slices refer to this node:
  474. dimindex upto dimindex+rank; */
  475. ASSERT((segment != NULL));
  476. rank = segment->rank;
  477. if(xnode->nctype == NC_Sequence)
  478. rank--; /* ignore the sequence dim */
  479. if(rank == 0) {
  480. odom = newdapodometer1(1);
  481. } else if(caching || unconstrainable) {
  482. odom = newdapodometer(segment->slices,0,rank);
  483. } else { /*Since vara was projected out, build a simple odometer*/
  484. odom = newsimpledapodometer(segment,rank);
  485. }
  486. while(dapodometermore(odom)) {
  487. OCmode mode;
  488. /* Compute which instance to move to*/
  489. dimoffset = dapodometercount(odom);
  490. dimcontent = oc_data_new(conn);
  491. ocstat = oc_data_ith(conn,currentcontent,dimoffset,dimcontent);
  492. if(ocstat != OC_NOERR) {THROWCHK(ocstat); goto fail;}
  493. ocstat = oc_data_mode(conn,dimcontent,&mode);
  494. ASSERT((mode == OCFIELDMODE
  495. || (mode == OCSEQUENCEMODE && xnode->nctype == NC_Sequence)));
  496. ncstat = movetor(nccomm,dimcontent,
  497. path,depth,
  498. xgetvar,dimindex+rank,
  499. memory,segments);
  500. dapodometerincr(odom);
  501. }
  502. freedapodometer(odom);
  503. break;
  504. case CASE(NC_Sequence,OCSEQUENCEMODE): {
  505. DCEslice* uslice;
  506. ASSERT((segment != NULL));
  507. /* Get and check the corresponding sequence dimension from DDS */
  508. ASSERT((xnode->attachment != NULL));
  509. /* use uslice to walk the sequence; however, watch out
  510. for the case when the user set a limit and that limit
  511. is not actually reached in this request.
  512. */
  513. /* By construction, this sequence represents the first
  514. (and only) dimension of this segment */
  515. uslice = &segment->slices[0];
  516. reccontent = oc_data_new(conn);
  517. for(i=uslice->first;i<uslice->stop;i+=uslice->stride) {
  518. OCmode eos;
  519. ocstat = oc_data_ith(conn,currentcontent,i,reccontent);
  520. if(ocstat != OC_NOERR) {THROWCHK(ocstat); goto fail;}
  521. ocstat = oc_data_mode(conn,reccontent,&eos);
  522. if(ocstat != OC_NOERR) {THROWCHK(ocstat); goto fail;}
  523. if(eos == OCNULLMODE) {
  524. /* We asked for too much */
  525. ncstat = THROW(NC_EINVALCOORDS);
  526. goto fail;
  527. }
  528. ncstat = movetor(nccomm,reccontent,
  529. path,depth,
  530. xgetvar,dimindex+1,
  531. memory,segments);
  532. if(ncstat != OC_NOERR) {THROWCHK(ncstat); goto fail;}
  533. }
  534. } break;
  535. case CASE(NC_Primitive,OCPRIMITIVEMODE):
  536. if(hasstringdim)
  537. ncstat = extractstring(nccomm, xgetvar, xnode, segment, conn, currentcontent, memory);
  538. else
  539. ncstat = extract(nccomm, xgetvar, xnode, segment, conn, currentcontent, memory);
  540. break;
  541. }
  542. goto ok;
  543. fail:
  544. ok:
  545. oc_data_free(conn,dimcontent);
  546. oc_data_free(conn,fieldcontent);
  547. oc_data_free(conn,reccontent);
  548. if(ocstat != OC_NOERR) ncstat = ocerrtoncerr(ocstat);
  549. return THROW(ncstat);
  550. }
  551. /* Determine the index in the odometer at which
  552. the odometer will be walking the whole subslice
  553. This will allow us to optimize.
  554. */
  555. static int
  556. wholeslicepoint(Dapodometer* odom)
  557. {
  558. unsigned int i;
  559. int point;
  560. for(point=-1,i=0;i<odom->rank;i++) {
  561. ASSERT((odom->slices[i].declsize != 0));
  562. if(odom->slices[i].first != 0 || odom->slices[i].stride != 1
  563. || odom->slices[i].length != odom->slices[i].declsize)
  564. point = i;
  565. }
  566. if(point == -1)
  567. point = 0; /* wholevariable */
  568. else if(point == (odom->rank - 1))
  569. point = -1; /* no whole point */
  570. else
  571. point += 1; /* intermediate point */
  572. return point;
  573. }
  574. static int
  575. findfield(CDFnode* node, CDFnode* field)
  576. {
  577. size_t i;
  578. for(i=0;i<nclistlength(node->subnodes);i++) {
  579. CDFnode* test = (CDFnode*) nclistget(node->subnodes,i);
  580. if(test == field) return i;
  581. }
  582. return -1;
  583. }
  584. int
  585. nc3d_getvarmx(int ncid, int varid,
  586. const size_t *start,
  587. const size_t *edges,
  588. const ptrdiff_t* stride,
  589. const ptrdiff_t* map,
  590. void* data,
  591. nc_type dsttype0)
  592. {
  593. NCerror ncstat = NC_NOERR;
  594. int i;
  595. NC* drno;
  596. NC* substrate;
  597. NCDAPCOMMON* dapcomm;
  598. NC_var* var;
  599. CDFnode* cdfvar; /* cdf node mapping to var*/
  600. NClist* varnodes;
  601. nc_type dsttype;
  602. size_t externsize;
  603. size_t dimsizes[NC_MAX_VAR_DIMS];
  604. Dapodometer* odom = NULL;
  605. unsigned int ncrank;
  606. NClist* ncdims = NULL;
  607. size_t nelems;
  608. #ifdef NEWVARM
  609. char* localcopy; /* of whole variable */
  610. #endif
  611. ncstat = NC_check_id(ncid, (NC**)&drno);
  612. if(ncstat != NC_NOERR) goto done;
  613. dapcomm = (NCDAPCOMMON*)drno->dispatchdata;
  614. ncstat = NC_check_id(drno->substrate, (NC**)&substrate);
  615. if(ncstat != NC_NOERR) goto done;
  616. var = NC_lookupvar(substrate,varid);
  617. if(var == NULL) {ncstat = NC_ENOTVAR; goto done;}
  618. /* Locate var node via varid */
  619. varnodes = dapcomm->cdf.varnodes;
  620. for(i=0;i<nclistlength(varnodes);i++) {
  621. CDFnode* node = (CDFnode*)nclistget(varnodes,i);
  622. if(node->array.basevar == NULL
  623. && node->nctype == NC_Primitive
  624. && node->ncid == varid) {
  625. cdfvar = node;
  626. break;
  627. }
  628. }
  629. ASSERT((cdfvar != NULL));
  630. ASSERT((strcmp(cdfvar->ncfullname,var->name->cp)==0));
  631. if(nclistlength(cdfvar->array.dimsetplus) == 0) {
  632. /* The variable is a scalar; consequently, there is only one
  633. thing to get and only one place to put it. (Why was I
  634. called?) */
  635. /* recurse with additional parameters */
  636. return THROW(nc3d_getvarx(ncid,varid,
  637. NULL,NULL,NULL,
  638. data,dsttype0));
  639. }
  640. dsttype = (dsttype0);
  641. /* Default to using the inquiry type for this var*/
  642. if(dsttype == NC_NAT) dsttype = cdfvar->externaltype;
  643. /* Validate any implied type conversion*/
  644. if(cdfvar->etype != dsttype && dsttype == NC_CHAR) {
  645. /* The only disallowed conversion is to/from char and non-byte
  646. numeric types*/
  647. switch (cdfvar->etype) {
  648. case NC_STRING: case NC_URL:
  649. case NC_CHAR: case NC_BYTE: case NC_UBYTE:
  650. break;
  651. default:
  652. return THROW(NC_ECHAR);
  653. }
  654. }
  655. externsize = nctypesizeof(dsttype);
  656. /* Accumulate the dimension sizes and the total # of elements */
  657. ncdims = cdfvar->array.dimsetall;
  658. ncrank = nclistlength(ncdims);
  659. nelems = 1; /* also Compute the number of elements being retrieved */
  660. for(i=0;i<ncrank;i++) {
  661. CDFnode* dim = (CDFnode*)nclistget(ncdims,i);
  662. dimsizes[i] = dim->dim.declsize;
  663. nelems *= edges[i];
  664. }
  665. /* Originally, this code repeatedly extracted single values
  666. using get_var1. In an attempt to improve performance,
  667. I have converted to reading the whole variable at once
  668. and walking it locally.
  669. */
  670. #ifdef NEWVARM
  671. localcopy = (char*)malloc(nelems*externsize);
  672. /* We need to use the varieties of get_vars in order to
  673. properly do conversion to the external type
  674. */
  675. switch (dsttype) {
  676. case NC_CHAR:
  677. ncstat = nc_get_vars_text(ncid,varid,start, edges, stride,
  678. (char*)localcopy);
  679. break;
  680. case NC_BYTE:
  681. ncstat = nc_get_vars_schar(ncid,varid,start, edges, stride,
  682. (signed char*)localcopy);
  683. break;
  684. case NC_SHORT:
  685. ncstat = nc_get_vars_short(ncid,varid, start, edges, stride,
  686. (short*)localcopy);
  687. break;
  688. case NC_INT:
  689. ncstat = nc_get_vars_int(ncid,varid,start, edges, stride,
  690. (int*)localcopy);
  691. break;
  692. case NC_FLOAT:
  693. ncstat = nc_get_vars_float(ncid,varid,start, edges, stride,
  694. (float*)localcopy);
  695. break;
  696. case NC_DOUBLE:
  697. ncstat = nc_get_vars_double(ncid,varid, start, edges, stride,
  698. (double*)localcopy);
  699. break;
  700. default: break;
  701. }
  702. odom = newdapodometer2(start,edges,stride,0,ncrank);
  703. /* Walk the local copy */
  704. for(i=0;i<nelems;i++) {
  705. size_t voffset = dapodometervarmcount(odom,map,dimsizes);
  706. void* dataoffset = (void*)(((char*)data) + (externsize*voffset));
  707. char* localpos = (localcopy + externsize*i);
  708. /* extract the indexset'th value from local copy */
  709. memcpy(dataoffset,(void*)localpos,externsize);
  710. /*
  711. fprintf(stderr,"new: %lu -> %lu %f\n",
  712. (unsigned long)(i),
  713. (unsigned long)voffset,
  714. *(float*)localpos);
  715. */
  716. dapodometerincr(odom);
  717. }
  718. #else
  719. odom = newdapodometer2(start,edges,stride,0,ncrank);
  720. while(dapodometermore(odom)) {
  721. size_t* indexset = dapodometerindices(odom);
  722. size_t voffset = dapodometervarmcount(odom,map,dimsizes);
  723. char internalmem[128];
  724. char externalmem[128];
  725. void* dataoffset = (void*)(((char*)data) + (externsize*voffset));
  726. /* get the indexset'th value using variable's internal type */
  727. ncstat = nc_get_var1(ncid,varid,indexset,(void*)&internalmem);
  728. if(ncstat != NC_NOERR) goto done;
  729. /* Convert to external type */
  730. ncstat = dapconvert3(cdfvar->etype,dsttype,externalmem,internalmem);
  731. if(ncstat != NC_NOERR) goto done;
  732. memcpy(dataoffset,(void*)externalmem,externsize);
  733. /*
  734. fprintf(stderr,"old: %lu -> %lu %f\n",
  735. (unsigned long)dapodometercount(odom),
  736. (unsigned long)voffset,
  737. *(float*)externalmem);
  738. */
  739. dapodometerincr(odom);
  740. }
  741. #endif
  742. done:
  743. return ncstat;
  744. }
  745. static int
  746. conversionrequired(nc_type t1, nc_type t2)
  747. {
  748. if(t1 == t2)
  749. return 0;
  750. if(nctypesizeof(t1) != nctypesizeof(t2))
  751. return 1;
  752. /* Avoid too many cases by making t1 < t2 */
  753. if(t1 > t2) {int tmp = t1; t1 = t2; t2 = tmp;}
  754. #undef CASE
  755. #define CASE(t1,t2) ((t1)<<5 | (t2))
  756. switch (CASE(t1,t2)) {
  757. case CASE(NC_BYTE,NC_UBYTE):
  758. case CASE(NC_BYTE,NC_CHAR):
  759. case CASE(NC_CHAR,NC_UBYTE):
  760. case CASE(NC_SHORT,NC_USHORT):
  761. case CASE(NC_INT,NC_UINT):
  762. case CASE(NC_INT64,NC_UINT64):
  763. return 0;
  764. default: break;
  765. }
  766. return 1;
  767. }
  768. /* We are at a primitive variable or scalar that has no string dimensions.
  769. Extract the data.
  770. (This is way too complicated)
  771. */
  772. static int
  773. extract(
  774. NCDAPCOMMON* nccomm,
  775. Getvara* xgetvar,
  776. CDFnode* xnode,
  777. DCEsegment* segment,
  778. OClink conn,
  779. OCdata currentcontent,
  780. struct NCMEMORY* memory
  781. )
  782. {
  783. OCerror ocstat = OC_NOERR;
  784. NCerror ncstat = NC_NOERR;
  785. size_t rank;
  786. Dapodometer* odom = OCNULL;
  787. int wholepoint;
  788. size_t externtypesize;
  789. size_t interntypesize;
  790. char* localmemory = NULL;
  791. size_t odomsubsize;
  792. size_t internlen;
  793. int requireconversion;
  794. char value[16];
  795. ASSERT((segment != NULL));
  796. requireconversion = conversionrequired(xgetvar->dsttype,xnode->etype);
  797. rank = segment->rank;
  798. if(rank == 0) {/* scalar */
  799. char* mem = (requireconversion?value:memory->next);
  800. ASSERT((segment != NULL));
  801. externtypesize = nctypesizeof(xgetvar->dsttype);
  802. ASSERT(externtypesize <= sizeof(value));
  803. /* Read the whole scalar directly into memory */
  804. ocstat = oc_data_get(conn,currentcontent,mem,externtypesize,0,1);
  805. if(ocstat != OC_NOERR) {THROWCHK(ocstat); goto done;}
  806. if(requireconversion) {
  807. /* convert the value to external type */
  808. ncstat = dapconvert3(xnode->etype,xgetvar->dsttype,memory->next,value,1);
  809. if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}
  810. }
  811. memory->next += (externtypesize);
  812. } else {/* rank > 0 */
  813. #ifdef DEBUG2
  814. fprintf(stderr,"moveto: primitive: segment=%s",
  815. dcetostring((DCEnode*)segment));
  816. fprintf(stderr," iswholevariable=%d",xgetvar->cache->wholevariable);
  817. fprintf(stderr,"\n");
  818. #endif
  819. ASSERT(xgetvar->cache != NULL);
  820. if(xgetvar->cache->wholevariable) {
  821. odom = newdapodometer(segment->slices,0,rank);
  822. } else { /*!xgetvar->cache->wholevariable*/
  823. odom = newsimpledapodometer(segment,rank);
  824. }
  825. /* Optimize off the use of the odometer by checking the slicing
  826. to see if the whole variable, or some whole subslice
  827. is being extracted.
  828. However do not do this if the external type conversion is needed
  829. or if the whole slice point is rank-1 (normal case anyway).
  830. */
  831. externtypesize = nctypesizeof(xgetvar->dsttype);
  832. interntypesize = nctypesizeof(xnode->etype);
  833. wholepoint = wholeslicepoint(odom);
  834. if(wholepoint == -1)
  835. odomsubsize = 1; /* no whole point */
  836. else
  837. odomsubsize = dapodometerspace(odom,wholepoint);
  838. internlen = (odomsubsize*interntypesize);
  839. if(requireconversion) {
  840. /* copy the data locally before conversion */
  841. localmemory = (char*)malloc(internlen);
  842. } else {
  843. localmemory = memory->next;
  844. }
  845. #ifdef DEBUG2
  846. fprintf(stderr,"moveto: primitive: ");
  847. fprintf(stderr," wholepoint=%d",wholepoint);
  848. fprintf(stderr,"\n");
  849. #endif
  850. if(wholepoint == 0) {/* whole variable */
  851. /* Read the whole n elements directly into memory.*/
  852. ocstat = oc_data_get(conn,currentcontent,localmemory,
  853. internlen,0,odomsubsize);
  854. if(ocstat != OC_NOERR) {THROWCHK(ocstat); goto done;}
  855. if(requireconversion) {
  856. /* do conversion */
  857. ncstat = dapconvert3(xnode->etype,xgetvar->dsttype,
  858. memory->next,localmemory,odomsubsize);
  859. if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}
  860. }
  861. memory->next += (externtypesize*odomsubsize);
  862. } else if(wholepoint > 0) {/* whole subslice */
  863. odom->rank = wholepoint; /* truncate */
  864. while(dapodometermore(odom)) {
  865. size_t dimoffset = dapodometercount(odom) * odomsubsize;
  866. ocstat = oc_data_get(conn,currentcontent,localmemory,
  867. internlen,dimoffset,odomsubsize);
  868. if(ocstat != OC_NOERR) {THROWCHK(ocstat); goto done;}
  869. if(requireconversion) {
  870. /* do conversion */
  871. ncstat = dapconvert3(xnode->etype,xgetvar->dsttype,
  872. memory->next,localmemory,odomsubsize);
  873. if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}
  874. }
  875. memory->next += (externtypesize*odomsubsize);
  876. dapodometerincr(odom);
  877. }
  878. } else { /* Oh well, use the odometer to walk to the
  879. appropriate fields*/
  880. while(dapodometermore(odom)) {
  881. char* mem = (requireconversion?value:memory->next);
  882. size_t dimoffset = dapodometercount(odom);
  883. ocstat = oc_data_get(conn,currentcontent,mem,externtypesize,dimoffset,1);
  884. if(ocstat != OC_NOERR) {THROWCHK(ocstat); goto done;}
  885. if(requireconversion) {
  886. ncstat = dapconvert3(xnode->etype,xgetvar->dsttype,memory->next,value,1);
  887. if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}
  888. }
  889. memory->next += externtypesize;
  890. dapodometerincr(odom);
  891. }
  892. }
  893. freedapodometer(odom);
  894. if(requireconversion) nullfree(localmemory);
  895. }
  896. done:
  897. return THROW(ncstat);
  898. }
  899. static NCerror
  900. slicestring(OCconnection conn, char* stringmem, DCEslice* slice, struct NCMEMORY* memory)
  901. {
  902. size_t stringlen;
  903. unsigned int i;
  904. NCerror ncstat = NC_NOERR;
  905. char* lastchar;
  906. size_t charcount; /* number of characters inserted into memory */
  907. /* libnc-dap chooses to convert string escapes to the corresponding
  908. character; so we do likewise.
  909. */
  910. dapexpandescapes(stringmem);
  911. stringlen = strlen(stringmem);
  912. #ifdef DEBUG2
  913. fprintf(stderr,"moveto: slicestring: string/%lu=%s\n",stringlen,stringmem);
  914. fprintf(stderr,"slicestring: %lu string=|%s|\n",stringlen,stringmem);
  915. fprintf(stderr,"slicestring: slice=[%lu:%lu:%lu/%lu]\n",
  916. slice->first,slice->stride,slice->stop,slice->declsize);
  917. #endif
  918. /* Stride across string; if we go past end of string, then pad*/
  919. charcount = 0;
  920. for(i=slice->first;i<slice->length;i+=slice->stride) {
  921. if(i < stringlen)
  922. *memory->next = stringmem[i];
  923. else /* i >= stringlen*/
  924. *memory->next = NC_FILL_CHAR;
  925. memory->next++;
  926. charcount++;
  927. }
  928. lastchar = (memory->next);
  929. if(charcount > 0) {
  930. lastchar--;
  931. }
  932. return THROW(ncstat);
  933. }
  934. /*
  935. Extract data for a netcdf variable that has a string dimension.
  936. */
  937. static int
  938. extractstring(
  939. NCDAPCOMMON* nccomm,
  940. Getvara* xgetvar,
  941. CDFnode* xnode,
  942. DCEsegment* segment,
  943. OClink conn,
  944. OCdata currentcontent,
  945. struct NCMEMORY* memory
  946. )
  947. {
  948. NCerror ncstat = NC_NOERR;
  949. OCerror ocstat = OC_NOERR;
  950. int i;
  951. size_t rank;
  952. int caching = FLAGSET(nccomm->controls,NCF_CACHE);
  953. int unconstrainable = FLAGSET(nccomm->controls,NCF_UNCONSTRAINABLE);
  954. NClist* strings = NULL;
  955. Dapodometer* odom = OCNULL;
  956. rank = segment->rank;
  957. /* A number of optimizations are possible but none is currently used. */
  958. /* Use the odometer to walk to the appropriate fields*/
  959. if(rank == 1) {
  960. odom = newdapodometer1(1); /* scalar case */
  961. } else if(caching || unconstrainable) {
  962. odom = newdapodometer(segment->slices,0,rank-1);
  963. } else { /*Since vara was projected out, build a simple odometer*/
  964. odom = newsimpledapodometer(segment,rank-1);
  965. }
  966. /* step thru the odometer obtaining each string and storing it in an OClist */
  967. strings = nclistnew();
  968. nclistsetalloc(strings,dapodometerspace(odom,0)); /* preallocate */
  969. while(dapodometermore(odom)) {
  970. char* value = NULL;
  971. size_t dimoffset = dapodometercount(odom);
  972. ocstat = oc_data_get(conn,currentcontent,&value,sizeof(value),dimoffset,1);
  973. if(ocstat != OC_NOERR) goto done;
  974. nclistpush(strings,(ncelem)value);
  975. dapodometerincr(odom);
  976. }
  977. freedapodometer(odom);
  978. /* Get each string in turn, slice it and store in user
  979. supplied memory */
  980. for(i=0;i<nclistlength(strings);i++) {
  981. char* s = (char*)nclistget(strings,i);
  982. slicestring(conn,s,&segment->slices[rank-1],memory);
  983. free(s);
  984. }
  985. nclistfree(strings);
  986. done:
  987. if(ocstat != OC_NOERR) ncstat = ocerrtoncerr(ocstat);
  988. return THROW(ncstat);
  989. }