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

6
#define LF 32
saeed's avatar
saeed committed
7

saeed's avatar
saeed committed
8
9
10
long int numLines(FILE *FS){
  long int nl = 0L;
  int c = 0;
11
  while((c=fgetc(FS)) != EOF){
saeed's avatar
saeed committed
12
13
14
15
16
17
18
19
20
21
    if (c=='\n'){
      nl += 1;
    }
  }
  rewind(FS);
  return nl;
}

int Strlen(char *sa){
  int len = 0;
saeed's avatar
saeed committed
22
23
24
25
26
  while(*sa != '\0'){ 
    len += 1;
    sa++;
  }
  return len;
saeed's avatar
saeed committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
}

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
47
int main(int argc, char *argv[]){
saeed's avatar
saeed committed
48
    
49
  if (argc != 5){
saeed's avatar
saeed committed
50
    printf("Usage %s filename (stationame)\n",argv[0]);
saeed's avatar
saeed committed
51
52
53
54
55
    return 1;
  }
    
  char *filename = argv[1];
  char *station  = argv[2];
56
57
58
59
  char *datebeg  = argv[3];
  char *datend   = argv[4];
  int idbeg = atoi(datebeg);
  int idend = atoi(datend);
saeed's avatar
saeed committed
60

saeed's avatar
saeed committed
61
  FILE *FS = NULL;
saeed's avatar
saeed committed
62
  FS = fopen(filename,"r");
saeed's avatar
saeed committed
63
  if (FS == NULL){
saeed's avatar
saeed committed
64
    printf("error in opening the file %s\n", filename);
saeed's avatar
saeed committed
65
66
    return 1;
  }
67

saeed's avatar
saeed committed
68
  long int nline = numLines(FS);
saeed's avatar
saeed committed
69
  
saeed's avatar
saeed committed
70
  char cc = ' ';
saeed's avatar
saeed committed
71
  char field[LF] ="";
72
  char ifield[LF] ="";
saeed's avatar
saeed committed
73
74
75
  int ne = 0;
  int nfl = 0;
  int nl = 0;
76
  //Here we calculate the number of the field from the CSV header
saeed's avatar
saeed committed
77
78
79
80
81
82
83
84
85
86
  while ((cc=fgetc(FS)) != EOF){
    if (cc != ','){
      if(cc=='\n'){
	nl += 1;
	if (nl == 1) break;
      }
    }
    else{
      nfl += 1;
    }
saeed's avatar
saeed committed
87
  }
88
  //printf("number of fields is %d \n", nfl);
saeed's avatar
saeed committed
89
90
  rewind(FS);

91
  char **cfl = malloc(sizeof(char *)*nfl);  // each field is a array of character array
saeed's avatar
saeed committed
92
  
saeed's avatar
saeed committed
93
  nfl = 0;
saeed's avatar
saeed committed
94
95
  nl  = 0;
  ne  = 0;
saeed's avatar
saeed committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

  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
113
      for(int ii=0; ii < (LF-1); ++ii) field[ii] = ' ';
saeed's avatar
saeed committed
114
115
116
117
      ne = 0;
    }
  }

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

saeed's avatar
saeed committed
120
121
  //now we read date time;
  ne = 0;
saeed's avatar
saeed committed
122
  nl = 0;
123
124
  int nit = 0;
  int ifld = 0;
saeed's avatar
saeed committed
125
126
127
  int *icontent = NULL;
  float *fcontent = NULL;
  int ic = 0;
saeed's avatar
saeed committed
128
  for (int i=0; i < nfl; ++i){
saeed's avatar
saeed committed
129
130
    if (Strcmp(cfl[i],station)){
      ic = i;
131
132
      icontent = malloc(sizeof(int) *(nline -1));
      fcontent = malloc(sizeof(float) *(nline -1));
saeed's avatar
saeed committed
133
134
135
136
      while ((cc=fgetc(FS)) != EOF){
	if (cc != ','){
	  if(cc=='\n'){
	    ne = 0;
137
	    nit = 0;
saeed's avatar
saeed committed
138
	    nl += 1;
saeed's avatar
saeed committed
139
	    ifld = 0;
saeed's avatar
saeed committed
140
	  }
saeed's avatar
saeed committed
141
	  else{
142
143
	    if (ic == ifld)  {field[ne]   = cc; ne  += 1;}
	    if (ifld == 0)   {ifield[nit] = cc; nit += 1;}
saeed's avatar
saeed committed
144
145
146
	  }
	}
	else{
147
148
	  if (ifld == 0)  icontent[nl] = atoi(ifield);
	  if (ic == ifld )fcontent[nl] = atof(field);
saeed's avatar
saeed committed
149
150
151
152
	  ifld += 1;
	  ne=0;
	}
      }
saeed's avatar
saeed committed
153
154
    }
  }
saeed's avatar
saeed committed
155
  
156
157
158
159
160
  if (icontent == NULL || fcontent == NULL){ 
    printf("something went wrong. Perhaps the station %s is not in the csv file %s\n",station,filename);
    return 1;
    fclose(FS);
  }
saeed's avatar
saeed committed
161
  fclose(FS);
162
163
164
165
166
167
168
169
170
171

  int ii = 0;
  int ia = 0, ib = 0;
  for(ii=0; ii < (nline-1);++ii){
    if (icontent[ii] == 0 ) break; 
  }

  for(ia=0; ia < (ii+1);++ia){
    if (icontent[ia] == idbeg) break; 
  }
saeed's avatar
saeed committed
172
  
173
174
  for(ib=0; ib < (ii+1);++ib){
    if (icontent[ib] == idend) break; 
saeed's avatar
saeed committed
175
176
  }

177
  printf("%d\t%ld\t%d\t%d\n",ii,(nline -1),ia,ib); 
saeed's avatar
saeed committed
178

179
180
181

  for (int i=ia; i <= ib;++i){
    printf("%d\t%f\n",icontent[i],fcontent[i]);
saeed's avatar
saeed committed
182
  }
183
184
185
186
187
188
189


  float rmax = sfmax(fcontent,ii+1);
  float rmin = sfmin(fcontent,ii+1);
 
  //printf("%f\t%f\n",rmin,rmax); 
  svgmeta svgm;                                                                                                                                                                                              
saeed's avatar
saeed committed
190
  
191
192
193
  FILE * filsvg = svg_init(900, 600, 0.0,(float)(ii+1),rmin,rmax,"ssh.html",&svgm);

  float *xax = (float *) malloc(sizeof(float) *(ii+1));
saeed's avatar
saeed committed
194

195
196
197
198
199
200
201
202
203
204
205
  for (int i=0; i < (ii+1);++i) xax[i] = (float)(i);
  
  if ( filsvg != NULL){                                                                                                                                                                                        
    svg_add(filsvg,xax,fcontent,(ii+1),&svgm);
    svg_close(filsvg);                                                                                                                                                                                            
  }                                     
      
  free(cfl);
  free(icontent);
  free(fcontent);
    
saeed's avatar
saeed committed
206
207
  return 0;
}