GetFileNames.c
4.12 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
119
120
121
122
123
124
125
126
127
/* $Id: GetFileNames.c,v 1.2 2008/06/16 12:11:58 elena Exp $ */
/**
* @file GetFileNames.c
* @brief Print File names
* @arg dataSetID, StartTime, StopTime
*
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <netcdf.h>
#include <DD.h>
#define NAME_LENGTH 100
#define NAME_DIM 32L
int DataID, RecID, StartID, StopID, NameID;;
size_t RecNum;
char StartT[TIMELENGTH], StopT[TIMELENGTH];
size_t count[2] = {1L,TIMELENGTH};
size_t start[2] = {0L,0L};
size_t NameCount[2] = {1, NAME_DIM};
double StartTime, StopTime;
char Name[NAME_LENGTH];
/*---------------- 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, len, StartRecord, StopRecord;
int i, hi, lo = 0;
double StartD0, StartD1 = 0.0, StopD0, StopD1;
if (argc < 4)
{
fprintf(stderr,"Usage: GetFileNames ViId StartTime StopTime\n");
exit(0);
}
/* find VI in reference file */
StartTime = atof(argv[2]);
StopTime = atof(argv[3]);
/* Open VI_times.nc */
if ((status = nc_open(argv[1], NC_NOWRITE, &DataID)) != NC_NOERR)
nc_handle_error(status);
if ((status = nc_inq_dimid(DataID, "record", &RecID)) != NC_NOERR)
nc_handle_error(status);
/* Get Number of Records */
if ((status = nc_inq_dimlen(DataID, RecID, &RecNum)) != NC_NOERR)
nc_handle_error(status);
if ((status = nc_inq_varid(DataID, "StartTime", &StartID)) != NC_NOERR)
nc_handle_error(status);
if ((status = nc_inq_varid(DataID, "StopTime", &StopID)) != NC_NOERR)
nc_handle_error(status);
if ((status = nc_inq_varid(DataID, "FileName", &NameID)) != NC_NOERR)
nc_handle_error(status);
/* Get The First Start and The Last Stop Times */
start[0] = 0;
if((status = nc_get_vara_text(DataID, StartID, start, count, StartT)) != NC_NOERR)
nc_handle_error(status);
start[0] = RecNum-1;
if((status = nc_get_vara_text(DataID, StopID, start, count, StopT)) != NC_NOERR)
nc_handle_error(status);
if (StartTime > DD_Time2Double(StopT) || StopTime < DD_Time2Double(StartT))
{
fprintf(stdout, "OUTOFTIME\0");
nc_close(DataID);
exit(0);
}
hi = RecNum;
/* Find Start Record */
while (lo < hi)
{
i = (lo+hi)/2;
start[0] = i + 1;
status = nc_get_vara_text(DataID,StartID,start,count,(char *)StartT);
StartD1 = DD_Time2Double(StartT);
if (StartD1 > StartTime) hi--;
start[0] = i;
status = nc_get_vara_text(DataID,StartID,start,count,(char *)StartT);
StartD0 = DD_Time2Double(StartT);
if (StartD0 < StartTime) lo++;
if (StartD1 <= StartTime && StartD0 >= StartTime) break;
}
status = nc_get_vara_text(DataID, NameID, start, NameCount,(char *)Name);
fprintf(stdout, "%s;",Name);
status = nc_get_vara_text(DataID,StopID,start,count,(char *)StopT);
StopD0 = DD_Time2Double(StopT);
while (StopTime > StopD0 && start[0] <= RecNum) {
status = nc_get_vara_text(DataID,StopID,start,count,(char *)StopT);
status = nc_get_vara_text(DataID, NameID, start, NameCount,(char *)Name);
if (start[0] > i) fprintf(stdout, "%s;", Name);
StopD0 = DD_Time2Double(StopT);
start[0]++;
}
nc_close(DataID);
}