[R-sig-Geo] Rasterbrick to netCDF

Thiago V. dos Santos thi_veloso at yahoo.com.br
Fri Jan 25 03:37:56 CET 2013

Hi Prajjwal,

I think the best approach in your case is to create a new, updated netcd file instead of adding a dimension to an existing file. By using the ncdf4 package to create the file, you have lots of options to customize names and descriptions of variables and dimensions.

At the end of this message you can see the code that I use to create a netcdf file with global coverage at 0.5 degree resolution based obviously on a netcdf with the same features. The resulting netcdf will contain one level "slice" and 12 time "slices". One detail about netcdf files is that the coordinates of point refers to the center of the point and not to the edge. That's why I start longitude at -179.75 instead of -180 and longitude at 89.75 instead of 90.

If the resulting file looks like longitude and latitude are inverted, you must transpose the data when converting raster to matrix:
data <- t(as.matrix(r))

I hope it helps,


#--------------------------------------------------------begin of code-----------------------------------------------------

# Load required packages

# Load your data
r <- raster('old_file.nc')

# Here's the trick: convert your data to a matrix
data <- as.matrix(r)

# Now that we have the data we need, make output file in the correct format

# Create dimensions lon, lat, level and time
dim_lon  <- ncdim_def('longitude', 'degrees_east', seq(-179.75,179.75,by=0.5))
dim_lat  <- ncdim_def('latitude', 'degrees_north', seq(89.75,-89.75,by=-0.5))
dim_lev  <- ncdim_def('level', 'level/index', 1)
dim_time <- ncdim_def('time', "years since 1990-01-01", 1:12, unlim=T)

# Create a new variable "precipitation", create netcdf file, put updated contents on it and close file
# Note that variable "data" is the actual contents of the original netcdf file
var_out <- ncvar_def('precipitation', 'mm/day', list(dim_lon,dim_lat,dim_lev,dim_time), 9.e20)
ncid_out <- nc_create('updated_netcdf.nc', var_out)
ncvar_put(ncid_out, var_out, data, start=c(1, 1, 1, 1), count=c(720, 360, 1, 12))
#--------------------------------------------------------end of code-----------------------------------------------------

----- Original Message -----
From: ppanday <ppanday at clarku.edu>
To: r-sig-geo at r-project.org
Sent: Thursday, January 24, 2013 3:54 PM
Subject: [R-sig-Geo] Rasterbrick to netCDF


I have an output from R-script in the form of a rasterbrick with 3
dimensions (time, latitude, longitude). I want to be able to write it to a
netCDF format, add another dimension (time, level, latitude, longitude), and
be able to edit dimension attributes.
Below you will see the properties when I write my rasterbrick to a netCDF
format using writeRaster.
I would like to change 'value' dimension to time, and edit time:units as
Alternatively, is there a way I can copy dimensions and attributes from
another netCDF template and put my rasterbrick in that empty netCDF file?
Any help is greatly appreciated.


    longitude = 720 ;
    latitude = 360 ;
    value = UNLIMITED ; // (12 currently)
    double longitude(longitude) ;
        longitude:units = "degrees_east" ;
        longitude:long_name = "longitude" ;
    double latitude(latitude) ;
        latitude:units = "degrees_north" ;
        latitude:long_name = "latitude" ;
    int value(value) ;
        value:units = "unknown" ;
        value:long_name = "value" ;
    float prec(value, latitude, longitude) ;
        prec:_FillValue = -3.4e+38 ;
        prec:missing_value = -3.4e+38 ;
        prec:long_name = "prec" ;
        prec:projection = "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
        prec:projection_format = "PROJ.4" ;
        prec:min = 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0. ;
        prec:max = 663.033325195312, 553.867553710938, 595.225219726562,
577.460388183594, 688.026123046875, 1178.05859375, 1098.39819335938,
1269.41442871094, 1184.70092773438, 1013.62524414062, 755.313537597656,
633.667541503906 ;

// global attributes:
        :Conventions = "CF-1.4" ;
        :created_by = "R, packages ncdf and raster (version 2.0-31)" ;
        :date = "2013-01-24 15:58:09" ;

View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Rasterbrick-to-netCDF-tp7582376.html
Sent from the R-sig-geo mailing list archive at Nabble.com.

R-sig-Geo mailing list
R-sig-Geo at r-project.org

More information about the R-sig-Geo mailing list