4

I have a netcdf file of downward longwave radiation for the whole of China dated 1979 - December. It has got three dimensions namely: longitude , latitude and time. The file contains the data for every 3 hours, which means that for the month of December (31 days), there are 248 values of downward longwave radiation. I have listed the characteristics of the source netcdf file below:

 1 variables (excluding dimension variables):
    short lrad[lon,lat,time]   
        scale_factor: 0.25
        add_offset: 685
        _FillValue: -32767
        missing_value: -32767
        units: W m-2
        long_name: Surface downward longwave radiation

 3 dimensions:
    lon  Size:700
        units: degrees_east
        long_name: Longitude
    lat  Size:400
        units: degrees_north
        long_name: Latitude
    time  Size:248   *** is unlimited ***
        units: hours since 1900-01-01 00:00:0.0
        long_name: Time

I have converted the nc file to raster using the following code:

lradin <- raster("lrad_ITPCAS-CMFD_V0106_B-01_197912.nc")

The internal raster attributes of the created raster object "lradin" shows the following information:

class       : RasterLayer 
band        : 1  (of  248  bands)
dimensions  : 400, 700, 280000  (nrow, ncol, ncell)
resolution  : 0.1, 0.1  (x, y)
extent      : 70, 140, 15, 55  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : C:\Program Files\RStudio\bin\lrad_ITPCAS-CMFD_V0106_B-
01_197912.nc 
names       : Surface.downward.longwave.radiation 
z-value     : 1979-12-01 00:00:00 
zvar        : lrad 

The problem is that the attribute of the raster object "lradin" shows only one band of time, i.e. out of time size of 248, "lradin" is showing only 1 time size. This means that I will have to create 248 rasters (for every 3 hours) for the month of December.

Does anybody have idea about how to create the multiband raster that has all the time size of 248 in it ? So that I could have a single raster for the month of December ?

  • 2
    Use lradin <- brick("lrad_ITPCAS-CMFD_V0106_B-01_197912.nc") to get access to all bands, use ?subset or lradin[[1]] to get specific band/s. Use ?getZ to find out the specific date for each band. Use extract(lradin, cbind(lon, lat)) to get time series at specific point/s. There are lots of tricks to this, and some things are slow or fast depending on the data file itself, unfortunately. – mdsumner Aug 16 '17 at 11:21
  • 1
    Thank you so much. It worked. The output raster is showing the 248 time dimensions. :) – Sahadeep Aug 16 '17 at 14:03
4

Use this to get access to all bands

lradin <- brick("lrad_ITPCAS-CMFD_V0106_B-01_197912.nc")

Use ?subset or lradin[[1]] to get specific band/s.

Use ?getZ to find out the specific date for each band. Use extract(lradin, cbind(lon, lat)) to get time series at specific point/s.

Feel free to ask other focussed questions like this as needed.

There are lots of tricks to this, and some things are slow or fast depending on the data file itself, unfortunately

  • Is it possible to extract the varname from a netcdf brick? – SoilSciGuy Jan 23 at 22:48
  • 1
    attr(x@data, "zvar") is the only way I know – mdsumner Jan 24 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.