[R-sig-Geo] Convert netcdf file to a raster ...

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Wed Mar 14 20:10:01 CET 2012


On Wed, Mar 14, 2012 at 4:14 PM, Arnaud Mosnier <a.mosnier at gmail.com> wrote:
> Hi all,
>
> I am trying to open a netCDF file (
> ftp://globcolour_data:fgh678@ftp.acri.fr/GLOB_4KM/RAN/CHL1/MERGED/GSM/2007/DAILY/L3b_20070615__GLOB_4_GSM-MERMODSWF_CHL1_DAY_00.nc.gz
> ).
>
> However, when I use the raster package to load it as a raster ...
>
> library(raster)
> test<-raster("D:\\temp\\testAQUATERRA\\rawtif\\L3b_20070615__GLOB_4_GSM-MERMODSWF_CHL1_DAY_00.nc",
> varnam= "CHL1_mean")
>
> Error in .rasterObjectFromCDF(x, type = objecttype, band = band, ...) :
>  CHL1_mean only has a single dimension; I cannot make a RasterLayer from
> this
>
>
> Here is the structure of the file ...
> Variable "CHL1_mean is linked to a bin, not XY coordinates
>
> library(ncdf)
> test<-open.ncdf("D:\\temp\\L3b_20070615__GLOB_4_GSM-MERMODSWF_CHL1_DAY_00.nc")
> test
>
> [1] "file D:\\temp\\L3b_20070615__GLOB_4_GSM-MERMODSWF_CHL1_DAY_00.nc has 2
> dimensions:"
> [1] "bin   Size: 3308394"
> [1] "row   Size: 3121"
> [1] "------------------------"
> [1] "file D:\\temp\\L3b_20070615__GLOB_4_GSM-MERMODSWF_CHL1_DAY_00.nc has 8
> variables:"
> [1] "short col[bin]  Longname:Longitudinal index of the bins stored in the
> product, zero based and beginning at west Missval:NA"
> [1] "float center_lat[row]  Longname:Center latitude for each useful row
> Missval:1e+30"
> [1] "float center_lon[row]  Longname:Center longitude of the first left bin
> (the first bin in the global ISIN grid, not the first valid bin) for each
> useful row Missval:1e+30"
> [1] "float lon_step[row]  Longname:Longitude step for each useful row
> Missval:1e+30"
> [1] "float CHL1_mean[bin]  Longname:Chlorophyll-a concentration. Case 1
> water - Mean of the binned pixels Missval:1e+30"
> [1] "short CHL1_count[bin]  Longname:Chlorophyll-a concentration. Case 1
> water - Total number of binned pixels Missval:NA"
> [1] "short CHL1_flags[bin]  Longname:Chlorophyll-a concentration. Case 1
> water - Flags Missval:NA"
> [1] "short CHL1_error[bin]  Longname:Chlorophyll-a concentration. Case 1
> water - Error estimation Missval:NA"
>
> Is there a simple way to open this file directly as a raster ?

 There's nothing in it that is explicitly two-dimensional.  The list
of variables printed are all one-dimensional.

 The file has two 'dimensions': "bin" which is 3308394 long, and 'row'
which is 3121 long. Every other variable is one of those lengths long,
and one-dimensional.

 If there was a 2d grid in there you'd see something like:

[1] "file /data/rowlings/MapLibrary/World/Climate/CRUTEM3.nc has 4 dimensions:"
[1] "longitude   Size: 72"
[1] "latitude   Size: 36"
[1] "unspecified   Size: 1"
[1] "t   Size: 1925"
[1] "------------------------"
[1] "file /data/rowlings/MapLibrary/World/Climate/CRUTEM3.nc has 1 variables:"
[1] "float temp[longitude,latitude,unspecified,t]
Longname:Temperature T Missval:2.00000004008175e+20"

which is defining a 36x72x1x1925 grid (lon x lat x 1 x t).

 So I'm guessing that maybe some of those vectors are actually
unwrapped grids? Quite what their dimension is I don't know, since
3121 doesn't go into 3308394 exactly.

 You'll have to use the ncdf package to have a look at the variables
and see if you can make sense of them:

 > L = open.ncdf("L3b_20070615__GLOB_4_GSM-MERMODSWF_CHL1_DAY_00.nc")

 > get.var.ncdf(L,"lon_step",1,10)
 [1] 0.06079027 0.06073899 0.06069803 0.06064690 0.06059586 0.06055509
 [7] 0.06050420 0.06046356 0.06041282 0.06037230

Barry



More information about the R-sig-Geo mailing list