Blame view

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

d45f025c   Elena.Budnik   merge remote and ...
13
#include <stdio.h>
74b77f58   Elena.Budnik   init remote branch
14
15
16
17
18
19
20
21
#include <stdlib.h>
#include <string.h>
#include <netcdf.h>
 
void check(int stat) 
{
	if (stat != NC_NOERR) 
	{
67a09be7   Elena.Budnik   new info for remote
22
23
		fprintf(stdout,"Unknown");
        // printf("NetCDF error: %s\n", nc_strerror(stat));
74b77f58   Elena.Budnik   init remote branch
24
25
26
27
28
29
30
31
32
33
34
35
36
37
		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
38
39
40
	int infoId;
	float number_attr;
	
67a09be7   Elena.Budnik   new info for remote
41
	const char *info[4];
25f87ff8   Elena.Budnik   info for remote
42
43
	info[0] = "UNITS";
	info[1] = "_FillValue";
67a09be7   Elena.Budnik   new info for remote
44
45
46
47
    info[2] = "FILLVAL";
    info[3] = "VIRTUAL";
    info[4] = "DISPLAY_TYPE";
    info[5] = "DEPEND_1";
25f87ff8   Elena.Budnik   info for remote
48
49

	if (argc <= 3) {
74b77f58   Elena.Budnik   init remote branch
50
51
52
		printf("Incorrect number of arguments\n"); 
		exit(1);
	}
777c3ca9   Benjamin Renard   Fix buffer init i...
53

25f87ff8   Elena.Budnik   info for remote
54
55
	infoId = atoi(argv[3]);
	
777c3ca9   Benjamin Renard   Fix buffer init i...
56
	stat = nc_open(argv[1],0,&ncID); check(stat);
74b77f58   Elena.Budnik   init remote branch
57
58

	stat = nc_inq_varid(ncID, argv[2], &varID); check(stat);
777c3ca9   Benjamin Renard   Fix buffer init i...
59

74b77f58   Elena.Budnik   init remote branch
60
	stat = nc_inq_varndims(ncID, varID, &ndims); check(stat);
777c3ca9   Benjamin Renard   Fix buffer init i...
61

74b77f58   Elena.Budnik   init remote branch
62
	stat = nc_inq_vardimid(ncID, varID, dimids); check(stat);
777c3ca9   Benjamin Renard   Fix buffer init i...
63

74b77f58   Elena.Budnik   init remote branch
64
	size_t len = 0; 	
67a09be7   Elena.Budnik   new info for remote
65
    // size
74b77f58   Elena.Budnik   init remote branch
66
67
68
   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
69
				if (infoId == 0)
74b77f58   Elena.Budnik   init remote branch
70
71
72
73
74
				{
					fprintf(stdout, "%d ", len);
				}
		}
	else
25f87ff8   Elena.Budnik   info for remote
75
		if (infoId == 0)
74b77f58   Elena.Budnik   init remote branch
76
77
78
79
		{ 
			fprintf(stdout,"%d ", len);
		}
		
67a09be7   Elena.Budnik   new info for remote
80
	if (infoId == 1) // components
74b77f58   Elena.Budnik   init remote branch
81
82
	{
		size_t attlen = 0;
67a09be7   Elena.Budnik   new info for remote
83
84
85
86
87
88
		stat = nc_inq_attlen(ncID, varID, "LABL_PTR_1", &attlen);
        
		if (stat != NC_NOERR) {
            fprintf(stdout, "-1");
            exit(0);
        }
777c3ca9   Benjamin Renard   Fix buffer init i...
89
90
		unsigned char *string_attr = (unsigned char *)malloc(attlen * sizeof(char*));
		memset(string_attr, 0, attlen * sizeof(char*));	
67a09be7   Elena.Budnik   new info for remote
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
		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);
        }
74b77f58   Elena.Budnik   init remote branch
111
		for (i = 0; i < ndims; i++) {
25f87ff8   Elena.Budnik   info for remote
112
				stat = nc_inq_dimlen(ncID, dimids[i], &len); check(stat);
74b77f58   Elena.Budnik   init remote branch
113
114
115
116
117
118
119
120
				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++)
		{
777c3ca9   Benjamin Renard   Fix buffer init i...
121
			memset(string_var, 0, length * sizeof(char*));
74b77f58   Elena.Budnik   init remote branch
122
			stat = nc_get_vara(ncID, labelID,  start, count, string_var); check(stat);
25f87ff8   Elena.Budnik   info for remote
123
124
			fprintf(stdout, "%s",  string_var);
			if (i < size -1) fprintf(stdout,",");
74b77f58   Elena.Budnik   init remote branch
125
126
127
128
129
130
			start[0]++;
		}
		free(string_attr);
		free(string_var);
	}
	
67a09be7   Elena.Budnik   new info for remote
131
	if (infoId == 2) // Units
25f87ff8   Elena.Budnik   info for remote
132
133
134
135
136
	{
		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*));	
777c3ca9   Benjamin Renard   Fix buffer init i...
137
		memset(string_attr, 0, attlen * sizeof(char*));
25f87ff8   Elena.Budnik   info for remote
138
139
140
141
142
		stat = nc_get_att(ncID, varID, info[infoId-2], string_attr); check(stat);
		fprintf(stdout, "%s",  string_attr);	
		free(string_attr);
	}
	
67a09be7   Elena.Budnik   new info for remote
143
	if (infoId == 3) // Fill Value
25f87ff8   Elena.Budnik   info for remote
144
	{
67a09be7   Elena.Budnik   new info for remote
145
146
147
148
149
150
151
		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);	 
25f87ff8   Elena.Budnik   info for remote
152
153
	}
	
67a09be7   Elena.Budnik   new info for remote
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
	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*));
777c3ca9   Benjamin Renard   Fix buffer init i...
176
            memset(string_attr, 0, attlen * sizeof(char*));
67a09be7   Elena.Budnik   new info for remote
177
178
179
180
181
182
183
184
185
            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); 
        }
	}
74b77f58   Elena.Budnik   init remote branch
186
187
188
	stat = nc_close(ncID);
	exit(0);
}