Guest User

Untitled

a guest
Jan 14th, 2020
88
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Analyzer {
  2. List<Days> myDaysList;
  3. List<Double> averageForce = new ArrayList<>();
  4. List<Double> averageTemperature = new ArrayList<>();
  5. //use the file in the directory to generate analyzer class
  6. public Analyzer(File file){
  7. myDaysList = new ArrayList<>();
  8. try {
  9. BufferedReader br = new BufferedReader(new FileReader(file));
  10. //first line introduces item: force,temp,month,day,time1,time2
  11. String line = br.readLine();
  12. Log.v("line",line);
  13. line = br.readLine();
  14. int year = 0;
  15. int month = 0;
  16. int day = 0;
  17. int hour;
  18. int minute;
  19. int oldDay = 0;
  20. int oldMonth = 0;
  21. double force;
  22. double temperature;
  23. Days myDays = new Days(year,month,day);
  24. Records myRecords;
  25. boolean firstOneRemove = true;
  26. while ((line = br.readLine()) != null) {
  27. Log.v("line",line);
  28. try{
  29. String[] values = line.split(", ");
  30.  
  31. force = Double.valueOf(values[0]);
  32. temperature = Double.valueOf(values[1]);
  33. year = Integer.valueOf(values[2]);
  34. month = Integer.valueOf(values[3]);
  35. day = Integer.valueOf(values[4]);
  36. hour = Integer.valueOf(values[5]);
  37. minute = Integer.valueOf(values[6]);
  38. myRecords = new Records(year,month,day,hour,minute,force,temperature);
  39. //create new day
  40. if (oldDay != day){
  41. myDaysList.add(myDays);
  42. if (firstOneRemove){
  43. firstOneRemove = false;
  44. myDaysList.remove(0);
  45. }
  46.  
  47. //check month
  48. if (oldMonth != month){
  49. //check if need to change year
  50. if((oldMonth == 12) && (month == 1)) {
  51. year ++;
  52. }
  53. oldMonth = month;
  54. }
  55. myDays = new Days(year,month,day);
  56. oldDay = day;
  57. }
  58. myDays.recordsList.add(myRecords);
  59. }
  60. catch (Exception e){
  61. continue;
  62. }
  63. }
  64. myDaysList.add(myDays);
  65.  
  66. br.close();
  67.  
  68. double[] averageF = new double[15];
  69. double[] averageT = new double[15];
  70. int counter = 0;
  71. for (Days days:myDaysList){
  72. for (Records rec:days.getRecordsList()){
  73. averageF[counter%15] = rec.getForceVal();
  74. averageT[counter%15] = rec.getTempVal();
  75. if (counter == 15){
  76. for (int i = 0; i < 15; i++) {
  77. averageForce.add(calculateAvg(averageF));
  78. averageTemperature.add(calculateAvg(averageT));
  79. }
  80. }
  81. if (counter > 15){
  82. averageForce.add(calculateAvg(averageF));
  83. averageTemperature.add(calculateAvg(averageT));
  84. }
  85. counter++;
  86. }
  87. }
  88. }
  89. catch(FileNotFoundException e){
  90.  
  91. }
  92. catch (IOException e) {
  93. //You'll need to add proper error handling here
  94. }
  95. }
  96.  
  97. public List<Days> getMyDaysList() {
  98. return myDaysList;
  99. }
  100.  
  101. private double calculateAvg(double[] arrays){
  102. double total = 0;
  103. for(int i=0; i<arrays.length; i++){
  104. total = total + arrays[i];
  105. }
  106. return total/arrays.length;
  107. }
  108.  
  109. public List<Double> getAverageForce(){
  110. return averageForce;
  111. }
  112.  
  113. public List<Double> getAverageTemperature(){
  114. return averageTemperature;
  115. }
  116.  
  117. public Days getDaysFromDaysList(int year, int month, int day){
  118. for (Days days:myDaysList){
  119. if (days.isEqual(year,month,day)){
  120. return days;
  121. }
  122. }
  123. return null;
  124. }
  125.  
  126. public int[] getStartEndIndex(int[] startTime, int[] endTime){
  127. int[] result = new int[]{0,0};
  128. int counter = 0;
  129. Records startRecord = new Records(startTime[0],startTime[1],startTime[2],startTime[3],startTime[4],0,0);
  130. Records endRecord = new Records(endTime[0],endTime[1],endTime[2],endTime[3],endTime[4],0,0);
  131. for (Days days:myDaysList){
  132. for (Records rec:days.getRecordsList()) {
  133. if (rec.dateEquals(startRecord)){
  134. result[0] = counter;
  135. }
  136. if (rec.dateEquals(endRecord)){
  137. result[1] = counter;
  138. break;
  139. }
  140. counter++;
  141. }
  142. }
  143. return result;
  144. }
  145.  
  146. //use the downloaded data object to generate analyzer class
  147. public Analyzer(List<Records> downloadedData){
  148. //first line must be Header+year
  149. myDaysList = new ArrayList<>();
  150. int year = downloadedData.get(0).getYear();
  151. int month= downloadedData.get(0).getMonth();
  152. int day= downloadedData.get(0).getDate();
  153. int hour,minute;
  154. int oldDay = 0;
  155. int oldMonth = 0;
  156. double force;
  157. double temperature;
  158. Days myDays = new Days(year,month,day);
  159.  
  160. boolean firstOneRemove = true;
  161. for (int i =1;i<downloadedData.size();i++){
  162. Records myRecords = downloadedData.get(i);
  163. try{
  164. //create new day
  165. if (oldDay != day){
  166. myDaysList.add(myDays);
  167. if (firstOneRemove){
  168. firstOneRemove = false;
  169. myDaysList.remove(0);
  170. }
  171. //check month
  172. if (oldMonth != month){
  173. //check if need to change year
  174. if((oldMonth == 12) && (month == 1)) {
  175. year ++;
  176. }
  177. oldMonth = month;
  178. }
  179. myDays = new Days(year,month,day);
  180. oldDay = day;
  181. }
  182. myDays.recordsList.add(myRecords);
  183. }
  184. catch (Exception e){
  185. continue;
  186. }
  187. }
  188. myDaysList.add(myDays);
  189.  
  190. double[] averageF = new double[15];
  191. double[] averageT = new double[15];
  192. int counter = 0;
  193. for (Days days:myDaysList){
  194. for (Records rec:days.getRecordsList()){
  195. averageF[counter%15] = rec.getForceVal();
  196. averageT[counter%15] = rec.getTempVal();
  197. if (counter == 15){
  198. for (int i = 0; i < 15; i++) {
  199. averageForce.add(calculateAvg(averageF));
  200. averageTemperature.add(calculateAvg(averageT));
  201. }
  202. }
  203. if (counter > 15){
  204. averageForce.add(calculateAvg(averageF));
  205. averageTemperature.add(calculateAvg(averageT));
  206. }
  207. counter++;
  208. }
  209. }
  210. }
  211. }
RAW Paste Data