sshval.c 4.06 KB
Newer Older
saeed's avatar
saeed committed
1
2
#include<stdio.h>
#include<stdlib.h>
3
#include<math.h>
saeed's avatar
saeed committed
4
#include"sort.h"
saeed's avatar
saeed committed
5

saeed's avatar
saeed committed
6
#define LF (64)
saeed's avatar
saeed committed
7

saeed's avatar
saeed committed
8
9
10
11
12
13
typedef struct Data{
  int   *icontent;
  float *fcontent;
}Data;


saeed's avatar
saeed committed
14
15
16
long int numLines(FILE *FS){
  long int nl = 0L;
  int c = 0;
17
  while((c=fgetc(FS)) != EOF){
saeed's avatar
saeed committed
18
19
20
21
22
23
24
25
26
27
    if (c=='\n'){
      nl += 1;
    }
  }
  rewind(FS);
  return nl;
}

int Strlen(char *sa){
  int len = 0;
saeed's avatar
saeed committed
28
29
30
31
32
  while(*sa != '\0'){ 
    len += 1;
    sa++;
  }
  return len;
saeed's avatar
saeed committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
}

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;
  }
}

saeed's avatar
saeed committed
52
int Readsshcsv(char *filename, char *station, int idbeg, int idend, Data *sshdata){
saeed's avatar
saeed committed
53

saeed's avatar
saeed committed
54
  FILE *FS = NULL;
saeed's avatar
saeed committed
55
  FS = fopen(filename,"r");
saeed's avatar
saeed committed
56
  if (FS == NULL){
saeed's avatar
saeed committed
57
    printf("error in opening the file %s\n", filename);
saeed's avatar
saeed committed
58
59
    return 1;
  }
60

saeed's avatar
saeed committed
61
62
  long int nline = numLines(FS);
  char cc = ' ';
saeed's avatar
saeed committed
63
  char field[LF] ="";
64
  char ifield[LF] ="";
saeed's avatar
saeed committed
65
66
67
  int ne = 0;
  int nfl = 0;
  int nl = 0;
68
  //Here we calculate the number of the field from the CSV header
saeed's avatar
saeed committed
69
70
71
72
73
74
75
76
77
78
  while ((cc=fgetc(FS)) != EOF){
    if (cc != ','){
      if(cc=='\n'){
	nl += 1;
	if (nl == 1) break;
      }
    }
    else{
      nfl += 1;
    }
saeed's avatar
saeed committed
79
  }
80
  //printf("number of fields is %d \n", nfl);
saeed's avatar
saeed committed
81
  rewind(FS);
82
  char **cfl = malloc(sizeof(char *)*nfl);  // each field is a array of character array
saeed's avatar
saeed committed
83
  nfl = 0;
saeed's avatar
saeed committed
84
85
  nl  = 0;
  ne  = 0;
saeed's avatar
saeed committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  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;
saeed's avatar
saeed committed
102
      for(int ii=0; ii < (LF-1); ++ii) field[ii] = ' ';
saeed's avatar
saeed committed
103
104
105
106
      ne = 0;
    }
  }

saeed's avatar
saeed committed
107
  for(int ii=0; ii < (LF -1); ++ii) field[ii] = ' ';
saeed's avatar
saeed committed
108

saeed's avatar
saeed committed
109
  //now we read content;
saeed's avatar
saeed committed
110
  ne = 0;
saeed's avatar
saeed committed
111
  nl = 0;
112
113
  int nit = 0;
  int ifld = 0;
saeed's avatar
saeed committed
114
115
  sshdata->icontent = NULL;
  sshdata->fcontent = NULL;
saeed's avatar
saeed committed
116
  int ic = 0;
saeed's avatar
saeed committed
117
  for (int i=0; i < nfl; ++i){
saeed's avatar
saeed committed
118
119
    if (Strcmp(cfl[i],station)){
      ic = i;
saeed's avatar
saeed committed
120
121
      sshdata->icontent = malloc(sizeof(int) *(nline -1));
      sshdata->fcontent = malloc(sizeof(float) *(nline -1));
saeed's avatar
saeed committed
122
123
124
125
      while ((cc=fgetc(FS)) != EOF){
	if (cc != ','){
	  if(cc=='\n'){
	    ne = 0;
126
	    nit = 0;
saeed's avatar
saeed committed
127
	    nl += 1;
saeed's avatar
saeed committed
128
	    ifld = 0;
saeed's avatar
saeed committed
129
	  }
saeed's avatar
saeed committed
130
	  else{
131
132
	    if (ic == ifld)  {field[ne]   = cc; ne  += 1;}
	    if (ifld == 0)   {ifield[nit] = cc; nit += 1;}
saeed's avatar
saeed committed
133
134
135
	  }
	}
	else{
saeed's avatar
saeed committed
136
137
	  if (ifld == 0)  sshdata->icontent[nl] = atoi(ifield);
	  if (ic == ifld )sshdata->fcontent[nl] = atof(field);
saeed's avatar
saeed committed
138
139
140
141
	  ifld += 1;
	  ne=0;
	}
      }
saeed's avatar
saeed committed
142
143
    }
  }
saeed's avatar
saeed committed
144
  
saeed's avatar
saeed committed
145
  if (sshdata->icontent == NULL || sshdata->fcontent == NULL){ 
146
147
    printf("something went wrong. Perhaps the station %s is not in the csv file %s\n",station,filename);
    fclose(FS);
saeed's avatar
saeed committed
148
    return 1;
149
  }
saeed's avatar
saeed committed
150
  fclose(FS);
151
152

  int ii = 0;
saeed's avatar
saeed committed
153
154
  int ia = -1, ib = -1;
  for(int i=0; i < (nline-1);++i){
saeed's avatar
saeed committed
155
    if (sshdata->icontent[i] == 0 ){
saeed's avatar
saeed committed
156
157
158
      ii = i;
      break; 
    }
159
  }
saeed's avatar
saeed committed
160
  for(int i=0; i < (ii+1);++i){
saeed's avatar
saeed committed
161
    if (sshdata->icontent[i] == idbeg){
saeed's avatar
saeed committed
162
163
164
      ia = i;
      break; 
    }
165
  }
saeed's avatar
saeed committed
166
  for(int i=0; i < (ii+1);++i){
saeed's avatar
saeed committed
167
    if (sshdata->icontent[i] == idend){
saeed's avatar
saeed committed
168
169
170
171
172
173
174
175
176
177
178
      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;
saeed's avatar
saeed committed
179
  }
180
  for (int i=ia; i <= ib;++i){
saeed's avatar
saeed committed
181
    printf("%d\t%f\n",sshdata->icontent[i],sshdata->fcontent[i]);
saeed's avatar
saeed committed
182
  }
saeed's avatar
saeed committed
183

184
  free(cfl);
saeed's avatar
saeed committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  
  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);

saeed's avatar
saeed committed
208
209
  if (sshdata.icontent != NULL)free(sshdata.icontent);
  if (sshdata.fcontent != NULL)free(sshdata.fcontent);
210
    
saeed's avatar
saeed committed
211
212
  return 0;
}