2 /* maintains the internal dive list structure */
8 void add_event(struct dive *dive, int time, int type, int flags, int value, const char *name)
10 struct event *ev, **p;
11 unsigned int size, len = strlen(name);
13 size = sizeof(*ev) + len + 1;
18 memcpy(ev->name, name, len);
19 ev->time.seconds = time;
32 double get_depth_units(unsigned int mm, int *frac, const char **units)
38 switch (output_units.length) {
57 struct dive *alloc_dive(void)
59 const int initial_samples = 5;
63 size = dive_size(initial_samples);
67 memset(dive, 0, size);
68 dive->alloc_samples = initial_samples;
72 struct sample *prepare_sample(struct dive **divep)
74 struct dive *dive = *divep;
76 int nr = dive->samples;
77 int alloc_samples = dive->alloc_samples;
78 struct sample *sample;
79 if (nr >= alloc_samples) {
82 alloc_samples = (alloc_samples * 3)/2 + 10;
83 size = dive_size(alloc_samples);
84 dive = realloc(dive, size);
87 dive->alloc_samples = alloc_samples;
90 sample = dive->sample + nr;
91 memset(sample, 0, sizeof(*sample));
97 void finish_sample(struct dive *dive, struct sample *sample)
103 * So when we re-calculate maxdepth and meandepth, we will
104 * not override the old numbers if they are close to the
107 * Why? Because a dive computer may well actually track the
108 * max depth and mean depth at finer granularity than the
109 * samples it stores. So it's possible that the max and mean
110 * have been reported more correctly originally.
112 * Only if the values calculated from the samples are clearly
113 * different do we override the normal depth values.
115 * This considers 1m to be "clearly different". That's
116 * a totally random number.
118 static void update_depth(depth_t *depth, int new)
123 if (abs(old - new) > 1000)
128 static void update_duration(duration_t *duration, int new)
131 duration->seconds = new;
134 static void update_temperature(temperature_t *temperature, int new)
137 int old = temperature->mkelvin;
139 if (abs(old - new) > 1000)
140 temperature->mkelvin = new;
145 * If you have more than 32 cylinders, you'd better have a 64-bit build.
146 * And if you have more than 64 cylinders, you need to use another tool,
147 * or fix this up to do something odd.
149 static unsigned long fixup_pressure(struct dive *dive, struct sample *sample, unsigned long flags)
152 unsigned int pressure, index;
155 pressure = sample->cylinderpressure.mbar;
158 index = sample->cylinderindex;
159 if (index >= MAX_CYLINDERS)
161 cyl = dive->cylinder + index;
162 if (!cyl->start.mbar)
163 cyl->start.mbar = pressure;
165 * If we already have an end pressure, without
166 * ever having seen a sample for this cylinder,
167 * that means that somebody set the end pressure
177 /* we need to handle the user entering beginning and end tank pressures
178 * - maybe even IF we have samples. But for now if we have air pressure
179 * data in the samples, we use that instead of the minimum
180 * if (!cyl->end.mbar || pressure < cyl->end.mbar)
182 cyl->end.mbar = pressure;
186 struct dive *fixup_dive(struct dive *dive)
189 double depthtime = 0;
191 int start = -1, end = -1;
192 int maxdepth = 0, mintemp = 0;
195 unsigned long flags = 0;
197 for (i = 0; i < dive->samples; i++) {
198 struct sample *sample = dive->sample + i;
199 int time = sample->time.seconds;
200 int depth = sample->depth.mm;
201 int temp = sample->temperature.mkelvin;
209 if (depth > maxdepth)
213 flags = fixup_pressure(dive, sample, flags);
217 * If we have consecutive identical
218 * temperature readings, throw away
219 * the redundant ones.
221 if (lasttemp == temp)
222 sample->temperature.mkelvin = 0;
226 if (!mintemp || temp < mintemp)
229 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
236 update_duration(&dive->duration, end - start);
238 depthtime /= (end - start);
240 update_depth(&dive->meandepth, depthtime);
241 update_temperature(&dive->watertemp, mintemp);
242 update_depth(&dive->maxdepth, maxdepth);
244 add_people(dive->buddy);
245 add_people(dive->divemaster);
246 add_location(dive->location);
247 for (i = 0; i < MAX_CYLINDERS; i++) {
248 cylinder_type_t *type = &dive->cylinder[i].type;
249 add_cylinder_description(type);
255 /* Don't pick a zero for MERGE_MIN() */
256 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
257 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
258 #define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n)
259 #define MERGE_NONZERO(res, a, b, n) res->n = a->n ? a->n : b->n
261 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
263 struct sample *p = prepare_sample(&dive);
268 p->time.seconds = time;
269 finish_sample(dive, p);
274 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
276 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
278 int asamples = a->samples;
279 int bsamples = b->samples;
280 struct sample *as = a->sample;
281 struct sample *bs = b->sample;
285 struct sample sample;
290 at = asamples ? as->time.seconds : -1;
291 bt = bsamples ? bs->time.seconds + offset : -1;
293 /* No samples? All done! */
294 if (at < 0 && bt < 0)
295 return fixup_dive(res);
297 /* Only samples from a? */
300 res = add_sample(as, at, res);
306 /* Only samples from b? */
309 res = add_sample(bs, bt, res);
320 /* same-time sample: add a merged sample. Take the non-zero ones */
323 sample.depth = as->depth;
324 if (as->temperature.mkelvin)
325 sample.temperature = as->temperature;
326 if (as->cylinderpressure.mbar)
327 sample.cylinderpressure = as->cylinderpressure;
328 if (as->cylinderindex)
329 sample.cylinderindex = as->cylinderindex;
331 res = add_sample(&sample, at, res);
340 static char *merge_text(const char *a, const char *b)
350 res = malloc(strlen(a) + strlen(b) + 9);
353 sprintf(res, "(%s) or (%s)", a, b);
357 #define SORT(a,b,field) \
358 if (a->field != b->field) return a->field < b->field ? -1 : 1
360 static int sort_event(struct event *a, struct event *b)
362 SORT(a,b,time.seconds);
366 return strcmp(a->name, b->name);
369 static void merge_events(struct dive *res, struct dive *src1, struct dive *src2, int offset)
372 struct event **p = &res->events;
377 b->time.seconds += offset;
392 s = sort_event(a, b);
400 /* Pick 'a' or neither */
410 /* Pick whichever has any info (if either). Prefer 'a' */
411 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
418 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
425 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
427 merge_cylinder_type(&res->type, &a->type, &b->type);
428 merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
429 MERGE_MAX(res, a, b, start.mbar);
430 MERGE_MIN(res, a, b, end.mbar);
434 * This could do a lot more merging. Right now it really only
435 * merges almost exact duplicates - something that happens easily
436 * with overlapping dive downloads.
438 struct dive *try_to_merge(struct dive *a, struct dive *b)
443 if ((a->when >= b->when + 60) || (a->when <= b->when - 60))
449 MERGE_NONZERO(res, a, b, latitude);
450 MERGE_NONZERO(res, a, b, longitude);
451 MERGE_TXT(res, a, b, location);
452 MERGE_TXT(res, a, b, notes);
453 MERGE_TXT(res, a, b, buddy);
454 MERGE_TXT(res, a, b, divemaster);
455 MERGE_MAX(res, a, b, number);
456 MERGE_MAX(res, a, b, maxdepth.mm);
457 res->meandepth.mm = 0;
458 MERGE_MAX(res, a, b, duration.seconds);
459 MERGE_MAX(res, a, b, surfacetime.seconds);
460 MERGE_MAX(res, a, b, airtemp.mkelvin);
461 MERGE_MIN(res, a, b, watertemp.mkelvin);
462 for (i = 0; i < MAX_CYLINDERS; i++)
463 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
464 merge_events(res, a, b, 0);
465 return merge_samples(res, a, b, 0);