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 int get_pressure_units(unsigned int mb, const char **units)
37 switch (output_units.pressure) {
43 pressure = (mb + 500) / 1000;
47 pressure = mbar_to_PSI(mb);
56 double get_temp_units(unsigned int mk, const char **units)
61 if (output_units.temperature == FAHRENHEIT) {
62 deg = mkelvin_to_F(mk);
63 unit = UTF8_DEGREE "F";
65 deg = mkelvin_to_C(mk);
66 unit = UTF8_DEGREE "C";
73 double get_volume_units(unsigned int ml, int *frac, const char **units)
79 switch (output_units.volume) {
98 double get_depth_units(unsigned int mm, int *frac, const char **units)
104 switch (output_units.length) {
123 double get_weight_units(unsigned int grams, int *frac, const char **units)
129 if (output_units.weight == LBS) {
130 value = grams_to_lbs(grams);
134 value = grams / 1000.0;
145 struct dive *alloc_dive(void)
147 const int initial_samples = 5;
151 size = dive_size(initial_samples);
155 memset(dive, 0, size);
156 dive->alloc_samples = initial_samples;
160 struct sample *prepare_sample(struct dive **divep)
162 struct dive *dive = *divep;
164 int nr = dive->samples;
165 int alloc_samples = dive->alloc_samples;
166 struct sample *sample;
167 if (nr >= alloc_samples) {
170 alloc_samples = (alloc_samples * 3)/2 + 10;
171 size = dive_size(alloc_samples);
172 dive = realloc(dive, size);
175 dive->alloc_samples = alloc_samples;
178 sample = dive->sample + nr;
179 memset(sample, 0, sizeof(*sample));
185 void finish_sample(struct dive *dive)
191 * So when we re-calculate maxdepth and meandepth, we will
192 * not override the old numbers if they are close to the
195 * Why? Because a dive computer may well actually track the
196 * max depth and mean depth at finer granularity than the
197 * samples it stores. So it's possible that the max and mean
198 * have been reported more correctly originally.
200 * Only if the values calculated from the samples are clearly
201 * different do we override the normal depth values.
203 * This considers 1m to be "clearly different". That's
204 * a totally random number.
206 static void update_depth(depth_t *depth, int new)
211 if (abs(old - new) > 1000)
216 static void update_duration(duration_t *duration, int new)
219 duration->seconds = new;
222 static void update_temperature(temperature_t *temperature, int new)
225 int old = temperature->mkelvin;
227 if (abs(old - new) > 1000)
228 temperature->mkelvin = new;
232 static void fixup_pressure(struct dive *dive, struct sample *sample)
234 unsigned int pressure, index;
237 pressure = sample->cylinderpressure.mbar;
240 index = sample->cylinderindex;
241 if (index >= MAX_CYLINDERS)
243 cyl = dive->cylinder + index;
244 if (!cyl->sample_start.mbar)
245 cyl->sample_start.mbar = pressure;
246 cyl->sample_end.mbar = pressure;
250 * If the cylinder tank pressures are within half a bar
251 * (about 8 PSI) of the sample pressures, we consider it
252 * to be a rounding error, and throw them away as redundant.
254 static int same_rounded_pressure(pressure_t a, pressure_t b)
256 return abs(a.mbar - b.mbar) <= 500;
259 static void sanitize_gasmix(struct gasmix *mix)
263 o2 = mix->o2.permille;
264 he = mix->he.permille;
266 /* Regular air: leave empty */
270 /* 20.9% or 21% O2 is just air */
271 if (o2 >= 209 && o2 <= 210) {
272 mix->o2.permille = 0;
278 if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
280 fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
281 memset(mix, 0, sizeof(*mix));
285 * See if the size/workingpressure looks like some standard cylinder
288 static void match_standard_cylinder(cylinder_type_t *type)
295 /* Do we already have a cylinder description? */
296 if (type->description)
299 cuft = ml_to_cuft(type->size.mliter);
300 cuft *= to_ATM(type->workingpressure);
301 psi = to_PSI(type->workingpressure);
304 case 2300 ... 2500: /* 2400 psi: LP tank */
307 case 2600 ... 2700: /* 2640 psi: LP+10% */
310 case 2900 ... 3100: /* 3000 psi: ALx tank */
313 case 3400 ... 3500: /* 3442 psi: HP tank */
316 case 3700 ... 3850: /* HP+10% */
322 len = snprintf(buffer, sizeof(buffer), fmt, (int) (cuft+0.5));
326 memcpy(p, buffer, len+1);
327 type->description = p;
332 * There are two ways to give cylinder size information:
333 * - total amount of gas in cuft (depends on working pressure and physical size)
336 * where "physical size" is the one that actually matters and is sane.
338 * We internally use physical size only. But we save the workingpressure
339 * so that we can do the conversion if required.
341 static void sanitize_cylinder_type(cylinder_type_t *type)
343 double volume_of_air, atm, volume;
345 /* If we have no working pressure, it had *better* be just a physical size! */
346 if (!type->workingpressure.mbar)
349 /* No size either? Nothing to go on */
350 if (!type->size.mliter)
353 if (input_units.volume == CUFT) {
354 /* confusing - we don't really start from ml but millicuft !*/
355 volume_of_air = cuft_to_l(type->size.mliter);
356 atm = to_ATM(type->workingpressure); /* working pressure in atm */
357 volume = volume_of_air / atm; /* milliliters at 1 atm: "true size" */
358 type->size.mliter = volume + 0.5;
361 /* Ok, we have both size and pressure: try to match a description */
362 match_standard_cylinder(type);
365 static void sanitize_cylinder_info(struct dive *dive)
369 for (i = 0; i < MAX_CYLINDERS; i++) {
370 sanitize_gasmix(&dive->cylinder[i].gasmix);
371 sanitize_cylinder_type(&dive->cylinder[i].type);
375 struct dive *fixup_dive(struct dive *dive)
378 double depthtime = 0;
381 int start = -1, end = -1;
382 int maxdepth = 0, mintemp = 0;
384 int lasttemp = 0, lastpressure = 0;
385 int pressure_delta[MAX_CYLINDERS] = {INT_MAX, };
387 sanitize_cylinder_info(dive);
388 for (i = 0; i < dive->samples; i++) {
389 struct sample *sample = dive->sample + i;
390 int time = sample->time.seconds;
391 int depth = sample->depth.mm;
392 int temp = sample->temperature.mkelvin;
393 int pressure = sample->cylinderpressure.mbar;
394 int index = sample->cylinderindex;
396 if (index == lastindex) {
397 /* Remove duplicate redundant pressure information */
398 if (pressure == lastpressure)
399 sample->cylinderpressure.mbar = 0;
400 /* check for simply linear data in the samples
401 +INT_MAX means uninitialized, -INT_MAX means not linear */
402 if (pressure_delta[index] != -INT_MAX && lastpressure) {
403 if (pressure_delta[index] == INT_MAX) {
404 pressure_delta[index] = abs(pressure - lastpressure);
406 int cur_delta = abs(pressure - lastpressure);
407 if (cur_delta && abs(cur_delta - pressure_delta[index]) > 150) {
408 /* ok the samples aren't just a linearisation
409 * between start and end */
410 pressure_delta[index] = -INT_MAX;
416 lastpressure = pressure;
424 if (depth > maxdepth)
428 fixup_pressure(dive, sample);
432 * If we have consecutive identical
433 * temperature readings, throw away
434 * the redundant ones.
436 if (lasttemp == temp)
437 sample->temperature.mkelvin = 0;
441 if (!mintemp || temp < mintemp)
444 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
448 /* if all the samples for a cylinder have pressure data that
449 * is basically equidistant throw out the sample cylinder pressure
450 * information but make sure we still have a valid start and end
452 * this happens when DivingLog decides to linearalize the
453 * pressure between beginning and end and for strange reasons
454 * decides to put that in the sample data as if it came from
455 * the dive computer; we don't want that (we'll visualize with
456 * constant SAC rate instead)
457 * WARNING WARNING - I have only seen this in single tank dives
458 * --- maybe I should try to create a multi tank dive and see what
459 * --- divinglog does there - but the code right now is only tested
460 * --- for the single tank case */
461 for (j = 0; j < MAX_CYLINDERS; j++) {
462 if (abs(pressure_delta[j]) != INT_MAX) {
463 cylinder_t *cyl = dive->cylinder + j;
464 for (i = 0; i < dive->samples; i++)
465 if (dive->sample[i].cylinderindex == j)
466 dive->sample[i].cylinderpressure.mbar = 0;
467 if (! cyl->start.mbar)
468 cyl->start.mbar = cyl->sample_start.mbar;
470 cyl->end.mbar = cyl->sample_end.mbar;
471 cyl->sample_start.mbar = 0;
472 cyl->sample_end.mbar = 0;
478 update_duration(&dive->duration, end - start);
480 depthtime /= (end - start);
482 update_depth(&dive->meandepth, depthtime);
483 update_temperature(&dive->watertemp, mintemp);
484 update_depth(&dive->maxdepth, maxdepth);
486 add_people(dive->buddy);
487 add_people(dive->divemaster);
488 add_location(dive->location);
489 add_suit(dive->suit);
490 for (i = 0; i < MAX_CYLINDERS; i++) {
491 cylinder_t *cyl = dive->cylinder + i;
492 add_cylinder_description(&cyl->type);
493 if (same_rounded_pressure(cyl->sample_start, cyl->start))
495 if (same_rounded_pressure(cyl->sample_end, cyl->end))
498 for (i = 0; i < MAX_WEIGHTSYSTEMS; i++) {
499 weightsystem_t *ws = dive->weightsystem + i;
500 add_weightsystem_description(ws);
506 /* Don't pick a zero for MERGE_MIN() */
507 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
508 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
509 #define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n)
510 #define MERGE_NONZERO(res, a, b, n) res->n = a->n ? a->n : b->n
512 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
514 struct sample *p = prepare_sample(&dive);
519 p->time.seconds = time;
525 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
527 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
529 int asamples = a->samples;
530 int bsamples = b->samples;
531 struct sample *as = a->sample;
532 struct sample *bs = b->sample;
536 struct sample sample;
541 at = asamples ? as->time.seconds : -1;
542 bt = bsamples ? bs->time.seconds + offset : -1;
544 /* No samples? All done! */
545 if (at < 0 && bt < 0)
546 return fixup_dive(res);
548 /* Only samples from a? */
551 res = add_sample(as, at, res);
557 /* Only samples from b? */
560 res = add_sample(bs, bt, res);
571 /* same-time sample: add a merged sample. Take the non-zero ones */
574 sample.depth = as->depth;
575 if (as->temperature.mkelvin)
576 sample.temperature = as->temperature;
577 if (as->cylinderpressure.mbar)
578 sample.cylinderpressure = as->cylinderpressure;
579 if (as->cylinderindex)
580 sample.cylinderindex = as->cylinderindex;
582 res = add_sample(&sample, at, res);
591 static char *merge_text(const char *a, const char *b)
601 res = malloc(strlen(a) + strlen(b) + 9);
604 sprintf(res, "(%s) or (%s)", a, b);
608 #define SORT(a,b,field) \
609 if (a->field != b->field) return a->field < b->field ? -1 : 1
611 static int sort_event(struct event *a, struct event *b)
613 SORT(a,b,time.seconds);
617 return strcmp(a->name, b->name);
620 static void merge_events(struct dive *res, struct dive *src1, struct dive *src2, int offset)
623 struct event **p = &res->events;
628 b->time.seconds += offset;
643 s = sort_event(a, b);
651 /* Pick 'a' or neither */
661 /* Pick whichever has any info (if either). Prefer 'a' */
662 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
669 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
676 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
678 merge_cylinder_type(&res->type, &a->type, &b->type);
679 merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
680 MERGE_MAX(res, a, b, start.mbar);
681 MERGE_MIN(res, a, b, end.mbar);
685 * This could do a lot more merging. Right now it really only
686 * merges almost exact duplicates - something that happens easily
687 * with overlapping dive downloads.
689 struct dive *try_to_merge(struct dive *a, struct dive *b)
694 if ((a->when >= b->when + 60) || (a->when <= b->when - 60))
700 MERGE_NONZERO(res, a, b, latitude);
701 MERGE_NONZERO(res, a, b, longitude);
702 MERGE_TXT(res, a, b, location);
703 MERGE_TXT(res, a, b, notes);
704 MERGE_TXT(res, a, b, buddy);
705 MERGE_TXT(res, a, b, divemaster);
706 MERGE_MAX(res, a, b, rating);
707 MERGE_TXT(res, a, b, suit);
708 MERGE_MAX(res, a, b, number);
709 MERGE_MAX(res, a, b, maxdepth.mm);
710 res->meandepth.mm = 0;
711 MERGE_MAX(res, a, b, duration.seconds);
712 MERGE_MAX(res, a, b, surfacetime.seconds);
713 MERGE_MAX(res, a, b, airtemp.mkelvin);
714 MERGE_MIN(res, a, b, watertemp.mkelvin);
715 for (i = 0; i < MAX_CYLINDERS; i++)
716 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
717 merge_events(res, a, b, 0);
718 return merge_samples(res, a, b, 0);