summaryrefslogtreecommitdiffstats
path: root/src/H5D.c
blob: 9aad6a4b60e039f84e1c71834397420b6a01cbce (plain)
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
1001
1002
1003
1004
/****************************************************************************
* NCSA HDF								   *
* Software Development Group						   *
* National Center for Supercomputing Applications			   *
* University of Illinois at Urbana-Champaign				   *
* 605 E. Springfield, Champaign IL 61820				   *
*									   *
* For conditions of distribution and use, see the accompanying		   *
* hdf/COPYING file.							   *
*									   *
****************************************************************************/

#ifdef RCSID
static char RcsId[] = "@(#)$Revision$";
#endif

/* $Id$ */


#include <H5private.h>		/* Generic Functions			*/
#include <H5Aprivate.h>		/* Atoms				*/
#include <H5ACprivate.h>	/* Cache				*/
#include <H5Cprivate.h>		/* Templates				*/
#include <H5Dprivate.h>		/* Dataset functions			*/
#include <H5Eprivate.h>		/* Error handling			*/
#include <H5Gprivate.h>		/* Group headers			*/
#include <H5Mprivate.h>		/* Meta data				*/
#include <H5MFprivate.h>	/* File space allocation header		*/
#include <H5MMprivate.h>	/* Memory management 			*/
#include <H5Mprivate.h>		/* Meta-Object API			*/
#include <H5Oprivate.h>		/* Object headers			*/

#define PABLO_MASK	H5D_mask

/*
 * A dataset is the following struct.
 */
struct H5D_t {
   H5F_t		*file;		/*file store for this object	*/
   H5G_entry_t		*ent;		/*cached object header stuff	*/
   H5T_t		*type;		/*datatype of this dataset	*/
   H5P_t		*space;		/*dataspace of this dataset	*/
   H5D_create_t		create_parms;	/*creation parameters		*/
   union {
      H5O_cstore_t	cstore;		/*contiguous storage info	*/
      H5O_istore_t	istore;		/*chunked storage info		*/
   } storage;
};

/* Default dataset creation template */
const H5D_create_t H5D_create_dflt = {
   H5D_CONTIGUOUS,		/* Layout				*/
   1, 				/* Chunk dimensions			*/
   {1, 1, 1, 1, 1, 1, 1, 1,	/* Chunk size.  These default values....*/
    1, 1, 1, 1, 1, 1, 1, 1,	/*...are quite useless.  Larger chunks..*/
    1, 1, 1, 1, 1, 1, 1, 1,	/*...produces fewer, but larger I/O.....*/
    1, 1, 1, 1, 1, 1, 1, 1}, 	/*...requests.				*/
};

/* Default dataset transfer template */
const H5D_xfer_t H5D_xfer_dflt = {
   0,  				/* Place holder - remove this later	*/
};


/* Interface initialization? */
static hbool_t interface_initialize_g = FALSE;
#define INTERFACE_INIT H5D_init_interface
static herr_t H5D_init_interface(void);
static void H5D_term_interface (void);


/*--------------------------------------------------------------------------
NAME
   H5D_init_interface -- Initialize interface-specific information
USAGE
    herr_t H5D_init_interface()
   
RETURNS
   SUCCEED/FAIL
DESCRIPTION
    Initializes any interface-specific data or routines.

--------------------------------------------------------------------------*/
static herr_t
H5D_init_interface (void)
{
   herr_t ret_value = SUCCEED;
   FUNC_ENTER (H5D_init_interface, FAIL);

   /* Initialize the atom group for the dataset IDs */
   if ((ret_value=H5Ainit_group (H5_DATASET, H5A_DATASETID_HASHSIZE,
				 H5D_RESERVED_ATOMS,
				 (herr_t (*)(void*))H5D_close))!=FAIL) {
      ret_value = H5_add_exit (H5D_term_interface);
   }
    
   FUNC_LEAVE (ret_value);
}

/*--------------------------------------------------------------------------
 NAME
    H5D_term_interface
 PURPOSE
    Terminate various H5D objects
 USAGE
    void H5D_term_interface()
 RETURNS
    SUCCEED/FAIL
 DESCRIPTION
    Release the atom group and any other resources allocated.
 GLOBAL VARIABLES
 COMMENTS, BUGS, ASSUMPTIONS
     Can't report errors...
 EXAMPLES
 REVISION LOG
--------------------------------------------------------------------------*/
static void
H5D_term_interface (void)
{
    H5Adestroy_group (H5_DATASET);
}


/*-------------------------------------------------------------------------
 * Function:	H5Dcreate
 *
 * Purpose:	Creates a new dataset named NAME in file FILE_ID, opens the
 *		dataset for access, and associates with that dataset constant
 *		and initial persistent properties including the type of each
 *		datapoint as stored in the file (TYPE_ID), the size of the
 *		dataset (SPACE_ID), and other initial miscellaneous
 *		properties (CREATE_PARMS_ID).
 *
 *		All arguments are copied into the dataset, so the caller is
 *		allowed to derive new types, data spaces, and creation
 *		parameters from the old ones and reuse them in calls to
 *		create other datasets.
 *
 * Return:	Success:	The object ID of the new dataset.  At this
 *				point, the dataset is ready to receive its
 *				raw data.  Attempting to read raw data from
 *				the dataset will probably return the fill
 *				value.  The dataset should be closed when
 *				the caller is no longer interested in it.
 *
 *		Failure:	FAIL
 *
 * Errors:
 *		ARGS      BADTYPE       Not a data space. 
 *		ARGS      BADTYPE       Not a dataset creation template. 
 *		ARGS      BADTYPE       Not a file. 
 *		ARGS      BADTYPE       Not a type. 
 *		ARGS      BADVALUE      No name. 
 *		DATASET   CANTINIT      Can't create dataset. 
 *		DATASET   CANTREGISTER  Can't register dataset. 
 *
 * Programmer:	Robb Matzke
 *		Wednesday, December  3, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
hid_t
H5Dcreate (hid_t file_id, const char *name, hid_t type_id, hid_t space_id,
	   hid_t create_parms_id)
{
   H5F_t		*f = NULL;
   H5T_t		*type = NULL;
   H5P_t		*space = NULL;
   H5D_t		*new_dset = NULL;
   hid_t		ret_value = FAIL;
   const H5D_create_t	*create_parms = NULL;
   
   FUNC_ENTER (H5Dcreate, FAIL);
   H5ECLEAR;

   /* check arguments */
   if (H5_FILE!=H5Aatom_group (file_id) ||
       NULL==(f=H5Aatom_object (file_id))) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL); /*not a file*/
   }
   if (!name || !*name) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADVALUE, FAIL); /*no name*/
   }
   if (H5_DATATYPE!=H5Aatom_group (type_id) ||
       NULL==(type=H5Aatom_object (type_id))) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL); /*not a type*/
   }
   if (H5_DATASPACE!=H5Aatom_group (space_id) ||
       NULL==(space=H5Aatom_object (space_id))) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL); /*not a data space */
   }
   if (create_parms_id>=0) {
      if (H5C_DATASET_CREATE!=H5Cget_class (create_parms_id) ||
	  NULL==(create_parms=H5Aatom_object (create_parms_id))) {
	 /* Not a dataset creation template */
	 HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL);
      }
   } else {
      create_parms = &H5D_create_dflt;
   }

   /* build and open the new dataset */
   if (NULL==(new_dset=H5D_create (f, name, type, space, create_parms))) {
      HRETURN_ERROR (H5E_DATASET, H5E_CANTINIT, FAIL); /*can't create dataset*/
   }

   /* Register the new datatype and get an ID for it */
   if ((ret_value=H5Aregister_atom (H5_DATASET, new_dset))<0) {
      H5D_close (new_dset);
      /* Can't register dataset */
      HRETURN_ERROR (H5E_DATASET, H5E_CANTREGISTER, FAIL);
   }

   FUNC_LEAVE (ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5Dopen
 *
 * Purpose:	Finds a dataset named NAME in file FILE_ID, opens it, and
 *		returns its ID.  The dataset should be close when the caller
 *		is no longer interested in it.
 *
 * Return:	Success:	A new dataset ID
 *
 *		Failure:	FAIL
 *
 * Errors:
 *		ARGS      BADTYPE       Not a file. 
 *		ARGS      BADVALUE      No name. 
 *		DATASET   CANTREGISTER  Can't register dataset. 
 *		DATASET   NOTFOUND      Dataset not found. 
 *
 * Programmer:	Robb Matzke
 *              Thursday, December  4, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
hid_t
H5Dopen (hid_t file_id, const char *name)
{
   H5F_t	*file = NULL;		/*file holding the dataset	*/
   H5D_t	*dataset = NULL;	/*the dataset			*/
   hid_t	ret_value = FAIL;

   FUNC_ENTER (H5Dopen, FAIL);
   H5ECLEAR;

   /* Check args */
   if (H5_FILE!=H5Aatom_group (file_id) ||
       NULL==(file=H5Aatom_object (file_id))) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL); /*not a file*/
   }
   if (!name || !*name) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADVALUE, FAIL); /*no name*/
   }

   /* Find the dataset */
   if (NULL==(dataset=H5D_open (file, name))) {
      HRETURN_ERROR (H5E_DATASET, H5E_NOTFOUND, FAIL); /*dataset not found*/
   }

   /* Create an atom for the dataset */
   if ((ret_value=H5Aregister_atom (H5_DATASET, dataset))<0) {
      H5D_close (dataset);
      /* Can't register dataset */
      HRETURN_ERROR (H5E_DATASET, H5E_CANTREGISTER, FAIL);
   }

   FUNC_LEAVE (ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5Dclose
 *
 * Purpose:	Closes access to a dataset (DATASET_ID) and releases
 *		resources used by it. It is illegal to subsequently use that
 *		same dataset ID in calls to other dataset functions.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Errors:
 *		ARGS      BADTYPE       Not a dataset. 
 *		DATASET   CANTINIT      Can't free. 
 *
 * Programmer:	Robb Matzke
 *              Thursday, December  4, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Dclose (hid_t dataset_id)
{
   H5D_t	*dataset = NULL;	 /* dataset object to release */

   FUNC_ENTER (H5Dclose, FAIL);
   H5ECLEAR;

   /* Check args */
   if (H5_DATASET!=H5Aatom_group (dataset_id) ||
       NULL==(dataset=H5Aatom_object (dataset_id)) ||
       NULL==dataset->file) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL); /*not a dataset*/
   }

   /*
    * Decrement the counter on the dataset.  It will be freed if the count
    * reaches zero.
    */
   if (H5A_dec_ref (dataset_id)<0) {
      HRETURN_ERROR (H5E_DATASET, H5E_CANTINIT, FAIL); /*can't free*/
   }

   FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5Dread
 *
 * Purpose:	Reads (part of) a DATASET from the file into application
 *		memory BUF. The part of the dataset to read is defined with
 *		SPACE_ID (if SPACE_ID is negative then we assume that the
 *		caller desires to read the entire dataset).  The data points
 *		are converted from their file type to the TYPE_ID specified.
 *		Additional miscellaneous data transfer properties can be
 *		passed to this function with the XFER_PARMS_ID argument.
 *
 *		The SPACE_ID can be the constant H5P_ALL in which case the
 *		destination (memory) data space is the same as the source
 *		(file) data space defined when the dataset was created.
 *
 *		The XFER_PARMS_ID can be the constant H5C_DEFAULT in which
 *		case the default data transfer properties are used.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Errors:
 *		ARGS      BADTYPE       Not a data space. 
 *		ARGS      BADTYPE       Not a data type. 
 *		ARGS      BADTYPE       Not a dataset. 
 *		ARGS      BADTYPE       Not xfer parms. 
 *		ARGS      BADVALUE      No output buffer. 
 *		DATASET   READERROR     Can't read data. 
 *
 * Programmer:	Robb Matzke
 *              Thursday, December  4, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Dread (hid_t dataset_id, hid_t type_id, hid_t space_id,
	 hid_t xfer_parms_id,  void *buf/*out*/)
{
   H5D_t		*dataset = NULL;
   const H5T_t		*type = NULL;
   const H5P_t		*space = NULL;
   const H5D_xfer_t	*xfer_parms = NULL;
   
   FUNC_ENTER (H5Dread, FAIL);
   H5ECLEAR;

   /* check arguments */
   if (H5_DATASET!=H5Aatom_group (dataset_id) ||
       NULL==(dataset=H5Aatom_object (dataset_id)) ||
       NULL==dataset->file) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL); /*not a dataset*/
   }
   if (H5_DATATYPE!=H5Aatom_group (type_id) ||
       NULL==(type=H5Aatom_object (type_id))) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL); /*not a data type*/
   }
   if (H5P_ALL!=space_id) {
      if (H5_DATASPACE!=H5Aatom_group (space_id) ||
	  NULL==(space=H5Aatom_object (space_id))) {
	 HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL); /*not a data space*/
      }
   }
   if (H5C_DEFAULT==xfer_parms_id) {
      xfer_parms = &H5D_xfer_dflt;
   } else if (H5C_DATASET_XFER!=H5Cget_class (xfer_parms_id) ||
	      NULL==(xfer_parms=H5Aatom_object (xfer_parms_id))) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL); /*not xfer parms*/
   }
   if (!buf) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADVALUE, FAIL); /*no output buffer*/
   }

   /* read raw data */
   if (H5D_read (dataset, type, space, xfer_parms, buf/*out*/)<0) {
      HRETURN_ERROR (H5E_DATASET, H5E_READERROR, FAIL); /*can't read data*/
   }
   
   FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5Dwrite
 *
 * Purpose:	Writes (part of) a DATASET from application memory BUF to the
 *		file.  The part of the dataset to write is defined with the
 *		SPACE_ID (if SPACE_ID is negative then we assume that the
 *		caller desires to write the entire dataset). The data points
 *		are converted from their current type (TYPE_ID) to their file
 *		data type.  Additional miscellaneous data transfer properties
 *		can be passed to this function with the XFER_PARMS_ID
 *		argument.
 *
 *		The SPACE_ID can be the constant H5P_ALL in which case the
 *		source (memory) data space is the same as the destination
 *		(file) memory space which was defined when the dataset was
 * 		created.
 *
 *		The XFER_PARMS_ID can be the constant H5C_DEFAULT in which
 *		case the default data transfer properties are used.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Errors:
 *
 * Programmer:	Robb Matzke
 *              Thursday, December  4, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Dwrite (hid_t dataset_id, hid_t type_id, hid_t space_id,
	  hid_t xfer_parms_id, const void *buf)
{
   H5D_t		*dataset = NULL;
   const H5T_t		*type = NULL;
   const H5P_t		*space = NULL;
   const H5D_xfer_t	*xfer_parms = NULL;
   
   FUNC_ENTER (H5Dwrite, FAIL);
   H5ECLEAR;

   /* check arguments */
   if (H5_DATASET!=H5Aatom_group (dataset_id) ||
       NULL==(dataset=H5Aatom_object (dataset_id)) ||
       NULL==dataset->file) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL); /*not a dataset*/
   }
   if (H5_DATATYPE!=H5Aatom_group (type_id) ||
       NULL==(type=H5Aatom_object (type_id))) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL); /*not a data type*/
   }
   if (H5P_ALL!=space_id) {
      if (H5_DATASPACE!=H5Aatom_group (space_id) ||
	  NULL==(space=H5Aatom_object (space_id))) {
	 HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL); /*not a data space*/
      }
   }
   if (H5C_DEFAULT==xfer_parms_id) {
      xfer_parms = &H5D_xfer_dflt;
   } else if (H5C_DATASET_XFER!=H5Cget_class (xfer_parms_id) ||
	      NULL==(xfer_parms=H5Aatom_object (xfer_parms_id))) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL); /*not xfer parms*/
   }
   if (!buf) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADVALUE, FAIL); /*no output buffer*/
   }

   /* write raw data */
   if (H5D_write (dataset, type, space, xfer_parms, buf)<0) {
      HRETURN_ERROR (H5E_DATASET, H5E_READERROR, FAIL); /*can't write data*/
   }
   
   FUNC_LEAVE (SUCCEED);
}





   

/*-------------------------------------------------------------------------
 * Function:	H5D_find_name
 *
 * Purpose:	This is a callback for H5Mfind_name().  It does the same
 *		thing as H5Dopen() except it takes an extra argument which
 *		isn't used.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Errors:
 *
 * Programmer:	Robb Matzke
 *              Thursday, December  4, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
hid_t
H5D_find_name (hid_t file_id, group_t UNUSED, const char *name)
{
   return H5Dopen (file_id, name);
}









/*-------------------------------------------------------------------------
 * Function:	H5D_create
 *
 * Purpose:	Creates a new dataset with name NAME in file F and associates
 *		with it a datatype TYPE for each element as stored in the
 *		file, dimensionality information or dataspace SPACE, and
 *		other miscellaneous properties CREATE_PARMS.  All arguments
 *		are deep-copied before being associated with the new dataset,
 *		so the caller is free to subsequently modify them without
 *		affecting the dataset.
 *
 * Return:	Success:	Pointer to a new dataset
 *
 *		Failure:	NULL
 *
 * Errors:
 *		DATASET   CANTINIT      Can't update dataset header. 
 *		DATASET   CANTINIT      Problem with the dataset name. 
 *
 * Programmer:	Robb Matzke
 *              Thursday, December  4, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
H5D_t *
H5D_create (H5F_t *f, const char *name, const H5T_t *type, const H5P_t *space,
	    const H5D_create_t *create_parms)
{
   H5D_t	*new_dset = NULL;
   H5D_t	*ret_value = NULL;
   size_t	nbytes;
   intn		ndims;

   FUNC_ENTER (H5D_create, NULL);

   /* check args */
   assert (f);
   assert (name && *name);
   assert (type);
   assert (space);
   assert (create_parms);
   
   /* Initialize the dataset object */
   new_dset = H5MM_xcalloc (1, sizeof(H5D_t));
   new_dset->file = f;
   new_dset->type = H5T_copy (type);
   new_dset->space = H5P_copy (space);
   new_dset->create_parms = *create_parms;

   /*
    * Open (and create) a new file object and update the object header
    * immediately with the new information so when others access the dataset
    * they see the most current info.  This `write through the dataset'
    * policy is used throughout this package, and because the object header
    * is cached it shouldn't cause any disk activity.
    */
   if (NULL==(new_dset->ent = H5G_create (f, name, H5D_MINHDR_SIZE))) {
      /* Problem with the dataset name */
      HGOTO_ERROR (H5E_DATASET, H5E_CANTINIT, NULL);
   }

   /* Update the type and space header messages */
   if (H5O_modify (f, NO_ADDR, new_dset->ent, H5O_DTYPE, 0,
		   new_dset->type)<0 ||
       H5P_modify (f, new_dset->ent, new_dset->space)<0) {
      /* Can't update type or space header messages */
      HGOTO_ERROR (H5E_DATASET, H5E_CANTINIT, NULL);
   }

   /* Total raw data size */
   nbytes = H5T_get_size (type) * H5P_get_npoints (space);

   /* Initialize storage */
   switch (new_dset->create_parms.layout) {
   case H5D_CONTIGUOUS:
      new_dset->storage.cstore.size = nbytes;
      if (H5MF_alloc (f, H5MF_RAW, nbytes,
		      &(new_dset->storage.cstore.addr))<0) {
	 /* Can't allocate raw file storage */
	 HGOTO_ERROR (H5E_DATASET, H5E_NOSPACE, NULL);
      }
      if (H5O_modify (f, NO_ADDR, new_dset->ent, H5O_CSTORE, 0,
		      &(new_dset->storage.cstore))<0) {
	 /* Can't update dataset object header */
	 HGOTO_ERROR (H5E_DATASET, H5E_CANTINIT, NULL);
      }
      break;

   case H5D_CHUNKED:
      /*
       * The dimensionality of the chunk should match the dimensionality of
       * the data space.  We will add one more dimension here though, to
       * describe the individual bytes of a data point.  Therefore, there
       * must be room in the template for one more dimension size.
       */
      ndims = new_dset->create_parms.chunk_ndims;
      if (ndims != H5P_get_ndims (space)) {
	 /* Dimensionality of chunks doesn't match the data space */
	 HGOTO_ERROR (H5E_DATASET, H5E_BADVALUE, NULL);
      }
      assert (ndims<NELMTS (new_dset->create_parms.chunk_size));
      new_dset->create_parms.chunk_size[ndims] = H5T_get_size (type);

      if (H5F_istore_create (f, &(new_dset->storage.istore), ndims+1,
			     new_dset->create_parms.chunk_size)<0) {
	 /* Can't initialize chunked storage */
	 HGOTO_ERROR (H5E_DATASET, H5E_CANTINIT, NULL);
      }
      if (H5O_modify (f, NO_ADDR, new_dset->ent, H5O_ISTORE, 0,
		      &(new_dset->storage.istore))<0) {
	 /* Can't update dataset object header */
	 HGOTO_ERROR (H5E_DATASET, H5E_CANTINIT, NULL);
      }
      break;
      
   default:
      assert ("not implemented yet" && 0);
      HGOTO_ERROR (H5E_DATASET, H5E_UNSUPPORTED, NULL);
   }

   /* Success */
   ret_value = new_dset;
   
   
 done:
   if (!ret_value && new_dset) {
      if (new_dset->type) H5T_close (new_dset->type);
      if (new_dset->space) H5P_close (new_dset->space);
      if (new_dset->ent) H5G_close (f, new_dset->ent);
      new_dset->file = NULL;
      H5MM_xfree (new_dset);
   }
   
   FUNC_LEAVE (ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5D_open
 *
 * Purpose:	Finds a dataset named NAME in file F and builds a descriptor
 *		for it, opening it for access.
 *
 * Return:	Success:	Pointer to a new dataset descriptor.
 *
 *		Failure:	NULL
 *
 * Errors:
 *
 * Programmer:	Robb Matzke
 *              Thursday, December  4, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
H5D_t *
H5D_open (H5F_t *f, const char *name)
{
   H5D_t	*dataset = NULL;	/*the dataset which was found	*/
   H5D_t	*ret_value = NULL;	/*return value			*/
   H5O_istore_t	*istore = NULL;
   intn		i;

   FUNC_ENTER (H5D_open, NULL);

   /* check args */
   assert (f);
   assert (name && *name);

   dataset = H5MM_xcalloc (1, sizeof(H5D_t));
   dataset->file = f;
   dataset->create_parms = H5D_create_dflt;
    
   /* Open the dataset object */
   if (NULL==(dataset->ent=H5G_open (f, name))) {
      HGOTO_ERROR (H5E_DATASET, H5E_NOTFOUND, NULL); /*not found*/
   }

   /* Get the type and space */
   if (NULL==(dataset->type = H5O_read (f, NO_ADDR, dataset->ent, H5O_DTYPE,
					0, NULL)) ||
       NULL==(dataset->space = H5P_read (f, dataset->ent))) {
      /* Can't load type of space info from dataset header */
      HGOTO_ERROR (H5E_DATASET, H5E_CANTINIT, NULL);
   }

   /*
    * Get the raw data storage info.  It's actually stored in two locations:
    * the storage message of the dataset (dataset->storage) and certain
    * values are copied to the dataset create template so the user can query
    * them.
    */
   if (H5O_read (dataset->file, NO_ADDR, dataset->ent, H5O_CSTORE, 0,
		 &(dataset->storage.cstore))) {
      /* Contiguous storage */
      dataset->create_parms.layout = H5D_CONTIGUOUS;
      
   } else if (H5O_read (dataset->file, NO_ADDR, dataset->ent, H5O_ISTORE, 0,
			&(dataset->storage.istore))) {
      /*
       * Chunked storage.  The creation template's dimension is one less than
       * the chunk dimension because the chunk includes a dimension for the
       * individual bytes of the data type.
       */
      istore = &(dataset->storage.istore);
      dataset->create_parms.layout = H5D_CHUNKED;
      dataset->create_parms.chunk_ndims = istore->ndims - 1;
      assert (istore->ndims<=NELMTS (dataset->create_parms.chunk_size));
      for (i=0; i<istore->ndims-1; i++) {
	 dataset->create_parms.chunk_size[i] = istore->alignment[i];
      }
      
   } else {
      assert ("not implemented yet" && 0);
      HGOTO_ERROR (H5E_DATASET, H5E_UNSUPPORTED, NULL);
   }

   /* Success */
   ret_value = dataset;
   

 done:
   if (!ret_value && dataset) {
      if (dataset->ent) H5G_close (f, dataset->ent);
      if (dataset->type) H5T_close (dataset->type);
      if (dataset->space) H5P_close (dataset->space);
      dataset->file = NULL;
      H5MM_xfree (dataset);
   }
   
   FUNC_LEAVE (ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5D_close
 *
 * Purpose:	Insures that all data has been saved to the file, closes the
 *		dataset object header, and frees all resources used by the
 *		descriptor.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Errors:
 *		DATASET   CANTINIT      Couldn't free the type or space,
 *		                        but the dataset was freed anyway. 
 *
 * Programmer:	Robb Matzke
 *              Thursday, December  4, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5D_close (H5D_t *dataset)
{
   hbool_t	free_failed;
   
   FUNC_ENTER (H5D_close, FAIL);

   /* check args */
   assert (dataset && dataset->file);
   
   /* Close the dataset object */
   H5G_close (dataset->file, dataset->ent);
   dataset->ent = NULL;

   /*
    * Release dataset type and space - there isn't much we can do if one of
    * these fails, so we just continue.
    */
   free_failed = (H5T_close (dataset->type)<0 ||
		  H5P_close (dataset->space)<0);

   /*
    * Free memory.  Before freeing the memory set the file pointer to NULL.
    * We always check for a null file pointer in other H5D functions to be
    * sure we're not accessing an already freed dataset (see the assert()
    * above).
    */
   dataset->file = NULL;
   H5MM_xfree (dataset);

   if (free_failed) {
      /* Couldn't free the type or space, but the dataset was freed anyway. */
      HRETURN_ERROR (H5E_DATASET, H5E_CANTINIT, FAIL);
   }

   FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5D_read
 *
 * Purpose:	Reads (part of) a DATASET into application memory BUF. The
 *		SPACE argument determines what part of the dataset to read
 *		(the whole thing is read if SPACE is null) and individual
 *		data points are translated from their file data type to the
 *		specified TYPE.  The XFER_PARMS contains additional
 *		miscellaneous properties that control the data transfer.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Thursday, December  4, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5D_read (H5D_t *dataset, const H5T_t *type, const H5P_t *space,
	  const H5D_xfer_t *xfer_parms, void *buf/*out*/)
{
   size_t	nbytes;
   size_t	offset[H5O_ISTORE_NDIMS];
   size_t	size[H5O_ISTORE_NDIMS];
   intn		i;
   
   FUNC_ENTER (H5D_read, FAIL);

   /* check args */
   assert (dataset && dataset->file);
   assert (type);
   assert (xfer_parms);
   assert (buf);

   if (H5D_CONTIGUOUS!=dataset->create_parms.layout) {
      /* Layout is not supported yet */
      HRETURN_ERROR (H5E_DATASET, H5E_UNSUPPORTED, FAIL);
   }
   if (H5T_cmp (type, dataset->type)) {
      /* Type conversion not supported yet */
      HRETURN_ERROR (H5E_DATASET, H5E_UNSUPPORTED, FAIL);
   }
   if (space && H5P_cmp (space, dataset->space)) {
      /* Space conversion not supported yet */
      HRETURN_ERROR (H5E_DATASET, H5E_UNSUPPORTED, FAIL);
   }

   /* Compute the size of the request */
   nbytes = H5T_get_size (dataset->type) * H5P_get_npoints (dataset->space);

   switch (dataset->create_parms.layout) {
   case H5D_CONTIGUOUS:
      /*
       * Read a block of contiguous data.
       */
      if (H5F_block_read (dataset->file, &(dataset->storage.cstore.addr),
			  nbytes, buf)<0) {
	 HRETURN_ERROR (H5E_IO, H5E_READERROR, FAIL); /*read failed*/
      }
      break;

   case H5D_CHUNKED:
      /*
       * Read one or more chunks from indexed storage.
       */
      for (i=0; i<dataset->storage.istore.ndims; i++) offset[i] = 0;
      H5P_get_dims (dataset->space, size);
      size[dataset->storage.istore.ndims-1] = H5T_get_size (dataset->type);
      if (H5F_istore_read (dataset->file, &(dataset->storage.istore),
			   offset, size, buf)<0) {
	 HRETURN_ERROR (H5E_IO, H5E_READERROR, FAIL); /*read failed*/
      }
      break;

   default:
      assert ("not implemented yet" && 0);
      HRETURN_ERROR (H5E_INTERNAL, H5E_UNSUPPORTED, FAIL);
   }

   FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5D_write
 *
 * Purpose:	Writes (part of) a DATASET to a file from application memory
 *		BUF. The SPACE argument determines what part of the dataset
 *		to write (the whole thing is read if SPACE is null) and
 *		individual data points are translated from their memory data
 *		type (TYPE) to the file data type.  The XFER_PARMS contains
 *		additional miscellaneous properties that control the data
 *		transfer.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Thursday, December  4, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5D_write (H5D_t *dataset, const H5T_t *type, const H5P_t *space,
	   const H5D_xfer_t *xfer_parms, const void *buf)
{
   size_t	nbytes;
   size_t	offset[H5O_ISTORE_NDIMS];
   size_t	size[H5O_ISTORE_NDIMS];
   intn		i;
   
   FUNC_ENTER (H5D_write, FAIL);

   /* check args */
   assert (dataset && dataset->file);
   assert (type);
   assert (xfer_parms);
   assert (buf);

   if (H5D_CONTIGUOUS!=dataset->create_parms.layout) {
      /* Layout is not supported yet */
      HRETURN_ERROR (H5E_DATASET, H5E_UNSUPPORTED, FAIL);
   }
   if (H5T_cmp (type, dataset->type)) {
      /* Type conversion not supported yet */
      HRETURN_ERROR (H5E_DATASET, H5E_UNSUPPORTED, FAIL);
   }
   if (space && H5P_cmp (space, dataset->space)) {
      /* Space conversion not supported yet */
      HRETURN_ERROR (H5E_DATASET, H5E_UNSUPPORTED, FAIL);
   }

   /* Compute the size of the request */
   nbytes = H5T_get_size (dataset->type) * H5P_get_npoints (dataset->space);


   switch (dataset->create_parms.layout) {
   case H5D_CONTIGUOUS:
      /*
       * Write a contiguous chunk of data.
       */
      if (H5F_block_write (dataset->file, &(dataset->storage.cstore.addr),
			   nbytes, buf)<0) {
	 HRETURN_ERROR (H5E_IO, H5E_WRITEERROR, FAIL); /*write failed*/
      }
      break;

   case H5D_CHUNKED:
      /*
       * Write one or more chunks to indexed storage.
       */
      for (i=0; i<dataset->storage.istore.ndims; i++) offset[i] = 0;
      H5P_get_dims (dataset->space, size);
      size[dataset->storage.istore.ndims-1] = H5T_get_size (dataset->type);
      if (H5F_istore_write (dataset->file, &(dataset->storage.istore),
			    offset, size, buf)<0) {
	 HRETURN_ERROR (H5E_IO, H5E_WRITEERROR, FAIL); /*write failed*/
      }
      break;

   default:
      assert ("not implemented yet" && 0);
      HRETURN_ERROR (H5E_INTERNAL, H5E_UNSUPPORTED, FAIL);
   }
   
   FUNC_LEAVE (SUCCEED);
}