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_temp_units(unsigned int mk, const char **units)
37 if (output_units.temperature == FAHRENHEIT) {
38 deg = mkelvin_to_F(mk);
39 unit = UTF8_DEGREE "F";
41 deg = mkelvin_to_C(mk);
42 unit = UTF8_DEGREE "C";
49 double get_volume_units(unsigned int ml, int *frac, const char **units)
55 switch (output_units.volume) {
74 double get_depth_units(unsigned int mm, int *frac, const char **units)
80 switch (output_units.length) {
99 struct dive *alloc_dive(void)
101 const int initial_samples = 5;
105 size = dive_size(initial_samples);
109 memset(dive, 0, size);
110 dive->alloc_samples = initial_samples;
114 struct sample *prepare_sample(struct dive **divep)
116 struct dive *dive = *divep;
118 int nr = dive->samples;
119 int alloc_samples = dive->alloc_samples;
120 struct sample *sample;
121 if (nr >= alloc_samples) {
124 alloc_samples = (alloc_samples * 3)/2 + 10;
125 size = dive_size(alloc_samples);
126 dive = realloc(dive, size);
129 dive->alloc_samples = alloc_samples;
132 sample = dive->sample + nr;
133 memset(sample, 0, sizeof(*sample));
139 void finish_sample(struct dive *dive, struct sample *sample)
145 * So when we re-calculate maxdepth and meandepth, we will
146 * not override the old numbers if they are close to the
149 * Why? Because a dive computer may well actually track the
150 * max depth and mean depth at finer granularity than the
151 * samples it stores. So it's possible that the max and mean
152 * have been reported more correctly originally.
154 * Only if the values calculated from the samples are clearly
155 * different do we override the normal depth values.
157 * This considers 1m to be "clearly different". That's
158 * a totally random number.
160 static void update_depth(depth_t *depth, int new)
165 if (abs(old - new) > 1000)
170 static void update_duration(duration_t *duration, int new)
173 duration->seconds = new;
176 static void update_temperature(temperature_t *temperature, int new)
179 int old = temperature->mkelvin;
181 if (abs(old - new) > 1000)
182 temperature->mkelvin = new;
187 * If you have more than 32 cylinders, you'd better have a 64-bit build.
188 * And if you have more than 64 cylinders, you need to use another tool,
189 * or fix this up to do something odd.
191 static unsigned long fixup_pressure(struct dive *dive, struct sample *sample, unsigned long flags)
194 unsigned int pressure, index;
197 pressure = sample->cylinderpressure.mbar;
200 index = sample->cylinderindex;
201 if (index >= MAX_CYLINDERS)
203 cyl = dive->cylinder + index;
204 if (!cyl->start.mbar)
205 cyl->start.mbar = pressure;
207 * If we already have an end pressure, without
208 * ever having seen a sample for this cylinder,
209 * that means that somebody set the end pressure
219 /* we need to handle the user entering beginning and end tank pressures
220 * - maybe even IF we have samples. But for now if we have air pressure
221 * data in the samples, we use that instead of the minimum
222 * if (!cyl->end.mbar || pressure < cyl->end.mbar)
224 cyl->end.mbar = pressure;
228 struct dive *fixup_dive(struct dive *dive)
231 double depthtime = 0;
233 int start = -1, end = -1;
234 int maxdepth = 0, mintemp = 0;
237 unsigned long flags = 0;
239 for (i = 0; i < dive->samples; i++) {
240 struct sample *sample = dive->sample + i;
241 int time = sample->time.seconds;
242 int depth = sample->depth.mm;
243 int temp = sample->temperature.mkelvin;
251 if (depth > maxdepth)
255 flags = fixup_pressure(dive, sample, flags);
259 * If we have consecutive identical
260 * temperature readings, throw away
261 * the redundant ones.
263 if (lasttemp == temp)
264 sample->temperature.mkelvin = 0;
268 if (!mintemp || temp < mintemp)
271 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
278 update_duration(&dive->duration, end - start);
280 depthtime /= (end - start);
282 update_depth(&dive->meandepth, depthtime);
283 update_temperature(&dive->watertemp, mintemp);
284 update_depth(&dive->maxdepth, maxdepth);
286 add_people(dive->buddy);
287 add_people(dive->divemaster);
288 add_location(dive->location);
289 for (i = 0; i < MAX_CYLINDERS; i++) {
290 cylinder_type_t *type = &dive->cylinder[i].type;
291 add_cylinder_description(type);
297 /* Don't pick a zero for MERGE_MIN() */
298 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
299 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
300 #define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n)
301 #define MERGE_NONZERO(res, a, b, n) res->n = a->n ? a->n : b->n
303 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
305 struct sample *p = prepare_sample(&dive);
310 p->time.seconds = time;
311 finish_sample(dive, p);
316 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
318 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
320 int asamples = a->samples;
321 int bsamples = b->samples;
322 struct sample *as = a->sample;
323 struct sample *bs = b->sample;
327 struct sample sample;
332 at = asamples ? as->time.seconds : -1;
333 bt = bsamples ? bs->time.seconds + offset : -1;
335 /* No samples? All done! */
336 if (at < 0 && bt < 0)
337 return fixup_dive(res);
339 /* Only samples from a? */
342 res = add_sample(as, at, res);
348 /* Only samples from b? */
351 res = add_sample(bs, bt, res);
362 /* same-time sample: add a merged sample. Take the non-zero ones */
365 sample.depth = as->depth;
366 if (as->temperature.mkelvin)
367 sample.temperature = as->temperature;
368 if (as->cylinderpressure.mbar)
369 sample.cylinderpressure = as->cylinderpressure;
370 if (as->cylinderindex)
371 sample.cylinderindex = as->cylinderindex;
373 res = add_sample(&sample, at, res);
382 static char *merge_text(const char *a, const char *b)
392 res = malloc(strlen(a) + strlen(b) + 9);
395 sprintf(res, "(%s) or (%s)", a, b);
399 #define SORT(a,b,field) \
400 if (a->field != b->field) return a->field < b->field ? -1 : 1
402 static int sort_event(struct event *a, struct event *b)
404 SORT(a,b,time.seconds);
408 return strcmp(a->name, b->name);
411 static void merge_events(struct dive *res, struct dive *src1, struct dive *src2, int offset)
414 struct event **p = &res->events;
419 b->time.seconds += offset;
434 s = sort_event(a, b);
442 /* Pick 'a' or neither */
452 /* Pick whichever has any info (if either). Prefer 'a' */
453 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
460 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
467 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
469 merge_cylinder_type(&res->type, &a->type, &b->type);
470 merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
471 MERGE_MAX(res, a, b, start.mbar);
472 MERGE_MIN(res, a, b, end.mbar);
476 * This could do a lot more merging. Right now it really only
477 * merges almost exact duplicates - something that happens easily
478 * with overlapping dive downloads.
480 struct dive *try_to_merge(struct dive *a, struct dive *b)
485 if ((a->when >= b->when + 60) || (a->when <= b->when - 60))
491 MERGE_NONZERO(res, a, b, latitude);
492 MERGE_NONZERO(res, a, b, longitude);
493 MERGE_TXT(res, a, b, location);
494 MERGE_TXT(res, a, b, notes);
495 MERGE_TXT(res, a, b, buddy);
496 MERGE_TXT(res, a, b, divemaster);
497 MERGE_MAX(res, a, b, number);
498 MERGE_MAX(res, a, b, maxdepth.mm);
499 res->meandepth.mm = 0;
500 MERGE_MAX(res, a, b, duration.seconds);
501 MERGE_MAX(res, a, b, surfacetime.seconds);
502 MERGE_MAX(res, a, b, airtemp.mkelvin);
503 MERGE_MIN(res, a, b, watertemp.mkelvin);
504 for (i = 0; i < MAX_CYLINDERS; i++)
505 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
506 merge_events(res, a, b, 0);
507 return merge_samples(res, a, b, 0);