0

I am learning to use ERA-interim in my work. I need to analyze a two-year series for a meteorological station that is a point in shapefile format and also visualize the data for a basin. So far I have read the netcdf file and declared variables

rm(list=ls())
graphics.off()
library(raster)
library(ncdf4)
library(maps)
library(maptools)
library(rgdal)
library(tidyverse)
library(reticulate)
library(ggthemes)
library(viridis)

setwd("C:/Users/TPPC/Desktop")
rutas <- list.files(path=getwd())
ncin<-nc_open("interim_2t_20152016.nc")

lat=ncvar_get(ncin,'latitude')
lon=ncvar_get(ncin,'longitude')
t<- ncvar_get(ncin, "time")

tunits<-ncatt_get(ncin,'time')
tunits$units
tustr<- strsplit(tunits$units, " ")
timestamp = as.POSIXct(t*3600,tz='GMT',origin=tustr[[1]][3])

data<-data_frame(name=attributes(ncin$var)$names) %>%
  bind_cols(map_df(.$name,ncatt_get,nc=ncin)) %>%
  mutate(values=map(name,ncvar_get,nc=ncin))
nc_close(ncin)
data

df<-expand.grid(lon=lon-360,lat=lat,timestamp=timestamp,name=data$name) %>%
  mutate(coord=factor(paste(lon,lat,'/')))

*But in this last line I get the next error Error: cannot allocate vector of size 645.2 Mb

tmsppc
  • 107
  • 3
  • 10
  • 1
    Possible duplicate of [R memory management / cannot allocate vector of size n Mb](https://stackoverflow.com/questions/5171593/r-memory-management-cannot-allocate-vector-of-size-n-mb) – Bart Feb 20 '19 at 06:38
  • Have you considered the possibility of using `raster`? It has a few built-in functions that work with shapefiles, namely `shapefile`, `crop`, `mask`... the new package `stars` can also do this easily by subsetting any `stars` object with any `sf` object, see [here](https://r-spatial.github.io/stars/articles/stars1.html). Generally, I would avoid using `ncdf4` directly for any high-level work, as it will greatly increase your coding time. `stars` and `raster` usually read most netCDF files correctly. – AF7 Feb 20 '19 at 19:32

0 Answers0