Plotting x and y values using data from two separate netCDF files in R
Thanks, again.? It's strange, as the variable "ncfname" is reading off the original file name "cumulative_emissions_1pctCO2.nc", and yet, it says that it cannot find the variable"cum_co2_emi-CanESM2 (and that is the correct variable name with no typos). ? -----Original Message----- From: Michael Sumner <mdsumner at gmail.com> To: rain1290 <rain1290 at aim.com> Cc: r-sig-geo <r-sig-geo at r-project.org> Sent: Tue, Mar 26, 2019 5:10 pm Subject: Re: [R-sig-Geo] Plotting x and y values using data from two separate netCDF files in R Use the file name as the first argument, and the variable name you want as varname =? Raster doesn't work with output of nc_open See ?brick Good luck
On Wed, Mar 27, 2019, 08:05 <rain1290 at aim.com> wrote:
Hi Michael, Thank you so much for your reply!? I was just trying your suggestion, but when I run the following in R: x<-raster::brick(ncfname, varname="cum_co2_emi-CanESM2") I receive the following error: Error in .varName(nc, varname, warn = warn) : varname: cum_co2_emi-CanESM2 does not exist in the file. Select one from: I tried switching "ncfname" with "Model1", but I then receive this error: Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ?brick? for signature ?"ncdf4"? Is there a reason for that? Thanks, again, -----Original Message----- From: Michael Sumner <mdsumner at gmail.com> To: rain1290 <rain1290 at aim.com> Cc: r-sig-geo <r-sig-geo at r-project.org> Sent: Tue, Mar 26, 2019 4:34 pm Subject: Re: [R-sig-Geo] Plotting x and y values using data from two separate netCDF files in R I would try for a single point:? x <- raster::brick(ncfname, varname =?"cum_co2_emi-CanESM2")y <- raster::brick(ncfname1, varname = "onedaymax") pt <- cbind(30, -5)to_plot <- cbind(raster::extract(x, pt), raster::extract(y, pt)) plot(to_plot) Is that close?? You might be better off using raster::as.data.frame(x, xy = TRUE, long = TRUE) if you want all locations at their actual centre.? See if the times of the 3rd axis are valid (and the same) in getZ(x) and getZ(y).? There's rarely a need to use ncdf4 directly, though that's important sometimes, more so for grids that raster's regular-affine referencing model doesn't support.? cheers, Mike
On Wed, 27 Mar 2019 at 05:29 rain1290--- via R-sig-Geo <r-sig-geo at r-project.org> wrote:
Hi there,
I am currently trying to plot precipitation data (y-axis values) with cumulative emissions data (x-axis) using R. Both of these data are found on two separate netCDF files that I have already read into R. Ultimately, What I would like to do is plot precipitation as a function of cumulative emissions for a selected location (as shown below in the following code). I have, so far, used the following code (with "#" to highlight each step):?? ? library(raster)
??? library(ncdf4)
??? library(maps)
??? library(maptools)
??? library(rasterVis)
??? library(ggplot2)
??? library(rgdal)
??? library(sp)? ??#Geting cumulative emissions data for x-axis? ?? ? ncfname<-"cumulative_emissions_1pctCO2.nc"
??? Model1<-nc_open(ncfname)
??? print(Model1)
??? get<-ncvar_get(Model1, "cum_co2_emi-CanESM2") #units of terratones of??
??? carbon (TtC) for x-axis (140 values)
??? print(get)
??? Year<-ncvar_get(Model1, "time") #140 years
?#Getting Model data for extreme precipitation (units of millimeters/day) for y-axis? ?? ? ncfname1<-"MaxPrecCCCMACanESM21pctCO2.nc"
??? Model2<-nc_open(ncfname1)
??? print(Model2)
??? get1<-ncvar_get(Model2, "onedaymax") #units of millimeters/day
??? print(get1)
? ??#Reading in latitude, longitude and time from this file:
? ??? ? latitude<-ncvar_get(Model2, "lat") #64 degrees latitude
??? longitude<-ncvar_get(Model2, "lon") #128 degrees longitude
??? Year1<-ncvar_get(Model2, "Year") #140 years
? ? #Plotting attempt? ??? ? randompointlon<-30 #selecting a longitude
??? randompointlat<--5 #selecting a latitude
??? Hope<-extract(r_brick,
??? SpatialPoints(cbind(randompointlon,randompointlat)),method='simple')
??? df<-data.frame(cumulativeemissions=seq(from=1, to=140, by=1),??
??? Precipitation=t(Hope))
??? ggplot(data=df, aes(x=get, y=Precipitation,
??? group=1))+geom_line()+ggtitle("One-day maximum precipitation (mm/day)??
??? for random location for CanESM2 1pctCO2 as a function of cumulative
??? emissions")
print(Model1) yields the following (I read in variable #2 for now):
File cumulative_emissions_1pctCO2.nc (NC_FORMAT_NETCDF4):
14 variables (excluding dimension variables):
? ? ? ??? ? ? ? float cum_co2_emi-BNU-ESM[time]?? (Contiguous storage)?
??????????? long_name: Cumulative carbon emissions for BNU-ESM
??????????? units: Tt C
??????? float cum_co2_emi-CanESM2[time]?? (Contiguous storage)?
??????????? long_name: Cumulative carbon emissions for CanESM2
??????????? units: Tt C
??????? float cum_co2_emi-CESM1-BGC[time]?? (Contiguous storage)?
??????????? long_name: Cumulative carbon emissions for CESM1-BGC
??????????? units: Tt C
??????? float cum_co2_emi-HadGEM2-ES[time]?? (Contiguous storage)?
??????????? long_name: Cumulative carbon emissions for HadGEM2-ES
??????????? units: Tt C
??????? float cum_co2_emi-inmcm4[time]?? (Contiguous storage)?
??????????? long_name: Cumulative carbon emissions for inmcm4
??????????? units: Tt C
??????? float cum_co2_emi-IPSL-CM5A-LR[time]?? (Contiguous storage)?
??????????? long_name: Cumulative carbon emissions for IPSL-CM5A-LR
??????????? units: Tt C
??????? float cum_co2_emi-IPSL-CM5A-MR[time]?? (Contiguous storage)?
??????????? long_name: Cumulative carbon emissions for IPSL-CM5A-MR
??????????? units: Tt C
??????? float cum_co2_emi-IPSL-CM5B-LR[time]?? (Contiguous storage)?
??????????? long_name: Cumulative carbon emissions for IPSL-CM5B-LR
??????????? units: Tt C
??????? float cum_co2_emi-MIROC-ESM[time]?? (Contiguous storage)?
??????????? long_name: Cumulative carbon emissions for MIROC-ESM
??????????? units: Tt C
??????? float cum_co2_emi-MPI-ESM-LR[time]?? (Contiguous storage)?
??????????? long_name: Cumulative carbon emissions for MPI-ESM-LR
??????????? units: Tt C
??????? float cum_co2_emi-MPI-ESM-MR[time]?? (Contiguous storage)?
??????????? long_name: Cumulative carbon emissions for MPI-ESM-MR
??????????? units: Tt C
??????? float cum_co2_emi-NorESM1-ME[time]?? (Contiguous storage)?
??????????? long_name: Cumulative carbon emissions for NorESM1-ME
??????????? units: Tt C
??????? float cum_co2_emi-GFDL-ESM2G[time]?? (Contiguous storage)?
??????????? long_name: Cumulative carbon emissions for GFDL-ESM2G
??????????? units: Tt C
??????? float cum_co2_emi-GFDL-ESM2M[time]?? (Contiguous storage)?
??????????? long_name: Cumulative carbon emissions for GFDL-ESM2M
??????????? units: Tt C? ???1 dimensions:
??????? time? Size:140
??????????? units: years since 0-1-1 0:0:0
??????????? long_name: time
??????????? standard_name: time
??????????? calender: noleap? ?4 global attributes:
??????? description: Cumulative carbon emissions for the 1pctCO2 scenario from the CMIP5 dataset.
??????? history: Created Fri Jul 21 14:50:39 2017
??????? source: CMIP5 archieve
???????
print(Model2) yields the following:File MaxPrecCCCMACanESM21pctCO2.nc (NC_FORMAT_NETCDF4):? ? ?3 variables (excluding dimension variables):
??????? double onedaymax[lon,lat,time]?? (Contiguous storage)?
??????????? units: mm/day
??????? double fivedaymax[lon,lat,time]?? (Contiguous storage)?
??????????? units: mm/day
??????? short Year[time]?? (Contiguous storage)? ? ? ?3 dimensions:
??????? time? Size:140
??????? lat? Size:64
??????????? units: degree North
??????? lon? Size:128
??????????? units: degree East? ? ??3 global attributes:
??????? description: Annual global maximum precipitation from the CanESM2 1pctCO2 scenario
??????? history: Created Mon Jun? 4 11:24:02 2018
??????? contact: rain1290 at aim.com
So, in general, this is what I am trying to achieve, but I am not sure if what I am doing in the ggplot function is the right approach for this.
Any assistance with this would be greatly appreciated!
Thanks,
? ? ? ? [[alternative HTML version deleted]]
_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
--
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia
Dr. Michael Sumner Software and Database Engineer Australian Antarctic Division 203 Channel Highway Kingston Tasmania 7050 Australia [[alternative HTML version deleted]]