Skip to content

Commit 705a05e

Browse files
authored
More NetCDF functionality (#2082)
1 parent 5fa68f4 commit 705a05e

File tree

3 files changed

+159
-36
lines changed

3 files changed

+159
-36
lines changed

src/hdf5_fun.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ namespace lib {
557557
field = new DULongGDL(dim);
558558
} else if (ourType == GDL_LONG64) {
559559
field = new DLong64GDL(dim);
560-
} else if (ourType == GDL_LONG64) {
560+
} else if (ourType == GDL_ULONG64) {
561561
field = new DULong64GDL(dim);
562562
} else if (ourType == GDL_FLOAT) {
563563
field = new DFloatGDL(dim);

src/ncdf_cl.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ namespace lib {
4545
switch (vartype)
4646
{
4747
case NC_BYTE: return DStringGDL("BYTE");//8 bit
48-
case NC_UBYTE: return DStringGDL("UBYTE");//8 bit
48+
case NC_UBYTE: return DStringGDL("UBYTE");//8 bit
4949
case NC_CHAR: return DStringGDL("CHAR");//8 bit as string
5050
case NC_SHORT: return DStringGDL("INT");//16 bit
51-
case NC_USHORT: return DStringGDL("UINT");//16 bit
51+
case NC_USHORT:return DStringGDL("UINT");//16 bit
5252
case NC_INT: return DStringGDL("LONG");//32 bit
53-
case NC_UINT: return DStringGDL("ULONG");//32 bit
53+
case NC_UINT: return DStringGDL("ULONG");//32 bit
54+
case NC_INT64: return DStringGDL("LONG64");//64 bit
55+
case NC_UINT64:return DStringGDL("ULONG64");//64 bit
5456
case NC_FLOAT: return DStringGDL("FLOAT");//32 bit float
5557
case NC_DOUBLE:return DStringGDL("DOUBLE");//64 bit double
5658
case NC_STRING:return DStringGDL("STRING");//String

src/ncdf_var_cl.cpp

Lines changed: 153 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,28 @@ else if(var_type == NC_LONG)
312312
GDLDelete(e->GetParGlobal(2));
313313
e->GetParGlobal(2) = new DULongGDL((uivar));
314314
}
315+
else if(var_type == NC_INT64)
316+
{
317+
318+
int ivar;
319+
status=nc_get_var1_int(cdfid,
320+
varid,
321+
index,
322+
&ivar);
323+
GDLDelete(e->GetParGlobal(2));
324+
e->GetParGlobal(2) = new DLong64GDL((ivar));
325+
}
326+
else if(var_type == NC_UINT64)
327+
{
328+
329+
unsigned int uivar;
330+
status=nc_get_var1_uint(cdfid,
331+
varid,
332+
index,
333+
&uivar);
334+
GDLDelete(e->GetParGlobal(2));
335+
e->GetParGlobal(2) = new DULong64GDL((uivar));
336+
}
315337
else if(var_type == NC_SHORT)
316338
{
317339

@@ -351,6 +373,13 @@ else if(var_type == NC_LONG)
351373
GDLDelete(e->GetParGlobal(2));
352374
e->GetParGlobal(2) = new DByteGDL((bvar));
353375
}
376+
else if (var_type == NC_STRING) {
377+
char *stemp;
378+
status = nc_get_var1_string(cdfid, varid, index, &stemp);
379+
GDLDelete(e->GetParGlobal(2));
380+
e->GetParGlobal(2)=new DStringGDL(stemp);
381+
free(stemp);
382+
}
354383

355384
ncdf_handle_error(e,status,"NCDF_VARGET1");
356385
}
@@ -400,7 +429,6 @@ else if(var_type == NC_LONG)
400429
transposed_dim_length[trans[i]] = dim_length[i];
401430
ncdf_handle_error(e, status, "NCDF_VARGET");
402431
}
403-
404432
// for (int i = 0; i < var_ndims; ++i) index[i]=0;//defaults
405433

406434
/*Here we have the minimum required details for getting any kind of data
@@ -482,6 +510,22 @@ else if(var_type == NC_LONG)
482510
GDLDelete(e->GetParGlobal(2));
483511
e->GetParGlobal(2)=temp;
484512
}
513+
else if (var_type == NC_INT64)
514+
{
515+
DLong64GDL* temp=new DLong64GDL(dim,BaseGDL::NOZERO);
516+
status=nc_get_var_longlong(cdfid, varid,&(*temp)[0]);
517+
ncdf_var_handle_error(e, status, "NCDF_VARGET", temp);
518+
GDLDelete(e->GetParGlobal(2));
519+
e->GetParGlobal(2)=temp;
520+
}
521+
else if (var_type == NC_UINT64)
522+
{
523+
DULong64GDL* temp=new DULong64GDL(dim,BaseGDL::NOZERO);
524+
status=nc_get_var_ulonglong(cdfid, varid,&(*temp)[0]);
525+
ncdf_var_handle_error(e, status, "NCDF_VARGET", temp);
526+
GDLDelete(e->GetParGlobal(2));
527+
e->GetParGlobal(2)=temp;
528+
}
485529
else if (var_type == NC_BYTE || var_type == NC_UBYTE )
486530
{
487531
DByteGDL* temp=new DByteGDL(dim,BaseGDL::NOZERO);
@@ -506,6 +550,24 @@ else if(var_type == NC_LONG)
506550
GDLDelete(e->GetParGlobal(2));
507551
e->GetParGlobal(2) = temp;
508552
}
553+
else if (var_type == NC_STRING)
554+
{
555+
// Read NC_STRING
556+
// Below should read an array of strings into char** stmp
557+
char **stemp = new char*[array_size];
558+
status = nc_get_var_string(cdfid, varid, stemp);
559+
ncdf_handle_error(e, status, "NCDF_VARGET");
560+
// Need to copy strings in stemp to the returned structure...
561+
GDLDelete(e->GetParGlobal(2));
562+
SizeT num_elems=array_size;
563+
dimension flat_dim(&num_elems, 1);
564+
BaseGDL *str_arr = new DStringGDL(flat_dim);
565+
// assign array pointers
566+
for (size_t i=0; i<array_size; i++) (*(static_cast<DStringGDL*> (str_arr)))[i] = stemp[i];
567+
// re-shape array & add as tag
568+
(static_cast<BaseGDL*>(str_arr))->SetDim(dim);
569+
e->GetParGlobal(2)=str_arr;
570+
}
509571
//done read all.
510572
}
511573
else
@@ -628,6 +690,22 @@ else if(var_type == NC_LONG)
628690
GDLDelete(e->GetParGlobal(2));
629691
e->GetParGlobal(2) = temp;
630692
}
693+
else if(var_type == NC_INT64)
694+
{
695+
DLong64GDL *temp = new DLong64GDL(dim,BaseGDL::NOZERO);
696+
status = nc_get_vara_longlong(cdfid, varid, off, cou, &(*temp)[0]);
697+
ncdf_var_handle_error(e, status, "NCDF_VARGET", temp);
698+
GDLDelete(e->GetParGlobal(2));
699+
e->GetParGlobal(2) = temp;
700+
}
701+
else if(var_type == NC_UINT64)
702+
{
703+
DULong64GDL *temp = new DULong64GDL(dim,BaseGDL::NOZERO);
704+
status = nc_get_vara_ulonglong(cdfid, varid, off, cou, &(*temp)[0]);
705+
ncdf_var_handle_error(e, status, "NCDF_VARGET", temp);
706+
GDLDelete(e->GetParGlobal(2));
707+
e->GetParGlobal(2) = temp;
708+
}
631709
else if (var_type == NC_BYTE || var_type == NC_UBYTE )
632710
{
633711
DByteGDL *temp=new DByteGDL(dim,BaseGDL::NOZERO);
@@ -644,6 +722,24 @@ else if(var_type == NC_LONG)
644722
GDLDelete(e->GetParGlobal(2));
645723
e->GetParGlobal(2) = temp;
646724
}
725+
else if (var_type == NC_STRING)
726+
{
727+
// Read NC_STRING
728+
// Below should read an array of strings into char** stmp
729+
char **stemp = new char*[array_size];
730+
status = nc_get_vara_string(cdfid, varid, off, cou, stemp);
731+
ncdf_handle_error(e, status, "NCDF_VARGET");
732+
// Need to copy strings in stemp to the returned structure...
733+
GDLDelete(e->GetParGlobal(2));
734+
SizeT num_elems=array_size;
735+
dimension flat_dim(&num_elems, 1);
736+
BaseGDL *str_arr = new DStringGDL(flat_dim);
737+
// assign array pointers
738+
for (size_t i=0; i<array_size; i++) (*(static_cast<DStringGDL*> (str_arr)))[i] = stemp[i];
739+
// re-shape array & add as tag
740+
(static_cast<BaseGDL*>(str_arr))->SetDim(dim);
741+
e->GetParGlobal(2)=str_arr;
742+
}
647743
}
648744
else
649745
{
@@ -747,6 +843,22 @@ else if(var_type == NC_LONG)
747843
GDLDelete(e->GetParGlobal(2));
748844
e->GetParGlobal(2) = temp;
749845
}
846+
else if(var_type == NC_INT64)
847+
{
848+
DLong64GDL *temp = new DLong64GDL(dim, BaseGDL::NOZERO);
849+
status = nc_get_vars_longlong(cdfid, varid, off,cou, stri, &(*temp)[0]);
850+
ncdf_var_handle_error(e, status, "NCDF_VARGET", temp);
851+
GDLDelete(e->GetParGlobal(2));
852+
e->GetParGlobal(2) = temp;
853+
}
854+
else if(var_type == NC_UINT64)
855+
{
856+
DULong64GDL *temp = new DULong64GDL(dim, BaseGDL::NOZERO);
857+
status = nc_get_vars_ulonglong(cdfid, varid, off,cou, stri, &(*temp)[0]);
858+
ncdf_var_handle_error(e, status, "NCDF_VARGET", temp);
859+
GDLDelete(e->GetParGlobal(2));
860+
e->GetParGlobal(2) = temp;
861+
}
750862
else if(var_type == NC_BYTE || var_type == NC_UBYTE )
751863
{
752864
DByteGDL *temp=new DByteGDL(dim, BaseGDL::NOZERO);
@@ -763,6 +875,24 @@ else if(var_type == NC_LONG)
763875
GDLDelete(e->GetParGlobal(2));
764876
e->GetParGlobal(2) = temp;
765877
}
878+
else if (var_type == NC_STRING)
879+
{
880+
// Read NC_STRING
881+
// Below should read an array of strings into char** stmp
882+
char **stemp = new char*[array_size];
883+
status = nc_get_vars_string(cdfid, varid, off, cou, stri, stemp);
884+
ncdf_handle_error(e, status, "NCDF_VARGET");
885+
// Need to copy strings in stemp to the returned structure...
886+
GDLDelete(e->GetParGlobal(2));
887+
SizeT num_elems=array_size;
888+
dimension flat_dim(&num_elems, 1);
889+
BaseGDL *str_arr = new DStringGDL(flat_dim);
890+
// assign array pointers
891+
for (size_t i=0; i<array_size; i++) (*(static_cast<DStringGDL*> (str_arr)))[i] = stemp[i];
892+
// re-shape array & add as tag
893+
(static_cast<BaseGDL*>(str_arr))->SetDim(dim);
894+
e->GetParGlobal(2)=str_arr;
895+
}
766896
}
767897
}
768898
}
@@ -1076,56 +1206,46 @@ else if(var_type == NC_LONG)
10761206
status = nc_put_vars_short(cdfid, varid, offset, count, stride,
10771207
&((*static_cast<DIntGDL*>(v))[0]));
10781208
break;
1209+
case GDL_UINT :
1210+
status = nc_put_vars_ushort(cdfid, varid, offset, count, stride,
1211+
&((*static_cast<DUIntGDL*>(v))[0]));
1212+
break;
10791213
case GDL_LONG :
10801214
status = nc_put_vars_int(cdfid, varid, offset, count, stride,
10811215
&((*static_cast<DLongGDL*>(v))[0]));
10821216
break;
1083-
case GDL_BYTE :
1084-
status = nc_put_vars_uchar(cdfid, varid, offset, count, stride,
1085-
&((*static_cast<DByteGDL*>(v))[0]));
1217+
case GDL_ULONG :
1218+
status = nc_put_vars_uint(cdfid, varid, offset, count, stride,
1219+
&((*static_cast<DULongGDL*>(v))[0]));
1220+
break;
1221+
case GDL_LONG64 :
1222+
status = nc_put_vars_longlong(cdfid, varid, offset, count, stride,
1223+
&((*static_cast<DLong64GDL*>(v))[0]));
10861224
break;
1087-
// initially using GDL methods for data type convertion
1225+
case GDL_ULONG64 :
1226+
status = nc_put_vars_ulonglong(cdfid, varid, offset, count, stride,
1227+
&((*static_cast<DULong64GDL*>(v))[0]));
1228+
break;
1229+
case GDL_BYTE :
10881230
case GDL_COMPLEXDBL :
10891231
case GDL_COMPLEX :
1090-
case GDL_UINT :
1091-
case GDL_ULONG :
1092-
case GDL_LONG64:
1093-
case GDL_ULONG64 :
10941232
{
10951233
BaseGDL* val;
10961234
Guard<BaseGDL> val_guard;
10971235
switch (var_type)
10981236
{
1099-
case NC_BYTE : // 8-bit signed integer
1100-
case NC_SHORT : // 16-bit signed integer
1101-
val = v->Convert2(GDL_INT, BaseGDL::COPY);
1102-
val_guard.Init(val);
1103-
status = nc_put_vars_short(cdfid, varid, offset, count, stride,
1104-
&((*static_cast<DIntGDL*>(val))[0]));
1105-
break;
1237+
case NC_UBYTE : // 8-bit unsigned integer
11061238
case NC_CHAR : // 8-bit unsigned integer
11071239
val = v->Convert2(GDL_BYTE, BaseGDL::COPY);
11081240
val_guard.Init(val);
11091241
status = nc_put_vars_uchar(cdfid, varid, offset, count, stride,
11101242
&((*static_cast<DByteGDL*>(val))[0]));
11111243
break;
1112-
case NC_INT : // 32-bit signed integer
1113-
val = v->Convert2(GDL_LONG, BaseGDL::COPY);
1114-
val_guard.Init(val);
1115-
status = nc_put_vars_int(cdfid, varid, offset, count, stride,
1116-
&((*static_cast<DLongGDL*>(val))[0]));
1117-
break;
1118-
case NC_FLOAT : // 32-bit floating point
1119-
val = v->Convert2(GDL_FLOAT, BaseGDL::COPY);
1120-
val_guard.Init(val);
1121-
status = nc_put_vars_float(cdfid, varid, offset, count, stride,
1122-
&((*static_cast<DFloatGDL*>(val))[0]));
1123-
break;
1124-
case NC_DOUBLE : // 64-bit floating point
1125-
val = v->Convert2(GDL_DOUBLE, BaseGDL::COPY);
1244+
case NC_BYTE : // 8-bit signed integer - IDL does not have one, but try to behave like IDL and write to the data type anyway...
1245+
val = v->Convert2(GDL_BYTE, BaseGDL::COPY);
11261246
val_guard.Init(val);
1127-
status = nc_put_vars_double(cdfid, varid, offset, count, stride,
1128-
&((*static_cast<DDoubleGDL*>(val))[0]));
1247+
status = nc_put_vars_schar(cdfid, varid, offset, count, stride,
1248+
(signed char*)&((*static_cast<DByteGDL*>(val))[0]));
11291249
break;
11301250
}
11311251
break;
@@ -1162,6 +1282,7 @@ else if(var_type == NC_LONG)
11621282
e->Throw("GDL internal error, please report!"
11631283
+ e->GetParString(2));
11641284
}
1285+
if ( status == NC_ERANGE ) status = NC_NOERR; // Ignore data-type range errors (like IDL does)
11651286
ncdf_handle_error(e, status, "NCDF_VARPUT");
11661287
}
11671288

0 commit comments

Comments
 (0)