Skip to content

Commit

Permalink
Merge pull request #4 from RIMS-Code/saturation_curve_fix
Browse files Browse the repository at this point in the history
change saturation curve to be contained in array
  • Loading branch information
trappitsch authored May 2, 2024
2 parents a13cfc2 + 99d01d7 commit 1e09cc9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
32 changes: 23 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,19 +446,26 @@ impl eframe::App for TemplateApp {
});
ui.add_space(VERTICAL_SPACE);

// Grid view of existing saturation curve
if !self.saturation_curves.is_empty() {
ui.label("List of existing saturation curve entries:");
ui.label("Sorted list of existing saturation curve entries:");
ui.add_space(VERTICAL_SPACE);

egui::Grid::new("saturation_curve_table")
.striped(true)
.min_col_width(COL_MIN_WIDTH)
.show(ui, |ui| {
ui.label("Title of curve");
ui.label("Delete entry?");
ui.end_row();
for (it, val) in self.saturation_curves.clone().iter().enumerate() {
ui.label(&val.title);

// Move up and down buttons
if ui.button("Move up").clicked() && it > 0 {
self.saturation_curves.swap(it, it - 1);
}
if ui.button("Move down").clicked() && it < self.saturation_curves.len() - 1 {
self.saturation_curves.swap(it, it + 1);
}

// Delete button
if ui.button("Delete").clicked() {
self.saturation_curves.remove(it);
}
Expand Down Expand Up @@ -516,13 +523,20 @@ impl eframe::App for TemplateApp {

egui::Grid::new("reference_table")
.striped(true)
.min_col_width(COL_MIN_WIDTH)
.show(ui, |ui| {
ui.label("DOI");
ui.label("Delete entry?");
ui.end_row();
for (it, val) in self.references.clone().iter().enumerate() {
// Label
ui.label(val);

// Move up and down buttons
if ui.button("Move up").clicked() && it > 0 {
self.references.swap(it, it - 1);
}
if ui.button("Move down").clicked() && it < self.references.len() - 1 {
self.references.swap(it, it + 1);
}

// Delete button
if ui.button("Delete").clicked() {
self.references.remove(it);
}
Expand Down
43 changes: 23 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,6 @@ fn create_json_output(app_entries: &TemplateApp) -> Result<String, String> {
"unit": scheme_unit_json,
},
},
"saturation_curves": {
},
"references": app_entries.references,
"submitted_by": app_entries.submitted_by,
});
Expand All @@ -614,13 +612,15 @@ fn create_json_output(app_entries: &TemplateApp) -> Result<String, String> {
}
}

let mut sat_curves_arr: Vec<Value> = Vec::new();
for val in app_entries.saturation_curves.iter() {
let sat_unit_json = match val.units {
SaturationCurveUnit::WCM2 => "W * cm^-2",
SaturationCurveUnit::W => "W",
};

json_out["saturation_curves"][&val.title] = json!({
let mut json_tmp = json!({
"title": val.title,
"notes": val.notes,
"unit": sat_unit_json,
"fit": val.fit,
Expand All @@ -630,18 +630,17 @@ fn create_json_output(app_entries: &TemplateApp) -> Result<String, String> {
}
});
match &val.xdat_unc {
Some(x) => {
json_out["saturation_curves"][&val.title]["data"]["x_err"] = Value::from(x.clone())
}
Some(x) => json_tmp["data"]["x_err"] = Value::from(x.clone()),
None => (),
}
match &val.ydat_unc {
Some(y) => {
json_out["saturation_curves"][&val.title]["data"]["y_err"] = Value::from(y.clone())
}
Some(y) => json_tmp["data"]["y_err"] = Value::from(y.clone()),
None => (),
}

sat_curves_arr.push(json_tmp);
}
json_out["saturation_curves"] = Value::from(sat_curves_arr);

match to_string_pretty(&json_out) {
Ok(json) => Ok(json),
Expand Down Expand Up @@ -763,17 +762,21 @@ fn load_config_file(app_entries: &mut TemplateApp) -> Result<(), String> {
app_entries.references = references;

// Load saturation curves if they are there
let sats = config_json["saturation_curves"].as_array();
let mut saturation_curves: Vec<SaturationCurve> = Vec::new();
if config_json["saturation_curves"].is_object() {
let sat_json_all = &config_json["saturation_curves"];
for (title, value) in sat_json_all.as_object().unwrap() {
let notes = value["notes"].as_str().unwrap_or("").to_owned();
let units = match value["unit"].as_str() {
if let Some(sats) = sats {
for sat in sats {
let title = match sat["title"].as_str() {
Some(t) => t.to_owned(),
None => continue,
};
let notes = sat["notes"].as_str().unwrap_or("").to_owned();
let units = match sat["unit"].as_str() {
Some("W") => SaturationCurveUnit::W,
_ => SaturationCurveUnit::WCM2,
};
let fit = value["fit"].as_bool().unwrap_or(true);
let xdat = match value["data"]["x"].as_array() {
let fit = sat["fit"].as_bool().unwrap_or(true);
let xdat = match sat["data"]["x"].as_array() {
Some(x) => json_array_to_f64(x)?,
None => {
return Err(
Expand All @@ -782,7 +785,7 @@ fn load_config_file(app_entries: &mut TemplateApp) -> Result<(), String> {
)
}
};
let ydat = match value["data"]["y"].as_array() {
let ydat = match sat["data"]["y"].as_array() {
Some(y) => json_array_to_f64(y)?,
None => {
return Err(
Expand All @@ -791,16 +794,16 @@ fn load_config_file(app_entries: &mut TemplateApp) -> Result<(), String> {
)
}
};
let xunc = match value["data"]["x_err"].as_array() {
let xunc = match sat["data"]["x_err"].as_array() {
Some(x) => Some(json_array_to_f64(x)?),
None => None,
};
let yunc = match value["data"]["y_err"].as_array() {
let yunc = match sat["data"]["y_err"].as_array() {
Some(y) => Some(json_array_to_f64(y)?),
None => None,
};
saturation_curves.push(SaturationCurve {
title: title.to_owned(),
title,
notes,
units,
fit,
Expand Down

0 comments on commit 1e09cc9

Please sign in to comment.