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_depth_units(unsigned int mm, int *frac, const char **units)
55 switch (output_units.length) {
74 struct dive *alloc_dive(void)
76 const int initial_samples = 5;
80 size = dive_size(initial_samples);
84 memset(dive, 0, size);
85 dive->alloc_samples = initial_samples;
89 struct sample *prepare_sample(struct dive **divep)
91 struct dive *dive = *divep;
93 int nr = dive->samples;
94 int alloc_samples = dive->alloc_samples;
95 struct sample *sample;
96 if (nr >= alloc_samples) {
99 alloc_samples = (alloc_samples * 3)/2 + 10;
100 size = dive_size(alloc_samples);
101 dive = realloc(dive, size);
104 dive->alloc_samples = alloc_samples;
107 sample = dive->sample + nr;
108 memset(sample, 0, sizeof(*sample));
114 void finish_sample(struct dive *dive, struct sample *sample)
120 * So when we re-calculate maxdepth and meandepth, we will
121 * not override the old numbers if they are close to the
124 * Why? Because a dive computer may well actually track the
125 * max depth and mean depth at finer granularity than the
126 * samples it stores. So it's possible that the max and mean
127 * have been reported more correctly originally.
129 * Only if the values calculated from the samples are clearly
130 * different do we override the normal depth values.
132 * This considers 1m to be "clearly different". That's
133 * a totally random number.
135 static void update_depth(depth_t *depth, int new)
140 if (abs(old - new) > 1000)
145 static void update_duration(duration_t *duration, int new)
148 duration->seconds = new;
151 static void update_temperature(temperature_t *temperature, int new)
154 int old = temperature->mkelvin;
156 if (abs(old - new) > 1000)
157 temperature->mkelvin = new;
162 * If you have more than 32 cylinders, you'd better have a 64-bit build.
163 * And if you have more than 64 cylinders, you need to use another tool,
164 * or fix this up to do something odd.
166 static unsigned long fixup_pressure(struct dive *dive, struct sample *sample, unsigned long flags)
169 unsigned int pressure, index;
172 pressure = sample->cylinderpressure.mbar;
175 index = sample->cylinderindex;
176 if (index >= MAX_CYLINDERS)
178 cyl = dive->cylinder + index;
179 if (!cyl->start.mbar)
180 cyl->start.mbar = pressure;
182 * If we already have an end pressure, without
183 * ever having seen a sample for this cylinder,
184 * that means that somebody set the end pressure
194 /* we need to handle the user entering beginning and end tank pressures
195 * - maybe even IF we have samples. But for now if we have air pressure
196 * data in the samples, we use that instead of the minimum
197 * if (!cyl->end.mbar || pressure < cyl->end.mbar)
199 cyl->end.mbar = pressure;
203 struct dive *fixup_dive(struct dive *dive)
206 double depthtime = 0;
208 int start = -1, end = -1;
209 int maxdepth = 0, mintemp = 0;
212 unsigned long flags = 0;
214 for (i = 0; i < dive->samples; i++) {
215 struct sample *sample = dive->sample + i;
216 int time = sample->time.seconds;
217 int depth = sample->depth.mm;
218 int temp = sample->temperature.mkelvin;
226 if (depth > maxdepth)
230 flags = fixup_pressure(dive, sample, flags);
234 * If we have consecutive identical
235 * temperature readings, throw away
236 * the redundant ones.
238 if (lasttemp == temp)
239 sample->temperature.mkelvin = 0;
243 if (!mintemp || temp < mintemp)
246 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
253 update_duration(&dive->duration, end - start);
255 depthtime /= (end - start);
257 update_depth(&dive->meandepth, depthtime);
258 update_temperature(&dive->watertemp, mintemp);
259 update_depth(&dive->maxdepth, maxdepth);
261 add_people(dive->buddy);
262 add_people(dive->divemaster);
263 add_location(dive->location);
264 for (i = 0; i < MAX_CYLINDERS; i++) {
265 cylinder_type_t *type = &dive->cylinder[i].type;
266 add_cylinder_description(type);
272 /* Don't pick a zero for MERGE_MIN() */
273 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
274 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
275 #define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n)
276 #define MERGE_NONZERO(res, a, b, n) res->n = a->n ? a->n : b->n
278 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
280 struct sample *p = prepare_sample(&dive);
285 p->time.seconds = time;
286 finish_sample(dive, p);
291 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
293 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
295 int asamples = a->samples;
296 int bsamples = b->samples;
297 struct sample *as = a->sample;
298 struct sample *bs = b->sample;
302 struct sample sample;
307 at = asamples ? as->time.seconds : -1;
308 bt = bsamples ? bs->time.seconds + offset : -1;
310 /* No samples? All done! */
311 if (at < 0 && bt < 0)
312 return fixup_dive(res);
314 /* Only samples from a? */
317 res = add_sample(as, at, res);
323 /* Only samples from b? */
326 res = add_sample(bs, bt, res);
337 /* same-time sample: add a merged sample. Take the non-zero ones */
340 sample.depth = as->depth;
341 if (as->temperature.mkelvin)
342 sample.temperature = as->temperature;
343 if (as->cylinderpressure.mbar)
344 sample.cylinderpressure = as->cylinderpressure;
345 if (as->cylinderindex)
346 sample.cylinderindex = as->cylinderindex;
348 res = add_sample(&sample, at, res);
357 static char *merge_text(const char *a, const char *b)
367 res = malloc(strlen(a) + strlen(b) + 9);
370 sprintf(res, "(%s) or (%s)", a, b);
374 #define SORT(a,b,field) \
375 if (a->field != b->field) return a->field < b->field ? -1 : 1
377 static int sort_event(struct event *a, struct event *b)
379 SORT(a,b,time.seconds);
383 return strcmp(a->name, b->name);
386 static void merge_events(struct dive *res, struct dive *src1, struct dive *src2, int offset)
389 struct event **p = &res->events;
394 b->time.seconds += offset;
409 s = sort_event(a, b);
417 /* Pick 'a' or neither */
427 /* Pick whichever has any info (if either). Prefer 'a' */
428 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
435 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
442 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
444 merge_cylinder_type(&res->type, &a->type, &b->type);
445 merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
446 MERGE_MAX(res, a, b, start.mbar);
447 MERGE_MIN(res, a, b, end.mbar);
451 * This could do a lot more merging. Right now it really only
452 * merges almost exact duplicates - something that happens easily
453 * with overlapping dive downloads.
455 struct dive *try_to_merge(struct dive *a, struct dive *b)
460 if ((a->when >= b->when + 60) || (a->when <= b->when - 60))
466 MERGE_NONZERO(res, a, b, latitude);
467 MERGE_NONZERO(res, a, b, longitude);
468 MERGE_TXT(res, a, b, location);
469 MERGE_TXT(res, a, b, notes);
470 MERGE_TXT(res, a, b, buddy);
471 MERGE_TXT(res, a, b, divemaster);
472 MERGE_MAX(res, a, b, number);
473 MERGE_MAX(res, a, b, maxdepth.mm);
474 res->meandepth.mm = 0;
475 MERGE_MAX(res, a, b, duration.seconds);
476 MERGE_MAX(res, a, b, surfacetime.seconds);
477 MERGE_MAX(res, a, b, airtemp.mkelvin);
478 MERGE_MIN(res, a, b, watertemp.mkelvin);
479 for (i = 0; i < MAX_CYLINDERS; i++)
480 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
481 merge_events(res, a, b, 0);
482 return merge_samples(res, a, b, 0);