[chronojump] Fixed: export/import of forceSensor files (and maybe other tests) with '.', ', ' depending on locales



commit bbf110446274fe98bea7b0f062943ca140451d4d
Author: Xavier de Blas <xaviblas gmail com>
Date:   Mon Dec 21 11:23:29 2020 +0100

    Fixed: export/import of forceSensor files (and maybe other tests) with '.', ',' depending on locales

 src/forceSensor.cs          | 21 +++++++++++++--------
 src/gui/app1/forceSensor.cs | 23 +++++++++++++++--------
 src/util.cs                 | 30 ++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 16 deletions(-)
---
diff --git a/src/forceSensor.cs b/src/forceSensor.cs
index 84b0acfa..bd442cdd 100644
--- a/src/forceSensor.cs
+++ b/src/forceSensor.cs
@@ -1379,11 +1379,12 @@ public class ForceSensorGraph
        private int startSample;
        private int endSample;
        private bool startEndOptimized;
+       private bool decimalIsPoint;
 
        public ForceSensorGraph(ForceSensor.CaptureOptions fsco, List<ForceSensorRFD> rfdList,
                        ForceSensorImpulse impulse, int testLength, int percentChange,
                        string title, string exercise, string datetime, TriggerList triggerList,
-                       int startSample, int endSample, bool startEndOptimized)
+                       int startSample, int endSample, bool startEndOptimized, bool decimalIsPoint)
        {
                this.fsco = fsco;
                this.rfdList = rfdList;
@@ -1397,6 +1398,7 @@ public class ForceSensorGraph
                this.startSample = startSample;
                this.endSample = endSample;
                this.startEndOptimized = startEndOptimized;
+               this.decimalIsPoint = decimalIsPoint;
 
                averageLength = 0.1;
                vlineT0 = false;
@@ -1422,9 +1424,14 @@ public class ForceSensorGraph
                System.Globalization.NumberFormatInfo localeInfo = new 
System.Globalization.NumberFormatInfo();
                localeInfo = System.Globalization.NumberFormatInfo.CurrentInfo;
 
+               //since 2.0.3 decimalChar is . (before it was locale specific)
+               string decimalChar = ".";
+               if(! decimalIsPoint)
+                       decimalChar = localeInfo.NumberDecimalSeparator;
+
                string scriptOptions =
                        "#os\n" +                       UtilEncoder.OperatingSystemForRGraphs() + "\n" +
-                       "#decimalChar\n" +              localeInfo.NumberDecimalSeparator + "\n" +
+                       "#decimalChar\n" +              decimalChar + "\n" +
                        "#graphWidth\n" +               graphWidth.ToString() + "\n" +
                        "#graphHeight\n" +              graphHeight.ToString() + "\n" +
                        "#averageLength\n" +            Util.ConvertToPoint(averageLength) + "\n" +
@@ -1608,12 +1615,10 @@ public class ForceSensorAnalyzeInstant
                                if(strFull.Length != 2)
                                        continue;
 
-                               /*
-                                * TODO: Make this work with decimals as comma and decimals as point
-                                * to fix problems on importing data on different localised computer
-                                */
+                               //this can takt forces recorded as , or as . because before 2.0.3 forces 
decimal was locale specific.
+                               //since 2.0.3 forces are recorded with .
 
-                               if(Util.IsNumber(strFull[0], false) && Util.IsNumber(strFull[1], true))
+                               if(Util.IsNumber(strFull[0], false) && 
Util.IsNumber(Util.ChangeDecimalSeparator(strFull[1]), true))
                                {
                                        double timeD = Convert.ToDouble(strFull[0]);
 
@@ -1633,7 +1638,7 @@ public class ForceSensorAnalyzeInstant
                                        */
 
                                        times.Add(Convert.ToInt32(timeD));
-                                       forces.Add(Convert.ToDouble(strFull[1]));
+                                       forces.Add(Convert.ToDouble(Util.ChangeDecimalSeparator(strFull[1])));
                                }
                        }
                }
diff --git a/src/gui/app1/forceSensor.cs b/src/gui/app1/forceSensor.cs
index 17fb05f6..bbe362b1 100644
--- a/src/gui/app1/forceSensor.cs
+++ b/src/gui/app1/forceSensor.cs
@@ -1247,7 +1247,8 @@ public partial class ChronoJumpWindow
                        //if(forceSensorCaptureOption != ForceSensor.CaptureOptions.NORMAL)
                        //      LogB.Information(string.Format("with abs or inverted flag: time: {0}, force: 
{1}", time, forceCalculated));
 
-                       writer.WriteLine(time.ToString() + ";" + force.ToString()); //on file force is stored 
without flags
+                       //force decimal is . since 2.0.3 Before was culture specific.
+                       writer.WriteLine(time.ToString() + ";" + Util.ConvertToPoint(force)); //on file force 
is stored without flags
 
                        forceSensorValues.TimeLast = time;
                        forceSensorValues.ValueLast = forceCalculated;
@@ -2158,10 +2159,18 @@ LogB.Information(" fs R ");
                        sampleB += hscale_force_sensor_ai_b_BeforeZoom;
                }
 
+
+               /*
+                * (*) check if decimal is point
+                * before 2.0.3 decimal point of forces was culture specific. From 2.0.3 is .
+                * read this file to see which is the decimal point
+                */
+
                ForceSensorGraph fsg = new ForceSensorGraph(getForceSensorCaptureOptions(), rfdList, impulse,
                                duration, Convert.ToInt32(spin_force_rfd_duration_percent.Value),
                                title, exercise, currentForceSensor.DateTimePublic, triggerListForceSensor,
-                               sampleA, sampleB, preferences.forceSensorStartEndOptimized
+                               sampleA, sampleB, preferences.forceSensorStartEndOptimized,
+                               Util.CSVDecimalColumnIsPoint(UtilEncoder.GetmifCSVFileName(), 1)              
  // (*)
                                );
 
                int imageWidth = UtilGtk.WidgetWidth(viewport_force_sensor_graph);
@@ -2230,15 +2239,13 @@ LogB.Information(" fs R ");
                                if(strFull.Length != 2)
                                        continue;
 
-                               /*
-                                * TODO: Make this work with decimals as comma and decimals as point
-                                * to fix problems on importing data on different localised computer
-                                */
+                               //this can take forces recorded as , or as . because before 2.0.3 forces 
decimal was locale specific.
+                               //since 2.0.3 forces are recorded with .
 
-                               if(Util.IsNumber(strFull[0], false) && Util.IsNumber(strFull[1], true))
+                               if(Util.IsNumber(strFull[0], false) && 
Util.IsNumber(Util.ChangeDecimalSeparator(strFull[1]), true))
                                {
                                        times.Add(Convert.ToInt32(strFull[0]));
-                                       forces.Add(Convert.ToDouble(strFull[1]));
+                                       forces.Add(Convert.ToDouble(Util.ChangeDecimalSeparator(strFull[1])));
                                }
                        }
                }
diff --git a/src/util.cs b/src/util.cs
index 82e19302..7e3e4a12 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -44,6 +44,36 @@ public class Util
                return false;
        }
 
+       /*
+        * before 2.0.3 decimal point of forceSensor forces was culture specific. From 2.0.3 is .
+        * this method helps to see how is stored to be opened in R
+        */
+       public static bool CSVDecimalColumnIsPoint(string filename, int column) //column starts at 0
+       {
+               List<string> contents = Util.ReadFileAsStringList(filename);
+               bool headersRow = true;
+
+               foreach(string str in contents)
+               {
+                       //avoid header row (if any)
+                       if(headersRow)
+                               headersRow = false;
+                       else {
+                               string [] strFull = str.Split(new char[] {';'});
+                               if(strFull.Length < column)
+                                       continue;
+
+                               //check that is a number when converted to current locale
+                               if(! Util.IsNumber(Util.ChangeDecimalSeparator(strFull[column]), true))
+                                       continue;
+
+                               return(strFull[column].Contains("."));
+                       }
+               }
+               //if nothing is found, just say is point '.' Is not relevant
+               return(true);
+       }
+
        //all numbers are saved in database with '.' as decimal separator (method for numbers)
        public static string ConvertToPoint (double myDouble)
        {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]