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
|
/* This is part of the netCDF package. Copyright 2005-2018,
University Corporation for Atmospheric Research/Unidata. See
COPYRIGHT file for conditions of use.
Test that NetCDF-4 can read HDF4 files.
Ed Hartnett, Ward Fisher
*/
#include <config.h>
#include <nc_tests.h>
#include "err_macros.h"
#include <mfhdf.h>
#include <netcdf_f.h>
#define FILE_NAME "tst_hdf4_extra.h4"
#define PRES_NAME "pres"
#define LAT_LEN 3
#define LON_LEN 2
#define NDIMS2 2
#define ATT_NAME "Caesar"
#define NAME_DUMB "Bozo"
int
create_hdf4_file()
{
int32 sd_id, sds_id;
int32 dim_size[NDIMS2] = {LAT_LEN, LON_LEN};
int32 start[NDIMS2] = {0, 0}, edge[NDIMS2] = {LAT_LEN, LON_LEN};
int data_out[LAT_LEN][LON_LEN];
int test_val = 42;
int i, j;
int count = 0;
/* Create some data. */
for (i = 0; i < LAT_LEN; i++)
for (j = 0; j < LON_LEN; j++)
data_out[i][j] = count++;
/* Create a file with one SDS, containing our phony data. */
sd_id = SDstart(FILE_NAME, DFACC_CREATE);
sds_id = SDcreate(sd_id, PRES_NAME, DFNT_INT32, NDIMS2, dim_size);
if (SDwritedata(sds_id, start, NULL, edge, (void *)data_out)) ERR;
/* Add a global attribute. */
if (SDsetattr(sd_id, ATT_NAME, DFNT_INT32, 1, &test_val)) ERR;
/* Shut down. */
if (SDendaccess(sds_id)) ERR;
if (SDend(sd_id)) ERR;
return 0;
}
int
main(int argc, char **argv)
{
printf("\n*** Testing HDF4/NetCDF-4 interoperability extra stuff...\n");
/* Create our test file. */
if (create_hdf4_file()) ERR;
printf("*** testing data conversion...");
{
int ncid;
size_t start[NDIMS2] = {0, 0}, count[NDIMS2] = {LAT_LEN, LON_LEN};
int data_int[LAT_LEN * LON_LEN];
short data_short[LAT_LEN * LON_LEN];
int data_int2[LAT_LEN * LON_LEN];
float data_float[LAT_LEN * LON_LEN];
double data_double[LAT_LEN * LON_LEN];
int i = 0;
/* Open HDF4 file with netCDF. */
if (nc_open(FILE_NAME, 0, &ncid)) ERR;
/* These won't work. */
if (nc_get_vara_int(ncid, 0, NULL, count, data_int) != NC_EINVALCOORDS) ERR;
if (nc_get_vara_int(ncid + TEST_VAL_42, 0, start, count, data_int) != NC_EBADID) ERR;
/* Read data as short. */
if (nc_get_vara_short(ncid, 0, start, count, data_short)) ERR;
for (i = 0; i < LAT_LEN * LON_LEN; i++)
if (data_short[i] != (short)i) ERR;
/* Read data as int. */
if (nc_get_vara_int(ncid, 0, start, count, data_int)) ERR;
for (i = 0; i < LAT_LEN * LON_LEN; i++)
if (data_int[i] != i) ERR;
/* NULL count is treated as meaning entire variable. */
if (nc_get_vara_int(ncid, 0, start, NULL, data_int2)) ERR;
for (i = 0; i < LAT_LEN * LON_LEN; i++)
if (data_int2[i] != i) ERR;
/* Read data as float. */
if (nc_get_vara_float(ncid, 0, start, count, data_float)) ERR;
for (i = 0; i < LAT_LEN * LON_LEN; i++)
if (data_float[i] != (float)i) ERR;
/* Read data as double. */
if (nc_get_vara_double(ncid, 0, start, count, data_double)) ERR;
for (i = 0; i < LAT_LEN * LON_LEN; i++)
if (data_double[i] != (double)i) ERR;
/* Close the file. */
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing bad parameters, read-only writes, and abort...");
{
int ncid;
int ndims, nvars, ngatts, unlimdimid;
size_t start[NDIMS2] = {0, 0}, count[NDIMS2] = {1, 1};
int test_val;
/* These will not work. */
if (nc_open(FILE_NAME, NC_MMAP, &ncid) != NC_EINVAL) ERR;
if (nc_open(FILE_NAME, NC_64BIT_OFFSET, &ncid) != NC_EINVAL) ERR;
if (nc_open(FILE_NAME, NC_DISKLESS, &ncid) != NC_EINVAL) ERR;
/* Now open with netCDF. */
if (nc_open(FILE_NAME, 0, &ncid)) ERR;
/* Check it out. */
if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
if (ndims != 2 || nvars != 1 || ngatts != 1 || unlimdimid != -1) ERR;
if (nc_inq(ncid, NULL, NULL, NULL, NULL)) ERR;
if (nc_inq(ncid + TEST_VAL_42, NULL, NULL, NULL, NULL) != NC_EBADID) ERR;
/* These only work for netCDF-3 files. */
if (nc_set_base_pe(ncid, 0)) ERR;
if (nc_inq_base_pe(ncid, NULL)) ERR;
/* Attempt to write. */
if (nc_rename_att(ncid, NC_GLOBAL, ATT_NAME, NAME_DUMB) != NC_EPERM) ERR;
if (nc_del_att(ncid, NC_GLOBAL, ATT_NAME) != NC_EPERM) ERR;
if (nc_put_att_int(ncid, NC_GLOBAL, NAME_DUMB, NC_INT, 0, NULL) != NC_EPERM) ERR;
if (nc_def_dim(ncid, NAME_DUMB, 1, NULL) != NC_EPERM) ERR;
if (nc_def_var(ncid, "hh", NC_INT, 0, NULL, NULL) != NC_EPERM) ERR;
if (nc_def_var_fill(ncid, 0, 0, &test_val) != NC_EPERM) ERR;
if (nc_rename_var(ncid, 0, NAME_DUMB) != NC_EPERM) ERR;
if (nc_put_vara_int(ncid, 0, start, count, &test_val) != NC_EPERM) ERR;
if (nc_set_fill(ncid, 0, NULL) != NC_EPERM) ERR;
if (nc_rename_dim(ncid, 0, NULL) != NC_EPERM) ERR;
/* These succeed but do nothing. */
if (nc_enddef(ncid)) ERR;
if (nc_sync(ncid)) ERR;
/* These netcdf-4 operations are not supported. */
if (nc_def_var_filter(ncid, 0, 0, 0, NULL) != NC_ENOTNC4) ERR;
if (nc_def_var_fletcher32(ncid, 0, 0) != NC_ENOTNC4) ERR;
if (nc_def_var_endian(ncid, 0, 0) != NC_ENOTNC4) ERR;
if (nc_def_grp(ncid, NAME_DUMB, NULL) != NC_ENOTNC4) ERR;
if (nc_rename_grp(ncid, NAME_DUMB) != NC_ENOTNC4) ERR;
if (nc_def_compound(ncid, 1, NAME_DUMB, NULL) != NC_ENOTNC4) ERR;
if (nc_insert_compound(ncid, 1, NAME_DUMB, 1, 1) != NC_ENOTNC4) ERR;
if (nc_insert_array_compound(ncid, 1, NAME_DUMB, 1, 1, 1, NULL) != NC_ENOTNC4) ERR;
if (nc_inq_compound_field(ncid, 1, 1, NULL, NULL, NULL, NULL, NULL) != NC_ENOTNC4) ERR;
if (nc_inq_compound_fieldindex(ncid, 1, NULL, NULL) != NC_ENOTNC4) ERR;
if (nc_def_opaque(ncid, 1, NULL, NULL) != NC_ENOTNC4) ERR;
if (nc_def_vlen(ncid, NULL, 1, NULL) != NC_ENOTNC4) ERR;
if (nc_def_enum(ncid, 1, NULL, NULL) != NC_ENOTNC4) ERR;
if (nc_inq_enum_ident(ncid, 1, 1, NULL) != NC_ENOTNC4) ERR;
if (nc_inq_enum_member(ncid, 1, 1, NULL, NULL) != NC_ENOTNC4) ERR;
if (nc_insert_enum(ncid, 1, NULL, NULL) != NC_ENOTNC4) ERR;
if (nc_put_vlen_element(ncid, 1, NULL, 1, NULL) != NC_ENOTNC4) ERR;
if (nc_get_vlen_element(ncid, 1, NULL, NULL, NULL) != NC_ENOTNC4) ERR;
if (nc_set_var_chunk_cache(ncid, 1, 1, 1, 1.0) != NC_ENOTNC4) ERR;
if (nc_get_var_chunk_cache(ncid, 1, NULL, NULL, NULL) != NC_ENOTNC4) ERR;
/* Abort is the same as nc_close, since HDF4 is read-only.) */
if (nc_abort(ncid)) ERR;
}
SUMMARIZE_ERR;
FINAL_RESULTS;
}
|