[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[netCDF #CYJ-220332]: Working with Netcdf files in R
- Subject: [netCDF #CYJ-220332]: Working with Netcdf files in R
- Date: Mon, 05 Feb 2018 18:21:21 -0700
Unfortunately, we will not be able to provide much help
since we have no particular experience with R.
We suggest that you look for R support, R mailing list, and R
google groups and ask your question there.
You can try posting the question to the netcdf-group mailing list;
Perhaps someone may have the requisite R background.
Sorry not to be of more help.
>
> Can you help me with extracting data using this R code from a Netdcf file
> to a dataframe. At the end R gives this error
>
> Error in TX[lat_indices, lon_indices, timeseries] : (subscript) logical
> subscript too long
>
> I have copied the script below. All efforts to find a solutions online have
> not given any results. I greatly appreciate your assistance on this.
>
>
> rm(list=ls())
> gc()
> gc(verbose = getOption("verbose"), reset = T)
> library(ncdf4)
> library(chron)
> library(lattice)
> library(RColorBrewer)
> library(raster) # package for raster manipulation
> library(rgdal) # package for geospatial analysis
> library(ggplot2) # package for plotting
> library(readr)
> library(dplyr)
> library(tidyr)
> setwd("D:/cordex/AFR-44/rcp45/CLMcom-CCLM4-8-17_v1/CNRM-
> CERFACS-CNRM-CM5_r1i1p1/mon/native")
> TXx <- nc_open("D:/cordex/AFR-44/rcp45/CLMcom-CCLM4-8-17_v1/
> CNRM-CERFACS-CNRM-CM5_r1i1p1/mon/native/TXx_AFR-44_CNRM-
> CERFACS-CNRM-CM5_historical-rcp45_r1i1p1_CLMcom-CCLM4-8-
> 17_v1_mon_19510101-20991130.nc")
> print(TXx)
> lon <- ncvar_get(TXx, "lon")
> lat <- ncvar_get(TXx, "lat", verbose = F)
> TX <- ncdf4::ncvar_get(TXx, varid="TXx")
> time <- ncvar_get(TXx, "time")
> time_d <- as.Date(time, format="%j", origin=as.Date("1949-12-01"))
> time_years <- format(time_d, "%Y")
> time_months <- format(time_d, "%m")
> time_year_months <- format(time_d, "%Y-%m")
> head(time_d)
> nc_close(TXx)
> time_months %in% c("06")
> timeseries <- seq(from = as.Date("2020-01-01"), to = as.Date("2060-12-31"),
> by = 'month')
> lat_range <- seq(-22.2716118303, -15.5077869605)
> lon_range <- seq(25.2642257016, 32.8498608742)
> lat_indices <- lat %in% lat_range
> lon_indices <- lon %in% lon_range
> TX[lat_indices, lon_indices, timeseries]
> --
>
> Kudzai Mpakairi
> Research Assistant
> Vision RI
>
> +2637 770 015 38
>
>
=Dennis Heimbigner
Unidata
Ticket Details
===================
Ticket ID: CYJ-220332
Department: Support netCDF
Priority: Normal
Status: Closed
===================
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata
inquiry tracking system and then made publicly available through the web. If
you do not want to have your interactions made available in this way, you must
let us know in each email you send to us.