knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Based on this NCL

library(eixport)
library(raster)
library(stars)
library(cptcity)
library(sf)
library(vein)
library(ggplot2)

Reading Temperature and crop coast lines for our study area

wrfo <- "/home/sergio/R/x86_64-pc-linux-gnu-library/4.3/helios/extras/wrfout_d01_2020-01-01_01%3A00%3A00_sub.nc"
t2 <- mean(wrf_get(wrfo, "T2", as_raster = T))
t2[] <- t2[] -273.15# we select one

Find colour palette for temperature

find_cpt("temperature")

Let us create a line between c(-46.5,-23.85) and c(-46.35, -23.95)

cx <- as.data.frame(coordinates(projectRaster(t2, crs="+proj=longlat")))

m <- cbind(c(min(cx$x),  # xini
             max(cx$y)), # xend
           c(min(cx$y),  # yini
             max(cx$y))) # yend
cross = st_linestring(m)
(cross <- st_sfc(cross, crs = 4326))
plot(mean(t2), 
     main = "Temperature using plot", 
     col = cpt("arendal_temperature"))
plot(cross, add = T)

Define a helper function

points_extract <- function(m, sta) {
  cross = st_linestring(m)
  cross <- st_sfc(cross, crs = 4326)
  t2s <- st_as_sf(sta)
  lt <- st_intersection(t2s, cross) 
  geo <- st_geometry(lt)
  lt <- st_set_geometry(lt, NULL)
  na <- names(lt)
  lt$id <- 1:nrow(lt)
  dx <- vein::wide_to_long(df = lt, 
                           column_with_data = na, 
                           column_fixed = "id")
  stf <- st_sf(dx, geometry = geo)
  lt <- st_centroid(stf)
  lt <- cbind(lt, st_coordinates(lt))

  return(lt)
}
sta = st_as_stars(t2)
sta <- st_transform(sta, 4326)
names(sta) <- "temperature"
df <- points_extract(m, sta = sta)

Let us check the data

head(df)

Add time variable, select and plot

library(ggplot2)
ggplot(df, 
       aes(x = X, y = V1, colour = V3)) +
  labs(y =expression(paste("Temperature [",degree,"C]")),
       x = expression(paste("Longitude [",degree,"]")))+
  geom_line() +
  theme_bw()+
  theme(legend.title = element_blank())


atmoschem/eixport documentation built on Feb. 20, 2025, 8:09 a.m.