[R-sig-Geo] Convert ncdf data to dataframe

Stachelek, Joseph jstachel at sfwmd.gov
Thu Jun 2 15:20:39 CEST 2016


Hi Milu,

I would suggest reading your .nc file directly into a raster object using the `raster` package.

###
library(raster)
ncname <- ("/file_path")
ncfname <- paste(ncname, ".nc", sep = "")
r <- raster::raster(ncfname) # use raster::stack() if more than one "layer"
###

I have suggested that the author of the `ncdf4` package (David Pierce) make note of this easier way to load .nc files in the `ncdf4` documentation. If you need the coordinates of each raster cell you might take a look at the instructions here:

http://gis.stackexchange.com/questions/142156/r-how-to-get-latitudes-and-longitudes-from-a-rasterlayer

Joseph Stachelek

-----Original Message-----
From: R-sig-Geo [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of Miluji Sb
Sent: Thursday, June 02, 2016 4:43 AM
To: R-sig-geo mailing list <r-sig-geo at r-project.org>
Subject: [R-sig-Geo] Convert ncdf data to dataframe

Dear all,

I have used the following code to read in a ncdf file

library(chron)
library(lattice)
library(ncdf4)
library(data.table)

ncname <- ("/file_path")
ncfname <- paste(ncname, ".nc", sep = "")
dname <- "ssl"  # note: tmp means temperature (not temporary)

ncin <- nc_open(ncfname)
print(ncin)

The attributes of the file are:

     4 variables (excluding dimension variables):
        double longitude[row]
            long_name: longitude
            units: degrees_east
            standard_name: longitude
        double latitude[row]
            long_name: latitude
            units: degrees_north
            standard_name: latitude
        double ssl[col,row]
            long_name: storm surge level
            units: m
            _FillValue: -99999
            scale_factor: 1
            add_offset: 1
        float RP[col]
            long_name: return period
            units: yr
            Contents: The RPs have been estimated following the Peak Over
Threshold Method (see reference below)
            Starting date: 01-Dec-2009
            End date: 30-Nov-2099 21:00:00
     2 dimensions:
        col  Size:8
        row  Size:2242

I would like to convert the data into a dataframe by longitude and
latitude. Is that possible? Thank you!

Sincerely,

Milu

        [[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


We value your opinion. Please take a few minutes to shar...{{dropped:5}}



More information about the R-sig-Geo mailing list