]> git.tdb.fi Git - ext/subsurface.git/blob - dive.h
Merge branch 'stars' of git://git.hohndel.org/subsurface
[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 extern int get_pressure_units(unsigned int mb, const char **units);
91 extern double get_depth_units(unsigned int mm, int *frac, const char **units);
92 extern double get_volume_units(unsigned int mm, int *frac, const char **units);
93 extern double get_temp_units(unsigned int mm, const char **units);
94
95 static inline double ml_to_cuft(int ml)
96 {
97         return ml / 28316.8466;
98 }
99
100 static inline double cuft_to_l(double cuft)
101 {
102         return cuft * 28.3168466;
103 }
104
105 static inline double mm_to_feet(int mm)
106 {
107         return mm * 0.00328084;
108 }
109
110 static inline int to_feet(depth_t depth)
111 {
112         return mm_to_feet(depth.mm) + 0.5;
113 }
114
115 static double mkelvin_to_C(int mkelvin)
116 {
117         return (mkelvin - 273150) / 1000.0;
118 }
119
120 static double mkelvin_to_F(int mkelvin)
121 {
122         return mkelvin * 9 / 5000.0 - 459.670;
123 }
124
125 static inline int to_C(temperature_t temp)
126 {
127         if (!temp.mkelvin)
128                 return 0;
129         return mkelvin_to_C(temp.mkelvin) + 0.5;
130 }
131
132 static inline int to_F(temperature_t temp)
133 {
134         if (!temp.mkelvin)
135                 return 0;
136         return mkelvin_to_F(temp.mkelvin) + 0.5;
137 }
138
139 static inline int to_K(temperature_t temp)
140 {
141         if (!temp.mkelvin)
142                 return 0;
143         return (temp.mkelvin + 499)/1000;
144 }
145
146 static inline double psi_to_bar(double psi)
147 {
148         return psi / 14.5037738;
149 }
150 static inline int to_PSI(pressure_t pressure)
151 {
152         return pressure.mbar * 0.0145037738 + 0.5;
153 }
154
155 static inline double bar_to_atm(double bar)
156 {
157         return bar / 1.01325;
158 }
159
160 static inline double to_ATM(pressure_t pressure)
161 {
162         return pressure.mbar / 1013.25;
163 }
164
165 static inline int mbar_to_PSI(int mbar)
166 {
167         pressure_t p = {mbar};
168         return to_PSI(p);
169 }
170
171 struct sample {
172         duration_t time;
173         depth_t depth;
174         temperature_t temperature;
175         pressure_t cylinderpressure;
176         int cylinderindex;
177 };
178
179 /*
180  * Events are currently pretty meaningless. This is
181  * just based on the random data that libdivecomputer
182  * gives us. I'm not sure what a real "architected"
183  * event model would actually look like, but right
184  * now you can associate a list of events with a dive,
185  * and we'll do something about it.
186  */
187 struct event {
188         struct event *next;
189         duration_t time;
190         int type, flags, value;
191         char name[];
192 };
193
194 #define MAX_CYLINDERS (8)
195
196 struct dive {
197         int number;
198         time_t when;
199         char *location;
200         char *notes;
201         char *divemaster, *buddy;
202         int rating;
203         double latitude, longitude;
204         depth_t maxdepth, meandepth;
205         duration_t duration, surfacetime;
206         depth_t visibility;
207         temperature_t airtemp, watertemp;
208         cylinder_t cylinder[MAX_CYLINDERS];
209         int sac, otu;
210         struct event *events;
211         int samples, alloc_samples;
212         struct sample sample[];
213 };
214
215 /*
216  * We keep our internal data in well-specified units, but
217  * the input and output may come in some random format. This
218  * keeps track of those units.
219  */
220 /* turns out in Win32 PASCAL is defined as a calling convention */
221 #ifdef WIN32
222 #undef PASCAL
223 #endif
224 struct units {
225         enum { METERS, FEET } length;
226         enum { LITER, CUFT } volume;
227         enum { BAR, PSI, PASCAL } pressure;
228         enum { CELSIUS, FAHRENHEIT, KELVIN } temperature;
229         enum { KG, LBS } weight;
230 };
231
232 extern const struct units SI_units, IMPERIAL_units;
233 extern struct units input_units, output_units;
234
235 extern int verbose;
236
237 struct dive_table {
238         int nr, allocated, preexisting;
239         struct dive **dives;
240 };
241
242 extern struct dive_table dive_table;
243
244 extern int selected_dive;
245 #define current_dive (get_dive(selected_dive))
246
247 static inline struct dive *get_dive(unsigned int nr)
248 {
249         if (nr >= dive_table.nr)
250                 return NULL;
251         return dive_table.dives[nr];
252 }
253
254 extern void parse_xml_init(void);
255 extern void parse_xml_file(const char *filename, GError **error);
256 extern void set_filename(const char *filename);
257
258 #ifdef XSLT
259 extern xmlDoc *test_xslt_transforms(xmlDoc *doc);
260 #endif
261
262 extern void show_dive_info(struct dive *);
263
264 extern void show_dive_equipment(struct dive *);
265
266 extern void show_dive_stats(struct dive *);
267
268 extern void update_dive(struct dive *new_dive);
269 extern void save_dives(const char *filename);
270
271 static inline unsigned int dive_size(int samples)
272 {
273         return sizeof(struct dive) + samples*sizeof(struct sample);
274 }
275
276 extern time_t utc_mktime(struct tm *tm);
277
278 extern struct dive *alloc_dive(void);
279 extern void record_dive(struct dive *dive);
280
281 extern struct sample *prepare_sample(struct dive **divep);
282 extern void finish_sample(struct dive *dive, struct sample *sample);
283
284 extern void report_dives(gboolean imported);
285 extern struct dive *fixup_dive(struct dive *dive);
286 extern struct dive *try_to_merge(struct dive *a, struct dive *b);
287
288 extern void renumber_dives(int nr);
289
290 extern void add_event(struct dive *dive, int time, int type, int flags, int value, const char *name);
291
292 /* UI related protopypes */
293
294 extern void init_ui(int *argcp, char ***argvp);
295
296 extern void run_ui(void);
297
298 extern void report_error(GError* error);
299
300 extern void add_cylinder_description(cylinder_type_t *);
301 extern void add_people(const char *string);
302 extern void add_location(const char *string);
303 extern void remember_event(const char *eventname);
304 extern void evn_foreach(void (*callback)(const char *, int *, void *), void *data);
305
306 extern int edit_dive_info(struct dive *dive);
307 extern void dive_list_update_dives(void);
308 extern void flush_divelist(struct dive *dive);
309
310 #define DIVE_ERROR_PARSE 1
311
312 const char *weekday(int wday);
313 const char *monthname(int mon);
314
315 #define UTF8_DEGREE "\xc2\xb0"
316 #define UTF8_SUBSCRIPT_2 "\xe2\x82\x82"
317 #define UTF8_WHITESTAR "\xe2\x98\x86"
318 #define UTF8_BLACKSTAR "\xe2\x98\x85"
319 #define ZERO_STARS      UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR
320 #define ONE_STARS       UTF8_BLACKSTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR
321 #define TWO_STARS       UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_WHITESTAR UTF8_WHITESTAR UTF8_WHITESTAR
322 #define THREE_STARS     UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_WHITESTAR UTF8_WHITESTAR
323 #define FOUR_STARS      UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_WHITESTAR
324 #define FIVE_STARS      UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR UTF8_BLACKSTAR
325 extern const char *star_strings[];
326
327 #endif /* DIVE_H */