DD_TimeVar.c
1.74 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
/*=====================================================================
* DD SYSTEM base package
* DD_Server library
* DD_TimeVar.c
* V.1.0
* Last revision:
* June 03 2019: Version 1.0. Common methods used for time variable
*/
#include <netcdf.h>
#include "DD.h"
#include "DD_comm.h"
int Time_GetVarID(int ncID) {
static char TimeName[] = "Time";
int status = NC_NOERR;
int TimeID = -1;
status = nc_inq_varid(ncID, TimeName, &TimeID);
if (status != NC_NOERR) {
return -1;
}
return TimeID;
}
int Time_IsDoubleVar(int ncID, int timeVarID) {
nc_type TimeType;
int status = NC_NOERR;
status = nc_inq_var (ncID, timeVarID, NULL, &TimeType, NULL, NULL, NULL);
if (status != NC_NOERR) {
return 0;
}
return (TimeType == NC_DOUBLE) ? 1 : 0;
}
double Time_GetValueFromTextVar(int ncID, int timeVarID, int recNum) {
static char TimeStr[TIMELENGTH];
static size_t TimeCountText[2] = {1,TIMELENGTH};
size_t TimeStartText[2] = {recNum,0};
int status = NC_NOERR;
status = nc_get_vara_text(ncID,timeVarID,TimeStartText,TimeCountText,TimeStr);
if (status != NC_NOERR) {
return 0.;
}
return DD_Time2Double(TimeStr);
}
double Time_GetValueFromDoubleVar(int ncID, int timeVarID, int recNum) {
static size_t TimeCountDouble[1] = {1};
size_t TimeStartDouble[1] = {recNum};
double val;
int status = NC_NOERR;
status = nc_get_vara_double (ncID,timeVarID,TimeStartDouble,TimeCountDouble,&val);
if (status != NC_NOERR) {
return 0.;
}
return val;
}
double Time_GetValueFromVar(int ncID, int timeVarID, int isDoubleTime, int recNum) {
if (isDoubleTime) {
return Time_GetValueFromDoubleVar(ncID, timeVarID, recNum);
}
return Time_GetValueFromTextVar(ncID, timeVarID, recNum);
}