ncinfo_remote.c 2.22 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netcdf.h>
 
void check(int stat) 
{
	if (stat != NC_NOERR) 
	{
		printf("NetCDF error: %s\n", nc_strerror(stat));
		exit(1);
	}
} 
 
main(int argc, char **argv)
{
	int ncID, varID, labelID, ndims, dimids[NC_MAX_DIMS];
	size_t i, k;
//	char varname[NC_MAX_NAME];
	int stat = 0;
	nc_type xtype;
	size_t start[] = {0, 0};
	size_t count[] = {1, 0};
	size_t size, length;
		
	if (argc <= 2) {
		printf("Incorrect number of arguments\n"); 
		exit(1);
	}
		
	stat = nc_open(argv[1],NC_WRITE|NC_SHARE,&ncID); check(stat);

	stat = nc_inq_varid(ncID, argv[2], &varID); check(stat);
 
	stat = nc_inq_varndims(ncID, varID, &ndims); check(stat);
	
	stat = nc_inq_vardimid(ncID, varID, dimids); check(stat);
	
	size_t len = 0; 	
   if (ndims > 1)
		for (i = 1; i < ndims; i++) {
				stat = nc_inq_dimlen(ncID, dimids[i], &len); check(stat);				
				if (argc == 3)
				{
					fprintf(stdout, "%d ", len);
				}
		}
	else
		if (argc == 3)
		{ 
			fprintf(stdout,"%d ", len);
		}
		
	if (argc == 4)
	{
		size_t attlen = 0;
		stat = nc_inq_attlen(ncID, varID, "LABL_PTR_1", &attlen); check(stat);
	//	stat = nc_inq_atttype(ncID, varID, "LABL_PTR_1", &xtype);  check(stat);	
		
		unsigned char *string_attr = (unsigned char *)malloc(attlen * sizeof(char*));	
		stat = nc_get_att(ncID, varID, "LABL_PTR_1", string_attr); check(stat);
		
	//	printf("attribute = %s\n",  string_attr);
		stat = nc_inq_varid(ncID, string_attr, &labelID); check(stat);
	//	stat = nc_inq_vartype(ncID, labelID,  &xtype);  check(stat);
			
	//	printf("%d \n", xtype);
	
		stat = nc_inq_varndims(ncID, labelID, &ndims); check(stat);
		
		stat = nc_inq_vardimid(ncID, labelID, dimids); check(stat);
		
		for (i = 0; i < ndims; i++) {
				stat = nc_inq_dimlen(ncID, dimids[i], &len); check(stat);				
	//			printf("%d \n", len);
				if (i == 0) size = len;
				if (i == 1) length = len;
		} 
		count[1] = length;
		
		unsigned char *string_var = (unsigned char *)malloc(length * sizeof(char*));
		for (i = 0; i < size; i++)
		{
			stat = nc_get_vara(ncID, labelID,  start, count, string_var); check(stat);
			fprintf(stdout, "%s;",  string_var);	
			start[0]++;
		}
		free(string_attr);
		free(string_var);
	}
	
	stat = nc_close(ncID);
	exit(0);
}