[R] problem writing .bil files in netcdf

Michael Sumner mdsumner at gmail.com
Fri Sep 2 17:09:43 CEST 2016

On Fri, 2 Sep 2016 at 00:43 Abdoulaye SARR <abdoulayesar at gmail.com> wrote:

> Dear List,
> I have daily rainfall data in .bil format and can get info of the file
> using rgdal:
> > library(rgdal)
> > GDALinfo("/1981/v2p19810101.bil")
> rows        1600
> columns     1500
> bands       1
> lower left origin.x        -20
> lower left origin.y        -40
> res.x       0.05
> res.y       0.05
> ysign       -1
> oblique.x   0
> oblique.y   0
> driver      EHdr
> projection  NA
> How can I read all daily file and write them as netcdf files and
> concatenate as one yearly file and also avoid boundary pixels alter
> rainfall values.

You can read the single .bil and write it to NetCDF with raster (and the
rgdal and ncdf4) package:

r <- raster("/1981/v2p19810101.bil")
writeRaster(r, "v2p19810101.nc")

But, if you read in multi .bil files and build a multilayer raster, i.e.

st <- stack(list.files("/1981", pattern = "bil$", full.names = TRUE))

you *can* write it out to NetCDF, very similar to above with writeRaster,
but I think it will generate a variable (an netcdf array) for each layer.

To really write to .nc in a specific way you'll need to delve into the
standard tools in ncdf4, to create a file, create
variables/dimensions/attributes, and then populate the variable, in this
case probably one 3rd-level slice for each .bil. That might be better done
at the command line, say with nco (the "nc operators"). There are
copy-create idioms which is probably the way to go if you have a template
data file or CDF specification.

There may be some higher level tools in other packages on CRAN, check the
reverse depends/imports/suggests on CRAN for ncdf4. Also you should explore
RNetCDF which has an independent implementation.

 (raster really blitzes the field in terms of high-level tools here, but it
has limits, with writing NetCDF *in specific ways* being one of them. GDAL
has similar limitations, since it sees the world in this
"array-as-2d-bands" way).

HTH, at least a little.

Cheers, Mike.

Best regards,
> Eus
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia

	[[alternative HTML version deleted]]

More information about the R-help mailing list