Skip to content

Commit 074cd5c

Browse files
authored
Merge pull request #2586 from jedwards4b/jedwards/remove_netcdf4_requirement_for_udf
make UDF0 not require NC_NETCDF4
2 parents d29edee + efc1e70 commit 074cd5c

3 files changed

Lines changed: 87 additions & 43 deletions

File tree

libdispatch/dfile.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ int
125125
nc_def_user_format(int mode_flag, NC_Dispatch *dispatch_table, char *magic_number)
126126
{
127127
/* Check inputs. */
128-
if (mode_flag != NC_UDF0 && mode_flag != NC_UDF1)
129-
return NC_EINVAL;
130128
if (!dispatch_table)
131129
return NC_EINVAL;
132130
if (magic_number && strlen(magic_number) > NC_MAX_MAGIC_NUMBER_LEN)
@@ -135,21 +133,29 @@ nc_def_user_format(int mode_flag, NC_Dispatch *dispatch_table, char *magic_numbe
135133
/* Check the version of the dispatch table provided. */
136134
if (dispatch_table->dispatch_version != NC_DISPATCH_VERSION)
137135
return NC_EINVAL;
138-
136+
/* user defined magic numbers not allowed with netcdf3 modes */
137+
if (magic_number && (fIsSet(mode_flag, NC_64BIT_OFFSET) ||
138+
fIsSet(mode_flag, NC_64BIT_DATA) ||
139+
(fIsSet(mode_flag, NC_CLASSIC_MODEL) &&
140+
!fIsSet(mode_flag, NC_NETCDF4))))
141+
return NC_EINVAL;
139142
/* Retain a pointer to the dispatch_table and a copy of the magic
140143
* number, if one was provided. */
141-
switch(mode_flag)
144+
if (fIsSet(mode_flag,NC_UDF0))
142145
{
143-
case NC_UDF0:
144146
UDF0_dispatch_table = dispatch_table;
145147
if (magic_number)
146148
strncpy(UDF0_magic_number, magic_number, NC_MAX_MAGIC_NUMBER_LEN);
147-
break;
148-
case NC_UDF1:
149+
}
150+
else if(fIsSet(mode_flag, NC_UDF1))
151+
{
149152
UDF1_dispatch_table = dispatch_table;
150153
if (magic_number)
151154
strncpy(UDF1_magic_number, magic_number, NC_MAX_MAGIC_NUMBER_LEN);
152-
break;
155+
}
156+
else
157+
{
158+
return NC_EINVAL;
153159
}
154160

155161
return NC_NOERR;
@@ -175,23 +181,23 @@ int
175181
nc_inq_user_format(int mode_flag, NC_Dispatch **dispatch_table, char *magic_number)
176182
{
177183
/* Check inputs. */
178-
if (mode_flag != NC_UDF0 && mode_flag != NC_UDF1)
179-
return NC_EINVAL;
180-
181-
switch(mode_flag)
184+
if (fIsSet(mode_flag,NC_UDF0))
182185
{
183-
case NC_UDF0:
184186
if (dispatch_table)
185187
*dispatch_table = UDF0_dispatch_table;
186188
if (magic_number)
187189
strncpy(magic_number, UDF0_magic_number, NC_MAX_MAGIC_NUMBER_LEN);
188-
break;
189-
case NC_UDF1:
190+
}
191+
else if(fIsSet(mode_flag,NC_UDF1))
192+
{
190193
if (dispatch_table)
191194
*dispatch_table = UDF1_dispatch_table;
192195
if (magic_number)
193196
strncpy(magic_number, UDF1_magic_number, NC_MAX_MAGIC_NUMBER_LEN);
194-
break;
197+
}
198+
else
199+
{
200+
return NC_EINVAL;
195201
}
196202

197203
return NC_NOERR;

libdispatch/dinfermodel.c

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ static struct FORMATMODES {
118118
{"classic",NC_FORMATX_NC3,0}, /* ditto */
119119
{"netcdf-4",NC_FORMATX_NC4,NC_FORMAT_NETCDF4},
120120
{"enhanced",NC_FORMATX_NC4,NC_FORMAT_NETCDF4},
121-
{"udf0",NC_FORMATX_UDF0,NC_FORMAT_NETCDF4},
122-
{"udf1",NC_FORMATX_UDF1,NC_FORMAT_NETCDF4},
121+
{"udf0",NC_FORMATX_UDF0,0},
122+
{"udf1",NC_FORMATX_UDF1,0},
123123
{"nczarr",NC_FORMATX_NCZARR,NC_FORMAT_NETCDF4},
124124
{"zarr",NC_FORMATX_NCZARR,NC_FORMAT_NETCDF4},
125125
{"bytes",NC_FORMATX_NC4,NC_FORMAT_NETCDF4}, /* temporary until 3 vs 4 is determined */
@@ -182,8 +182,8 @@ static struct Readable {
182182
{NC_FORMATX_PNETCDF,1},
183183
{NC_FORMATX_DAP2,0},
184184
{NC_FORMATX_DAP4,0},
185-
{NC_FORMATX_UDF0,0},
186-
{NC_FORMATX_UDF1,0},
185+
{NC_FORMATX_UDF0,1},
186+
{NC_FORMATX_UDF1,1},
187187
{NC_FORMATX_NCZARR,0}, /* eventually make readable */
188188
{0,0},
189189
};
@@ -762,13 +762,31 @@ NC_omodeinfer(int useparallel, int cmode, NCmodel* model)
762762
* use some of the other flags, like NC_NETCDF4, so we must first
763763
* check NC_UDF0 and NC_UDF1 before checking for any other
764764
* flag. */
765-
if(fIsSet(cmode,(NC_UDF0|NC_UDF1))) {
766-
model->format = NC_FORMAT_NETCDF4;
767-
if(fIsSet(cmode,NC_UDF0)) {
765+
if(fIsSet(cmode, NC_UDF0) || fIsSet(cmode, NC_UDF1))
766+
{
767+
if(fIsSet(cmode, NC_UDF0))
768+
{
768769
model->impl = NC_FORMATX_UDF0;
769770
} else {
770771
model->impl = NC_FORMATX_UDF1;
771772
}
773+
if(fIsSet(cmode,NC_64BIT_OFFSET))
774+
{
775+
model->format = NC_FORMAT_64BIT_OFFSET;
776+
}
777+
else if(fIsSet(cmode,NC_64BIT_DATA))
778+
{
779+
model->format = NC_FORMAT_64BIT_DATA;
780+
}
781+
else if(fIsSet(cmode,NC_NETCDF4))
782+
{
783+
if(fIsSet(cmode,NC_CLASSIC_MODEL))
784+
model->format = NC_FORMAT_NETCDF4_CLASSIC;
785+
else
786+
model->format = NC_FORMAT_NETCDF4;
787+
}
788+
if(! model->format)
789+
model->format = NC_FORMAT_CLASSIC;
772790
goto done;
773791
}
774792

@@ -981,8 +999,6 @@ NC_infermodel(const char* path, int* omodep, int iscreate, int useparallel, void
981999
case NC_FORMATX_NC4:
9821000
case NC_FORMATX_NC_HDF4:
9831001
case NC_FORMATX_DAP4:
984-
case NC_FORMATX_UDF0:
985-
case NC_FORMATX_UDF1:
9861002
case NC_FORMATX_NCZARR:
9871003
omode |= NC_NETCDF4;
9881004
if(model->format == NC_FORMAT_NETCDF4_CLASSIC)
@@ -1001,6 +1017,17 @@ NC_infermodel(const char* path, int* omodep, int iscreate, int useparallel, void
10011017
case NC_FORMATX_DAP2:
10021018
omode &= ~(NC_NETCDF4|NC_64BIT_OFFSET|NC_64BIT_DATA|NC_CLASSIC_MODEL);
10031019
break;
1020+
case NC_FORMATX_UDF0:
1021+
case NC_FORMATX_UDF1:
1022+
if(model->format == NC_FORMAT_64BIT_OFFSET)
1023+
omode |= NC_64BIT_OFFSET;
1024+
else if(model->format == NC_FORMAT_64BIT_DATA)
1025+
omode |= NC_64BIT_DATA;
1026+
else if(model->format == NC_FORMAT_NETCDF4)
1027+
omode |= NC_NETCDF4;
1028+
else if(model->format == NC_FORMAT_NETCDF4_CLASSIC)
1029+
omode |= NC_NETCDF4|NC_CLASSIC_MODEL;
1030+
break;
10041031
default:
10051032
{stat = NC_ENOTNC; goto done;}
10061033
}
@@ -1513,23 +1540,10 @@ static int
15131540
NC_interpret_magic_number(char* magic, NCmodel* model)
15141541
{
15151542
int status = NC_NOERR;
1543+
int tmpimpl = 0;
15161544
/* Look at the magic number */
1517-
#ifdef USE_NETCDF4
1518-
if (strlen(UDF0_magic_number) && !strncmp(UDF0_magic_number, magic,
1519-
strlen(UDF0_magic_number)))
1520-
{
1521-
model->impl = NC_FORMATX_UDF0;
1522-
model->format = NC_FORMAT_NETCDF4;
1523-
goto done;
1524-
}
1525-
if (strlen(UDF1_magic_number) && !strncmp(UDF1_magic_number, magic,
1526-
strlen(UDF1_magic_number)))
1527-
{
1528-
model->impl = NC_FORMATX_UDF1;
1529-
model->format = NC_FORMAT_NETCDF4;
1530-
goto done;
1531-
}
1532-
#endif /* USE_NETCDF4 */
1545+
if(model->impl == NC_FORMATX_UDF0 || model->impl == NC_FORMATX_UDF1)
1546+
tmpimpl = model->impl;
15331547

15341548
/* Use the complete magic number string for HDF5 */
15351549
if(memcmp(magic,HDF5_SIGNATURE,sizeof(HDF5_SIGNATURE))==0) {
@@ -1561,10 +1575,29 @@ NC_interpret_magic_number(char* magic, NCmodel* model)
15611575
}
15621576
}
15631577
/* No match */
1564-
status = NC_ENOTNC;
1578+
if (!tmpimpl)
1579+
status = NC_ENOTNC;
1580+
15651581
goto done;
15661582

15671583
done:
1584+
/* if model->impl was UDF0 or UDF1 on entry, make it so on exit */
1585+
if(tmpimpl)
1586+
model->impl = tmpimpl;
1587+
/* if this is a UDF magic_number update the model->impl */
1588+
if (strlen(UDF0_magic_number) && !strncmp(UDF0_magic_number, magic,
1589+
strlen(UDF0_magic_number)))
1590+
{
1591+
model->impl = NC_FORMATX_UDF0;
1592+
status = NC_NOERR;
1593+
}
1594+
if (strlen(UDF1_magic_number) && !strncmp(UDF1_magic_number, magic,
1595+
strlen(UDF1_magic_number)))
1596+
{
1597+
model->impl = NC_FORMATX_UDF1;
1598+
status = NC_NOERR;
1599+
}
1600+
15681601
return check(status);
15691602
}
15701603

nc_test4/tst_udf.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ main(int argc, char **argv)
307307
* priority. If NC_NETCDF4 flag were given priority, then
308308
* nc_abort() will not return TEST_VAL_42, but instead will
309309
* return 0. */
310-
if (nc_open(FILE_NAME, mode[i]|NC_NETCDF4, &ncid)) ERR;
310+
if (nc_open(FILE_NAME, mode[i], &ncid)) ERR;
311311
if (nc_inq_format(ncid, NULL) != TEST_VAL_42) ERR;
312312
if (nc_inq_format_extended(ncid, NULL, NULL) != TEST_VAL_42) ERR;
313313
if (nc_abort(ncid) != TEST_VAL_42) ERR;
@@ -336,6 +336,7 @@ main(int argc, char **argv)
336336
for (i = 0; i < NUM_UDFS; i++)
337337
{
338338
/* Add our test user defined format. */
339+
mode[i] = mode[i]|NC_NETCDF4;
339340
if (nc_def_user_format(mode[i], &tst_dispatcher, magic_number)) ERR;
340341

341342
/* Check that our user-defined format has been added. */
@@ -360,13 +361,17 @@ main(int argc, char **argv)
360361
printf("*** testing bad version causes dispatch table to be rejected...");
361362
{
362363
int i;
364+
char magic_number[5] = "1111";
363365

364366
/* Test all available user-defined format slots. */
365367
for (i = 0; i < NUM_UDFS; i++)
366368
{
367369
/* Make sure our bad version format is rejected. */
368370
if (nc_def_user_format(mode[i], &tst_dispatcher_bad_version,
369371
NULL) != NC_EINVAL) ERR;
372+
/* Make sure defining a magic number with netcdf3 is rejected. */
373+
if (nc_def_user_format(NC_CLASSIC_MODEL, &tst_dispatcher,
374+
magic_number) != NC_EINVAL) ERR;
370375
}
371376
}
372377
SUMMARIZE_ERR;

0 commit comments

Comments
 (0)