Blame view

php/src/ncinfo_remote.c 2.77 KB
25f87ff8   Elena.Budnik   info for remote
1
2
3
4
5
6
7
8
9
10
/*
 * get parameter info from CDAWEB dataset nc file
 * args : ncfile varId infoId
 * infoId :
 * 0 - dimension
 * 1 - labels
 * 2 - units
 * 3 - fillvalue
 */

d45f025c   Elena.Budnik   merge remote and ...
11
#include <stdio.h>
74b77f58   Elena.Budnik   init remote branch
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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;
25f87ff8   Elena.Budnik   info for remote
35
36
37
38
39
40
41
42
	int infoId;
	float number_attr;
	
	const char *info[2];
	info[0] = "UNITS";
	info[1] = "_FillValue";

	if (argc <= 3) {
74b77f58   Elena.Budnik   init remote branch
43
44
45
		printf("Incorrect number of arguments\n"); 
		exit(1);
	}
25f87ff8   Elena.Budnik   info for remote
46
47
48
	
	infoId = atoi(argv[3]);
	
74b77f58   Elena.Budnik   init remote branch
49
50
51
	stat = nc_open(argv[1],NC_WRITE|NC_SHARE,&ncID); check(stat);

	stat = nc_inq_varid(ncID, argv[2], &varID); check(stat);
74b77f58   Elena.Budnik   init remote branch
52
53
54
55
56
57
58
59
	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);				
25f87ff8   Elena.Budnik   info for remote
60
				if (infoId == 0)
74b77f58   Elena.Budnik   init remote branch
61
62
63
64
65
				{
					fprintf(stdout, "%d ", len);
				}
		}
	else
25f87ff8   Elena.Budnik   info for remote
66
		if (infoId == 0)
74b77f58   Elena.Budnik   init remote branch
67
68
69
70
		{ 
			fprintf(stdout,"%d ", len);
		}
		
25f87ff8   Elena.Budnik   info for remote
71
	if (infoId == 1)
74b77f58   Elena.Budnik   init remote branch
72
73
	{
		size_t attlen = 0;
25f87ff8   Elena.Budnik   info for remote
74
		stat = nc_inq_attlen(ncID, varID, "LABL_PTR_1", &attlen); check(stat);	
74b77f58   Elena.Budnik   init remote branch
75
76
77
		
		unsigned char *string_attr = (unsigned char *)malloc(attlen * sizeof(char*));	
		stat = nc_get_att(ncID, varID, "LABL_PTR_1", string_attr); check(stat);
25f87ff8   Elena.Budnik   info for remote
78

74b77f58   Elena.Budnik   init remote branch
79
		stat = nc_inq_varid(ncID, string_attr, &labelID); check(stat);
74b77f58   Elena.Budnik   init remote branch
80
81
82
83
84
		stat = nc_inq_varndims(ncID, labelID, &ndims); check(stat);
		
		stat = nc_inq_vardimid(ncID, labelID, dimids); check(stat);
		
		for (i = 0; i < ndims; i++) {
25f87ff8   Elena.Budnik   info for remote
85
				stat = nc_inq_dimlen(ncID, dimids[i], &len); check(stat);
74b77f58   Elena.Budnik   init remote branch
86
87
88
89
90
91
92
93
94
				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);
25f87ff8   Elena.Budnik   info for remote
95
96
			fprintf(stdout, "%s",  string_var);
			if (i < size -1) fprintf(stdout,",");
74b77f58   Elena.Budnik   init remote branch
97
98
99
100
101
102
			start[0]++;
		}
		free(string_attr);
		free(string_var);
	}
	
25f87ff8   Elena.Budnik   info for remote
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
	if (infoId == 2)
	{
		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*));	
		stat = nc_get_att(ncID, varID, info[infoId-2], string_attr); check(stat);
		fprintf(stdout, "%s",  string_attr);	
		free(string_attr);
	}
	
	if (infoId == 3)
	{
		stat = nc_get_att(ncID, varID, info[infoId-2], &number_attr); check(stat);
		fprintf(stdout, "%e",  number_attr);	
		 
	}
	
74b77f58   Elena.Budnik   init remote branch
121
122
123
	stat = nc_close(ncID);
	exit(0);
}