TransWikia.com

File conversion from .nc to .tiff

Geographic Information Systems Asked on April 11, 2021

I’m downloading .nc file from here: https://www.cpc.ncep.noaa.gov/products/CFSv2/dataInd3/glbPrecMon.nc

I want to convert .nc file to .tiff file. I used raster to read file its not working. It’s showing like this

In .rasterObjectFromCDF(x, type = objecttype, band = band, ...): anom has more than 4 dimensions

One Answer

NetCDF files are complex structured data that can't always be simply read into a raster or raster stack. This error is telling you as much.

Use the ncdf4 package to open and inspect the data:

> library(ncdf4)
> gln = nc_open("glbPrecMon.nc")
> gln
File glbPrecMon.nc (NC_FORMAT_CLASSIC):

     1 variables (excluding dimension variables):
        float anom[LON,LAT,LEVEL,TIME,ENS]   
        ....

This shows you the file has one variable called anom which is five-dimensional. The info for the dimensions follows:

 5 dimensions:
    ENS  Size:1
        standard_name: ens
        long_name: ensemble mean
        units: none
        axis: E
        member: 40 forecast members ensemble mean
    TIME  Size:6
        standard_name: time
        long_name: the 1st day of the forecast target month
        units: days since 1970-01-01     
        axis: T
    LEVEL  Size:1
        standard_name: level
        long_name: level
        units: none
        axis: Z
    LAT  Size:190
        standard_name: lat
        long_name: latitude
        units: degrees_north
        axis: Y
        valid_min: -89.2770004272461
        valid_max: 89.2770004272461
    LON  Size:384
        standard_name: lon
        long_name: longitude
        units: degrees_east
        axis: X
        valid_min: 0
        valid_max: 359.0625

Let's get the data for the variable. I'll ask it not to collapse single-length dimensions:

> gl_anom = ncvar_get(gln,"anom", collapse_degen=FALSE)

That should give us a five-dimensional array:

> dim(gl_anom)
[1] 384 190   1   6   1

You can then get the values along these dimensions from the gln object:

> gln$dim["TIME"][[1]]$vals
[1] 18628 18659 18687 18718 18748 18779
> gln$dim["LAT"][[1]]$vals
  [1] -89.2766 -88.3398 -87.3973 -86.4535 -85.5093 -84.5649 -83.6203 -82.6756
  [9] -81.7309 -80.7862 -79.8414 -78.8966 -77.9518 -77.0070 -76.0622 -75.1174
 [17] -74.1725 -73.2277 -72.2829 -71.3380 -70.3931 -69.4483 -68.5034 -67.5586

Since two of the dimensions are of length 1, you can put this into a raster stack. Let's do that for you.

First re-read the variable and collapse the length-1 dimensions:

gl_anom = ncvar_get(gln,"anom", collapse_degen=TRUE)
dim(gl_anom)
## [1] 384 190   6

Now get the lat-long coordinates:

LAT = gln$dim["LAT"][[1]]$vals
LON = gln$dim["LON"][[1]]$vals

range(LAT)
## [1] -89.2766  89.2766

Now here you need to find out exactly what these coordinates are - cell centres, cell corners etc. One thing they aren't giving you is the raster stack extent because there's the same number as the dimensions. On inspection it looks like the extent is 0 to 360 and -90 to 90. I'll plug that in but YOU NEED TO CHECK THIS.

Also, you need to swap the lat-long coordinates in the array using aperm, which seems to be needed to get it into the right order (more on this later):

gl_stack = brick(aperm(gl_anom,c(2,1,3)), xmn=0,xmx=360, ymn=-90, ymx=90)

now you can plot slices of the array and slices of the brick:

par(mfrow=c(2,1))
plot(gl_stack[[1]],main="raster stack 1")
image(gl_anom[,,1],asp=.5)
title("data slice")

enter image description here

Apart from the colour palette you should see that one of these is upside down (NS inverted). I don't know which and I can't make out any surface characteristics so I'm not sure which is right. Flipping it is easy with standard raster or array functions so I'll stop here and let you sort it out. I could also be wrong in other ways, such as messing up lat-long with the aperm function change, but the data doesn't look right in the other direction.

Answered by Spacedman on April 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP