[R-sig-Geo] Plotting NetCDF layer over a map as a raster...

Robert J. Hijmans r.hijmans at gmail.com
Tue Oct 15 19:03:38 CEST 2013


Guillermo,

I think there is a problem with the file. The file specifies that it
uses the "CF-1.4" convention, but I do not think this is true.

The ncdf file also specifies that the data have an "ease_projection:
cylindrical"
This projection is described here:
http://nsidc.org/data/ease/ease_grid.html#global_map
and this is probably the corresponding proj.4:
http://www.spatialreference.org/ref/epsg/3410/proj4/

However, the file does not provide the actual x an y values of the
cells (as it should do), instead it provides the lon/lat values (which
would normally be an optional bonus).

So what you could do is (a) write the data provider and (b) transform
the lon/lat values to the projection used, and use that to make a
raster.

Robert

On Mon, Oct 14, 2013 at 11:22 PM, Guillermo E. Ponce-Campos
<geponce at gmail.com> wrote:
> Hi Robert:
>
> Thank you for the answer.
>
> Here is a link to the file...  http://bit.ly/15xryk1
>
> The file have several layers...   I think the issue is that x and y are not
> regularly spaced.
> I tried to do this, following your suggestion with the rasterFromXYZ
> function:
>
> l<-list(x,y,z)  # referring to x,y,z from the initial code
> varMatrix<-do.call(rbind,l)
> varRaster2 <- rasterFromXYZ(varMatrix)
>
> And got this message...
> Error in rasterFromXYZ(varMatrix) : x cell sizes are not regular
>
> #Using the variable "varRaster" from the initial code
>
> res(varRaster)
> [1] 0.2603037 0.2916659
>
> Gives the not regular spaced data set...
>
> If I add "useRaster=TRUE" to the 'image' function I get similar error
> regarding non-regular grid...
>
> Guillermo
>
>
> On Mon, Oct 14, 2013 at 10:08 PM, Robert J. Hijmans <r.hijmans at gmail.com>
> wrote:
>>
>> It seems that the files has other variables than lon & lat that are
>> used. It looks like a projection is used, but then again, the axes
>> range suggests lon/lat. Can you show more info about the file contents
>> (e.g.  print(varRaster) ) or str(ex.nc).
>> Even better if you can make the file available to me. In principle you
>> can of course make a RasterLayer from your x, y and z, particularly if
>> x and y are regularly spaced.
>> Robert
>>
>> On Mon, Oct 14, 2013 at 11:04 AM, Guillermo E. Ponce-Campos
>> <geponce at gmail.com> wrote:
>> > Hello:
>> >
>> > I have been trying to plot a netcdf layer as a raster and have found one
>> > single issue that I'd like know if I'm doing something wrong.  If I use
>> > the
>> > function "image" from raster package there's no problem at all, the
>> > thing
>> > is that I would like to handle this layer as a raster and be able to map
>> > it
>> > correctly or save it as a geotif.
>> >
>> > This is an example using both approaches to map the netcdf layer: as a
>> > raster and using "image(x,y,z...).
>> >
>> > library(ncdf)
>> >
>> > library(maptools)
>> >
>> > library(raster)
>> >
>> > varRaster<-raster("SMOS_File.nc", varname="Soil_Moisture")
>> >
>> > plot(varRaster)
>> > data(wrld_simpl)
>> > plot(wrld_simpl, add = TRUE)
>> >
>> > # Resulting plot (some offset is occurring by opening the layer as a
>> > raster
>> >  --> http://bit.ly/19IFfA2
>> >
>> >
>> > #Using "image" there's no problem...
>> >
>> > ex.nc = open.ncdf("SMOS_File.nc")
>> >
>> > print(ex.nc)
>> > summary(ex.nc)
>> > y = get.var.ncdf( ex.nc, "lat")
>> > x = get.var.ncdf( ex.nc, "lon")
>> > z = get.var.ncdf( ex.nc, "Soil_Moisture")
>> >
>> > image(x,y,z, zlim=c(-0.9,1), col = heat.colors(37))
>> > plot(wrld_simpl, add = TRUE)
>> >
>> > # Resulting plot --> http://bit.ly/1cJ0vXo
>> >
>> > Is there anyway to handle the netcdf layer as a raster and be able to
>> > map
>> > it correctly.  I already tried to write the raster as a geotif and open
>> > it
>> > in other software such as GRASS or ArcMap and is still showing up with
>> > the
>> > offset.
>> >
>> >
>> > Thanks for any help with this.
>> > Guillermo
>> >
>> > --
>> > *Guillermo E. Ponce-Campos*
>> > *PGP Key ID*: 0x57EDC060
>> >
>> >         [[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
>
>
>
>
> --
> Guillermo E. Ponce-Campos
> PGP Key ID: 0x57EDC060
> Ph. (520) 261-8486



More information about the R-sig-Geo mailing list