00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <nc4internal.h>
00011 #include <netcdf.h>
00012 #include <nc_tests.h>
00013
00014 #define FILE_NAME "tst_converts.nc"
00015 #define ATT1_NAME "att1"
00016 #define ATT2_NAME "att2"
00017 #define DIM1_NAME "dim1"
00018 #define DIM1_LEN 3
00019 #define DIM2_NAME "dim2"
00020 #define DIM2_LEN 15
00021 #define VAR1_NAME "var1"
00022 #define VAR2_NAME "var2"
00023
00024
00025 void check_file(int format, unsigned char *uchar_out);
00026 void create_file(int format, unsigned char *uchar_out);
00027
00028 int
00029 main(int argc, char **argv)
00030 {
00031 unsigned char uchar_out[DIM1_LEN] = {0, 128, 255};
00032 int format;
00033
00034 printf("\n*** Testing netcdf data conversion.\n");
00035
00036
00037 for (format = 1; format < 5; format++)
00038 {
00039 printf("*** Testing conversion in netCDF %s files... ", format_name[format]);
00040 create_file(format, uchar_out);
00041 check_file(format, uchar_out);
00042 SUMMARIZE_ERR;
00043 }
00044
00045 FINAL_RESULTS;
00046 }
00047
00048
00049 void
00050 create_file(int format, unsigned char *uchar_out)
00051 {
00052 int ncid, varid, cflags=0, dimids[1];
00053 int retval;
00054
00055 if (format == NC_FORMAT_64BIT)
00056 cflags |= NC_64BIT_OFFSET;
00057 else if (format == NC_FORMAT_NETCDF4_CLASSIC)
00058 {
00059 cflags |= (NC_NETCDF4|NC_CLASSIC_MODEL);
00060 }
00061 else if (format == NC_FORMAT_NETCDF4)
00062 cflags |= NC_NETCDF4;
00063
00064 if (nc_create(FILE_NAME, cflags, &ncid)) ERR;
00065 if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR;
00066 if (nc_def_var(ncid, VAR1_NAME, NC_BYTE, 1, dimids, &varid)) ERR;
00067 if (nc_enddef(ncid)) ERR;
00068 retval = nc_put_var_uchar(ncid, varid, uchar_out);
00069 if ((format != NC_FORMAT_NETCDF4) && retval) ERR;
00070 if ((format == NC_FORMAT_NETCDF4) && (retval != NC_ERANGE)) ERR;
00071 if (nc_close(ncid)) ERR;
00072 }
00073
00074 void
00075 check_file(int format, unsigned char *uchar_out)
00076 {
00077
00078 int ncid;
00079 int ndims, natts;
00080 int dimids_var[1], var_type;
00081 char var_name[NC_MAX_NAME+1];
00082 unsigned char uchar_in[DIM1_LEN];
00083 signed char char_in[DIM1_LEN];
00084 unsigned short ushort_in[DIM1_LEN];
00085 short short_in[DIM1_LEN];
00086 unsigned int uint_in[DIM1_LEN];
00087 int int_in[DIM1_LEN];
00088 long long int64_in[DIM1_LEN];
00089 unsigned long long uint64_in[DIM1_LEN];
00090 int i, res;
00091
00092
00093 if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
00094 if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, dimids_var, &natts)) ERR;
00095 if (strcmp(var_name, VAR1_NAME) || natts !=0 || ndims != 1 ||
00096 dimids_var[0] != 0 || var_type != NC_BYTE) ERR;
00097
00098
00099
00100
00101
00102 res = nc_get_var_uchar(ncid, 0, uchar_in);
00103 if (format == NC_FORMAT_NETCDF4)
00104 {
00105 if (res != NC_ERANGE) ERR;
00106 }
00107 else if (res) ERR;
00108
00109 for (i=0; i<DIM1_LEN; i++)
00110 if (uchar_in[i] != uchar_out[i]) ERR;
00111
00112 if (nc_get_var_schar(ncid, 0, char_in)) ERR;
00113 for (i=0; i<DIM1_LEN; i++)
00114 if (char_in[i] != (signed char)uchar_out[i]) ERR;
00115
00116 if (nc_get_var_short(ncid, 0, short_in)) ERR;
00117 for (i=0; i<DIM1_LEN; i++)
00118 if (short_in[i] != (signed char)uchar_out[i]) ERR;
00119
00120 if (nc_get_var_int(ncid, 0, int_in)) ERR;
00121 for (i=0; i<DIM1_LEN; i++)
00122 if (int_in[i] != (signed char)uchar_out[i]) ERR;
00123
00124 if (format == NC_FORMAT_NETCDF4 || format == NC_FORMAT_NETCDF4_CLASSIC)
00125 {
00126
00127
00128
00129
00130
00131 if (nc_get_var_ushort(ncid, 0, ushort_in) != NC_ERANGE) ERR;
00132 for (i=0; i<DIM1_LEN; i++)
00133 if (ushort_in[i] != (unsigned short)(signed char)uchar_out[i]) ERR;
00134
00135 if (nc_get_var_uint(ncid, 0, uint_in) != NC_ERANGE) ERR;
00136 for (i=0; i<DIM1_LEN; i++)
00137 if (uint_in[i] != (unsigned int)(signed char)uchar_out[i]) ERR;
00138
00139 if (nc_get_var_ulonglong(ncid, 0, uint64_in) != NC_ERANGE) ERR;
00140 for (i=0; i<DIM1_LEN; i++)
00141 if (uint64_in[i] != (unsigned long long)(signed char)uchar_out[i]) ERR;
00142
00143 if (nc_get_var_longlong(ncid, 0, int64_in)) ERR;
00144 for (i=0; i<DIM1_LEN; i++)
00145 if (int64_in[i] != (signed char)uchar_out[i]) ERR;
00146
00147 }
00148
00149 if (nc_close(ncid)) ERR;
00150 }