-
Notifications
You must be signed in to change notification settings - Fork 1
/
sanity.c
135 lines (109 loc) · 3.63 KB
/
sanity.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
-------------------------------------------------------------------------
OBJECT NAME: sanity.c
FULL NAME: SAnity Checks for netCDF file.
DESCRIPTION:
NOTES:
COPYRIGHT: University Corporation for Atmospheric Research, 2006
-------------------------------------------------------------------------
*/
#include "define.h"
#include <netcdf.h>
bool getNCattr(int ncid, int varID, const char attr[], std::string & dest);
/* -------------------------------------------------------------------- */
static void checkNumberRecords(int InputFile, DATAFILE_INFO * curFile)
{
int id;
if (nc_inq_dimid(InputFile, "Time", &id) == NC_NOERR)
{
size_t length;
size_t deltaT = (size_t)(curFile->FileEndTime[3] - curFile->FileStartTime[3]) + 1;
nc_inq_dimlen(InputFile, id, &length);
if (length != deltaT)
{
fprintf(stderr, "dataIO.c::GetTimeInterval(): Sanity check failure.");
fprintf(stderr, " %ld records vs. %ld computed.\n", length, deltaT);
nc_close(InputFile);
exit(1);
}
}
}
/* -------------------------------------------------------------------- */
void checkStarts(int fd)
{
int id;
size_t edge[3];
time_t oldBaseTime, BaseTime;
struct tm StartFlight;
bool fileGood = true;
std::string tmpS;
edge[0] = edge[1] = edge[2] = 0;
// We can't do checks without FlightDate. Some asc2cdf files don't
// have it.
if (getNCattr(fd, NC_GLOBAL, "FlightDate", tmpS) == false)
return;
sscanf(tmpS.c_str(), "%2d/%2d/%4d", &StartFlight.tm_mon,
&StartFlight.tm_mday,
&StartFlight.tm_year);
if (getNCattr(fd, NC_GLOBAL, "TimeInterval", tmpS) == false)
return;
sscanf(tmpS.c_str(), "%02d:%02d:%02d",
&StartFlight.tm_hour,
&StartFlight.tm_min,
&StartFlight.tm_sec);
StartFlight.tm_mon--;
StartFlight.tm_year -= 1900;
BaseTime = timegm(&StartFlight);
if (nc_inq_varid(fd, "base_time", &id) == NC_NOERR &&
nc_get_var1_int(fd, id, edge, (int*)&oldBaseTime) == NC_NOERR &&
oldBaseTime != BaseTime)
{
fileGood = false;
printf("\nSanity check failure: File has incorrect base_time\n");
printf(" is (%u) %s",
(unsigned)oldBaseTime, asctime(gmtime(&oldBaseTime)));
printf(" should be (%u) %s",
(unsigned)BaseTime, asctime(gmtime(&BaseTime)));
}
if (nc_inq_varid(fd, "Time", &id) == NC_NOERR)
{
char att[128], oldAtt[128];
size_t len;
nc_get_att_text(fd, id, "units", oldAtt);
nc_inq_attlen(fd, id, "units", &len);
oldAtt[len] = '\0';
strftime(att, 128, "seconds since %F %T %z", &StartFlight);
if (strcmp(att, oldAtt) != 0)
{
fileGood = false;
printf("\nSanity check failure: File has incorrect Time:units\n");
printf(" is: %s\n", oldAtt);
printf(" should be: %s\n", att);
}
}
if (fileGood == false)
{
nc_close(fd);
exit(1);
}
}
/* -------------------------------------------------------------------- */
void performSanityChecks(int InputFile, DATAFILE_INFO * curFile)
{
std::string tmpS;
if (getNCattr(InputFile, NC_GLOBAL, "TimeInterval", tmpS) == false)
return;
if (strcmp(tmpS.c_str(), "00:00:00-00:00:00") == 0)
{
fprintf(stderr, "\nSanity check failure: TimeInterval of 00:00:00-00:00:00, no data in this file?\n");
nc_close(InputFile);
exit(1);
}
return;
// Check that # records in file matches deltaT of GLOBAL_ATTTR:TimeInterval
checkNumberRecords(InputFile, curFile);
// Check that base_time & Time:units have correct start, there are a # of
// files where they had a 1 second offset from FlightDate/TimeInterval
// pair, which were correct.
checkStarts(InputFile);
}