GetGranules.c
3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/** @file GetGranules.c
* @brief Stand-alone executable <br> Returns granules for given VI in DDBase <br>
*
* @details GetGranules(string *) <br>
* @arg \c argv[1] Path to VI
* @arg \c argv[2] Times file name
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <sys/stat.h>
#include <netcdf.h>
#include <DD.h>
/*---------------- NC ERROR --------------------------------------*/
void nc_handle_error(int status)
{
fprintf(stderr, "%s\n", nc_strerror(status));
exit(1);
}
/* ----------------------- MAIN ------------------------------------*/
int main(int argc, char *argv[])
{
int status;
if (argc < 3){
fprintf(stderr,"Usage: GetGranules path_to_vi times_file_name\n");
exit(1);
}
char timesPath[512];
memset(timesPath, 0, 512*sizeof(char));
strcat(timesPath, argv[1]);
strcat(timesPath, "/");
strcat(timesPath, argv[2]);
// Open VI_times.nc
int DataID;
if ((status = nc_open(timesPath, NC_NOWRITE, &DataID)) != NC_NOERR)
nc_handle_error(status);
int StartID;
if ((status = nc_inq_varid(DataID, "StartTime", &StartID)) != NC_NOERR)
nc_handle_error(status);
int ndims;
int Startdims[NC_MAX_VAR_DIMS];
if ((status = nc_inq_var(DataID, StartID, 0, 0, &ndims, Startdims, NULL)) != NC_NOERR)
nc_handle_error(status);
int StopID;
if ((status = nc_inq_varid(DataID, "StopTime", &StopID)) != NC_NOERR)
nc_handle_error(status);
int Stopdims[2];
if ((status = nc_inq_var(DataID, StopID, NULL, NULL, NULL, Stopdims, NULL)) != NC_NOERR)
nc_handle_error(status);
int FileID;
if ((status = nc_inq_varid(DataID, "FileName", &FileID)) != NC_NOERR)
nc_handle_error(status);
int Filedims[2];
if ((status = nc_inq_var(DataID, FileID, NULL, NULL, NULL, Filedims, NULL)) != NC_NOERR)
nc_handle_error(status);
if ((Startdims[0] != Stopdims[0]) || (Startdims[0] != Filedims[0])) {
fprintf(stderr, "Error in variables definition\n");
exit(1);
}
size_t nb_records;
if ((nc_inq_dimlen(DataID, Startdims[0], &nb_records)) != NC_NOERR)
nc_handle_error(status);
int i;
char StartTime[TIMELENGTH];
char StopTime[TIMELENGTH];
char FileName[32];
double StartTimestamp;
double StopTimestamp;
size_t count[2] = {1L,TIMELENGTH};
size_t start[2] = {0L,0L};
char filePath[512];
struct stat attrib;
for (i = 0; i < nb_records; ++i) {
count[1] = TIMELENGTH;
start[0] = i;
if ((status = nc_get_vara_text(DataID, StartID, start, count, StartTime)) != NC_NOERR)
nc_handle_error(status);
StartTimestamp = DD_Time2Double(StartTime);
if ((status = nc_get_vara_text(DataID, StopID, start, count, StopTime)) != NC_NOERR)
nc_handle_error(status);
StopTimestamp = DD_Time2Double(StopTime);
count[1] = 32;
if ((status = nc_get_vara_text(DataID, FileID, start, count, FileName)) != NC_NOERR)
nc_handle_error(status);
memset(filePath, 0, 512*sizeof(char));
strcat(filePath, argv[1]);
strcat(filePath, "/");
strcat(filePath, FileName);
strcat(filePath, ".gz");
stat(filePath, &attrib);
printf("%f %f %s %d\n", StartTimestamp, StopTimestamp, FileName, attrib.st_mtime);
}
if ((status = nc_close(DataID)) != NC_NOERR) nc_handle_error(status);
}