Beispiel #1
0
int
main(int argc, char **argv)
{
    int c, stat;
    int ncid;
    NChdr* hdr;

    init();

    opterr=1;
    while ((c = getopt(argc, argv, "dvau:D:l:p:c:t:R:")) != EOF) {
      switch(c) {
      case 'v': verbose=1; break;
      case 'd': debug=1; break;
      case 'D': debug=atoi(optarg); break;
      default: break;
      }
    }

    if(debug > 0) {ocdebug = debug;}

    argc -= optind;
    argv += optind;

    if(argc > 0 && fileurl == NULL)
	fileurl = nulldup(argv[0]);

    if(fileurl == NULL) fileurl = getenv("FILEURL");

    if(fileurl == NULL) {
	fprintf(stderr,"no file url specified\n");
	usage();
    }

    if(verbose) dumpflags();

    if(verbose) {fprintf(stdout,"initializing\n"); fflush(stdout);}

    stat = nc_open(fileurl,NC_NOWRITE,&ncid);
    check_err(stat);

    /* dump meta data */
    stat = dumpmetadata(ncid,&hdr);
    check_err(stat);

    /* Get data */
if(1)
    {
	int i,j,limited;
	size_t start[NC_MAX_VAR_DIMS];
	size_t count[NC_MAX_VAR_DIMS];
	/* Walk each variable */
	for(i=0;i<hdr->nvars;i++) {
	    Var* var = &hdr->vars[i];
	    size_t nelems = 1;
	    limited = 0;
	    for(j=0;j<var->ndims;j++) {
		start[j] = 0;
		assert(var->dimids[j] == hdr->dims[var->dimids[j]].dimid);
		count[j] = hdr->dims[var->dimids[j]].size;
		/* put a limit on each count */
	        if(count[j] > LIMIT) {count[j] = LIMIT; limited = 1;}
		nelems *= count[j];
	    }
	    if(nelems > 0) {
		size_t typesize = nctypesizeof(var->nctype);
		size_t memsize = typesize*nelems;
	        void* memory = malloc(memsize);
	        fprintf(stdout,"%s: size=%lu (%lu * %lu) ; ",
			var->name, (unsigned long)memsize, (unsigned long)typesize, (unsigned long)nelems);
		fflush(stdout);
	        stat = nc_get_vara(ncid,var->varid,start,count,memory);
	        check_err(stat);
		/* dump memory */
		switch (var->nctype){
		case NC_CHAR:
	            fprintf(stdout,"\""); fflush(stdout);
		    dumpchars(nelems,memory);
	            fprintf(stdout,"\"");
		    break;		    
		default:
		    for(j=0;j<nelems;j++) {
		        if(j > 0) fprintf(stdout," ");
		        dumpdata1(var->nctype,j,memory);
		    }
		}
		if(limited) fprintf(stdout,"...");
	        fprintf(stdout,"\n");
		free(memory);
	    } else
		fprintf(stdout,"%s: no data\n",var->name);
	}
        fprintf(stdout,"\n");	
    }

    nc_close(ncid);
    return 0;
}
Beispiel #2
0
int main(int argc, char **argv)
{
    register int i;
    int tmp, tmp2;
    int err = 0;
    int dbl_order, flt_order, lng_order, int_order, shrt_order;

    /* Find native sizes */
    printf("\n/* Native machine sizes */\n");
    printf("#define NATIVE_DOUBLE %d\n", (nat_dbl = sizeof(double)));
    printf("#define NATIVE_FLOAT  %d\n", (nat_flt = sizeof(float)));
    printf("#define NATIVE_OFF_T  %d\n", (nat_off_t = sizeof(off_t)));
    printf("#define NATIVE_LONG   %d\n", (nat_lng = sizeof(long)));
    printf("#define NATIVE_INT    %d\n", (nat_int = sizeof(int)));
    printf("#define NATIVE_SHORT  %d\n", (nat_shrt = sizeof(short)));
    printf("#define NATIVE_CHAR   %d\n", (nat_char = sizeof(char)));

    /* Following code checks only if all assumptions are fulfilled */
    /* Check sizes */
    if (nat_dbl != PORT_DOUBLE) {
	fprintf(stderr, "ERROR, sizeof (double) != %d\n", PORT_DOUBLE);
	err = 1;
    }
    if (nat_flt != PORT_FLOAT) {
	fprintf(stderr, "ERROR, sizeof (float) != %d\n", PORT_FLOAT);
	err = 1;
    }
    /* port_off_t is variable */
    if (nat_lng < PORT_LONG) {
	fprintf(stderr, "ERROR, sizeof (long) < %d\n", PORT_LONG);
	err = 1;
    }
    if (nat_int < PORT_INT) {
	fprintf(stderr, "ERROR, sizeof (int) < %d\n", PORT_INT);
	err = 1;
    }
    if (nat_shrt < PORT_SHORT) {
	fprintf(stderr, "ERROR, sizeof (short) < %d\n", PORT_SHORT);
	err = 1;
    }
    if (nat_char != PORT_CHAR) {
	fprintf(stderr, "ERROR, sizeof (char) != %d\n", PORT_CHAR);
	err = 1;
    }

    /* Find for each byte in big endian test pattern (*_cmpr) 
     * offset of corresponding byte in machine native order.
     * Look if native byte order is little or big or some other (pdp)
     * endian.
     */
    /* Find double order */
    u.d = TEST_PATTERN;
    for (i = 0; i < PORT_DOUBLE; i++) {
	tmp = find_offset(u.c, dbl_cmpr[i], PORT_DOUBLE);
	if (-1 == tmp) {
	    fprintf(stderr, "ERROR, could not find '%x' in double\n",
		    dbl_cmpr[i]);
	    err = 1;
	}
	dbl_cnvrt[i] = tmp;
    }
    tmp = tmp2 = 1;
    for (i = 0; i < PORT_DOUBLE; i++) {
	if (dbl_cnvrt[i] != i)
	    tmp = 0;		/* isn't big endian */
	if (dbl_cnvrt[i] != (PORT_DOUBLE - i - 1))
	    tmp2 = 0;		/* isn't little endian */
    }
    if (tmp)
	dbl_order = ENDIAN_BIG;
    else if (tmp2)
	dbl_order = ENDIAN_LITTLE;
    else
	dbl_order = ENDIAN_OTHER;

    /* Find float order */
    u.f = TEST_PATTERN;
    for (i = 0; i < PORT_FLOAT; i++) {
	tmp = find_offset(u.c, flt_cmpr[i], PORT_FLOAT);
	if (-1 == tmp) {
	    fprintf(stderr, "ERROR, could not find '%x' in float\n",
		    flt_cmpr[i]);
	    err = 1;
	}
	flt_cnvrt[i] = tmp;
    }
    tmp = tmp2 = 1;
    for (i = 0; i < PORT_FLOAT; i++) {
	if (flt_cnvrt[i] != i)
	    tmp = 0;
	if (flt_cnvrt[i] != (PORT_FLOAT - i - 1))
	    tmp2 = 0;
    }
    if (tmp)
	flt_order = ENDIAN_BIG;
    else if (tmp2)
	flt_order = ENDIAN_LITTLE;
    else
	flt_order = ENDIAN_OTHER;

    /* Find off_t order */
    if (nat_off_t == 8)
	u.o = OFF_T_TEST;
    else
	u.o = LONG_TEST;
    for (i = 0; i < nat_off_t; i++) {
	tmp = find_offset(u.c, off_t_cmpr[i], nat_off_t);
	if (-1 == tmp) {
	    fprintf(stderr, "ERROR, could not find '%x' in off_t\n",
		    off_t_cmpr[i]);
	    err = 1;
	}
	off_t_cnvrt[i] = tmp;
    }
    tmp = tmp2 = 1;
    for (i = 0; i < nat_off_t; i++) {
	if (off_t_cnvrt[i] != (i + (nat_off_t - nat_off_t)))
	    tmp = 0;
	if (off_t_cnvrt[i] != (nat_off_t - i - 1))
	    tmp2 = 0;
    }
    if (tmp)
	off_t_order = ENDIAN_BIG;
    else if (tmp2)
	off_t_order = ENDIAN_LITTLE;
    else
	off_t_order = ENDIAN_OTHER;

    /* Find long order */
    u.l = LONG_TEST;
    for (i = 0; i < PORT_LONG; i++) {
	tmp = find_offset(u.c, lng_cmpr[i], nat_lng);
	if (-1 == tmp) {
	    fprintf(stderr, "ERROR, could not find '%x' in long\n",
		    lng_cmpr[i]);
	    err = 1;
	}
	lng_cnvrt[i] = tmp;
    }
    tmp = tmp2 = 1;
    for (i = 0; i < PORT_LONG; i++) {
	if (lng_cnvrt[i] != (i + (nat_lng - PORT_LONG)))
	    tmp = 0;
	if (lng_cnvrt[i] != (PORT_LONG - i - 1))
	    tmp2 = 0;
    }
    if (tmp)
	lng_order = ENDIAN_BIG;
    else if (tmp2)
	lng_order = ENDIAN_LITTLE;
    else
	lng_order = ENDIAN_OTHER;

    /* Find int order */
    u.i = INT_TEST;
    for (i = 0; i < PORT_INT; i++) {
	tmp = find_offset(u.c, int_cmpr[i], nat_int);
	if (-1 == tmp) {
	    fprintf(stderr, "ERROR, could not find '%x' in int\n",
		    int_cmpr[i]);
	    err = 1;
	}
	int_cnvrt[i] = tmp;
    }
    tmp = tmp2 = 1;
    for (i = 0; i < PORT_INT; i++) {
	if (int_cnvrt[i] != (i + (nat_lng - PORT_LONG)))
	    tmp = 0;
	if (int_cnvrt[i] != (PORT_INT - i - 1))
	    tmp2 = 0;
    }
    if (tmp)
	int_order = ENDIAN_BIG;
    else if (tmp2)
	int_order = ENDIAN_LITTLE;
    else
	int_order = ENDIAN_OTHER;

    /* Find short order */
    u.s = SHORT_TEST;
    for (i = 0; i < PORT_SHORT; i++) {
	tmp = find_offset(u.c, shrt_cmpr[i], nat_shrt);
	if (-1 == tmp) {
	    fprintf(stderr, "ERROR, could not find '%x' in shrt\n",
		    shrt_cmpr[i]);
	    err = 1;
	}
	shrt_cnvrt[i] = tmp;
    }
    tmp = tmp2 = 1;
    for (i = 0; i < PORT_SHORT; i++) {
	if (shrt_cnvrt[i] != (i + (nat_shrt - PORT_SHORT)))
	    tmp = 0;
	if (shrt_cnvrt[i] != (PORT_SHORT - i - 1))
	    tmp2 = 0;
    }
    if (tmp)
	shrt_order = ENDIAN_BIG;
    else if (tmp2)
	shrt_order = ENDIAN_LITTLE;
    else
	shrt_order = ENDIAN_OTHER;

    printf("\n/* Native machine byte orders */\n");
    printf("#define DOUBLE_ORDER %d\n", dbl_order);
    printf("#define FLOAT_ORDER  %d\n", flt_order);
    printf("#define OFF_T_ORDER  %d\n", off_t_order);
    printf("#define LONG_ORDER   %d\n", lng_order);
    printf("#define INT_ORDER    %d\n", int_order);
    printf("#define SHORT_ORDER  %d\n", shrt_order);

    printf("\n\n/* Translation matrices from big endian to native */\n");
    dumpflags();

    return (err);
}