ncinfo_remote.c 4.56 KB
/*
 * get parameter info from CDAWEB dataset nc file
 * args : ncfile varId infoId
 * infoId :
 * 0 - dimension
 * 1 - labels
 * 2 - units
 * 3 - fillvalue
 * 4 - is virtual
 * 5 - is spectra
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netcdf.h>
 
void check(int stat) 
{
	if (stat != NC_NOERR) 
	{
		fprintf(stdout,"Unknown");
        // 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;
	int infoId;
	float number_attr;
	
	const char *info[4];
	info[0] = "UNITS";
	info[1] = "_FillValue";
    info[2] = "FILLVAL";
    info[3] = "VIRTUAL";
    info[4] = "DISPLAY_TYPE";
    info[5] = "DEPEND_1";

	if (argc <= 3) {
		printf("Incorrect number of arguments\n"); 
		exit(1);
	}

	infoId = atoi(argv[3]);
	
	stat = nc_open(argv[1],0,&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; 	
    // size
   if (ndims > 1)
		for (i = 1; i < ndims; i++) {
				stat = nc_inq_dimlen(ncID, dimids[i], &len); check(stat);				
				if (infoId == 0)
				{
					fprintf(stdout, "%d ", len);
				}
		}
	else
		if (infoId == 0)
		{ 
			fprintf(stdout,"%d ", len);
		}
		
	if (infoId == 1) // components
	{
		size_t attlen = 0;
		stat = nc_inq_attlen(ncID, varID, "LABL_PTR_1", &attlen);
        
		if (stat != NC_NOERR) {
            fprintf(stdout, "-1");
            exit(0);
        }
		unsigned char *string_attr = (unsigned char *)malloc(attlen * sizeof(char*));
		memset(string_attr, 0, attlen * sizeof(char*));	
		stat = nc_get_att(ncID, varID, "LABL_PTR_1", string_attr);
        if (stat != NC_NOERR) {
            fprintf(stdout, "-1");
            exit(0);
        }
		stat = nc_inq_varid(ncID, string_attr, &labelID); 
        if (stat != NC_NOERR) {
            fprintf(stdout, "-1");
            exit(0);
        }
		stat = nc_inq_varndims(ncID, labelID, &ndims);
		 if (stat != NC_NOERR) {
            fprintf(stdout, "-1");
            exit(0);
        }
		stat = nc_inq_vardimid(ncID, labelID, dimids); 
        if (stat != NC_NOERR) {
            fprintf(stdout, "-1");
            exit(0);
        }
		for (i = 0; i < ndims; i++) {
				stat = nc_inq_dimlen(ncID, dimids[i], &len); check(stat);
				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++)
		{
			memset(string_var, 0, length * sizeof(char*));
			stat = nc_get_vara(ncID, labelID,  start, count, string_var); check(stat);
			fprintf(stdout, "%s",  string_var);
			if (i < size -1) fprintf(stdout,",");
			start[0]++;
		}
		free(string_attr);
		free(string_var);
	}
	
	if (infoId == 2) // Units
	{
		size_t attlen = 0;
		stat = nc_inq_attlen(ncID, varID, info[infoId-2], &attlen); check(stat);
		
		unsigned char *string_attr = (unsigned char *)malloc(attlen * sizeof(char*));	
		memset(string_attr, 0, attlen * sizeof(char*));
		stat = nc_get_att(ncID, varID, info[infoId-2], string_attr); check(stat);
		fprintf(stdout, "%s",  string_attr);	
		free(string_attr);
	}
	
	if (infoId == 3) // Fill Value
	{
		stat = nc_get_att(ncID, varID, info[infoId-2], &number_attr); 
        
        if (stat != NC_NOERR) {
            stat = nc_get_att(ncID, varID, info[infoId-1], &number_attr);   
            check(stat);
        }
		fprintf(stdout, "%e",  number_attr);	 
	}
	
	if (infoId == 4) // is virtual
	{
		size_t attlen = 0;
		stat = nc_inq_attlen(ncID, varID, info[infoId-1], &attlen);	
        if (stat != NC_NOERR) {
            fprintf(stdout, "1");	 
        }
        else {
            fprintf(stdout, "0");
        }
	}
	
	if (infoId == 5) // is spectrogram
	{
		size_t attlen = 0;
		stat = nc_inq_attlen(ncID, varID, info[infoId-1], &attlen);	
        
        if (stat != NC_NOERR) {
            fprintf(stdout, "0");	 
        }
        else {
            unsigned char *string_attr = (unsigned char *)malloc(attlen * sizeof(char*));
            memset(string_attr, 0, attlen * sizeof(char*));
            stat = nc_get_att(ncID, varID, info[infoId-1], string_attr); 
            if (strcasecmp(string_attr,"spectrogram") != 0)
                fprintf(stdout, "0");
            else
                fprintf(stdout, "1");
            
           free(string_attr); 
        }
	}
	stat = nc_close(ncID);
	exit(0);
}