2 /* maintains the internal dive list structure */
8 struct dive *alloc_dive(void)
10 const int initial_samples = 5;
14 size = dive_size(initial_samples);
18 memset(dive, 0, size);
19 dive->alloc_samples = initial_samples;
23 struct sample *prepare_sample(struct dive **divep)
25 struct dive *dive = *divep;
27 int nr = dive->samples;
28 int alloc_samples = dive->alloc_samples;
29 struct sample *sample;
30 if (nr >= alloc_samples) {
33 alloc_samples = (alloc_samples * 3)/2 + 10;
34 size = dive_size(alloc_samples);
35 dive = realloc(dive, size);
38 dive->alloc_samples = alloc_samples;
41 sample = dive->sample + nr;
42 memset(sample, 0, sizeof(*sample));
48 void finish_sample(struct dive *dive, struct sample *sample)
54 * So when we re-calculate maxdepth and meandepth, we will
55 * not override the old numbers if they are close to the
58 * Why? Because a dive computer may well actually track the
59 * max depth and mean depth at finer granularity than the
60 * samples it stores. So it's possible that the max and mean
61 * have been reported more correctly originally.
63 * Only if the values calculated from the samples are clearly
64 * different do we override the normal depth values.
66 * This considers 1m to be "clearly different". That's
67 * a totally random number.
69 static void update_depth(depth_t *depth, int new)
74 if (abs(old - new) > 1000)
79 static void update_duration(duration_t *duration, int new)
82 duration->seconds = new;
85 static void update_temperature(temperature_t *temperature, int new)
88 int old = temperature->mkelvin;
90 if (abs(old - new) > 1000)
91 temperature->mkelvin = new;
95 static void fixup_pressure(struct dive *dive, struct sample *sample)
97 unsigned int pressure, index;
100 pressure = sample->cylinderpressure.mbar;
103 index = sample->cylinderindex;
104 if (index >= MAX_CYLINDERS)
106 cyl = dive->cylinder + index;
107 if (!cyl->start.mbar)
108 cyl->start.mbar = pressure;
109 if (!cyl->end.mbar || pressure < cyl->end.mbar)
110 cyl->end.mbar = pressure;
113 struct dive *fixup_dive(struct dive *dive)
116 double depthtime = 0;
118 int start = -1, end = -1;
119 int maxdepth = 0, mintemp = 0;
123 for (i = 0; i < dive->samples; i++) {
124 struct sample *sample = dive->sample + i;
125 int time = sample->time.seconds;
126 int depth = sample->depth.mm;
127 int temp = sample->temperature.mkelvin;
135 if (depth > maxdepth)
139 fixup_pressure(dive, sample);
143 * If we have consecutive identical
144 * temperature readings, throw away
145 * the redundant ones.
147 if (lasttemp == temp)
148 sample->temperature.mkelvin = 0;
152 if (!mintemp || temp < mintemp)
155 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
162 update_duration(&dive->duration, end - start);
164 depthtime /= (end - start);
166 update_depth(&dive->meandepth, depthtime);
167 update_temperature(&dive->watertemp, mintemp);
168 update_depth(&dive->maxdepth, maxdepth);
173 /* Don't pick a zero for MERGE_MIN() */
174 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
175 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
177 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
179 struct sample *p = prepare_sample(&dive);
184 p->time.seconds = time;
185 finish_sample(dive, p);
190 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
192 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
194 int asamples = a->samples;
195 int bsamples = b->samples;
196 struct sample *as = a->sample;
197 struct sample *bs = b->sample;
201 struct sample sample;
206 at = asamples ? as->time.seconds : -1;
207 bt = bsamples ? bs->time.seconds + offset : -1;
209 /* No samples? All done! */
210 if (at < 0 && bt < 0)
211 return fixup_dive(res);
213 /* Only samples from a? */
216 res = add_sample(as, at, res);
222 /* Only samples from b? */
225 res = add_sample(bs, bt, res);
236 /* same-time sample: add a merged sample. Take the non-zero ones */
239 sample.depth = as->depth;
240 if (as->temperature.mkelvin)
241 sample.temperature = as->temperature;
242 if (as->cylinderpressure.mbar)
243 sample.cylinderpressure = as->cylinderpressure;
244 if (as->cylinderindex)
245 sample.cylinderindex = as->cylinderindex;
247 res = add_sample(&sample, at, res);
256 static char *merge_text(const char *a, const char *b)
266 res = malloc(strlen(a) + strlen(b) + 9);
269 sprintf(res, "(%s) or (%s)", a, b);
273 /* Pick whichever has any info (if either). Prefer 'a' */
274 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
281 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
288 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
290 merge_cylinder_type(&res->type, &a->type, &b->type);
291 merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
292 MERGE_MAX(res, a, b, start.mbar);
293 MERGE_MIN(res, a, b, end.mbar);
297 * This could do a lot more merging. Right now it really only
298 * merges almost exact duplicates - something that happens easily
299 * with overlapping dive downloads.
301 struct dive *try_to_merge(struct dive *a, struct dive *b)
306 if (a->when != b->when)
312 res->location = merge_text(a->location, b->location);
313 res->notes = merge_text(a->notes, b->notes);
314 MERGE_MAX(res, a, b, number);
315 MERGE_MAX(res, a, b, maxdepth.mm);
316 res->meandepth.mm = 0;
317 MERGE_MAX(res, a, b, duration.seconds);
318 MERGE_MAX(res, a, b, surfacetime.seconds);
319 MERGE_MAX(res, a, b, airtemp.mkelvin);
320 MERGE_MIN(res, a, b, watertemp.mkelvin);
321 for (i = 0; i < MAX_CYLINDERS; i++)
322 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
324 return merge_samples(res, a, b, 0);