Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HDF4.C
Go to the documentation of this file.
1 /* *******************************************************************
2  * Rocstar Simulation Suite *
3  * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4  * *
5  * Illinois Rocstar LLC *
6  * Champaign, IL *
7  * www.illinoisrocstar.com *
8  * sales@illinoisrocstar.com *
9  * *
10  * License: See LICENSE file in top level of distribution package or *
11  * http://opensource.org/licenses/NCSA *
12  *********************************************************************/
13 /* *******************************************************************
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22  *********************************************************************/
29 #include <algorithm>
30 #include <functional>
31 #include <iostream>
32 #include <limits>
33 #include <vector>
34 #include <mfhdf.h>
35 #include "HDF4.h"
36 
37 const int HDF_DEBUG = 0;
38 
39 #ifdef USE_PTHREADS
40 
47 struct HDF4Cmd {
48  HDF4Cmd(int cmd) : m_command(cmd) { m_result.asInt32 = 0; }
49 
54  enum Label {
55  Hishdf,
56  SDstart,
57  SDend,
58  SDfileinfo,
59  SDcreate,
60  SDselect,
61  SDendaccess,
62  SDfindattr,
63  SDgetinfo,
64  SDsetdatastrs,
65  SDgetdatastrs,
66  SDsetrange,
67  SDgetrange,
68  SDwritedata,
69  SDreaddata,
70  DFSDsetdims,
71  DFSDsetNT,
72  DFSDsetdatastrs,
73  DFSDsetrange,
74  DFSDadddata,
75  DFSDputdata,
76  Terminate
77  };
78  int m_command;
79 
81  union ArgUnion {
82  int32 asInt32;
83  intn asIntn;
84  char* asCharP;
85  int32* asInt32P;
86  VOIDP asVOIDP;
87  };
88  std::vector<ArgUnion> m_args;
89  union {
90  int32 asInt32;
91  intn asIntn;
92  } m_result;
93  Semaphore m_semaphore;
94 };
95 
96 Semaphore HDF4::sm_pending;
97 std::list<HDF4Cmd*> HDF4::sm_cmdQueue;
98 Mutex HDF4::sm_cs;
99 pthread_t HDF4::sm_id = 0;
100 int HDF4::sm_counter = 0;
101 
102 #endif // USE_PTHREADS
103 
109 {
110  if (HDF_DEBUG)
111  std::cout << "HDF4::init()" << std::endl;
112 
113 #ifdef USE_PTHREADS
114  ++sm_counter;
115 
116  // Make sure that a thread isn't already running.
117  if (sm_id != 0)
118  return;
119 
120  pthread_attr_t attr;
121  pthread_attr_init(&attr);
122  pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
123  pthread_create(&sm_id, &attr, HDF4::Entry, NULL);
124  pthread_attr_destroy(&attr);
125 #endif // USE_PTHREADS
126 }
127 
129 {
130  if (HDF_DEBUG)
131  std::cout << "HDF4::finalize()" << std::endl;
132 
133 #ifdef USE_PTHREADS
134  --sm_counter;
135  if (sm_counter > 0)
136  return;
137 
138  HDF4::Terminate();
139 
140  void* retval;
141  pthread_join(sm_id, &retval);
142 
143  sm_id = 0;
144 #endif // USE_PTHREADS
145 }
146 
148 
154 intn HDF4::Hishdf(const char* filename)
155 {
156  if (HDF_DEBUG)
157  std::cout << "HDF4::Hishdf" << std::endl;
158 
159 #ifdef USE_PTHREADS
160  // Sprecify the command.
161  HDF4Cmd c(HDF4Cmd::Hishdf);
162 
163  // Add the arguments to the argument list.
164  c.m_args.resize(1);
165  c.m_args[0].asCharP = const_cast<char*>(filename);
166 
167  // Push the command request onto the command queue.
168  PostCommand(&c);
169 
170  // Return the result.
171  return c.m_result.asIntn;
172 #else
173  return ::Hishdf(filename);
174 #endif // USE_PTHREADS
175 }
176 
177 int32 HDF4::SDstart(const char* filename, int32 accessMode)
178 {
179  if (HDF_DEBUG)
180  std::cout << "HDF4::SDstart( filename == " << filename << ", accessMode == " << accessMode << " )" << std::endl;
181 
182 #ifdef USE_PTHREADS
183  HDF4Cmd c(HDF4Cmd::SDstart);
184 
185  c.m_args.resize(2);
186  c.m_args[0].asCharP = const_cast<char*>(filename);
187  c.m_args[1].asInt32 = accessMode;
188 
189  if (HDF_DEBUG){
190  // std::cout << " Posting Command" << std::endl << " m_args[0] = " << c.m_args[0].asCharP << std::endl << " m_args[1] = " << c.m_args[1].asInt32 << std::endl;
191  }
192  PostCommand(&c);
193 
194  return c.m_result.asInt32;
195 #else
196  return ::SDstart(filename, accessMode);
197 #endif // USE_PTHREADS
198 }
199 
200 intn HDF4::SDend(int32 sd_id)
201 {
202  if (HDF_DEBUG)
203  std::cout << "HDF4::SDend" << std::endl;
204 
205 #ifdef USE_PTHREADS
206  HDF4Cmd c(HDF4Cmd::SDend);
207 
208  c.m_args.resize(1);
209  c.m_args[0].asInt32 = sd_id;
210 
211  PostCommand(&c);
212 
213  return c.m_result.asIntn;
214 #else
215  return ::SDend(sd_id);
216 #endif // USE_PTHREADS
217 }
218 
219 intn HDF4::SDfileinfo(int32 id, int32* dsCount, int32* nAttrs)
220 {
221  if (HDF_DEBUG)
222  std::cout << "HDF4::SDfileinfo" << std::endl;
223 
224 #ifdef USE_PTHREADS
225  HDF4Cmd c(HDF4Cmd::SDfileinfo);
226 
227  c.m_args.resize(3);
228  c.m_args[0].asInt32 = id;
229  c.m_args[1].asInt32P = dsCount;
230  c.m_args[2].asInt32P = nAttrs;
231 
232  if(HDF_DEBUG)
233  std::cout << " Posting command" << std::endl;
234  PostCommand(&c);
235 
236  if(HDF_DEBUG)
237  std::cout << " Returning" << std::endl;
238  return c.m_result.asIntn;
239 #else
240  return ::SDfileinfo(id, dsCount, nAttrs);
241 #endif // USE_PTHREADS
242 }
243 
244 int32 HDF4::SDcreate(int32 sds_id, const char* name, int32 dType, int32 rank,
245  int32* size)
246 {
247  if (HDF_DEBUG)
248  std::cout << "HDF4::SDcreate" << std::endl;
249 
250 #ifdef USE_PTHREADS
251  HDF4Cmd c(HDF4Cmd::SDcreate);
252 
253  c.m_args.resize(5);
254  c.m_args[0].asInt32 = sds_id;
255  c.m_args[1].asCharP = const_cast<char*>(name);
256  c.m_args[2].asInt32 = dType;
257  c.m_args[3].asInt32 = rank;
258  c.m_args[4].asInt32P = size;
259 
260  PostCommand(&c);
261 
262  return c.m_result.asIntn;
263 #else
264  return ::SDcreate(sds_id, name, dType, rank, size);
265 #endif // USE_PTHREADS
266 }
267 
268 int32 HDF4::SDselect(int32 sd_id, int32 index)
269 {
270  if (HDF_DEBUG)
271  std::cout << "HDF4::SDselect( sd_id == " << sd_id << ", index == " << index << " )" << std::endl;
272 
273 #ifdef USE_PTHREADS
274  HDF4Cmd c(HDF4Cmd::SDselect);
275 
276  c.m_args.resize(2);
277  c.m_args[0].asInt32 = sd_id;
278  c.m_args[1].asInt32 = index;
279 
280  PostCommand(&c);
281 
282  return c.m_result.asInt32;
283 #else
284  return ::SDselect(sd_id, index);
285 #endif // USE_PTHREADS
286 }
287 
288 intn HDF4::SDendaccess(int32 sds_id)
289 {
290  if (HDF_DEBUG)
291  std::cout << "HDF4::SDendaccess" << std::endl;
292 
293 #ifdef USE_PTHREADS
294  HDF4Cmd c(HDF4Cmd::SDendaccess);
295 
296  c.m_args.resize(1);
297  c.m_args[0].asInt32 = sds_id;
298 
299  PostCommand(&c);
300 
301  return c.m_result.asIntn;
302 #else
303  return ::SDendaccess(sds_id);
304 #endif // USE_PTHREADS
305 }
306 
307 int32 HDF4::SDfindattr(int32 id, const char* attrName)
308 {
309  if (HDF_DEBUG)
310  std::cout << "HDF4::SDfindattr" << std::endl;
311 
312 #ifdef USE_PTHREADS
313  HDF4Cmd c(HDF4Cmd::SDfindattr);
314 
315  c.m_args.resize(2);
316  c.m_args[0].asInt32 = id;
317  c.m_args[1].asCharP = const_cast<char*>(attrName);
318 
319  PostCommand(&c);
320 
321  return c.m_result.asInt32;
322 #else
323  return ::SDfindattr(id, attrName);
324 #endif // USE_PTHREADS
325 }
326 
327 intn HDF4::SDgetinfo(int32 sds_id, char* name, int32* rank, int32* size,
328  int32* dType, int32* nAttrs)
329 {
330  if (HDF_DEBUG)
331  std::cout << "HDF4::SDgetinfo" << std::endl;
332 
333 #ifdef USE_PTHREADS
334  HDF4Cmd c(HDF4Cmd::SDgetinfo);
335 
336  c.m_args.resize(6);
337  c.m_args[0].asInt32 = sds_id;
338  c.m_args[1].asCharP = name;
339  c.m_args[2].asInt32P = rank;
340  c.m_args[3].asInt32P = size;
341  c.m_args[4].asInt32P = dType;
342  c.m_args[5].asInt32P = nAttrs;
343 
344  PostCommand(&c);
345 
346  return c.m_result.asIntn;
347 #else
348  return ::SDgetinfo(sds_id, name, rank, size, dType, nAttrs);
349 #endif // USE_PTHREADS
350 }
351 
352 intn HDF4::SDsetdatastrs(int32 sds_id, const char* label, const char* units,
353  const char* format, const char* coordsys)
354 {
355  if (HDF_DEBUG)
356  std::cout << "HDF4::SDsetdatastrs" << std::endl;
357 
358 #ifdef USE_PTHREADS
359  HDF4Cmd c(HDF4Cmd::SDsetdatastrs);
360 
361  c.m_args.resize(5);
362  c.m_args[0].asInt32 = sds_id;
363  c.m_args[1].asCharP = const_cast<char*>(label);
364  c.m_args[2].asCharP = const_cast<char*>(units);
365  c.m_args[3].asCharP = const_cast<char*>(format);
366  c.m_args[4].asCharP = const_cast<char*>(coordsys);
367 
368  PostCommand(&c);
369 
370  return c.m_result.asIntn;
371 #else
372  return ::SDsetdatastrs(sds_id, label, units, format, coordsys);
373 #endif // USE_PTHREADS
374 }
375 
376 intn HDF4::SDgetdatastrs(int32 sds_id, char* label, char* units, char* format,
377  char* coordsys, intn length)
378 {
379  if (HDF_DEBUG)
380  std::cout << "HDF4::SDgetdatastrs" << std::endl;
381 
382 #ifdef USE_PTHREADS
383  HDF4Cmd c(HDF4Cmd::SDgetdatastrs);
384 
385  c.m_args.resize(6);
386  c.m_args[0].asInt32 = sds_id;
387  c.m_args[1].asCharP = label;
388  c.m_args[2].asCharP = units;
389  c.m_args[3].asCharP = format;
390  c.m_args[4].asCharP = coordsys;
391  c.m_args[5].asIntn = length;
392 
393  PostCommand(&c);
394 
395  return c.m_result.asIntn;
396 #else
397  return ::SDgetdatastrs(sds_id, label, units, format, coordsys, length);
398 #endif // USE_PTHREADS
399 }
400 
401 intn HDF4::SDsetrange(int32 sds_id, VOIDP max, VOIDP min)
402 {
403  if (HDF_DEBUG)
404  std::cout << "HDF4::SDsetrange" << std::endl;
405 
406 #ifdef USE_PTHREADS
407  HDF4Cmd c(HDF4Cmd::SDsetrange);
408 
409  c.m_args.resize(3);
410  c.m_args[0].asInt32 = sds_id;
411  c.m_args[1].asVOIDP = max;
412  c.m_args[2].asVOIDP = min;
413 
414  PostCommand(&c);
415 
416  return c.m_result.asIntn;
417 #else
418  return ::SDsetrange(sds_id, max, min);
419 #endif // USE_PTHREADS
420 }
421 
422 intn HDF4::SDgetrange(int32 sds_id, VOIDP max, VOIDP min)
423 {
424  if (HDF_DEBUG)
425  std::cout << "HDF4::SDgetrange" << std::endl;
426 
427 #ifdef USE_PTHREADS
428  HDF4Cmd c(HDF4Cmd::SDgetrange);
429 
430  c.m_args.resize(3);
431  c.m_args[0].asInt32 = sds_id;
432  c.m_args[1].asVOIDP = max;
433  c.m_args[2].asVOIDP = min;
434 
435  PostCommand(&c);
436 
437  return c.m_result.asIntn;
438 #else
439  return ::SDgetrange(sds_id, max, min);
440 #endif // USE_PTHREADS
441 }
442 
443 intn HDF4::SDwritedata(int32 sds_id, int32* start, int32* stripe, int32* end,
444  VOIDP data)
445 {
446  if (HDF_DEBUG)
447  std::cout << "HDF4::SDwritedata" << std::endl;
448 
449 #ifdef USE_PTHREADS
450  HDF4Cmd c(HDF4Cmd::SDwritedata);
451 
452  c.m_args.resize(5);
453  c.m_args[0].asInt32 = sds_id;
454  c.m_args[1].asInt32P = start;
455  c.m_args[2].asInt32P = stripe;
456  c.m_args[3].asInt32P = end;
457  c.m_args[4].asVOIDP = data;
458 
459  PostCommand(&c);
460 
461  return c.m_result.asIntn;
462 #else
463  return ::SDwritedata(sds_id, start, stripe, end, data);
464 #endif // USE_PTHREADS
465 }
466 
467 intn HDF4::SDreaddata(int32 sds_id, int32* start, int32* stripe, int32* end,
468  VOIDP data)
469 {
470  if (HDF_DEBUG)
471  std::cout << "HDF4::SDreaddata" << std::endl;
472 
473 #ifdef USE_PTHREADS
474  HDF4Cmd c(HDF4Cmd::SDreaddata);
475 
476  c.m_args.resize(5);
477  c.m_args[0].asInt32 = sds_id;
478  c.m_args[1].asInt32P = start;
479  c.m_args[2].asInt32P = stripe;
480  c.m_args[3].asInt32P = end;
481  c.m_args[4].asVOIDP = data;
482 
483  PostCommand(&c);
484 
485  return c.m_result.asIntn;
486 #else
487  return ::SDreaddata(sds_id, start, stripe, end, data);
488 #endif // USE_PTHREADS
489 }
490 
491 intn HDF4::DFSDsetdims(intn rank, int32 dimsizes[])
492 {
493  if (HDF_DEBUG)
494  std::cout << "HDF4::DFSDsetdims" << std::endl;
495 
496 #ifdef USE_PTHREADS
497  HDF4Cmd c(HDF4Cmd::DFSDsetdims);
498 
499  c.m_args.resize(2);
500  c.m_args[0].asIntn = rank;
501  c.m_args[1].asInt32P = dimsizes;
502 
503  PostCommand(&c);
504 
505  return c.m_result.asIntn;
506 #else
507  return ::DFSDsetdims(rank, dimsizes);
508 #endif // USE_PTHREADS
509 }
510 
511 intn HDF4::DFSDsetNT(int32 numbertype)
512 {
513  if (HDF_DEBUG)
514  std::cout << "HDF4::DFSDsetNT" << std::endl;
515 
516 #ifdef USE_PTHREADS
517  HDF4Cmd c(HDF4Cmd::DFSDsetNT);
518 
519  c.m_args.resize(1);
520  c.m_args[0].asInt32 = numbertype;
521 
522  PostCommand(&c);
523 
524  return c.m_result.asIntn;
525 #else
526  return ::DFSDsetNT(numbertype);
527 #endif // USE_PTHREADS
528 }
529 
530 intn HDF4::DFSDsetdatastrs(const char* label, const char* unit,
531  const char* format, const char* coordsys)
532 {
533  if (HDF_DEBUG)
534  std::cout << "HDF4::DFSDsetdatastrs" << std::endl;
535 
536 #ifdef USE_PTHREADS
537  HDF4Cmd c(HDF4Cmd::DFSDsetdatastrs);
538 
539  c.m_args.resize(4);
540  c.m_args[0].asCharP = const_cast<char*>(label);
541  c.m_args[1].asCharP = const_cast<char*>(unit);
542  c.m_args[2].asCharP = const_cast<char*>(format);
543  c.m_args[3].asCharP = const_cast<char*>(coordsys);
544 
545  PostCommand(&c);
546 
547  return c.m_result.asIntn;
548 #else
549  return ::DFSDsetdatastrs(label, unit, format, coordsys);
550 #endif // USE_PTHREADS
551 }
552 
553 intn HDF4::DFSDsetrange(VOIDP maxi, VOIDP mini)
554 {
555  if (HDF_DEBUG)
556  std::cout << "HDF4::DFSDsetrange" << std::endl;
557 
558 #ifdef USE_PTHREADS
559  HDF4Cmd c(HDF4Cmd::DFSDsetrange);
560 
561  c.m_args.resize(2);
562  c.m_args[0].asVOIDP = maxi;
563  c.m_args[1].asVOIDP = mini;
564 
565  PostCommand(&c);
566 
567  return c.m_result.asIntn;
568 #else
569  return ::DFSDsetrange(maxi, mini);
570 #endif // USE_PTHREADS
571 }
572 
573 intn HDF4::DFSDadddata(const char* filename, intn rank, int32 dimsizes[],
574  VOIDP data)
575 {
576  if (HDF_DEBUG)
577  std::cout << "HDF4::DFSDadddata" << std::endl;
578 
579 #ifdef USE_PTHREADS
580  HDF4Cmd c(HDF4Cmd::DFSDadddata);
581 
582  c.m_args.resize(4);
583  c.m_args[0].asCharP = const_cast<char*>(filename);
584  c.m_args[1].asIntn = rank;
585  c.m_args[2].asInt32P = dimsizes;
586  c.m_args[3].asVOIDP = data;
587 
588  PostCommand(&c);
589 
590  return c.m_result.asIntn;
591 #else
592  return ::DFSDadddata(filename, rank, dimsizes, data);
593 #endif // USE_PTHREADS
594 }
595 
596 intn HDF4::DFSDputdata(const char* filename, intn rank, int32 dimsizes[],
597  VOIDP data)
598 {
599  if (HDF_DEBUG)
600  std::cout << "HDF4::DFSDputdata" << std::endl;
601 
602 #ifdef USE_PTHREADS
603  HDF4Cmd c(HDF4Cmd::DFSDputdata);
604 
605  c.m_args.resize(4);
606  c.m_args[0].asCharP = const_cast<char*>(filename);
607  c.m_args[1].asIntn = rank;
608  c.m_args[2].asInt32P = dimsizes;
609  c.m_args[3].asVOIDP = data;
610 
611  PostCommand(&c);
612 
613  return c.m_result.asIntn;
614 #else
615  return ::DFSDputdata(filename, rank, dimsizes, data);
616 #endif // USE_PTHREADS
617 }
619 
626 {
627  if (HDF_DEBUG)
628  std::cout << "HDF4::Terminate()" << std::endl;
629 
630 #ifdef USE_PTHREADS
631  if (sm_counter > 0)
632  return;
633 
634  HDF4Cmd c(HDF4Cmd::Terminate);
635 
636  c.m_args.clear();
637 
638  PostCommand(&c);
639 #endif // USE_PTHREADS
640 }
641 
642 #ifdef USE_PTHREADS
643 void HDF4::PostCommand(HDF4Cmd* pCmd)
644 {
645  sm_cs.Lock();
646  sm_cmdQueue.push_back(pCmd);
647  sm_cs.Unlock();
648 
649  //std::cout << " Signal I/O thread that it has work" << std::endl;
650  // Signal the I/O thread that it has something to do.
651  sm_pending.Post();
652 
653  //std::cout << " Wait for completion of I/O thread" << std::endl;
654  // The I/O thread will signal completion by posting to the semaphore.
655  pCmd->m_semaphore.Wait();
656 }
657 #endif // USE_PTHREADS
658 
675 int32 HDF4::Select(int32 sd_id, int32& index, char* name, int32* rank,
676  int32* size, int32* dType, int32* nAttrs, int32 dsCount)
677 {
678  if (HDF_DEBUG)
679  std::cout << "HDF4::Select" << std::endl;
680  int32 sds_id;
681  intn status;
682 
683  // If the caller didn't specify the number of datasets in the file, then
684  // we have to call SDfileinfo(). <sigh>
685  if (dsCount == -1) {
686  int32 numAttrs;
687  status = HDF4::SDfileinfo(sd_id, &dsCount, &numAttrs);
688  if (status == FAIL)
689  dsCount = MAX_NC_VARS;
690  }
691 
692  while (index < dsCount) {
693  // Open the next dataset.
694  sds_id = HDF4::SDselect(sd_id, index);
695  if (sds_id == FAIL) {
696  std::cerr << "HDF4::Select(): unable to select dataset " << index
697  << '.' << std::endl;
698  return FAIL;
699  }
700 
701  // Get the dataset info.
702  status = HDF4::SDgetinfo(sds_id, name, rank, size, dType, nAttrs);
703  if (status == FAIL) {
704  std::cerr << "HDF4::Select(): SDgetinfo() failed for dataset "
705  << index << '.' << std::endl;
706  return FAIL;
707  }
708 
709  // Make sure that this isn't a "fakeDim" dataset (they're so annoying).
710  if (strncmp(name, "fakeDim", 7) != 0)
711  return sds_id;
712 
713  // If we got this far, then it was a "fakeDim" dataset. Close it and
714  // try the next one.
715  HDF4::SDendaccess(sds_id);
716  ++index;
717  }
718 
719  // Whoops, we're fresh out of datasets. Sorry.
720  return FAIL;
721 }
722 
723 
730 int HDF4::SizeOf(int32 dType)
731 {
732  if (HDF_DEBUG)
733  std::cout << "HDF4::SIZEOF" << std::endl;
734  switch (dType) {
735  case DFNT_CHAR8:
736  case DFNT_UCHAR8:
737  case DFNT_INT8:
738  case DFNT_NINT8:
739  case DFNT_LINT8:
740  case DFNT_UINT8:
741  case DFNT_NUINT8:
742  case DFNT_LUINT8:
743  return 1;
744 
745  case DFNT_INT16:
746  case DFNT_NINT16:
747  case DFNT_LINT16:
748  case DFNT_UINT16:
749  case DFNT_NUINT16:
750  case DFNT_LUINT16:
751  return 2;
752 
753  case DFNT_INT32:
754  case DFNT_NINT32:
755  case DFNT_LINT32:
756  case DFNT_UINT32:
757  case DFNT_NUINT32:
758  case DFNT_LUINT32:
759  case DFNT_FLOAT32:
760  case DFNT_NFLOAT32:
761  case DFNT_LFLOAT32:
762  return 4;
763 
764  case DFNT_FLOAT64:
765  case DFNT_NFLOAT64:
766  case DFNT_LFLOAT64:
767  return 8;
768  }
769 
770  return 0;
771 }
772 
773 #if USE_PTHREADS
774 
783 void* HDF4::Entry(void*)
784 {
785  if (HDF_DEBUG)
786  std::cout << "HDF4::ENTRY" << std::endl;
787  if (HDF_DEBUG)
788  std::cout << "HDF4 I/O thread started!" << std::endl;
789 
790  HDF4Cmd* c = NULL;
791  do {
792  // Wait for the signal that there's something to do.
793  if (!sm_pending.Wait()) {
794  std::cerr << "HDF4::Entry(): semaphore error." << std::endl;
795  return NULL;
796  }
797 
798  if (sm_cmdQueue.empty())
799  continue;
800 
801  // Get the command request.
802  sm_cs.Lock();
803  c = sm_cmdQueue.front();
804  sm_cmdQueue.pop_front();
805  sm_cs.Unlock();
806 
807  // Act on the command.
808  switch (c->m_command) {
809  case HDF4Cmd::Hishdf:
810  c->m_result.asIntn = ::Hishdf(c->m_args[0].asCharP);
811  break;
812 
813  case HDF4Cmd::SDstart:
814  c->m_result.asInt32 = ::SDstart(c->m_args[0].asCharP,
815  c->m_args[1].asInt32);
816  break;
817 
818  case HDF4Cmd::SDend:
819  c->m_result.asIntn = ::SDend(c->m_args[0].asInt32);
820  break;
821 
822  case HDF4Cmd::SDfileinfo:
823  c->m_result.asIntn = ::SDfileinfo(c->m_args[0].asInt32,
824  c->m_args[1].asInt32P,
825  c->m_args[2].asInt32P);
826  break;
827 
828  case HDF4Cmd::SDcreate:
829  c->m_result.asIntn = ::SDcreate(c->m_args[0].asInt32,
830  c->m_args[1].asCharP,
831  c->m_args[2].asInt32,
832  c->m_args[3].asInt32,
833  c->m_args[4].asInt32P);
834  break;
835 
836  case HDF4Cmd::SDselect:
837  c->m_result.asInt32 = ::SDselect(c->m_args[0].asInt32,
838  c->m_args[1].asInt32);
839  break;
840 
841  case HDF4Cmd::SDendaccess:
842  c->m_result.asIntn = ::SDendaccess(c->m_args[0].asInt32);
843  break;
844 
845  case HDF4Cmd::SDfindattr:
846  c->m_result.asInt32 = ::SDfindattr(c->m_args[0].asInt32,
847  c->m_args[1].asCharP);
848  break;
849 
850  case HDF4Cmd::SDgetinfo:
851  c->m_result.asIntn = ::SDgetinfo(c->m_args[0].asInt32,
852  c->m_args[1].asCharP,
853  c->m_args[2].asInt32P,
854  c->m_args[3].asInt32P,
855  c->m_args[4].asInt32P,
856  c->m_args[5].asInt32P);
857  break;
858 
859  case HDF4Cmd::SDsetdatastrs:
860  c->m_result.asIntn = ::SDsetdatastrs(c->m_args[0].asInt32,
861  c->m_args[1].asCharP,
862  c->m_args[2].asCharP,
863  c->m_args[3].asCharP,
864  c->m_args[4].asCharP);
865  break;
866 
867  case HDF4Cmd::SDgetdatastrs:
868  c->m_result.asIntn = ::SDgetdatastrs(c->m_args[0].asInt32,
869  c->m_args[1].asCharP,
870  c->m_args[2].asCharP,
871  c->m_args[3].asCharP,
872  c->m_args[4].asCharP,
873  c->m_args[5].asIntn);
874  break;
875 
876  case HDF4Cmd::SDsetrange:
877  c->m_result.asIntn = ::SDsetrange(c->m_args[0].asInt32,
878  c->m_args[1].asVOIDP,
879  c->m_args[2].asVOIDP);
880  break;
881 
882  case HDF4Cmd::SDgetrange:
883  c->m_result.asIntn = ::SDgetrange(c->m_args[0].asInt32,
884  c->m_args[1].asVOIDP,
885  c->m_args[2].asVOIDP);
886  break;
887 
888  case HDF4Cmd::SDwritedata:
889  c->m_result.asIntn = ::SDwritedata(c->m_args[0].asInt32,
890  c->m_args[1].asInt32P,
891  c->m_args[2].asInt32P,
892  c->m_args[3].asInt32P,
893  c->m_args[4].asVOIDP);
894  break;
895 
896  case HDF4Cmd::SDreaddata:
897  c->m_result.asIntn = ::SDreaddata(c->m_args[0].asInt32,
898  c->m_args[1].asInt32P,
899  c->m_args[2].asInt32P,
900  c->m_args[3].asInt32P,
901  c->m_args[4].asVOIDP);
902  break;
903 
904  case HDF4Cmd::DFSDsetdims:
905  c->m_result.asIntn = ::DFSDsetdims(c->m_args[0].asIntn,
906  c->m_args[1].asInt32P);
907  break;
908 
909  case HDF4Cmd::DFSDsetNT:
910  c->m_result.asIntn = ::DFSDsetNT(c->m_args[0].asInt32);
911  break;
912 
913  case HDF4Cmd::DFSDsetdatastrs:
914  c->m_result.asIntn = ::DFSDsetdatastrs(c->m_args[0].asCharP,
915  c->m_args[1].asCharP,
916  c->m_args[2].asCharP,
917  c->m_args[3].asCharP);
918  break;
919 
920  case HDF4Cmd::DFSDsetrange:
921  c->m_result.asIntn = ::DFSDsetrange(c->m_args[0].asVOIDP,
922  c->m_args[1].asVOIDP);
923  break;
924 
925  case HDF4Cmd::DFSDadddata:
926  c->m_result.asIntn = ::DFSDadddata(c->m_args[0].asCharP,
927  c->m_args[1].asIntn,
928  c->m_args[2].asInt32P,
929  c->m_args[3].asVOIDP);
930  break;
931 
932  case HDF4Cmd::DFSDputdata:
933  c->m_result.asIntn = ::DFSDputdata(c->m_args[0].asCharP,
934  c->m_args[1].asIntn,
935  c->m_args[2].asInt32P,
936  c->m_args[3].asVOIDP);
937  break;
938 
939  case HDF4Cmd::Terminate:
940  break;
941 
942  default:
943  std::cerr << "HDF4::Entry: ignoring unrecognised command ("
944  << c->m_command << ')' << std::endl;
945  break;
946  }
947 
948  // Signal the caller that processing is complete.
949  c->m_semaphore.Post();
950 
951  // Exit if we get a Terminate command or if Delete() has been called.
952  } while (c->m_command != HDF4Cmd::Terminate);
953 
954  if (HDF_DEBUG)
955  std::cout << "HDF4 I/O thread exiting!" << std::endl;
956 
957  return NULL;
958 }
959 #endif // USE_PTHREADS
960 
961 std::string HDF4::error_msg() {
962  return HEstring((hdf_err_code_t)HEvalue(1));
963 }
964 
965 
966 
967 
968 
969 
static int32 SDselect(int32 sd_id, int32 index)
Definition: HDF4.C:268
static int32 SDfindattr(int32 id, const char *attrName)
Definition: HDF4.C:307
static intn SDwritedata(int32 sds_id, int32 *start, int32 *stride, int32 *end, VOIDP data)
Definition: HDF4.C:443
#define MAX_NC_VARS
static intn SDgetinfo(int32 sds_id, char *name, int32 *rank, int32 *size, int32 *dType, int32 *nAttrs)
Definition: HDF4.C:327
static intn SDend(int32 sd_id)
Definition: HDF4.C:200
static intn SDendaccess(int32 sds_id)
Definition: HDF4.C:288
Vector_n max(const Array_n_const &v1, const Array_n_const &v2)
Definition: Vector_n.h:354
static intn DFSDsetdims(intn rank, int32 dimsizes[])
Single file interface.
Definition: HDF4.C:491
static int32 SDcreate(int32 sd_id, const char *name, int32 dType, int32 rank, int32 *size)
Definition: HDF4.C:244
static intn DFSDadddata(const char *filename, intn rank, int32 dimsizes[], VOIDP data)
Definition: HDF4.C:573
Definition: Sync.h:65
static intn SDgetrange(int32 sds_id, VOIDP max, VOIDP min)
Definition: HDF4.C:422
Definition: Sync.h:28
static int SizeOf(int32 dType)
Get the size in bytes of an HDF data type.
Definition: HDF4.C:730
double length(Vector3D *const v, int n)
static intn SDsetrange(int32 sds_id, VOIDP max, VOIDP min)
Definition: HDF4.C:401
static std::string error_msg()
return error message
Definition: HDF4.C:961
static void init()
Create and start the I/O thread.
Definition: HDF4.C:108
static intn DFSDsetNT(int32 numbertype)
Definition: HDF4.C:511
static intn SDsetdatastrs(int32 sds_id, const char *label, const char *units, const char *format, const char *coordsys)
Definition: HDF4.C:352
static int32 Select(int32 sd_id, int32 &index, char *name, int32 *rank, int32 *size, int32 *dType, int32 *nAttrs, int32 dsCount=-1)
&quot;Open&quot; and &quot;close&quot; HDF files efficiently.
Definition: HDF4.C:675
static intn SDreaddata(int32 sds_id, int32 *start, int32 *stride, int32 *end, VOIDP data)
Definition: HDF4.C:467
const int HDF_DEBUG
Definition: HDF4.C:37
static void Terminate()
Terminate the I/O thread.
Definition: HDF4.C:625
static intn Hishdf(const char *filename)
HDF4 wrapper functions.
Definition: HDF4.C:154
static intn SDfileinfo(int32 id, int32 *dsCount, int32 *nAttrs)
Definition: HDF4.C:219
static intn DFSDputdata(const char *filename, intn rank, int32 dimsizes[], VOIDP data)
Definition: HDF4.C:596
static void finalize()
Destroy the I/O thread.
Definition: HDF4.C:128
Vector_n min(const Array_n_const &v1, const Array_n_const &v2)
Definition: Vector_n.h:346
unsigned long id(const Leda_like_handle &x)
Definition: Handle.h:107
static int32 SDstart(const char *filename, int32 accessMode)
Definition: HDF4.C:177
static intn DFSDsetrange(VOIDP maxi, VOIDP mini)
Definition: HDF4.C:553
static intn SDgetdatastrs(int32 sds_id, char *label, char *units, char *format, char *coordsys, intn length)
Definition: HDF4.C:376
static intn DFSDsetdatastrs(const char *label, const char *unit, const char *format, const char *coordsys)
Definition: HDF4.C:530
static int rank
Definition: advectest.C:66