]> git.tdb.fi Git - ext/subsurface.git/blob - dive.h
Further cleanup of pressure and volume conversions
[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
9 /*
10  * Some silly typedefs to make our units very explicit.
11  *
12  * Also, the units are chosen so that values can be expressible as
13  * integers, so that we never have FP rounding issues. And they
14  * are small enough that converting to/from imperial units doesn't
15  * really matter.
16  *
17  * We also strive to make '0' a meaningless number saying "not
18  * initialized", since many values are things that may not have
19  * been reported (eg cylinder pressure or temperature from dive
20  * computers that don't support them). But sometimes -1 is an even
21  * more explicit way of saying "not there".
22  *
23  * Thus "millibar" for pressure, for example, or "millikelvin" for
24  * temperatures. Doing temperatures in celsius or fahrenheit would
25  * make for loss of precision when converting from one to the other,
26  * and using millikelvin is SI-like but also means that a temperature
27  * of '0' is clearly just a missing temperature or cylinder pressure.
28  *
29  * Also strive to use units that can not possibly be mistaken for a
30  * valid value in a "normal" system without conversion. If the max
31  * depth of a dive is '20000', you probably didn't convert from mm on
32  * output, or if the max depth gets reported as "0.2ft" it was either
33  * a really boring dive, or there was some missing input conversion,
34  * and a 60-ft dive got recorded as 60mm.
35  *
36  * Doing these as "structs containing value" means that we always
37  * have to explicitly write out those units in order to get at the
38  * actual value. So there is hopefully little fear of using a value
39  * in millikelvin as Fahrenheit by mistake.
40  *
41  * We don't actually use these all yet, so maybe they'll change, but
42  * I made a number of types as guidelines.
43  */
44 typedef struct {
45         int seconds;
46 } duration_t;
47
48 typedef struct {
49         int mm;
50 } depth_t;
51
52 typedef struct {
53         int mbar;
54 } pressure_t;
55
56 typedef struct {
57         int mkelvin;
58 } temperature_t;
59
60 typedef struct {
61         int mliter;
62 } volume_t;
63
64 typedef struct {
65         int permille;
66 } fraction_t;
67
68 typedef struct {
69         int grams;
70 } weight_t;
71
72 struct gasmix {
73         fraction_t o2;
74         fraction_t he;
75 };
76
77 typedef struct {
78         volume_t size;
79         pressure_t workingpressure;
80         const char *description;        /* "LP85", "AL72", "AL80", "HP100+" or whatever */
81 } cylinder_type_t;
82
83 typedef struct {
84         cylinder_type_t type;
85         struct gasmix gasmix;
86         pressure_t start, end;
87 } cylinder_t;
88
89 extern int get_pressure_units(unsigned int mb, const char **units);
90 extern double get_depth_units(unsigned int mm, int *frac, const char **units);
91 extern double get_volume_units(unsigned int mm, int *frac, const char **units);
92 extern double get_temp_units(unsigned int mm, const char **units);
93
94 static inline double bar_to_atm(double bar)
95 {
96         return bar / 1.01325;
97 }
98
99 static inline double ml_to_cuft(int ml)
100 {
101         return ml / 28316.8466;
102 }
103
104 static inline double cuft_to_ml(double cuft)
105 {
106         return cuft * 28.3168466;
107 }
108
109 static inline double mm_to_feet(int mm)
110 {
111         return mm * 0.00328084;
112 }
113
114 static inline int to_feet(depth_t depth)
115 {
116         return mm_to_feet(depth.mm) + 0.5;
117 }
118
119 static double mkelvin_to_C(int mkelvin)
120 {
121         return (mkelvin - 273150) / 1000.0;
122 }
123
124 static double mkelvin_to_F(int mkelvin)
125 {
126         return mkelvin * 9 / 5000.0 - 459.670;
127 }
128
129 static inline int to_C(temperature_t temp)
130 {
131         if (!temp.mkelvin)
132                 return 0;
133         return mkelvin_to_C(temp.mkelvin) + 0.5;
134 }
135
136 static inline int to_F(temperature_t temp)
137 {
138         if (!temp.mkelvin)
139                 return 0;
140         return mkelvin_to_F(temp.mkelvin) + 0.5;
141 }
142
143 static inline int to_K(temperature_t temp)
144 {
145         if (!temp.mkelvin)
146                 return 0;
147         return (temp.mkelvin + 499)/1000;
148 }
149
150 static inline double psi_to_bar(double psi)
151 {
152         return psi / 14.5037738;
153 }
154 static inline int to_PSI(pressure_t pressure)
155 {
156         return pressure.mbar * 0.0145037738 + 0.5;
157 }
158
159 static inline double to_ATM(pressure_t pressure)
160 {
161         return pressure.mbar / 1013.25;
162 }
163
164 static inline int mbar_to_PSI(int mbar)
165 {
166         pressure_t p = {mbar};
167         return to_PSI(p);
168 }
169
170 struct sample {
171         duration_t time;
172         depth_t depth;
173         temperature_t temperature;
174         pressure_t cylinderpressure;
175         int cylinderindex;
176 };
177
178 /*
179  * Events are currently pretty meaningless. This is
180  * just based on the random data that libdivecomputer
181  * gives us. I'm not sure what a real "architected"
182  * event model would actually look like, but right
183  * now you can associate a list of events with a dive,
184  * and we'll do something about it.
185  */
186 struct event {
187         struct event *next;
188         duration_t time;
189         int type, flags, value;
190         char name[];
191 };
192
193 #define MAX_CYLINDERS (8)
194
195 struct dive {
196         int number;
197         time_t when;
198         char *location;
199         char *notes;
200         char *divemaster, *buddy;
201         double latitude, longitude;
202         depth_t maxdepth, meandepth;
203         duration_t duration, surfacetime;
204         depth_t visibility;
205         temperature_t airtemp, watertemp;
206         cylinder_t cylinder[MAX_CYLINDERS];
207         int sac, otu;
208         struct event *events;
209         int samples, alloc_samples;
210         struct sample sample[];
211 };
212
213 /*
214  * We keep our internal data in well-specified units, but
215  * the input and output may come in some random format. This
216  * keeps track of those units.
217  */
218 /* turns out in Win32 PASCAL is defined as a calling convention */
219 #ifdef WIN32
220 #undef PASCAL
221 #endif
222 struct units {
223         enum { METERS, FEET } length;
224         enum { LITER, CUFT } volume;
225         enum { BAR, PSI, PASCAL } pressure;
226         enum { CELSIUS, FAHRENHEIT, KELVIN } temperature;
227         enum { KG, LBS } weight;
228 };
229
230 extern const struct units SI_units, IMPERIAL_units;
231 extern struct units input_units, output_units;
232
233 extern int verbose;
234
235 struct dive_table {
236         int nr, allocated, preexisting;
237         struct dive **dives;
238 };
239
240 extern struct dive_table dive_table;
241
242 extern int selected_dive;
243 #define current_dive (get_dive(selected_dive))
244
245 static inline struct dive *get_dive(unsigned int nr)
246 {
247         if (nr >= dive_table.nr)
248                 return NULL;
249         return dive_table.dives[nr];
250 }
251
252 extern void parse_xml_init(void);
253 extern void parse_xml_file(const char *filename, GError **error);
254 extern void set_filename(const char *filename);
255
256 extern void show_dive_info(struct dive *);
257 extern void flush_dive_info_changes(struct dive *);
258
259 extern void show_dive_equipment(struct dive *);
260 extern void flush_dive_equipment_changes(struct dive *);
261
262 extern void update_dive(struct dive *new_dive);
263 extern void save_dives(const char *filename);
264
265 static inline unsigned int dive_size(int samples)
266 {
267         return sizeof(struct dive) + samples*sizeof(struct sample);
268 }
269
270 extern time_t utc_mktime(struct tm *tm);
271
272 extern struct dive *alloc_dive(void);
273 extern void record_dive(struct dive *dive);
274
275 extern struct sample *prepare_sample(struct dive **divep);
276 extern void finish_sample(struct dive *dive, struct sample *sample);
277
278 extern void report_dives(gboolean imported);
279 extern struct dive *fixup_dive(struct dive *dive);
280 extern struct dive *try_to_merge(struct dive *a, struct dive *b);
281
282 extern void renumber_dives(int nr);
283
284 extern void add_event(struct dive *dive, int time, int type, int flags, int value, const char *name);
285
286 /* UI related protopypes */
287
288 extern void init_ui(int argc, char **argv);
289
290 extern void run_ui(void);
291
292 extern void report_error(GError* error);
293
294 extern void add_cylinder_description(cylinder_type_t *);
295 extern void add_people(const char *string);
296 extern void add_location(const char *string);
297 extern void remember_event(const char *eventname);
298 extern void evn_foreach(void (*callback)(const char *, int *, void *), void *data);
299
300 extern void dive_list_update_dives(void);
301 extern void flush_divelist(struct dive *dive);
302
303 #define DIVE_ERROR_PARSE 1
304
305 const char *weekday(int wday);
306 const char *monthname(int mon);
307
308 #define UTF8_DEGREE "\xc2\xb0"
309 #define UTF8_SUBSCRIPT_2 "\xe2\x82\x82"
310
311 #endif /* DIVE_H */