]> git.tdb.fi Git - ext/subsurface.git/blob - dive.c
Plot a sick kind of temperature curve
[ext/subsurface.git] / dive.c
1 #include <string.h>
2 #include <stdio.h>
3
4 #include "dive.h"
5
6 struct dive *alloc_dive(void)
7 {
8         const int initial_samples = 5;
9         unsigned int size;
10         struct dive *dive;
11
12         size = dive_size(initial_samples);
13         dive = malloc(size);
14         if (!dive)
15                 exit(1);
16         memset(dive, 0, size);
17         dive->alloc_samples = initial_samples;
18         return dive;
19 }
20
21 struct sample *prepare_sample(struct dive **divep)
22 {
23         struct dive *dive = *divep;
24         if (dive) {
25                 int nr = dive->samples;
26                 int alloc_samples = dive->alloc_samples;
27                 struct sample *sample;
28                 if (nr >= alloc_samples) {
29                         unsigned int size;
30
31                         alloc_samples = (alloc_samples * 3)/2 + 10;
32                         size = dive_size(alloc_samples);
33                         dive = realloc(dive, size);
34                         if (!dive)
35                                 return NULL;
36                         dive->alloc_samples = alloc_samples;
37                         *divep = dive;
38                 }
39                 sample = dive->sample + nr;
40                 memset(sample, 0, sizeof(*sample));
41                 return sample;
42         }
43         return NULL;
44 }
45
46 void finish_sample(struct dive *dive, struct sample *sample)
47 {
48         dive->samples++;
49 }
50
51 /*
52  * So when we re-calculate maxdepth and meandepth, we will
53  * not override the old numbers if they are close to the
54  * new ones.
55  *
56  * Why? Because a dive computer may well actually track the
57  * max depth and mean depth at finer granularity than the
58  * samples it stores. So it's possible that the max and mean
59  * have been reported more correctly originally.
60  *
61  * Only if the values calculated from the samples are clearly
62  * different do we override the normal depth values.
63  *
64  * This considers 1m to be "clearly different". That's
65  * a totally random number.
66  */
67 static void update_depth(depth_t *depth, int new)
68 {
69         if (new) {
70                 int old = depth->mm;
71
72                 if (abs(old - new) > 1000)
73                         depth->mm = new;
74         }
75 }
76
77 static void update_duration(duration_t *duration, int new)
78 {
79         if (new)
80                 duration->seconds = new;
81 }
82
83 static void update_temperature(temperature_t *temperature, int new)
84 {
85         if (new) {
86                 int old = temperature->mkelvin;
87
88                 if (abs(old - new) > 1000)
89                         temperature->mkelvin = new;
90         }
91 }
92
93 static void fixup_pressure(struct dive *dive, struct sample *sample)
94 {
95         unsigned int pressure, index;
96         cylinder_t *cyl;
97
98         pressure = sample->cylinderpressure.mbar;
99         if (!pressure)
100                 return;
101         index = sample->cylinderindex;
102         if (index >= MAX_CYLINDERS)
103                 return;
104         cyl = dive->cylinder + index;
105         if (!cyl->start.mbar)
106                 cyl->start.mbar = pressure;
107         if (!cyl->end.mbar || pressure < cyl->end.mbar)
108                 cyl->end.mbar = pressure;
109 }
110
111 struct dive *fixup_dive(struct dive *dive)
112 {
113         int i;
114         double depthtime = 0;
115         int lasttime = 0;
116         int start = -1, end = -1;
117         int maxdepth = 0, mintemp = 0;
118         int lastdepth = 0;
119         int lasttemp = 0;
120
121         for (i = 0; i < dive->samples; i++) {
122                 struct sample *sample = dive->sample + i;
123                 int time = sample->time.seconds;
124                 int depth = sample->depth.mm;
125                 int temp = sample->temperature.mkelvin;
126
127                 if (lastdepth)
128                         end = time;
129
130                 if (depth) {
131                         if (start < 0)
132                                 start = lasttime;
133                         if (depth > maxdepth)
134                                 maxdepth = depth;
135                 }
136
137                 fixup_pressure(dive, sample);
138
139                 if (temp) {
140                         /*
141                          * If we have consecutive identical
142                          * temperature readings, throw away
143                          * the redundant ones.
144                          */
145                         if (lasttemp == temp)
146                                 sample->temperature.mkelvin = 0;
147                         else
148                                 lasttemp = temp;
149
150                         if (!mintemp || temp < mintemp)
151                                 mintemp = temp;
152                 }
153                 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
154                 lastdepth = depth;
155                 lasttime = time;
156         }
157         if (end < 0)
158                 return dive;
159
160         update_duration(&dive->duration, end - start);
161         if (start != end)
162                 depthtime /= (end - start);
163
164         update_depth(&dive->meandepth, depthtime);
165         update_temperature(&dive->watertemp, mintemp);
166         update_depth(&dive->maxdepth, maxdepth);
167
168         return dive;
169 }
170
171 /* Don't pick a zero for MERGE_MIN() */
172 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
173 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
174
175 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
176 {
177         struct sample *p = prepare_sample(&dive);
178
179         if (!p)
180                 return NULL;
181         *p = *sample;
182         p->time.seconds = time;
183         finish_sample(dive, p);
184         return dive;
185 }
186
187 /*
188  * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
189  */
190 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
191 {
192         int asamples = a->samples;
193         int bsamples = b->samples;
194         struct sample *as = a->sample;
195         struct sample *bs = b->sample;
196
197         for (;;) {
198                 int at, bt;
199                 struct sample sample;
200
201                 if (!res)
202                         return NULL;
203
204                 at = asamples ? as->time.seconds : -1;
205                 bt = bsamples ? bs->time.seconds + offset : -1;
206
207                 /* No samples? All done! */
208                 if (at < 0 && bt < 0)
209                         return fixup_dive(res);
210
211                 /* Only samples from a? */
212                 if (bt < 0) {
213 add_sample_a:
214                         res = add_sample(as, at, res);
215                         as++;
216                         asamples--;
217                         continue;
218                 }
219
220                 /* Only samples from b? */
221                 if (at < 0) {
222 add_sample_b:
223                         res = add_sample(bs, bt, res);
224                         bs++;
225                         bsamples--;
226                         continue;
227                 }
228
229                 if (at < bt)
230                         goto add_sample_a;
231                 if (at > bt)
232                         goto add_sample_b;
233
234                 /* same-time sample: add a merged sample. Take the non-zero ones */
235                 sample = *bs;
236                 if (as->depth.mm)
237                         sample.depth = as->depth;
238                 if (as->temperature.mkelvin)
239                         sample.temperature = as->temperature;
240                 if (as->cylinderpressure.mbar)
241                         sample.cylinderpressure = as->cylinderpressure;
242                 if (as->cylinderindex)
243                         sample.cylinderindex = as->cylinderindex;
244
245                 res = add_sample(&sample, at, res);
246
247                 as++;
248                 bs++;
249                 asamples--;
250                 bsamples--;
251         }
252 }
253
254 static char *merge_text(const char *a, const char *b)
255 {
256         char *res;
257
258         if (!a || !*a)
259                 return (char *)b;
260         if (!b || !*b)
261                 return (char *)a;
262         if (!strcmp(a,b))
263                 return (char *)a;
264         res = malloc(strlen(a) + strlen(b) + 9);
265         if (!res)
266                 return (char *)a;
267         sprintf(res, "(%s) or (%s)", a, b);
268         return res;
269 }
270
271 /* Pick whichever has any info (if either). Prefer 'a' */
272 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
273 {
274         if (a->size.mliter)
275                 b = a;
276         *res = *b;
277 }
278
279 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
280 {
281         if (a->o2.permille)
282                 b = a;
283         *res = *b;
284 }
285
286 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
287 {
288         merge_cylinder_type(&res->type, &a->type, &b->type);
289         merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
290         MERGE_MAX(res, a, b, start.mbar);
291         MERGE_MIN(res, a, b, end.mbar);
292 }
293
294 /*
295  * This could do a lot more merging. Right now it really only
296  * merges almost exact duplicates - something that happens easily
297  * with overlapping dive downloads.
298  */
299 struct dive *try_to_merge(struct dive *a, struct dive *b)
300 {
301         int i;
302         struct dive *res;
303
304         if (a->when != b->when)
305                 return NULL;
306
307         res = alloc_dive();
308
309         res->when = a->when;
310         res->location = merge_text(a->location, b->location);
311         res->notes = merge_text(a->notes, b->notes);
312         MERGE_MAX(res, a, b, number);
313         MERGE_MAX(res, a, b, maxdepth.mm);
314         res->meandepth.mm = 0;
315         MERGE_MAX(res, a, b, duration.seconds);
316         MERGE_MAX(res, a, b, surfacetime.seconds);
317         MERGE_MAX(res, a, b, airtemp.mkelvin);
318         MERGE_MIN(res, a, b, watertemp.mkelvin);
319         for (i = 0; i < MAX_CYLINDERS; i++)
320                 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
321
322         return merge_samples(res, a, b, 0);
323 }