@@ -7,146 +7,132 @@ Multidimensional raster API tutorial
77Read the content of an array
88----------------------------
99
10- In C++
11- ++++++
12-
13- .. code-block :: c++
14-
15- #include "gdal_priv.h"
16- int main()
17- {
18- GDALAllRegister();
19- auto poDataset = std::unique_ptr<GDALDataset>(
20- GDALDataset::Open( "in.nc", GDAL_OF_MULTIDIM_RASTER ));
21- if( !poDataset )
22- {
23- exit(1);
24- }
25- auto poRootGroup = poDataset->GetRootGroup();
26- if( !poRootGroup )
27- {
28- exit(1);
29- }
30- auto poVar = poRootGroup->OpenMDArray("temperature");
31- if( !poVar )
32- {
33- exit(1);
34- }
35- size_t nValues = 1;
36- std::vector<size_t> anCount;
37- for( const auto poDim: poVar->GetDimensions() )
38- {
39- anCount.push_back(static_cast<size_t>(poDim->GetSize()));
40- nValues *= anCount.back();
41- }
42- std::vector<double> values(nValues);
43- poVar->Read(std::vector<GUInt64>{0,0,0}.data(),
44- anCount.data(),
45- nullptr, / * step: defaults to 1,1,1 */
46- nullptr, / * stride: default to row-major convention */
47- GDALExtendedDataType::Create(GDT_Float64),
48- &values[0]);
49- return 0;
50- }
51-
52- In C
53- ++++
54-
55- .. code-block :: c
56-
57- #include "gdal.h"
58- #include "cpl_conv.h"
59- int main()
60- {
61- GDALDatasetH hDS;
62- GDALGroupH hGroup;
63- GDALMDArrayH hVar;
64- size_t nDimCount;
65- GDALDimensionH* dims;
66- size_t nValues;
67- size_t i;
68- size_t* panCount;
69- GUInt64* panOffset;
70- double* padfValues;
71- GDALExtendedDataTypeH hDT;
72-
73- GDALAllRegister();
74- hDS = GDALOpenEx( "in.nc", GDAL_OF_MULTIDIM_RASTER, NULL, NULL, NULL);
75- if( !hDS )
76- {
77- exit(1);
78- }
79- hGroup = GDALDatasetGetRootGroup(hDS);
80- GDALReleaseDataset(hDS);
81- if( !hGroup )
82- {
83- exit(1);
84- }
85- hVar = GDALGroupOpenMDArray(hGroup, "temperature", NULL);
86- GDALGroupRelease(hGroup);
87- if( !hVar )
88- {
89- exit(1);
90- }
91-
92- dims = GDALMDArrayGetDimensions(hVar, &nDimCount);
93- panCount = (size_t*)CPLMalloc(nDimCount * sizeof(size_t));
94- nValues = 1;
95- for( i = 0; i < nDimCount; i++ )
96- {
97- panCount[i] = GDALDimensionGetSize(dims[i]);
98- nValues *= panCount[i];
99- }
100- GDALReleaseDimensions(dims, nDimCount);
101- panOffset = (GUInt64*)CPLCalloc(nDimCount, sizeof(GUInt64));
102-
103- padfValues = (double*)VSIMalloc2(nValues, sizeof(double));
104- if( !padfValues )
105- {
106- GDALMDArrayRelease(hVar);
107- CPLFree(panOffset);
108- CPLFree(panCount);
109- exit(1);
110- }
111- hDT = GDALExtendedDataTypeCreate(GDT_Float64);
112- GDALMDArrayRead(hVar,
113- panOffset,
114- panCount,
115- NULL, /* step: defaults to 1,1,1 */
116- NULL, /* stride: default to row-major convention */
117- hDT,
118- padfValues,
119- NULL, /* array start. Omitted */
120- 0 /* array size in bytes. Omitted */);
121- GDALExtendedDataTypeRelease(hDT);
122- GDALMDArrayRelease(hVar);
123- CPLFree(panOffset);
124- CPLFree(panCount);
125- VSIFree(padfValues);
126-
127- return 0;
128- }
129-
130- In Python
131- +++++++++
132-
133- .. code-block :: python
134-
135- from osgeo import gdal
136- ds = gdal.OpenEx(" in.nc" , gdal.OF_MULTIDIM_RASTER )
137- rootGroup = ds.GetRootGroup()
138- var = rootGroup.OpenMDArray(" temperature" )
139- data = var.Read(buffer_datatype = gdal.ExtendedDataType.Create(gdal.GDT_Float64 ))
140-
141- If NumPy is available:
142-
143- .. code-block :: python
144-
145- from osgeo import gdal
146- ds = gdal.OpenEx(" in.nc" , gdal.OF_MULTIDIM_RASTER )
147- rootGroup = ds.GetRootGroup()
148- var = rootGroup.OpenMDArray(" temperature" )
149- data = var.ReadAsArray(buffer_datatype = gdal.ExtendedDataType.Create(gdal.GDT_Float64 ))
10+ .. tabs ::
11+
12+ .. code-tab :: c++
13+
14+ #include "gdal_priv.h"
15+ int main()
16+ {
17+ GDALAllRegister();
18+ auto poDataset = std::unique_ptr<GDALDataset>(
19+ GDALDataset::Open( "in.nc", GDAL_OF_MULTIDIM_RASTER ));
20+ if( !poDataset )
21+ {
22+ exit(1);
23+ }
24+ auto poRootGroup = poDataset->GetRootGroup();
25+ if( !poRootGroup )
26+ {
27+ exit(1);
28+ }
29+ auto poVar = poRootGroup->OpenMDArray("temperature");
30+ if( !poVar )
31+ {
32+ exit(1);
33+ }
34+ size_t nValues = 1;
35+ std::vector<size_t> anCount;
36+ for( const auto poDim: poVar->GetDimensions() )
37+ {
38+ anCount.push_back(static_cast<size_t>(poDim->GetSize()));
39+ nValues *= anCount.back();
40+ }
41+ std::vector<double> values(nValues);
42+ poVar->Read(std::vector<GUInt64>{0,0,0}.data(),
43+ anCount.data(),
44+ nullptr, / * step: defaults to 1,1,1 */
45+ nullptr, / * stride: default to row-major convention */
46+ GDALExtendedDataType::Create(GDT_Float64),
47+ &values[0]);
48+ return 0;
49+ }
50+
51+ .. code-tab :: c
52+
53+ #include "gdal.h"
54+ #include "cpl_conv.h"
55+ int main()
56+ {
57+ GDALDatasetH hDS;
58+ GDALGroupH hGroup;
59+ GDALMDArrayH hVar;
60+ size_t nDimCount;
61+ GDALDimensionH* dims;
62+ size_t nValues;
63+ size_t i;
64+ size_t* panCount;
65+ GUInt64* panOffset;
66+ double* padfValues;
67+ GDALExtendedDataTypeH hDT;
68+
69+ GDALAllRegister();
70+ hDS = GDALOpenEx( "in.nc", GDAL_OF_MULTIDIM_RASTER, NULL, NULL, NULL);
71+ if( !hDS )
72+ {
73+ exit(1);
74+ }
75+ hGroup = GDALDatasetGetRootGroup(hDS);
76+ GDALReleaseDataset(hDS);
77+ if( !hGroup )
78+ {
79+ exit(1);
80+ }
81+ hVar = GDALGroupOpenMDArray(hGroup, "temperature", NULL);
82+ GDALGroupRelease(hGroup);
83+ if( !hVar )
84+ {
85+ exit(1);
86+ }
87+
88+ dims = GDALMDArrayGetDimensions(hVar, &nDimCount);
89+ panCount = (size_t*)CPLMalloc(nDimCount * sizeof(size_t));
90+ nValues = 1;
91+ for( i = 0; i < nDimCount; i++ )
92+ {
93+ panCount[i] = GDALDimensionGetSize(dims[i]);
94+ nValues *= panCount[i];
95+ }
96+ GDALReleaseDimensions(dims, nDimCount);
97+ panOffset = (GUInt64 *)CPLCalloc(nDimCount, sizeof(GUInt64));
98+
99+ padfValues = (double*)VSIMalloc2(nValues, sizeof(double));
100+ if( !padfValues )
101+ {
102+ GDALMDArrayRelease(hVar);
103+ CPLFree(panOffset);
104+ CPLFree(panCount);
105+ exit(1);
106+ }
107+ hDT = GDALExtendedDataTypeCreate(GDT_Float64);
108+ GDALMDArrayRead(hVar,
109+ panOffset,
110+ panCount,
111+ NULL, /* step: defaults to 1,1,1 */
112+ NULL, / * stride: default to row-major convention */
113+ hDT,
114+ padfValues,
115+ NULL, / * array start. Omitted */
116+ 0 / * array size in bytes. Omitted */);
117+ GDALExtendedDataTypeRelease(hDT);
118+ GDALMDArrayRelease(hVar);
119+ CPLFree(panOffset);
120+ CPLFree(panCount);
121+ VSIFree(padfValues);
122+
123+ return 0;
124+ }
125+
126+ .. code-tab :: python
127+
128+ from osgeo import gdal
129+ ds = gdal.OpenEx("in.nc", gdal.OF_MULTIDIM_RASTER)
130+ rootGroup = ds.GetRootGroup()
131+ var = rootGroup.OpenMDArray("temperature")
132+ data = var.Read(buffer_datatype = gdal.ExtendedDataType.Create(gdal.GDT_Float64))
133+
134+ # if NumPy is available we can use ReadAsArray
135+ data = var.ReadAsArray(buffer_datatype = gdal.ExtendedDataType.Create(gdal.GDT_Float64))
150136
151137
152138Other examples
0 commit comments