]> git.tdb.fi Git - ext/subsurface.git/blob - dive.c
Removed the unused startemp and enttemp calculations. This fixes a compiler warning...
[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
40         for (i = 0; i < dive->samples; i++) {
41                 struct sample *sample = dive->sample + i;
42                 int time = sample->time.seconds;
43                 int depth = sample->depth.mm;
44                 int press = sample->cylinderpressure.mbar;
45                 int temp = sample->temperature.mkelvin;
46
47                 if (lastdepth)
48                         end = time;
49
50                 if (depth) {
51                         if (start < 0)
52                                 start = lasttime;
53                         if (depth > maxdepth)
54                                 maxdepth = depth;
55                 }
56                 if (press) {
57                         endpress = press;
58                         if (!startpress)
59                                 startpress = press;
60                 }
61                 if (temp) {
62                         if (!mintemp || temp < mintemp)
63                                 mintemp = temp;
64                 }
65                 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
66                 lastdepth = depth;
67                 lasttime = time;
68         }
69         if (end < 0)
70                 return dive;
71         dive->duration.seconds = end - start;
72         if (start != end)
73                 update_depth(&dive->meandepth, depthtime / (end - start));
74         if (startpress)
75                 dive->beginning_pressure.mbar = startpress;
76         if (endpress)
77                 dive->end_pressure.mbar = endpress;
78         if (mintemp)
79                 dive->watertemp.mkelvin = mintemp;
80
81         if (maxdepth)
82                 update_depth(&dive->maxdepth, maxdepth);
83
84         return dive;
85 }
86
87 /* Don't pick a zero for MERGE_MIN() */
88 #define MIN(a,b) ((a)<(b)?(a):(b))
89 #define MAX(a,b) ((a)>(b)?(a):(b))
90 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
91 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
92
93 static int alloc_samples;
94
95 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
96 {
97         int nr = dive->samples;
98         struct sample *d;
99
100         if (nr >= alloc_samples) {
101                 alloc_samples = (alloc_samples + 64) * 3 / 2;
102                 dive = realloc(dive, dive_size(alloc_samples));
103                 if (!dive)
104                         return NULL;
105         }
106         dive->samples = nr+1;
107         d = dive->sample + nr;
108
109         *d = *sample;
110         d->time.seconds = time;
111         return dive;
112 }
113
114 /*
115  * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
116  */
117 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
118 {
119         int asamples = a->samples;
120         int bsamples = b->samples;
121         struct sample *as = a->sample;
122         struct sample *bs = b->sample;
123
124         for (;;) {
125                 int at, bt;
126                 struct sample sample;
127
128                 if (!res)
129                         return NULL;
130
131                 at = asamples ? as->time.seconds : -1;
132                 bt = bsamples ? bs->time.seconds + offset : -1;
133
134                 /* No samples? All done! */
135                 if (at < 0 && bt < 0)
136                         return fixup_dive(res);
137
138                 /* Only samples from a? */
139                 if (bt < 0) {
140 add_sample_a:
141                         res = add_sample(as, at, res);
142                         as++;
143                         asamples--;
144                         continue;
145                 }
146
147                 /* Only samples from b? */
148                 if (at < 0) {
149 add_sample_b:
150                         res = add_sample(bs, bt, res);
151                         bs++;
152                         bsamples--;
153                         continue;
154                 }
155
156                 if (at < bt)
157                         goto add_sample_a;
158                 if (at > bt)
159                         goto add_sample_b;
160
161                 /* same-time sample: add a merged sample. Take the non-zero ones */
162                 sample = *bs;
163                 if (as->depth.mm)
164                         sample.depth = as->depth;
165                 if (as->temperature.mkelvin)
166                         sample.temperature = as->temperature;
167                 if (as->cylinderpressure.mbar)
168                         sample.cylinderpressure = as->cylinderpressure;
169                 if (as->cylinderindex)
170                         sample.cylinderindex = as->cylinderindex;
171
172                 res = add_sample(&sample, at, res);
173
174                 as++;
175                 bs++;
176                 asamples--;
177                 bsamples--;
178         }
179 }
180
181 static char *merge_text(const char *a, const char *b)
182 {
183         char *res;
184
185         if (!a || !*a)
186                 return (char *)b;
187         if (!b || !*b)
188                 return (char *)a;
189         if (!strcmp(a,b))
190                 return (char *)a;
191         res = malloc(strlen(a) + strlen(b) + 9);
192         if (!res)
193                 return (char *)a;
194         sprintf(res, "(%s) or (%s)", a, b);
195         return res;
196 }
197
198 /* Pick whichever has any info (if either). Prefer 'a' */
199 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
200 {
201         if (a->size.mliter)
202                 b = a;
203         *res = *b;
204 }
205
206 static void merge_cylinder_mix(gasmix_t *res, gasmix_t *a, gasmix_t *b)
207 {
208         if (a->o2.permille)
209                 b = a;
210         *res = *b;
211 }
212
213 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
214 {
215         merge_cylinder_type(&res->type, &a->type, &b->type);
216         merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
217 }
218
219 /*
220  * This could do a lot more merging. Right now it really only
221  * merges almost exact duplicates - something that happens easily
222  * with overlapping dive downloads.
223  */
224 struct dive *try_to_merge(struct dive *a, struct dive *b)
225 {
226         int i;
227         struct dive *res;
228
229         if (a->when != b->when)
230                 return NULL;
231
232         alloc_samples = 5;
233         res = malloc(dive_size(alloc_samples));
234         if (!res)
235                 return NULL;
236         memset(res, 0, dive_size(alloc_samples));
237
238         res->when = a->when;
239         res->name = merge_text(a->name, b->name);
240         res->location = merge_text(a->location, b->location);
241         res->notes = merge_text(a->notes, b->notes);
242         MERGE_MAX(res, a, b, maxdepth.mm);
243         res->meandepth.mm = 0;
244         MERGE_MAX(res, a, b, duration.seconds);
245         MERGE_MAX(res, a, b, surfacetime.seconds);
246         MERGE_MAX(res, a, b, airtemp.mkelvin);
247         MERGE_MIN(res, a, b, watertemp.mkelvin);
248         MERGE_MAX(res, a, b, beginning_pressure.mbar);
249         MERGE_MAX(res, a, b, end_pressure.mbar);
250         for (i = 0; i < MAX_CYLINDERS; i++)
251                 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
252
253         return merge_samples(res, a, b, 0);
254 }