Commit a601c793ccc160360048622d0779f0387a1d6a84
1 parent
6544052e
Exists in
master
and in
3 other branches
Add script to retrieve granules list
Showing
1 changed file
with
101 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,101 @@ |
1 | + | |
2 | +/** @file GetGranules.c | |
3 | +* @brief Stand-alone executable <br> Returns granules for given VI in DDBase <br> | |
4 | +* | |
5 | +* @details GetGranules(string *) <br> | |
6 | +* @arg \c argv[1] Full name of *_times.nc | |
7 | +* | |
8 | +*/ | |
9 | + | |
10 | + | |
11 | +#include <stdlib.h> | |
12 | +#include <stdio.h> | |
13 | +#include <string.h> | |
14 | +#include <time.h> | |
15 | +#include <math.h> | |
16 | +#include <netcdf.h> | |
17 | +#include <DD.h> | |
18 | + | |
19 | + | |
20 | +/*---------------- NC ERROR --------------------------------------*/ | |
21 | +void nc_handle_error(int status) | |
22 | +{ | |
23 | + fprintf(stderr, "%s\n", nc_strerror(status)); | |
24 | + exit(1); | |
25 | +} | |
26 | + | |
27 | +/* ----------------------- MAIN ------------------------------------*/ | |
28 | +int main(int argc, char *argv[]) | |
29 | +{ | |
30 | + int status; | |
31 | + | |
32 | + if (argc < 2){ | |
33 | + fprintf(stderr,"Usage: GetGranules *_times.nc\n"); | |
34 | + exit(1); | |
35 | + } | |
36 | + | |
37 | + // Open VI_times.nc | |
38 | + int DataID; | |
39 | + if ((status = nc_open(argv[1], NC_NOWRITE, &DataID)) != NC_NOERR) | |
40 | + nc_handle_error(status); | |
41 | + | |
42 | + int StartID; | |
43 | + if ((status = nc_inq_varid(DataID, "StartTime", &StartID)) != NC_NOERR) | |
44 | + nc_handle_error(status); | |
45 | + | |
46 | + int ndims; | |
47 | + int Startdims[NC_MAX_VAR_DIMS]; | |
48 | + if ((status = nc_inq_var(DataID, StartID, 0, 0, &ndims, Startdims, NULL)) != NC_NOERR) | |
49 | + nc_handle_error(status); | |
50 | + | |
51 | + int StopID; | |
52 | + if ((status = nc_inq_varid(DataID, "StopTime", &StopID)) != NC_NOERR) | |
53 | + nc_handle_error(status); | |
54 | + | |
55 | + int Stopdims[2]; | |
56 | + if ((status = nc_inq_var(DataID, StopID, NULL, NULL, NULL, Stopdims, NULL)) != NC_NOERR) | |
57 | + nc_handle_error(status); | |
58 | + | |
59 | + int FileID; | |
60 | + if ((status = nc_inq_varid(DataID, "FileName", &FileID)) != NC_NOERR) | |
61 | + nc_handle_error(status); | |
62 | + | |
63 | + int Filedims[2]; | |
64 | + if ((status = nc_inq_var(DataID, FileID, NULL, NULL, NULL, Filedims, NULL)) != NC_NOERR) | |
65 | + nc_handle_error(status); | |
66 | + | |
67 | + if ((Startdims[0] != Stopdims[0]) || (Startdims[0] != Filedims[0])) { | |
68 | + fprintf(stderr, "Error in variables definition\n"); | |
69 | + exit(1); | |
70 | + } | |
71 | + | |
72 | + size_t nb_records; | |
73 | + if ((nc_inq_dimlen(DataID, Startdims[0], &nb_records)) != NC_NOERR) | |
74 | + nc_handle_error(status); | |
75 | + | |
76 | + int i; | |
77 | + char StartTime[TIMELENGTH]; | |
78 | + char StopTime[TIMELENGTH]; | |
79 | + char FileName[32]; | |
80 | + double StartTimestamp; | |
81 | + double StopTimestamp; | |
82 | + size_t count[2] = {1L,TIMELENGTH}; | |
83 | + size_t start[2] = {0L,0L}; | |
84 | + | |
85 | + for (i = 0; i < nb_records; ++i) { | |
86 | + count[1] = TIMELENGTH; | |
87 | + start[0] = i; | |
88 | + if ((status = nc_get_vara_text(DataID, StartID, start, count, StartTime)) != NC_NOERR) | |
89 | + nc_handle_error(status); | |
90 | + StartTimestamp = DD_Time2Double(StartTime); | |
91 | + if ((status = nc_get_vara_text(DataID, StopID, start, count, StopTime)) != NC_NOERR) | |
92 | + nc_handle_error(status); | |
93 | + StopTimestamp = DD_Time2Double(StopTime); | |
94 | + count[1] = 32; | |
95 | + if ((status = nc_get_vara_text(DataID, FileID, start, count, FileName)) != NC_NOERR) | |
96 | + nc_handle_error(status); | |
97 | + printf("%f %f\n", StartTimestamp, StopTimestamp); | |
98 | + } | |
99 | + | |
100 | + if ((status = nc_close(DataID)) != NC_NOERR) nc_handle_error(status); | |
101 | +} | ... | ... |