]> git.tdb.fi Git - ext/subsurface.git/blob - dive.c
Add various dive fixups, and show pressure (if any) in the plot
[ext/subsurface.git] / dive.c
1 #include <string.h>
2 #include <stdio.h>
3
4 #include "dive.h"
5
6 struct dive *fixup_dive(struct dive *dive)
7 {
8         int i;
9         double depthtime = 0;
10         int lasttime = 0;
11         int start = -1, end = -1;
12         int startpress = 0, endpress = 0;
13         int starttemp = 0, endtemp = 0;
14         int maxdepth = 0, mintemp = 0;
15         int lastdepth = 0;
16
17         for (i = 0; i < dive->samples; i++) {
18                 struct sample *sample = dive->sample + i;
19                 int time = sample->time.seconds;
20                 int depth = sample->depth.mm;
21                 int press = sample->tankpressure.mbar;
22                 int temp = sample->temperature.mkelvin;
23
24                 if (lastdepth)
25                         end = time;
26
27                 if (depth) {
28                         if (start < 0)
29                                 start = lasttime;
30                         if (depth > maxdepth)
31                                 maxdepth = depth;
32                 }
33                 if (press) {
34                         endpress = press;
35                         if (!startpress)
36                                 startpress = press;
37                 }
38                 if (temp) {
39                         endtemp = temp;
40                         if (!starttemp)
41                                 starttemp = temp;
42                         if (!mintemp || temp < mintemp)
43                                 mintemp = temp;
44                 }
45                 depthtime += (time - lasttime) * (lastdepth + depth) / 2;
46                 lastdepth = depth;
47                 lasttime = time;
48         }
49         if (end < 0)
50                 return dive;
51         dive->duration.seconds = end - start;
52         if (start != end)
53                 dive->meandepth.mm = depthtime / (end - start);
54         if (startpress)
55                 dive->beginning_pressure.mbar = startpress;
56         if (endpress)
57                 dive->end_pressure.mbar = endpress;
58         if (mintemp)
59                 dive->watertemp.mkelvin = mintemp;
60         if (maxdepth)
61                 dive->maxdepth.mm = maxdepth;
62
63         return dive;
64 }
65
66 /* Don't pick a zero for MERGE_MIN() */
67 #define MIN(a,b) ((a)<(b)?(a):(b))
68 #define MAX(a,b) ((a)>(b)?(a):(b))
69 #define MERGE_MAX(res, a, b, n) res->n = MAX(a->n, b->n)
70 #define MERGE_MIN(res, a, b, n) res->n = (a->n)?(b->n)?MIN(a->n, b->n):(a->n):(b->n)
71
72 static int alloc_samples;
73
74 static struct dive *add_sample(struct sample *sample, int time, struct dive *dive)
75 {
76         int nr = dive->samples;
77         struct sample *d;
78
79         if (nr >= alloc_samples) {
80                 alloc_samples = (alloc_samples + 64) * 3 / 2;
81                 dive = realloc(dive, dive_size(alloc_samples));
82                 if (!dive)
83                         return NULL;
84         }
85         dive->samples = nr+1;
86         d = dive->sample + nr;
87
88         *d = *sample;
89         d->time.seconds = time;
90         return dive;
91 }
92
93 /*
94  * Merge samples. Dive 'a' is "offset" seconds before Dive 'b'
95  */
96 static struct dive *merge_samples(struct dive *res, struct dive *a, struct dive *b, int offset)
97 {
98         int asamples = a->samples;
99         int bsamples = b->samples;
100         struct sample *as = a->sample;
101         struct sample *bs = b->sample;
102
103         for (;;) {
104                 int at, bt;
105                 struct sample sample;
106
107                 if (!res)
108                         return NULL;
109
110                 at = asamples ? as->time.seconds : -1;
111                 bt = bsamples ? bs->time.seconds + offset : -1;
112
113                 /* No samples? All done! */
114                 if (at < 0 && bt < 0)
115                         return fixup_dive(res);
116
117                 /* Only samples from a? */
118                 if (bt < 0) {
119 add_sample_a:
120                         res = add_sample(as, at, res);
121                         as++;
122                         asamples--;
123                         continue;
124                 }
125
126                 /* Only samples from b? */
127                 if (at < 0) {
128 add_sample_b:
129                         res = add_sample(bs, bt, res);
130                         bs++;
131                         bsamples--;
132                         continue;
133                 }
134
135                 if (at < bt)
136                         goto add_sample_a;
137                 if (at > bt)
138                         goto add_sample_b;
139
140                 /* same-time sample: add a merged sample. Take the non-zero ones */
141                 sample = *bs;
142                 if (as->depth.mm)
143                         sample.depth = as->depth;
144                 if (as->temperature.mkelvin)
145                         sample.temperature = as->temperature;
146                 if (as->tankpressure.mbar)
147                         sample.tankpressure = as->tankpressure;
148                 if (as->tankindex)
149                         sample.tankindex = as->tankindex;
150
151                 res = add_sample(&sample, at, res);
152
153                 as++;
154                 bs++;
155                 asamples--;
156                 bsamples--;
157         }
158 }
159
160 static char *merge_text(const char *a, const char *b)
161 {
162         char *res;
163
164         if (!a || !*a)
165                 return (char *)b;
166         if (!b || !*b)
167                 return (char *)a;
168         if (!strcmp(a,b))
169                 return (char *)a;
170         res = malloc(strlen(a) + strlen(b) + 9);
171         if (!res)
172                 return (char *)a;
173         sprintf(res, "(%s) or (%s)", a, b);
174         return res;
175 }
176
177 /*
178  * This could do a lot more merging. Right now it really only
179  * merges almost exact duplicates - something that happens easily
180  * with overlapping dive downloads.
181  */
182 struct dive *try_to_merge(struct dive *a, struct dive *b)
183 {
184         int i;
185         struct dive *res;
186
187         if (a->when != b->when)
188                 return NULL;
189
190         alloc_samples = 5;
191         res = malloc(dive_size(alloc_samples));
192         if (!res)
193                 return NULL;
194         memset(res, 0, dive_size(alloc_samples));
195
196         res->when = a->when;
197         res->name = merge_text(a->name, b->name);
198         res->location = merge_text(a->location, b->location);
199         res->notes = merge_text(a->notes, b->notes);
200         MERGE_MAX(res, a, b, maxdepth.mm);
201         res->meandepth.mm = 0;
202         MERGE_MAX(res, a, b, duration.seconds);
203         MERGE_MAX(res, a, b, surfacetime.seconds);
204         MERGE_MAX(res, a, b, airtemp.mkelvin);
205         MERGE_MIN(res, a, b, watertemp.mkelvin);
206         MERGE_MAX(res, a, b, beginning_pressure.mbar);
207         MERGE_MAX(res, a, b, end_pressure.mbar);
208         for (i = 0; i < MAX_MIXES; i++) {
209                 if (a->gasmix[i].o2.permille) {
210                         res->gasmix[i] = a->gasmix[i];
211                         continue;
212                 }
213                 res->gasmix[i] = b->gasmix[i];
214         }
215         return merge_samples(res, a, b, 0);
216 }