[R-sig-Geo] converting raster to netcdf .nc with keeping CRS information

Ben Tupper btupper @end|ng |rom b|ge|ow@org
Tue Feb 12 15:13:40 CET 2019


Hi,

Have you tried using the 'prj' argument to writeRaster?  I don't know that it will be the solution, but according to ...

https://www.rdocumentation.org/packages/raster/versions/2.8-19/topics/writeRaster <https://www.rdocumentation.org/packages/raster/versions/2.8-19/topics/writeRaster>

It is documented among the '...' arguments.  Setting prj = TRUE will cause the crs to be written to the file.

Cheers,
Ben

> On Feb 12, 2019, at 8:34 AM, Ahmed El-Gabbas <elgabass using gmail.com> wrote:
> 
> Hello all,
> 
> I am having a problem while converting a raster object as NetCDF (.nc) file, with keeping the CRS information in the output file.
> Here is a reproducible code:
> require(raster)
> require(ncdf4)
> CurrTemp <- tempfile()
> download.file(url = "https://seaice.uni-bremen.de/data/amsre/asi_daygrid_swath/s6250/2003/feb/Antarctic/asi-s6250-20030214-v5.hdf <https://seaice.uni-bremen.de/data/amsre/asi_daygrid_swath/s6250/2003/feb/Antarctic/asi-s6250-20030214-v5.hdf>", destfile = CurrTemp, mode = "wb", quiet = T)
> r <- raster(CurrTemp)
> r <- flip(r,2)
> extent(r) <- c(-3950000, 3950000, -3950000, 4350000)
> crs(r) <- "+proj=stere +lat_0=-90 +lat_ts=-70 +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378273 +b=6356889.449 +units=m +no_defs"
> r
> # class       : RasterLayer 
> # dimensions  : 1328, 1264, 1678592  (nrow, ncol, ncell)
> # resolution  : 6250, 6250  (x, y)
> # extent      : -3950000, 3950000, -3950000, 4350000  (xmin, xmax, ymin, ymax)
> # coord. ref. : +proj=stere +lat_0=-90 +lat_ts=-70 +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378273 +b=6356889.449 +units=m +no_defs 
> # data source : in memory
> # names       : layer 
> # values      : 0, 100  (min, max)
> So far the raster object reads well, with CRS information included.
> However, when I try to save it as .nc file, R prints "coord. ref. : NA", and the produced file does not contain the CRS information.
> writeRaster(r, filename = "O:/Ahmed/r001.nc <http://r001.nc/>", varname="IceConc",
>             overwrite=TRUE, format="CDF",
>             xname="Longitude", yname="Latitude")
> # class       : RasterLayer 
> # dimensions  : 1328, 1264, 1678592  (nrow, ncol, ncell)
> # resolution  : 6250, 6250  (x, y)
> # extent      : -3950000, 3950000, -3950000, 4350000  (xmin, xmax, ymin, ymax)
> # coord. ref. : NA 
> # data source : O:/Ahmed/r001.nc <http://r001.nc/> 
> # names       : IceConc 
> # zvar        : IceConc
> 
> raster("O:/Ahmed/r001.nc <http://r001.nc/>")
> # class       : RasterLayer 
> # dimensions  : 1328, 1264, 1678592  (nrow, ncol, ncell)
> # resolution  : 6250, 6250  (x, y)
> # extent      : -3950000, 3950000, -3950000, 4350000  (xmin, xmax, ymin, ymax)
> # coord. ref. : NA 
> # data source : O:/Ahmed/r001.nc <http://r001.nc/> 
> # names       : IceConc 
> # zvar        : IceConc
> Any solution?
> 
> N.B. I also sent the same question at stackoverflow <https://stackoverflow.com/questions/54593552/saving-r-raster-to-netcdf-nc-file-with-keeping-crs-information>, without a solution so far.
> 
> Thanks
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Dr. Ahmed El-Gabbas,
> Ocean Acoustics Lab,
> Alfred-Wegener-Institut
> Helmholtz-Zentrum für Polar und Meeresforschung
> <image.png>
> My Website:  https://elgabbas.github.io <https://elgabbas.github.io/>_______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

Ecological Forecasting: https://eco.bigelow.org/






	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list