[R-sig-Geo] Rasterbrick to netCDF

ppanday ppanday at clarku.edu
Thu Jan 31 20:21:26 CET 2013


Robert

Yes, the ncdf4 packages works fine. However, it seems it runs in the unix environment currently. Thank you for your response.

Sincerely,
Prajjwal
________________________________
From: Robert Hijmans [via R-sig-geo] [ml-node+s2731867n7582433h43 at n2.nabble.com]
Sent: Wednesday, January 30, 2013 7:57 PM
To: Panday, Prajjwal
Subject: Re: Rasterbrick to netCDF

Prajjwal,
As Thiago shows you can do such things directly via the ncdf(4) package;
raster does not provide too many options beyond setting variable names
(arguments varname, varunit, longname, xname, yname, zname, zunit), see
?writeRaster. It would be nice to expand that and allow for more flexible
ncdf file writing from Raster objects.
Robert


On Thu, Jan 24, 2013 at 6:37 PM, Thiago V. dos Santos <
[hidden email]<UrlBlockedError.aspx>> wrote:

> 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,
>
> Thiago.
>
> #--------------------------------------------------------begin of
> code-----------------------------------------------------
>
> # Load required packages
> library(raster)
> library(ncdf4)
>
> # 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))
> nc_close(ncid_out)
> #--------------------------------------------------------end of
> code-----------------------------------------------------
>
>
> ----- Original Message -----
> From: ppanday <[hidden email]<UrlBlockedError.aspx>>
> To: [hidden email]<UrlBlockedError.aspx>
> Cc:
> Sent: Thursday, January 24, 2013 3:54 PM
> Subject: [R-sig-Geo] Rasterbrick to netCDF
>
> Hello
>
> 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
> well.
> 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.
>
> Sincerely,
> Prajjwal
>
>
> dimensions:
>     longitude = 720 ;
>     latitude = 360 ;
>     value = UNLIMITED ; // (12 currently)
> variables:
>     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
> [hidden email]<UrlBlockedError.aspx>
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]<UrlBlockedError.aspx>
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]<UrlBlockedError.aspx>
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


________________________________
If you reply to this email, your message will be added to the discussion below:
http://r-sig-geo.2731867.n2.nabble.com/Rasterbrick-to-netCDF-tp7582376p7582433.html
To unsubscribe from Rasterbrick to netCDF, click here<http://r-sig-geo.2731867.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=7582376&code=cHBhbmRheUBjbGFya3UuZWR1fDc1ODIzNzZ8LTEwMDM2MDU1Njk=>.
NAML<http://r-sig-geo.2731867.n2.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>




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



More information about the R-sig-Geo mailing list