]> git.tdb.fi Git - ext/subsurface.git/blob - dive.h
Do a dive de-dup pass
[ext/subsurface.git] / dive.h
1 #ifndef DIVE_H
2 #define DIVE_H
3
4 /*
5  * Some silly typedefs to make our units very explicit.
6  *
7  * Also, the units are chosen so that values can be expressible as
8  * integers, so that we never have FP rounding issues. And they
9  * are small enough that converting to/from imperial units doesn't
10  * really matter.
11  *
12  * We also strive to make '0' a meaningless number saying "not
13  * initialized", since many values are things that may not have
14  * been reported (eg tank pressure or temperature from dive
15  * computers that don't support them). But sometimes -1 is an even
16  * more explicit way of saying "not there".
17  *
18  * Thus "millibar" for pressure, for example, or "millikelvin" for
19  * temperatures. Doing temperatures in celsius or fahrenheit would
20  * make for loss of precision when converting from one to the other,
21  * and using millikelvin is SI-like but also means that a temperature
22  * of '0' is clearly just a missing temperature or tank pressure.
23  *
24  * Also strive to use units that can not possibly be mistaken for a
25  * valid value in a "normal" system without conversion. If the max
26  * depth of a dive is '20000', you probably didn't convert from mm on
27  * output, or if the max depth gets reported as "0.2ft" it was either
28  * a really boring dive, or there was some missing input conversion,
29  * and a 60-ft dive got recorded as 60mm.
30  *
31  * Doing these as "structs containing value" means that we always
32  * have to explicitly write out those units in order to get at the
33  * actual value. So there is hopefully little fear of using a value
34  * in millikelvin as Fahrenheit by mistake.
35  *
36  * We don't actually use these all yet, so maybe they'll change, but
37  * I made a number of types as guidelines.
38  */
39 typedef struct {
40         int seconds;
41 } duration_t;
42
43 typedef struct {
44         int mm;
45 } depth_t;
46
47 typedef struct {
48         int mbar;
49 } pressure_t;
50
51 typedef struct {
52         int mkelvin;
53 } temperature_t;
54
55 typedef struct {
56         int mliter;
57 } volume_t;
58
59 typedef struct {
60         int permille;
61 } fraction_t;
62
63 typedef struct {
64         int grams;
65 } weight_t;
66
67 typedef struct {
68         fraction_t o2;
69         fraction_t he;
70 } gasmix_t;
71
72 typedef struct {
73         volume_t size;
74         pressure_t pressure;
75 } tank_type_t;
76
77 static inline int to_feet(depth_t depth)
78 {
79         return depth.mm * 0.00328084 + 0.5;
80 }
81
82 static inline int to_C(temperature_t temp)
83 {
84         if (!temp.mkelvin)
85                 return 0;
86         return (temp.mkelvin - 273150) / 1000;
87 }
88
89 static inline int to_PSI(pressure_t pressure)
90 {
91         return pressure.mbar * 0.0145037738 + 0.5;
92 }
93
94 struct sample {
95         duration_t time;
96         depth_t depth;
97         temperature_t temperature;
98         pressure_t tankpressure;
99         int tankindex;
100 };
101
102 #define MAX_MIXES (4)
103
104 struct dive {
105         const char *name;
106         time_t when;
107         char *location;
108         char *notes;
109         depth_t maxdepth, meandepth;
110         duration_t duration, surfacetime;
111         depth_t visibility;
112         temperature_t airtemp, watertemp;
113         pressure_t beginning_pressure, end_pressure;
114         gasmix_t gasmix[MAX_MIXES];
115         int samples;
116         struct sample sample[];
117 };
118
119 extern int verbose;
120
121 struct dive_table {
122         int nr, allocated;
123         struct dive **dives;
124 };
125
126 extern struct dive_table dive_table;
127
128 static inline struct dive *get_dive(unsigned int nr)
129 {
130         if (nr >= dive_table.nr)
131                 return NULL;
132         return dive_table.dives[nr];
133 }
134
135 extern void parse_xml_init(void);
136 extern void parse_xml_file(const char *filename);
137
138 extern void flush_dive_info_changes(void);
139 extern void save_dives(const char *filename);
140
141 static inline unsigned int dive_size(int samples)
142 {
143         return sizeof(struct dive) + samples*sizeof(struct sample);
144 }
145
146 #endif /* DIVE_H */