]> git.tdb.fi Git - ext/subsurface.git/blob - dive.h
Fix profile and average depth for freedives
[ext/subsurface.git] / dive.h
1 #ifndef DIVE_H
2 #define DIVE_H
3
4 #include <stdlib.h>
5 #include <time.h>
6
7 #include <glib.h>
8 #include <libxml/tree.h>
9
10 /*
11  * Some silly typedefs to make our units very explicit.
12  *
13  * Also, the units are chosen so that values can be expressible as
14  * integers, so that we never have FP rounding issues. And they
15  * are small enough that converting to/from imperial units doesn't
16  * really matter.
17  *
18  * We also strive to make '0' a meaningless number saying "not
19  * initialized", since many values are things that may not have
20  * been reported (eg cylinder pressure or temperature from dive
21  * computers that don't support them). But sometimes -1 is an even
22  * more explicit way of saying "not there".
23  *
24  * Thus "millibar" for pressure, for example, or "millikelvin" for
25  * temperatures. Doing temperatures in celsius or fahrenheit would
26  * make for loss of precision when converting from one to the other,
27  * and using millikelvin is SI-like but also means that a temperature
28  * of '0' is clearly just a missing temperature or cylinder pressure.
29  *
30  * Also strive to use units that can not possibly be mistaken for a
31  * valid value in a "normal" system without conversion. If the max
32  * depth of a dive is '20000', you probably didn't convert from mm on
33  * output, or if the max depth gets reported as "0.2ft" it was either
34  * a really boring dive, or there was some missing input conversion,
35  * and a 60-ft dive got recorded as 60mm.
36  *
37  * Doing these as "structs containing value" means that we always
38  * have to explicitly write out those units in order to get at the
39  * actual value. So there is hopefully little fear of using a value
40  * in millikelvin as Fahrenheit by mistake.
41  *
42  * We don't actually use these all yet, so maybe they'll change, but
43  * I made a number of types as guidelines.
44  */
45 typedef struct {
46         int seconds;
47 } duration_t;
48
49 typedef struct {
50         int mm;
51 } depth_t;
52
53 typedef struct {
54         int mbar;
55 } pressure_t;
56
57 typedef struct {
58         int mkelvin;
59 } temperature_t;
60
61 typedef struct {
62         int mliter;
63 } volume_t;
64
65 typedef struct {
66         int permille;
67 } fraction_t;
68
69 typedef struct {
70         int grams;
71 } weight_t;
72
73 struct gasmix {
74         fraction_t o2;
75         fraction_t he;
76 };
77
78 typedef struct {
79         volume_t size;
80         pressure_t workingpressure;
81         const char *description;        /* "LP85", "AL72", "AL80", "HP100+" or whatever */
82 } cylinder_type_t;
83
84 typedef struct {
85         cylinder_type_t type;
86         struct gasmix gasmix;
87         pressure_t start, end, sample_start, sample_end;
88 } cylinder_t;
89
90 typedef struct {
91         weight_t weight;
92         const char *description;        /* "integrated", "belt", "ankle" */
93 } weightsystem_t;
94
95 extern gboolean cylinder_none(void *_data);
96 extern gboolean no_cylinders(cylinder_t *cyl);
97 extern gboolean cylinders_equal(cylinder_t *cyl1, cylinder_t *cyl2);
98 extern void copy_cylinders(cylinder_t *cyl1, cylinder_t *cyl2);
99 extern gboolean no_weightsystems(weightsystem_t *ws);
100 extern gboolean weightsystems_equal(weightsystem_t *ws1, weightsystem_t *ws2);
101
102 extern int get_pressure_units(unsigned int mb, const char **units);
103 extern double get_depth_units(unsigned int mm, int *frac, const char **units);
104 extern double get_volume_units(unsigned int ml, int *frac, const char **units);
105 extern double get_temp_units(unsigned int mk, const char **units);
106 extern double get_weight_units(unsigned int grams, int *frac, const char **units);
107
108 static inline double grams_to_lbs(int grams)
109 {
110         return grams / 453.6;
111 }
112
113 static inline int lbs_to_grams(double lbs)
114 {
115         return lbs * 453.6 + 0.5;
116 }
117
118 static inline double ml_to_cuft(int ml)
119 {
120         return ml / 28316.8466;
121 }
122
123 static inline double cuft_to_l(double cuft)
124 {
125         return cuft * 28.3168466;
126 }
127
128 static inline double mm_to_feet(int mm)
129 {
130         return mm * 0.00328084;
131 }
132
133 static inline unsigned long feet_to_mm(double feet)
134 {
135         return feet * 304.8 + 0.5;
136 }
137
138 static inline int to_feet(depth_t depth)
139 {
140         return mm_to_feet(depth.mm) + 0.5;
141 }
142
143 static inline double mkelvin_to_C(int mkelvin)
144 {
145         return (mkelvin - 273150) / 1000.0;
146 }
147
148 static inline double mkelvin_to_F(int mkelvin)
149 {
150         return mkelvin * 9 / 5000.0 - 459.670;
151 }
152
153 static inline unsigned long F_to_mkelvin(double f)
154 {
155         return (f-32) * 1000 / 1.8 + 273150.5;
156 }
157
158 static inline int to_C(temperature_t temp)
159 {
160         if (!temp.mkelvin)
161                 return 0;
162         return mkelvin_to_C(temp.mkelvin) + 0.5;
163 }
164
165 static inline int to_F(temperature_t temp)
166 {
167         if (!temp.mkelvin)
168                 return 0;
169         return mkelvin_to_F(temp.mkelvin) + 0.5;
170 }
171
172 static inline int to_K(temperature_t temp)
173 {
174         if (!temp.mkelvin)
175                 return 0;
176         return (temp.mkelvin + 499)/1000;
177 }
178
179 static inline double psi_to_bar(double psi)
180 {
181         return psi / 14.5037738;
182 }
183
184 static inline unsigned long psi_to_mbar(double psi)
185 {
186         return psi_to_bar(psi)*1000 + 0.5;
187 }
188
189 static inline int to_PSI(pressure_t pressure)
190 {
191         return pressure.mbar * 0.0145037738 + 0.5;
192 }
193
194 static inline double bar_to_atm(double bar)
195 {
196         return bar / 1.01325;
197 }
198
199 static inline double to_ATM(pressure_t pressure)
200 {
201         return pressure.mbar / 1013.25;
202 }
203
204 static inline int mbar_to_PSI(int mbar)
205 {
206         pressure_t p = {mbar};
207         return to_PSI(p);
208 }
209
210 struct sample {
211         duration_t time;
212         depth_t depth;
213         temperature_t temperature;
214         pressure_t cylinderpressure;
215         int cylinderindex;
216 };
217
218 /*
219  * Events are currently pretty meaningless. This is
220  * just based on the random data that libdivecomputer
221  * gives us. I'm not sure what a real "architected"
222  * event model would actually look like, but right
223  * now you can associate a list of events with a dive,
224  * and we'll do something about it.
225  */
226 struct event {
227         struct event *next;
228         duration_t time;
229         int type, flags, value;
230         char name[];
231 };
232
233 #define MAX_CYLINDERS (8)
234 #define MAX_WEIGHTSYSTEMS (4)
235 #define W_IDX_PRIMARY 0
236 #define W_IDX_SECONDARY 1
237
238 typedef enum { TF_NONE, NO_TRIP, IN_TRIP, NUM_TRIPFLAGS } tripflag_t;
239 extern const char *tripflag_names[NUM_TRIPFLAGS];
240
241 struct dive {
242         int number;
243         tripflag_t tripflag;
244         int selected;
245         time_t when;
246         char *location;
247         char *notes;
248         char *divemaster, *buddy;
249         int rating;
250         double latitude, longitude;
251         depth_t maxdepth, meandepth;
252         duration_t duration, surfacetime;
253         depth_t visibility;
254         temperature_t airtemp, watertemp;
255         cylinder_t cylinder[MAX_CYLINDERS];
256         weightsystem_t weightsystem[MAX_WEIGHTSYSTEMS];
257         char *suit;
258         int sac, otu;
259         struct event *events;
260         int samples, alloc_samples;
261         struct sample sample[];
262 };
263
264 extern GList *dive_trip_list;
265 extern gboolean autogroup;
266 /* random threashold: three days without diving -> new trip
267  * this works very well for people who usually dive as part of a trip and don't
268  * regularly dive at a local facility; this is why trips are an optional feature */
269 #define TRIP_THRESHOLD 3600*24*3
270
271 #define UNGROUPED_DIVE(_dive) ((_dive)->tripflag == NO_TRIP)
272 #define DIVE_IN_TRIP(_dive) ((_dive)->tripflag == IN_TRIP)
273 #define NEXT_TRIP(_entry, _list) ((_entry) ? g_list_next(_entry) : (_list))
274 #define PREV_TRIP(_entry, _list) ((_entry) ? g_list_previous(_entry) : g_list_last(_list))
275 #define DIVE_TRIP(_trip) ((struct dive *)(_trip)->data)
276 #define DIVE_FITS_TRIP(_dive, _dive_trip) ((_dive_trip)->when - TRIP_THRESHOLD <= (_dive)->when)
277
278 static inline int dive_date_cmp(gconstpointer _a, gconstpointer _b) {
279         return ((struct dive *)(_a))->when - ((struct dive *)(_b))->when;
280 }
281
282 #define FIND_TRIP(_trip, _list) g_list_find_custom((_list), (_trip), dive_date_cmp)
283
284 #ifdef DEBUG_TRIP
285 static void dump_trip_list(void)
286 {
287         GList *p = NULL;
288         int i=0;
289         while ((p = NEXT_TRIP(p, dive_trip_list))) {
290                 struct tm *tm = gmtime(&DIVE_TRIP(p)->when);
291                 printf("trip %d to \"%s\" on %04u-%02u-%02u\n", ++i, DIVE_TRIP(p)->location,
292                         tm->tm_year + 1900, tm->tm_mon+1, tm->tm_mday);
293         }
294         printf("-----\n");
295 }
296 #endif
297
298 /* insert the trip into the list - but ensure you don't have two trips
299  * for the same date; but if you have, make sure you don't keep the
300  * one with less information */
301 static inline GList *insert_trip(struct dive *_trip, GList *_list)
302 {
303         GList *result = FIND_TRIP(_trip, _list);
304         if (result) {
305                 if (! DIVE_TRIP(result)->location)
306                         DIVE_TRIP(result)->location = _trip->location;
307         } else {
308                 result = g_list_insert_sorted((_list), (_trip), dive_date_cmp);
309         }
310 #ifdef DEBUG_TRIP
311         dump_trip_list();
312 #endif
313         return result;
314 }
315
316 /*
317  * We keep our internal data in well-specified units, but
318  * the input and output may come in some random format. This
319  * keeps track of those units.
320  */
321 /* turns out in Win32 PASCAL is defined as a calling convention */
322 #ifdef WIN32
323 #undef PASCAL
324 #endif
325 struct units {
326         enum { METERS, FEET } length;
327         enum { LITER, CUFT } volume;
328         enum { BAR, PSI, PASCAL } pressure;
329         enum { CELSIUS, FAHRENHEIT, KELVIN } temperature;
330         enum { KG, LBS } weight;
331 };
332
333 extern const struct units SI_units, IMPERIAL_units;
334 extern struct units input_units, output_units;
335
336 extern int verbose;
337
338 struct dive_table {
339         int nr, allocated, preexisting;
340         struct dive **dives;
341 };
342
343 extern struct dive_table dive_table;
344
345 extern int selected_dive;
346 #define current_dive (get_dive(selected_dive))
347
348 static inline struct dive *get_dive(unsigned int nr)
349 {
350         if (nr >= dive_table.nr || nr < 0)
351                 return NULL;
352         return dive_table.dives[nr];
353 }
354
355 /*
356  * Iterate over each dive, with the first parameter being the index
357  * iterator variable, and the second one being the dive one.
358  *
359  * I don't think anybody really wants the index, and we could make
360  * it local to the for-loop, but that would make us requires C99.
361  */
362 #define for_each_dive(_i,_x) \
363         for ((_i) = 0; ((_x) = get_dive(_i)) != NULL; (_i)++)
364
365 extern void parse_xml_init(void);
366 extern void parse_xml_buffer(const char *url, const char *buf, int size, GError **error);
367 extern void set_filename(const char *filename);
368
369 extern void parse_file(const char *filename, GError **error);
370
371 #ifdef XSLT
372 extern xmlDoc *test_xslt_transforms(xmlDoc *doc);
373 #endif
374
375 extern void show_dive_info(struct dive *);
376
377 extern void show_dive_equipment(struct dive *, int w_idx);
378
379 extern void show_dive_stats(struct dive *);
380
381 extern void update_dive(struct dive *new_dive);
382 extern void save_dives(const char *filename);
383
384 static inline unsigned int dive_size(int samples)
385 {
386         return sizeof(struct dive) + samples*sizeof(struct sample);
387 }
388
389 extern time_t utc_mktime(struct tm *tm);
390
391 extern struct dive *alloc_dive(void);
392 extern void record_dive(struct dive *dive);
393 extern void delete_dive(struct dive *dive);
394
395 extern struct sample *prepare_sample(struct dive **divep);
396 extern void finish_sample(struct dive *dive);
397
398 extern void report_dives(gboolean imported);
399 extern struct dive *fixup_dive(struct dive *dive);
400 extern struct dive *try_to_merge(struct dive *a, struct dive *b);
401
402 extern void renumber_dives(int nr);
403
404 extern void add_event(struct dive *dive, int time, int type, int flags, int value, const char *name);
405
406 /* UI related protopypes */
407
408 extern void init_ui(int *argcp, char ***argvp);
409
410 extern void run_ui(void);
411 extern void exit_ui(void);
412
413 extern void report_error(GError* error);
414
415 extern void add_cylinder_description(cylinder_type_t *);
416 extern void add_weightsystem_description(weightsystem_t *);
417 extern void add_people(const char *string);
418 extern void add_location(const char *string);
419 extern void add_suit(const char *string);
420 extern void remember_event(const char *eventname);
421 extern void evn_foreach(void (*callback)(const char *, int *, void *), void *data);
422
423 extern int add_new_dive(struct dive *dive);
424 extern int edit_dive_info(struct dive *dive);
425 extern int edit_multi_dive_info(struct dive *single_dive);
426 extern void dive_list_update_dives(void);
427 extern void flush_divelist(struct dive *dive);
428
429 #define DIVE_ERROR_PARSE 1
430
431 const char *weekday(int wday);
432 const char *monthname(int mon);
433
434 #define UTF8_DEGREE "\xc2\xb0"
435 #define UTF8_SUBSCRIPT_2 "\xe2\x82\x82"
436 #define UTF8_WHITESTAR "\xe2\x98\x86"
437 #define UTF8_BLACKSTAR "\xe2\x98\x85"
438 #define ZERO_STARS      UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR
439 #define ONE_STARS       UTF8_BLACKSTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR
440 #define TWO_STARS       UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR
441 #define THREE_STARS     UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_WHITESTAR UTF8_WHITESTAR
442 #define FOUR_STARS      UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_WHITESTAR
443 #define FIVE_STARS      UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR
444 extern const char *star_strings[];
445
446 #define AIR_PERMILLE 209
447
448 #endif /* DIVE_H */