#include #include #include #include #include "netcdf.h" #undef GENERATE #undef DEBUG /* Test (some) internal/external type conversions using following DAP dataset (test.02). Dataset { Byte b[DIMSIZE]; Int32 i32[DIMSIZE]; UInt32 ui32[DIMSIZE]; Int16 i16[DIMSIZE]; UInt16 ui16[DIMSIZE]; Float32 f32[DIMSIZE]; Float64 f64[DIMSIZE]; String s[DIMSIZE]; Url u[DIMSIZE]; } OneDimensionalSimpleArrays; */ #define NDIMS 1 #define DIMSIZE 25 #define STRLEN 64 #ifndef USE_NETCDF4 #define NC_UBYTE 7 /* unsigned 1 byte int */ #define NC_USHORT 8 /* unsigned 2-byte int */ #define NC_UINT 9 /* unsigned 4-byte int */ #define NC_INT64 10 /* signed 8-byte int */ #define NC_UINT64 11 /* unsigned 8-byte int */ #define NC_STRING 12 /* string */ #endif #define CHECK(expr) check(expr,__FILE__,__LINE__); #define COMMA (i==0?"":",") #define COMPARE(t1,t2,v1,v2) compare(t1,t2,(void*)v1,(void*)v2,#v2,__FILE__,__LINE__) static int fail = 0; static void compare(nc_type,nc_type,void*,void*,char*,char*,int); static void report(const int i, const char* var, const int line) { fprintf(stdout,"%s mismatch: [%d] file: %s line: %d\n",var,i,__FILE__,line); fail = 1; } static void check(int ncstat, char* file, int line) { if(ncstat == NC_NOERR) return; fprintf(stderr,"*** FAIL: %d (%s) at %s:%d\n", ncstat,nc_strerror(ncstat),file,line); exit(1); } /* return 1 if |f1-f2| > 0.05 */ static int fdiff(double f1, double f2) { double delta = (f1 - f2); if(delta < 0) delta = - delta; if(delta > 0.05) { fprintf(stdout,"fdiff: %1.3f %1.3f delta=%1.3f\n",f1,f2,delta); } return (delta > 0.05?1:0); } static char ch_data[DIMSIZE]; static signed char int8_data[DIMSIZE]; static unsigned char uint8_data[DIMSIZE]; static int int8toint32_data[DIMSIZE]; static float int82float32_data[DIMSIZE]; static short int16_data[DIMSIZE]; static int int16toint32_data[DIMSIZE]; static float int162float32_data[DIMSIZE]; static int int32_data[DIMSIZE]; static float int32tofloat32_data[DIMSIZE]; static long int32toilong_data[DIMSIZE]; static float float32_data[DIMSIZE]; static double float64_data[DIMSIZE]; #ifndef USE_NETCDF4 static char string3_data[DIMSIZE][STRLEN]; #endif #ifdef USE_NETCDF4 static unsigned char ubyte_data[DIMSIZE]; static unsigned short uint16_data[DIMSIZE]; static unsigned int uint32_data[DIMSIZE]; static long long int32toint64_data[DIMSIZE]; static unsigned long long int32touint64_data[DIMSIZE]; static char* string4_data[DIMSIZE]; static char chartostring4_data[DIMSIZE+1]; /* special case */ #endif static char ch[DIMSIZE]; static signed char int8[DIMSIZE]; static unsigned char uint8[DIMSIZE]; static short int16[DIMSIZE]; static int int32[DIMSIZE]; static float float32[DIMSIZE]; static double float64[DIMSIZE]; static char string3[DIMSIZE][STRLEN]; static long ilong[DIMSIZE]; #ifdef USE_NETCDF4 static unsigned short uint16[DIMSIZE]; static unsigned int uint32[DIMSIZE]; static long long int64[DIMSIZE]; static unsigned long long uint64[DIMSIZE]; static char* string4[DIMSIZE]; #endif int main() { int ncid, varid, ncstat; char* url; #ifndef USE_NETCDF4 int j; int i; #endif /* location of our target url*/ #ifdef DEBUG url = "[log][show=fetch]http://test.opendap.org:8080/dods/dts/test.02"; #else url = "http://test.opendap.org:8080/dods/dts/test.02"; #endif printf("*** Test: var conversions on URL: %s\n",url); /* open file, get varid */ #ifdef USE_NETCDF4 CHECK(nc_open(url, NC_NOWRITE|NC_NETCDF4, &ncid)); #else CHECK(nc_open(url, NC_NOWRITE, &ncid)); #endif /* extract the string case for netcdf-3*/ #ifndef USE_NETCDF4 CHECK(nc_inq_varid(ncid, "s", &varid)); CHECK(nc_get_var_text(ncid,varid,(char*)string3)); #ifdef GENERATE printf("static %s string3_data[DIMSIZE][STRLEN]={","char"); for(i=0;i 0 && string3[i][j] != '\n' && string3[i][j] != '\r' && string3[i][j] != '\t' &&(string3[i][j] < ' ' || string3[i][j] >= '\177')) string3[i][j] = '?'; } printf("%s\"%s\"",COMMA,string3[i]); } printf("};\n"); #else fprintf(stdout,"*** testing: %s\n","string3"); for(i=0;i