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;
31 double get_depth_units(unsigned int mm, int *frac, const char **units)
37 switch (output_units.length) {
56 struct dive *alloc_dive(void)
58 const int initial_samples = 5;
62 size = dive_size(initial_samples);
66 memset(dive, 0, size);
67 dive->alloc_samples = initial_samples;
71 struct sample *prepare_sample(struct dive **divep)
73 struct dive *dive = *divep;
75 int nr = dive->samples;
76 int alloc_samples = dive->alloc_samples;
77 struct sample *sample;
78 if (nr >= alloc_samples) {
81 alloc_samples = (alloc_samples * 3)/2 + 10;
82 size = dive_size(alloc_samples);
83 dive = realloc(dive, size);
86 dive->alloc_samples = alloc_samples;
89 sample = dive->sample + nr;
90 memset(sample, 0, sizeof(*sample));
96 void finish_sample(struct dive *dive, struct sample *sample)
102 * So when we re-calculate maxdepth and meandepth, we will
103 * not override the old numbers if they are close to the
106 * Why? Because a dive computer may well actually track the
107 * max depth and mean depth at finer granularity than the
108 * samples it stores. So it's possible that the max and mean
109 * have been reported more correctly originally.
111 * Only if the values calculated from the samples are clearly
112 * different do we override the normal depth values.
114 * This considers 1m to be "clearly different". That's
115 * a totally random number.
117 static void update_depth(depth_t *depth, int new)
122 if (abs(old - new) > 1000)
127 static void update_duration(duration_t *duration, int new)
130 duration->seconds = new;
133 static void update_temperature(temperature_t *temperature, int new)
136 int old = temperature->mkelvin;
138 if (abs(old - new) > 1000)
139 temperature->mkelvin = new;
143 static void fixup_pressure(struct dive *dive, struct sample *sample)
145 unsigned int pressure, index;
148 pressure = sample->cylinderpressure.mbar;
151 index = sample->cylinderindex;
152 if (index >= MAX_CYLINDERS)
154 cyl = dive->cylinder + index;
155 if (!cyl->start.mbar)
156 cyl->start.mbar = pressure;
157 if (!cyl->end.mbar || pressure < cyl->end.mbar)
158 cyl->end.mbar = pressure;
161 struct dive *fixup_dive(struct dive *dive)
164 double depthtime = 0;
166 int start = -1, end = -1;
167 int maxdepth = 0, mintemp = 0;
171 for (i = 0; i < dive->samples; i++) {
172 struct sample *sample = dive->sample + i;
173 int time = sample->time.seconds;
174 int depth = sample->depth.mm;
175 int temp = sample->temperature.mkelvin;
183 if (depth > maxdepth)
187 fixup_pressure(dive, sample);
191 * If we have consecutive identical
192 * temperature readings, throw away
193 * the redundant ones.
195 if (lasttemp == temp)
196 sample->temperature.mkelvin = 0;
200 if (!mintemp || temp < mintemp)
203 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
210 update_duration(&dive->duration, end - start);
212 depthtime /= (end - start);
214 update_depth(&dive->meandepth, depthtime);
215 update_temperature(&dive->watertemp, mintemp);
216 update_depth(&dive->maxdepth, maxdepth);
221 /* Don't pick a zero for MERGE_MIN() */
222 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
223 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
224 #define MERGE_TXT(res, a, b, n) res->n = merge_text(a->n, b->n)
226 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
228 struct sample *p = prepare_sample(&dive);
233 p->time.seconds = time;
234 finish_sample(dive, p);
239 * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
241 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
243 int asamples = a->samples;
244 int bsamples = b->samples;
245 struct sample *as = a->sample;
246 struct sample *bs = b->sample;
250 struct sample sample;
255 at = asamples ? as->time.seconds : -1;
256 bt = bsamples ? bs->time.seconds + offset : -1;
258 /* No samples? All done! */
259 if (at < 0 && bt < 0)
260 return fixup_dive(res);
262 /* Only samples from a? */
265 res = add_sample(as, at, res);
271 /* Only samples from b? */
274 res = add_sample(bs, bt, res);
285 /* same-time sample: add a merged sample. Take the non-zero ones */
288 sample.depth = as->depth;
289 if (as->temperature.mkelvin)
290 sample.temperature = as->temperature;
291 if (as->cylinderpressure.mbar)
292 sample.cylinderpressure = as->cylinderpressure;
293 if (as->cylinderindex)
294 sample.cylinderindex = as->cylinderindex;
296 res = add_sample(&sample, at, res);
305 static char *merge_text(const char *a, const char *b)
315 res = malloc(strlen(a) + strlen(b) + 9);
318 sprintf(res, "(%s) or (%s)", a, b);
322 #define SORT(a,b,field) \
323 if (a->field != b->field) return a->field < b->field ? -1 : 1
325 static int sort_event(struct event *a, struct event *b)
327 SORT(a,b,time.seconds);
331 return strcmp(a->name, b->name);
334 static void merge_events(struct dive *res, struct dive *src1, struct dive *src2, int offset)
337 struct event **p = &res->events;
342 b->time.seconds += offset;
357 s = sort_event(a, b);
365 /* Pick 'a' or neither */
375 /* Pick whichever has any info (if either). Prefer 'a' */
376 static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
383 static void merge_cylinder_mix(struct gasmix *res, struct gasmix *a, struct gasmix *b)
390 static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
392 merge_cylinder_type(&res->type, &a->type, &b->type);
393 merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
394 MERGE_MAX(res, a, b, start.mbar);
395 MERGE_MIN(res, a, b, end.mbar);
399 * This could do a lot more merging. Right now it really only
400 * merges almost exact duplicates - something that happens easily
401 * with overlapping dive downloads.
403 struct dive *try_to_merge(struct dive *a, struct dive *b)
408 if (a->when != b->when)
414 MERGE_TXT(res, a, b, location);
415 MERGE_TXT(res, a, b, notes);
416 MERGE_TXT(res, a, b, buddy);
417 MERGE_TXT(res, a, b, divemaster);
418 MERGE_MAX(res, a, b, number);
419 MERGE_MAX(res, a, b, maxdepth.mm);
420 res->meandepth.mm = 0;
421 MERGE_MAX(res, a, b, duration.seconds);
422 MERGE_MAX(res, a, b, surfacetime.seconds);
423 MERGE_MAX(res, a, b, airtemp.mkelvin);
424 MERGE_MIN(res, a, b, watertemp.mkelvin);
425 for (i = 0; i < MAX_CYLINDERS; i++)
426 merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
427 merge_events(res, a, b, 0);
428 return merge_samples(res, a, b, 0);