library(ggplot2)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
# Read CSV files
drone_data8 <- read.csv("Drone_data_VLask(Launch 8).csv", stringsAsFactors = FALSE)
new_drone_data8 <- read.csv("New_Drone_Data_VLask(Launch 8).csv", stringsAsFactors = FALSE)

# Inspect the data
head(drone_data8)
##   time_since_epoch      lat       lon altitude_m_MSL wind_direction_deg
## 1       1712587205 45.05322 -71.39005         520.27           241.6336
## 2       1712587206 45.05322 -71.39005         520.31           241.4537
## 3       1712587206 45.05322 -71.39005         520.31           241.4873
## 4       1712587207 45.05322 -71.39005         520.27           241.4849
## 5       1712587207 45.05322 -71.39005         520.21           241.7519
## 6       1712587208 45.05322 -71.39005         520.17           241.9309
##   wind_speed_ms timestamp air_temperature_C dew_point_temperature_C
## 1      3.699043   40:05.2              9.88               -2.810304
## 2      3.716799   40:05.7              9.88               -2.767154
## 3      3.727116   40:06.2              9.83               -2.747943
## 4      3.724167   40:06.7              9.81               -2.817960
## 5      3.699454   40:07.2              9.79               -2.832368
## 6      3.684914   40:07.7              9.84               -2.911016
##   relative_humidity_perc air_pressure_hPa  X Profile.Data.        X.1
## 1               40.84774         958.5313 NA          Time   UTC Time
## 2               40.97883         958.5170 NA         [sec] [HH:mm:ss]
## 3               41.17509         958.5310 NA             0   14:30:11
## 4               41.01653         958.4945 NA             2   14:30:13
## 5               41.02765         958.4985 NA             4   14:30:15
## 6               40.65206         958.5361 NA             6   14:30:17
##   Team.2.launch.21..14.30Z.     X.2 X.3   X.4    X.5    X.6     X.7 X.8 X.9
## 1                         P       T  Hu    Ws     Wd Geopot   Dewp.  NA  NA
## 2                     [hPa] [\xb0C] [%] [m/s] [\xb0]    [m] [\xb0C]  NA  NA
## 3                     970.9       8  51   1.3    313    407    -1.5  NA  NA
## 4                     969.2     7.9  47   2.7    277    421    -2.6  NA  NA
## 5                     967.4     7.6  44   2.7    277    436    -3.9  NA  NA
## 6                     965.7     7.4  44   2.7    277    451    -4.1  NA  NA
##   X.10 X.11 X.12 Dwell.Period.Data.               X.13          X.14      X.15
## 1   NA   NA   NA     altitude_m_MSL wind_direction_deg wind_speed_ms timestamp
## 2   NA   NA   NA             478.98        229.5387115   2.093972206   40:24.2
## 3   NA   NA   NA             478.91        230.6852112   2.121026516   40:24.7
## 4   NA   NA   NA             478.89        232.6106644   2.136803389   40:25.2
## 5   NA   NA   NA             478.91        237.2016373   2.111566067   40:26.2
## 6   NA   NA   NA             478.89        239.3268356   2.101919889   40:26.7
##                X.16
## 1 air_temperature_C
## 2              8.99
## 3                 9
## 4              9.06
## 5              9.05
## 6              9.06
head(new_drone_data8)
##   air_temperature_BME280 air_temperature_TMP117 relative_humidity_BME280
## 1               16.42000               16.00920                 28.22880
## 2               16.34054               15.95261                 28.26973
## 3               16.25946               15.89405                 28.31027
## 4               16.24000               15.81423                 28.35654
## 5               16.21527               15.75427                 28.34802
## 6               16.18092               15.70466                 28.31748
##   wind_speed wind_direction air_pressure air_pressure_int air_temperature_int
## 1   3.479669       242.1625     960.1223         952.3733            55.67040
## 2   3.416935       241.5843     960.1757         952.4787            55.65676
## 3   3.356676       241.0400     960.2193         952.5840            55.64324
## 4   3.302984       240.6851     960.3317         952.6862            55.62538
## 5   3.260545       240.4377     960.4312         952.7893            55.61176
## 6   3.221676       240.2088     960.5174         952.8930            55.60031
##         time      lat       lon vertical_speed flight_number
## 1 1712587210 45.05322 -71.39006      -0.486400             8
## 2 1712587211 45.05322 -71.39006      -1.259279             8
## 3 1712587212 45.05322 -71.39006      -2.034054             8
## 4 1712587212 45.05322 -71.39005      -1.357692             8
## 5 1712587212 45.05322 -71.39005      -1.474198             8
## 6 1712587213 45.05322 -71.39005      -2.077252             8
##   dew_point_temperature_BME280 altitude air_temperature_pp
## 1                     16.36835   525.22           9.834339
## 2                     16.28832   524.22           9.759588
## 3                     16.20666   523.22           9.681679
## 4                     16.18712   522.22           9.635377
## 5                     16.16218   521.22           9.599789
## 6                     16.12751   520.22           9.565736
##   dew_point_temperature_pp relative_humidity_pp altitude_agl datetime
## 1                -5.985304             32.26389          112  40:10.1
## 2                -6.051595             32.26246          111  40:10.9
## 3                -6.121738             32.25837          110  40:11.6
## 4                -6.145116             32.30109          109  40:12.1
## 5                -6.211840             32.21376          108  40:12.5
## 6                -6.300370             32.06976          107  40:12.9
# Assuming both datasets have a 'temperature' column
summary(drone_data8$air_temperature_C)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##    8.58    8.81    9.14    9.21    9.64    9.98       5
summary(new_drone_data8$air_temperature_pp)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   9.349   9.453   9.540   9.661   9.760  10.709
# Visual comparison with boxplots
boxplot(drone_data8$air_temperature_C, new_drone_data8$air_temperature_pp,
        names = c("Original", "Post-Processed"),
        main = "Temperature Comparison",
        ylab = "Temperature")

# Histogram for both datasets
hist(drone_data8$air_temperature_C, col = rgb(1, 0, 0, 0.5), main = "Temperature Distribution", xlab = "Temperature")
hist(new_drone_data8$air_temperature_pp, col = rgb(0, 0, 1, 0.5), add = TRUE)
legend("topright", legend = c("Original", "Post-Processed"), fill = c(rgb(1, 0, 0, 0.5), rgb(0, 0, 1, 0.5)))

# Check for equal length
if (length(drone_data8$air_temperature_C) == length(new_drone_data8$air_temperature_pp)) {
  # Perform paired t-test
  t_test_result <- t.test(drone_data8$air_temperature_C, new_drone_data8$air_temperature_pp, paired = TRUE)
  print(t_test_result)
} else {
  print("Datasets have different lengths, can't perform paired t-test.")
}
## [1] "Datasets have different lengths, can't perform paired t-test."
# Convert relevant columns to numeric safely
convert_to_numeric <- function(column) {
  suppressWarnings(as.numeric(column))
}

drone_data8$air_temperature_C <- convert_to_numeric(drone_data8$air_temperature_C)
new_drone_data8$air_temperature_pp <- convert_to_numeric(new_drone_data8$air_temperature_pp)

# Determine the shortest length to match both datasets
n <- min(nrow(drone_data8), nrow(new_drone_data8), na.rm = TRUE)

drone_data8 <- drone_data8[1:n, ]
new_drone_data8 <- new_drone_data8[1:n, ]

# Add an index for x-axis
drone_data8$Index <- seq_len(n)
new_drone_data8$Index <- seq_len(n)

# Combine data for plotting
valid_data <- rbind(
  data.frame(Index = drone_data8$Index, Temperature = drone_data8$air_temperature_C, Source = "Original"),
  data.frame(Index = new_drone_data8$Index, Temperature = new_drone_data8$air_temperature_pp, Source = "New")
)
valid_data <- valid_data[complete.cases(valid_data), ]

# Plot the comparison
ggplot(valid_data, aes(x = Index, y = Temperature, color = Source)) +
  geom_line(size = 1.2, alpha = 0.8) +
  geom_point(size = 2, alpha = 0.7) +
  scale_color_manual(values = c("Original" = "blue", "New" = "red")) +
  labs(title = "Comparison of Temperature Measurements",
       y = "Temperature (°C)",
       color = "Dataset") +
  theme_minimal() +
  theme(axis.title.x = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(),
        legend.position = "top", legend.title = element_blank(),
        panel.grid.major = element_line(color = "grey80"),
        panel.grid.minor = element_line(color = "grey90"))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.