Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. (function(app) {
  2. "use strict";
  3.  
  4. /**
  5. * This function gathers windinformation from the weatherInfo[] and pushes it into two other arrays.
  6. * @param weatherArray | The array you want to gather data from.
  7. * @return Two arrays with windvalues.
  8. */
  9. var getWindInfo = function(weatherArray) {
  10. var windStrenght = [];
  11. var windDirection = [];
  12. var timeString = "12:00:00Z";
  13. for (var i = 0; i < weatherArray.length; i++) {
  14. if (weatherArray[i].validTime.indexOf(timeString) !== -1) {
  15. windDirection.push(weatherArray[i].wd);
  16. windStrenght.push(weatherArray[i].ws + " m/s ");
  17. }
  18. }
  19. if (windDirection.length < 10) {
  20. windDirection.splice(0, 0, "Ej tillgängligt");
  21. windStrenght.splice(0, 0, "Ej tillgängligt");
  22. }
  23. return [windDirection, windStrenght];
  24. }
  25. app.getWindInfo = getWindInfo;
  26.  
  27. /**
  28. * This function gathers information about temperature from the weatherInfo[].
  29. * It gathers all temperaturevalues and then calculates a average value before pushing it into averageTemp[].
  30. * @param weatherArray | An array that contains all the information from SMHI.
  31. * @return Two arrays with temperaturevalues.
  32. */
  33. var getTemp = function(weatherArray) {
  34. var highestTempArray = [];
  35. var averageTempArray = [];
  36. var b = []; //Local array that stores all dates for all objects.
  37. var uniqueDates = []; //Local array that stores all UNIQUE dates.
  38.  
  39.  
  40. $.each(weatherArray, function(index, val) {
  41. var a = weatherArray[index].validTime.split('T');
  42. b.push(a[0]); //Push the datevalues (without hours, minutes and seconds) into the temporary array.
  43.  
  44. //Looping trough the array with all dates and removes duplicates.
  45. $.each(b, function(i, date) {
  46. if ($.inArray(date, uniqueDates) === -1) {
  47. uniqueDates.push(date);
  48. }
  49. });
  50. });
  51.  
  52. //Here I remove the first spot in the array if it contains information from the previuous day.
  53. //It takes a few hours on the new day before it actually gets the first spot in the JSON-object.
  54. var a = moment().format("YYYY-MM-DD");
  55. if(uniqueDates[0].indexOf(a) == -1){
  56. uniqueDates.splice(0, 1);
  57. }
  58.  
  59. //Here we have two loops. The outer goes trough the unique dates one at the time
  60. //The inner loop goes trough the entire weatherArray with all objects and picks out the ones with matching dates.
  61. //When the inner loop is completed it calculates the average temperature and pushes it into the averageTemp[].
  62. $.each(uniqueDates, function(index, val) {
  63. var currentDate = uniqueDates[index];
  64. var s = 0; //Variable s sums the values from the weatherArray.
  65. var n = ""; // Variable n counts the number of values.
  66. var maxArray = [];
  67.  
  68. $.each(weatherArray, function(index, val) {
  69. if (weatherArray[index].validTime.indexOf(currentDate) !== -1) {
  70. var number = parseFloat(weatherArray[index].t); //Turns the string into a number.
  71. s += number; //Adding the value.
  72. maxArray.push(number.toFixed(1)); //Pushing the value into an array which is used in the getHighestTemp-function.
  73. n++; //Increasing the amount of values with one.
  74. } else if (index == weatherArray.length - 1) {
  75. var fixed = (s / n).toFixed(1);
  76. averageTempArray.push(fixed);
  77. getHighestTemp(maxArray, highestTempArray);
  78. }
  79. });
  80. });
  81. return [averageTempArray, highestTempArray];
  82. }
  83. app.getTemp = getTemp;
  84.  
  85. /**
  86. * This function adds the highest temperatures to an array.
  87. * @param maxArray | An array with all temperatures.
  88. * @param highestTempArray | An array with the highest temperatures.
  89. */
  90. function getHighestTemp(maxArray, highestTempArray) {
  91. var max = maxArray[0];
  92.  
  93. $.each(maxArray, function(index, val) {
  94. if (parseFloat(val) > parseFloat(max)) {
  95. max = val;
  96. }
  97. });
  98. highestTempArray.push(max);
  99. }
  100. }(window.app || (window.app = {})));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement