svgtimeseries.c 4.01 KB
Newer Older
saeed's avatar
saeed committed
1
2
#include<stdio.h>
#include<stdlib.h>
saeed's avatar
saeed committed
3
#include<math.h>
saeed's avatar
saeed committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
float sfmin(float *y, int N){
  float zm = y[0];
  for (int i=1; i < N; ++i){
    if (y[i] < zm) zm = y[i];
  }
  return zm;
}

float sfmax(float *y, int N){
  float zm = y[0];
  for (int i=1; i < N; ++i){
    if (y[i] > zm) zm = y[i];
  }
  return zm;
}

void create_svg(int, float*, float*);

int main(void){

saeed's avatar
saeed committed
24
25
  float height= 600.0;
  float width = 900.0;
saeed's avatar
saeed committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  FILE *fin = fopen("barseback_ssh.dat","r");

  char c = 0;
  int nl = 0;

  while ((c = fgetc(fin)) != EOF){
    if (c == '\n') nl++; 
  }

  rewind(fin);

  float *ssh = (float *)malloc(sizeof(float) * nl);
  float *xa =  (float *)malloc(sizeof(float) * nl);
  float tmp = 0.0f;
  int ni = 0;
  while(fscanf(fin,"%f",&tmp) && !feof(fin)){
    ssh[ni] = tmp;
    printf("%f\n",ssh[ni]);
    xa[ni] = (float)(ni+1);
    ni++;
  }

  float vmin = sfmin(ssh, nl), vmax = sfmax(ssh, nl); 
saeed's avatar
saeed committed
49
50
  
  height -= 50.0;
saeed's avatar
saeed committed
51
  width  -= 50.0;
saeed's avatar
saeed committed
52
53
  vmin += (0.2*vmin);
  vmax += (0.2*vmax);
saeed's avatar
saeed committed
54
55
56
57
58
59
60

  printf("min is %f and max is %f \n", vmin,vmax);
  for (int i=0; i < nl; ++i){
    ssh[i] = height-((height / (vmax - vmin)) * ssh[i] - ((height *vmin) / (vmax - vmin)));
    xa[i] = (width / (xa[nl-1] - xa[0]) )* xa[i] - ((width *xa[0]) / (xa[nl-1] - xa[0]));
  }
  
saeed's avatar
saeed committed
61
62
  create_svg(nl,xa,ssh);

saeed's avatar
saeed committed
63
64
65
66
  #if 0
  for (int i=0; i < nl; ++i){
    printf("ssh[%d] %f\n",i,ssh[i]);
  }
saeed's avatar
saeed committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  
  float PI  = 4.0*atan(1.0);                                                                                                                                                            
  int   nx  = 100001;
  float dx  = (2.0*PI)/(nx -1);                                                                                                                                                         
  float *xx = (float*)malloc(nx *sizeof(float));                                                                                                                                      
  float *yy = (float*)malloc(nx *sizeof(float));                                                                                                                                     
  for (int i=0; i < nx;++i){                                                                                                                                                           
  xx[i]  = i*dx;                                                                                                                                                                        
  yy[i]  = sin(i*dx) * cos(i*dx);                                                                                                                                                       
}                                

  vmin = sfmin(yy, nx), vmax = sfmax(yy, nx); 
  vmin += (0.2*vmin);
  vmax += (0.2*vmax);
  
  for (int i=0; i < nx; ++i){
  yy[i] = height-((height / (vmax - vmin)) * yy[i] - ((height *vmin) / (vmax - vmin)));
  xx[i] = (width / (xx[nx-1] - xx[0]) )* xx[i] - ((width *xx[0]) / (xx[nx-1] - xx[0]));
}
  
  create_svg(nx,xx,yy);
saeed's avatar
saeed committed
88
89

  free(ssh);
saeed's avatar
saeed committed
90
91
92
  free(xa);
  free(xx);
  free(yy);
saeed's avatar
saeed committed
93

saeed's avatar
saeed committed
94
  #endif
saeed's avatar
saeed committed
95
96
97
98
99
100
101
102
103
104
105
  fclose(fin);

  return 0;

}

void create_svg(int N, float *x, float *y){
  
  FILE *fin = fopen("obs.svg","w");
  fprintf(fin,"<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n");
  fprintf(fin,"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
saeed's avatar
saeed committed
106
  fprintf(fin,"<svg height=\"600px\" version=\"1.1\" viewBox=\"0 0 900 600\" width=\"900px\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n");
saeed's avatar
saeed committed
107
108
109
110
111
112
113
114
115
116
117
118
  fprintf(fin,"<defs><style type=\"text/css\">*{stroke-linecap:square;stroke-linejoin:round;}</style></defs>");
  fprintf(fin,"<g id=\"line2d_1\"> <path d=\"\n");
  
  fprintf(fin,"M%f %f\n",x[0],y[0]);
  for (int i=1; i < N;++i){
    if (i == (N-1)){
      fprintf(fin,"L%f %f\"",x[i],y[i]);
    }
    else{
      fprintf(fin,"L%f %f\n",x[i],y[i]);
    }
  }
saeed's avatar
saeed committed
119
  fprintf(fin," style=\"fill:none;stroke:#ff0000;\"");
saeed's avatar
saeed committed
120
121
122
123
124
125
  fprintf(fin,"/>\n");
  fprintf(fin,"</g>\n");
  fprintf(fin,"</svg>");
  fclose(fin);

}