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