[R-sig-Geo] Problem with raster package and nc4 file

Michael Sumner mdsumner at gmail.com
Sat Jun 6 01:52:42 CEST 2015


Just another short comment below

On Sat, 6 Jun 2015 at 09:37 Andrea Timmermann <timmermann at gmx.at> wrote:

> Hi!
> Thanks a lot !!! I could get the raster now.
> This is what I got:
> > r
> class       : RasterLayer
> band        : 1  (of  365  bands)
> dimensions  : 4823, 5268, 25407564  (nrow, ncol, ncell)
>
> resolution  : 1000, 1000  (x, y)
> extent      : -2015000, 3253000, -3037000, 1786000  (xmin, xmax, ymin,
> ymax)
> coord. ref. : +proj=lcc +lon_0=-100 +lat_0=42.5 +x_0=0 +y_0=0 +lon_0=0
> +lat_1=25 +lat_2=60
> data source : C:\Users\Andi\Desktop\1980\tmin_1980.nc4
> names       : daily.minimum.temperature
> z-value     : 1980-01-01
> zvar        : tmin
>
>
> and
>
> > print(ncic)
> [1] "File tmin_1980.nc4 (NC_FORMAT_NETCDF4_CLASSIC):"
> [1] ""
> [1] "     6 variables (excluding dimension variables):"
> [1] "        short lambert_conformal_conic[]   "
> [1] "            grid_mapping_name: lambert_conformal_conic"
> [1] "            longitude_of_central_meridian: -100"
> [1] "            latitude_of_projection_origin: 42.5"
> [1] "            false_easting: 0"
> [1] "            false_northing: 0"
> [1] "            standard_parallel: 25" "            standard_parallel: 60"
> [1] "            longitude_of_prime_meridian: 0"
> [1] "        float lat[x,y]   "
> [1] "            units: degrees_north"
> [1] "            long_name: latitude coordinate"
> [1] "            standard_name: latitude"
> [1] "        float lon[x,y]   "
> [1] "            units: degrees_east"
> [1] "            long_name: longitude coordinate"
> [1] "            standard_name: longitude"
> [1] "        float time_bnds[nv,time]   "
> [1] "        float tmin[x,y,time]   "
> [1] "            long_name: daily minimum temperature"
> [1] "            units: degrees C"
> [1] "            missing_value: -9999"
> [1] "            _FillValue: -9999"
> [1] "            valid_range: -50" "            valid_range: 50"
> [1] "            coordinates: lat lon"
> [1] "            grid_mapping: lambert_conformal_conic"
> [1] "            cell_methods: area: mean time: minimum"
> [1] "        short yearday[time]   "
> [1] "            long_name: yearday"
> [1] "            valid_range: 1"   "            valid_range: 365"
> [1] ""
> [1] "     4 dimensions:"
> [1] "        y  Size:4823"
> [1] "            units: m"
> [1] "            long_name: y coordinate of projection"
> [1] "            standard_name: projection_y_coordinate"
> [1] "        x  Size:5268"
> [1] "            units: m"
> [1] "            long_name: x coordinate of projection"
> [1] "            standard_name: projection_x_coordinate"
> [1] "        time  Size:365   *** is unlimited ***"
> [1] "            long_name: time"
> [1] "            calendar: standard"
> [1] "            units: days since 1980-01-01 00:00:00 UTC"
> [1] "            bounds: time_bnds"
> [1] "        nv  Size:2"
> [1] ""
> [1] "    8 global attributes:"
> [1] "        start_year: 1980"
> [1] "        source: Daymet Software Version 2.0"
> [1] "        Version_software: Daymet Software Version 2.0"
> [1] "        Version_data: Daymet Data Version 2.1"
> [1] "        Conventions: CF-1.4"
> [1] "        citation: Please see http://daymet.ornl.gov/ for current
> Daymet data citation information"
> [1] "        references: Please see http://daymet.ornl.gov/ for current
> information on Daymet references"
> [1] "        institution: Oak Ridge National Laboratory Distributed Active
> Archive Center (ORNL DAAC)"
>
> The file can be downloaded from: http://daymet.ornl.gov/mosaics.html (The
> files with the daylength data are much smaller.)
> When I open the lat layer I see that the differences between consecutive
> cells are different depending on the location on the file, so I think that
> the sizes are not uniform.
> I am not sure how I can fix that.
>

There's nothing to fix, this is a regular grid in metres and is sensibly
specified by an extent in the projected coordinates - you don't need to
work with every cell's coordinate explicitly, and you can always generate
them on the fly as required - in whatever coordinate system you want. But I
don't think you need that at all.

Storing every coordinate in lon/lat arrays in that file is just an
unnecessary waste really, this is one of those "modelling/GIS" crossover
areas where things just don't quite mesh in a sensible way.

Cheers, Mike.


>
> Thanks a lot.
> Andi
>  *Gesendet:* Freitag, 05. Juni 2015 um 23:53 Uhr
> *Von:* "Michael Sumner" <mdsumner at gmail.com>
> *An:* "Andrea Timmermann" <timmermann at gmx.at>, r-sig-geo at r-project.org
> *Betreff:* Re: [R-sig-Geo] Problem with raster package and nc4 file
>
> On Sat, 6 Jun 2015 at 08:04 Andrea Timmermann <timmermann at gmx.at> wrote:
>
>> Hi,
>>
>> I have daymet data in nc4 format and could open it with the ncdf4
>> package. I also have the coordinates of some polygons representing
>> ecological areas.
>> Both, the polygon vertices and the nc4 file have the same projection
>> (LCC) and units (degrees).
>
>
>
> Do you have grid data in LCC but stored with longitude/latitude
> coordinates? This is unfortunate, but you can restore the LCC grid
> probably. I would try
>
> r <- raster("tmin_1980.nc4")
>
> and then print it out and tell us the result (or post the errors if it
> fails):
>
> r
>
> Do you have the details of the LCC projection? At a bare minimum you need
> the centre longitude and centre latitude, and the two standard parallels,
> and the datum/ellipsoid.  Can you open the file with ncdf4 and print out
> this?
>
> ncic <- nc_open("tmin_1980.nc4")
> print(ncic)
>
>  You are not supposed to mix use of the ncdf4 package functions with those
> of raster, raster is designed to do all the work - but it does expect
> sensibly specified regular grids.  If your data really are regular in LCC I
> would recommend sorting that out. Can you share the file, or a link to an
> analogous one?
>
> Once you have it sorted out in raster, you can read your shapefile in and
> do this
>
> meanpolydata <- extract(r, poly, fun = mean)
>
> Cheers, Mike.
>
>
>> What I would like to do is to get the average temperature for each
>> ecological area. So I wanted to use the polygons as a mask for the
>> temperature raster and then get the average temperature for each polygon.
>>
>> I wanted to create a raster object (using the raster function in the
>> raster package):
>>
>> ncic <- nc_open("tmin_1980.nc4")
>> raster(ncic, 'tmin')
>>
>> But I get this error:
>> Error in (function (classes, fdef, mtable)  :
>>   unable to find an inherited method for function ‘raster’ for signature
>> ‘"ncdf4"’
>>
>> Does anyone know how to fix this? Is there another way to get the data
>> that I need?
>>
>> I am a bit confused by the fact that the temperature data consists of one
>> matrix with latitude data, one with longitude data and one with the
>> temperature values. And by the fact that the spacing between the latitude
>> and longitude matrices is not uniform. Is the raster package accounting
>> somehow for this?
>>
>> Thanks a lot. Andi.
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list