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 <- "/media/sergio/ext5/WRF4/WRF/test/em_real/wrfout_d01_2014-10-03_00:00:00"

t2 <- wrf_get(wrfo, "T2", as_raster = T)
t2 <- t2 -273.15

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)

m <- cbind(c(-46.5, -46.35),  # xini xend
           c(-23.85, -23.95)) # yini yend
cross = st_linestring(m)
(cross <- st_sfc(cross, crs = 4326))
plot(t2$T2, 
     main = "Temperature using plot", 
     col = cpt("arendal_temperature"))
plot(cross, add = T)

Now, define several lines

m2 <- cbind(c(-46.05, -46.36),  # xini xend
            c(-23.85, -23.95))   # yini yend

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

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())


ibarraespinosa/eixport documentation built on Feb. 13, 2024, 12:46 a.m.