diff --git a/dive.c b/dive.c
index 35f1c3f46..e1c564dfb 100644
--- a/dive.c
+++ b/dive.c
@@ -484,34 +484,35 @@ static void fixup_watertemp(struct dive *dive)
dive->watertemp.mkelvin = (sum + nr / 2) / nr;
}
-void fixup_airtemp(struct dive *dive)
+/*
+ * What do the dive computers say the air temperature is?
+ */
+unsigned int dc_airtemp(struct divecomputer *dc)
{
- struct divecomputer *dc;
int sum = 0, nr = 0;
- if (dive->airtemp.mkelvin)
- return;
- for_each_dc(dive, dc) {
+ do {
if (dc->airtemp.mkelvin) {
sum += dc->airtemp.mkelvin;
nr++;
}
- }
- if (nr)
- dive->airtemp.mkelvin = (sum + nr / 2) / nr;
+ } while ((dc = dc->next) != NULL);
+ if (!nr)
+ return 0;
+ return (sum + nr / 2) / nr;
+}
+
+static void fixup_airtemp(struct dive *dive)
+{
+ if (!dive->airtemp.mkelvin)
+ dive->airtemp.mkelvin = dc_airtemp(&dive->dc);
}
/* zero out the airtemp in the dive structure if it was just created by
* running fixup on the dive. keep it if it had been edited by hand */
static void un_fixup_airtemp(struct dive *a)
{
- temperature_t temp;
- temp.mkelvin = a->airtemp.mkelvin;
- a->airtemp.mkelvin = 0;
- fixup_airtemp(a);
- if (a->airtemp.mkelvin && a->airtemp.mkelvin != temp.mkelvin)
- a->airtemp.mkelvin = temp.mkelvin;
- else
+ if (a->airtemp.mkelvin && a->airtemp.mkelvin == dc_airtemp(&a->dc))
a->airtemp.mkelvin = 0;
}
diff --git a/dive.h b/dive.h
index fdeac977a..8e68c9dab 100644
--- a/dive.h
+++ b/dive.h
@@ -561,7 +561,7 @@ extern void finish_sample(struct divecomputer *dc);
extern void sort_table(struct dive_table *table);
extern void report_dives(gboolean imported, gboolean prefer_imported);
extern struct dive *fixup_dive(struct dive *dive);
-extern void fixup_airtemp(struct dive *dive);
+extern unsigned int dc_airtemp(struct divecomputer *dc);
extern struct dive *merge_dives(struct dive *a, struct dive *b, int offset, gboolean prefer_downloaded);
extern struct dive *try_to_merge(struct dive *a, struct dive *b, gboolean prefer_downloaded);
extern void renumber_dives(int nr);
diff --git a/save-xml.c b/save-xml.c
index c0af41c4c..b3bf9701d 100644
--- a/save-xml.c
+++ b/save-xml.c
@@ -152,17 +152,14 @@ static void save_depths(FILE *f, struct divecomputer *dc)
static void save_dive_temperature(FILE *f, struct dive *dive)
{
- temperature_t temp;
- temp.mkelvin = dive->airtemp.mkelvin;
- dive->airtemp.mkelvin = 0;
- fixup_airtemp(dive);
- if (dive->airtemp.mkelvin && temp.mkelvin != dive->airtemp.mkelvin) {
- fputs(" \n", f);
- }
- dive->airtemp.mkelvin = temp.mkelvin;
+ if (!dive->airtemp.mkelvin)
+ return;
+ if (dive->airtemp.mkelvin == dc_airtemp(&dive->dc))
+ return;
+ fputs(" airtemp, " air='", "'");
+ fputs("/>\n", f);
}
static void save_temperatures(FILE *f, struct divecomputer *dc)