-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshval.c
212 lines (188 loc) · 4.07 KB
/
sshval.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include"sort.h"
#define LF (64)
typedef struct Data{
int *icontent;
float *fcontent;
}Data;
long int numLines(FILE *FS){
long int nl = 0L;
int c = 0;
while((c=fgetc(FS)) != EOF){
if (c=='\n'){
nl += 1;
}
}
rewind(FS);
return nl;
}
int Strlen(char *sa){
int len = 0;
while(*sa != '\0'){
len += 1;
sa++;
}
return len;
}
int Strcmp(char *sa, char *sb){
if (Strlen(sa) != Strlen(sb)){
return 0;
}
while ((*sa == *sb) && *sa != '\0' && *sb != '\0' ){
sa++;
sb++;
}
if (*sa == '\0'){
return 1;
}
else{
return 0;
}
}
static int Readsshcsv(char *filename, char *station, int idbeg, int idend, Data *sshdata){
FILE *FS = NULL;
FS = fopen(filename,"r");
if (FS == NULL){
printf("error in opening the file %s\n", filename);
return 1;
}
long int nline = numLines(FS);
char cc = ' ';
char field[LF] ="";
char ifield[LF] ="";
int ne = 0;
int nfl = 0;
int nl = 0;
//Here we calculate the number of the field from the CSV header
while ((cc=fgetc(FS)) != EOF){
if (cc != ','){
if(cc=='\n'){
nl += 1;
if (nl == 1) break;
}
}
else{
nfl += 1;
}
}
//printf("number of fields is %d \n", nfl);
rewind(FS);
char **cfl = malloc(sizeof(char *)*nfl); // each field is a array of character array
nfl = 0;
nl = 0;
ne = 0;
while ((cc=fgetc(FS)) != EOF){
if (cc != ','){
if(cc=='\n'){
ne = 0;
nl += 1;
if (nl == 1) break;
}
else {
field[ne] = cc;
ne += 1;
}
}
else{
cfl[nfl] = malloc(sizeof(char) *ne);
for(int ii=0; ii < ne; ++ii) cfl[nfl][ii] = field[ii];
nfl += 1;
for(int ii=0; ii < (LF-1); ++ii) field[ii] = ' ';
ne = 0;
}
}
for(int ii=0; ii < (LF -1); ++ii) field[ii] = ' ';
//now we read content;
ne = 0;
nl = 0;
int nit = 0;
int ifld = 0;
sshdata->icontent = NULL;
sshdata->fcontent = NULL;
int ic = 0;
for (int i=0; i < nfl; ++i){
if (Strcmp(cfl[i],station)){
ic = i;
sshdata->icontent = malloc(sizeof(int) *(nline -1));
sshdata->fcontent = malloc(sizeof(float) *(nline -1));
while ((cc=fgetc(FS)) != EOF){
if (cc != ','){
if(cc=='\n'){
ne = 0;
nit = 0;
nl += 1;
ifld = 0;
}
else{
if (ic == ifld) {field[ne] = cc; ne += 1;}
if (ifld == 0) {ifield[nit] = cc; nit += 1;}
}
}
else{
if (ifld == 0) sshdata->icontent[nl] = atoi(ifield);
if (ic == ifld )sshdata->fcontent[nl] = atof(field);
ifld += 1;
ne=0;
}
}
}
}
if (sshdata->icontent == NULL || sshdata->fcontent == NULL){
printf("something went wrong. Perhaps the station %s is not in the csv file %s\n",station,filename);
fclose(FS);
return 1;
}
fclose(FS);
int ii = 0;
int ia = -1, ib = -1;
for(int i=0; i < (nline-1);++i){
if (sshdata->icontent[i] == 0 ){
ii = i;
break;
}
}
for(int i=0; i < (ii+1);++i){
if (sshdata->icontent[i] == idbeg){
ia = i;
break;
}
}
for(int i=0; i < (ii+1);++i){
if (sshdata->icontent[i] == idend){
ib = i;
break;
}
}
if (ia == -1 || ib == -1){
printf("startdate %d or enddate %d do not exist in data\n", idbeg, idend);
return 1;
}
if (ia > ib ){
printf("startdate %d is bigger than enddate %d \n", idbeg, idend);
return 1;
}
for (int i=ia; i <= ib;++i){
printf("%d\t%f\n",sshdata->icontent[i],sshdata->fcontent[i]);
}
free(cfl);
return 0;
}
int main(int argc, char *argv[]){
if (argc != 5){
printf("Usage %s filename stationame startdate(YYYYMMDDHH) enddate(YYYYMMDDHH)\n",argv[0]);
return 1;
}
char *filename = argv[1];
char *station = argv[2];
char *datebeg = argv[3];
char *datend = argv[4];
int idbeg = atoi(datebeg);
int idend = atoi(datend);
Data sshdata = {NULL, NULL};
Readsshcsv(filename, station, idbeg, idend, &sshdata);
if (sshdata.icontent != NULL)free(sshdata.icontent);
if (sshdata.fcontent != NULL)free(sshdata.fcontent);
return 0;
}