ncvarinfo.c 1.58 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netcdf.h>
 
/**************************
   Function prototypes
**************************/
 
void handle_netcdf_error (int);
 

main(int argc, char **argv)
{
      int ncID, varID, ndims, dimids[NC_MAX_DIMS];
      nc_type type;
      size_t len, nrecs;
      char varname[NC_MAX_NAME];
       
      int i, ii, j, status;

        if (argc <= 2) {
            printf("Incorrect number of arguments\n"); 
   	    exit(0);
	}
	
	/* open nc file to write */
	if((status = nc_open(argv[1],NC_WRITE|NC_SHARE,&ncID)) != NC_NOERR){
	    printf("%d",status);
	    exit(0);
	}
		
	if ((status = nc_inq_varid(ncID, argv[2], &varID)) != NC_NOERR) {
	      printf("%d",status);
	      exit(0);                
	} 
	
	if ((status = nc_inq_vartype(ncID, varID, &type)) != NC_NOERR) {
	      printf("%d",status);
	      exit(0);   		 
	} 
 
	if ((status = nc_inq_varndims(ncID, varID, &ndims)) != NC_NOERR) {
	      printf("%d", status);
	      exit(0); 
	} 
	
	if ((status = nc_inq_vardimid(ncID, varID, dimids)) != NC_NOERR){
	      printf("%d", status);
	      exit(0); 
	}
	         
        printf("%d %d ", type, ndims);
	for (i = 0; i < ndims; i++) {
	       status = nc_inq_dimlen(ncID, dimids[i], &len);
	       printf("%d ", len);
	} 
        status = nc_close(ncID);
}
/*--------------------------------------------------------------------------
 *  Handles a netCDF error.
 *--------------------------------------------------------------------------*/

void handle_netcdf_error(int status) {
  fprintf(stderr, "%s\n", nc_strerror(status));  
}