# How to load a raster of a NC file with multiple monthly bands

**URL:** <https://discourse.julialang.org/t/how-to-load-a-raster-of-a-nc-file-with-multiple-monthly-bands/126487>\
**Category:** Geo\
**Tags:** question, rasters\
**Created:** [March 3, 2025, 8:35am UTC](https://discourse.julialang.org/t/how-to-load-a-raster-of-a-nc-file-with-multiple-monthly-bands/126487 "2025-03-03T08:35:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [March 3, 2025, 8:35am UTC](https://discourse.julialang.org/t/how-to-load-a-raster-of-a-nc-file-with-multiple-monthly-bands/126487/1 "2025-03-03T08:35:38Z")

</div>

I am using the [`chelsa_cmip6`](https://gitlabext.wsl.ch/karger/chelsa_cmip6) script to download climatic data, and the output is a NC file with the data embedded in monthly series, like [in this (small) example](https://nc.beta-lorraine.fr/s/a5L5xLtKAGPfK3e) of precipitations.  
How do I load it into a vector of rasters (perhaps better) or a 3 dimensional raster ?  
If I load it without parameters using Rasters.Raster I have a strange object without the X dimension:

```julia
julia> import NCDatasets;

julia> using Rasters;

julia> ncpath = "chelsa_monthly_precipitations_2014.nc";

julia> ncmap = Rasters.Raster(ncpath)
╭─────────────────────────────────────────────────╮
│ 2×24 Raster{Union{Missing, Float64},2} lat_bnds │
├─────────────────────────────────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── dims ┐
  ↓ bnds,
  → Y Mapped{Float64} [47.004027229800016, 47.01236056310003, …, 47.187360562400016, 47.19569389570003] ForwardOrdered Regular Points
├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── metadata ┤
  Metadata{Rasters.NCDsource} of Dict{String, Any} with 1 entry:
  "_FillValue" => NaN
├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── raster ┤
  extent: Extent(bnds = (1, 2), Y = (47.004027229800016, 47.19569389570003))
  missingval: missing
  crs: EPSG:4326
  mappedcrs: EPSG:4326
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
 47.004 47.0124 47.0207 47.029 47.0374 47.0457 47.054 47.0624 47.0707 47.079 … 47.129 47.1374 47.1457 47.154 47.1624 47.1707 47.179 47.1874 47.1957
 46.0714 46.0797 46.0881 46.0964 46.1047 46.1131 46.1214 46.1297 46.1381 46.1464 46.1964 46.2047 46.2131 46.2214 46.2297 46.2381 46.2464 46.2547 46.2631
 47.9366 47.945 47.9533 47.9616 47.97 47.9783 47.9866 47.995 48.0033 48.0116 48.0616 48.07 48.0783 48.0866 48.095 48.1033 48.1116 48.12 48.1283

julia> ds = NCDatasets.NCDataset(ncpath)
Dataset: chelsa_monthly_precipitations_2014.nc
Group: /

Dimensions
   lat = 24
   lon = 48
   month = 12
   bnds = 2

Variables
  lat (24)
    Datatype: Union{Missing, Float64} (Float64)
    Dimensions: lat
    Attributes:
     _FillValue = NaN
     standard_name = latitude
     long_name = latitude
     units = degrees_north

  lon (48)
    Datatype: Union{Missing, Float64} (Float64)
    Dimensions: lon
    Attributes:
     _FillValue = NaN
     standard_name = longitude
     long_name = longitude
     units = degrees_east

  month (12)
    Datatype: Dates.DateTime (Int64)
    Dimensions: month
    Attributes:
     units = days since 2014-01-15 00:00:00
     calendar = proleptic_gregorian

  lat_bnds (2 × 24)
    Datatype: Union{Missing, Float64} (Float64)
    Dimensions: bnds × lat
    Attributes:
     _FillValue = NaN

  lon_bnds (2 × 48)
    Datatype: Union{Missing, Float64} (Float64)
    Dimensions: bnds × lon
    Attributes:
     _FillValue = NaN

  pr (48 × 24 × 12)
    Datatype: Union{Missing, Float32} (Float32)
    Dimensions: lon × lat × month
    Attributes:
     _FillValue = NaN

Global attributes
  coordinates = lat_bnds lon_bnds

julia> size(ncmap)
(2, 24)

```

This is how it appears on QGIS (plot based on the first month):

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/e/cec95b0ee93d343ac6b3ce9dc3fc8d111e301e53.png)

I can obtain the row data with `ds["pr"][:,:,:]`, perhaps I can use the metadata of the ds object to create a vector of rasters manually using the data of `ds["pr"][:,:,month]` ??

> **Python code to get the example data**
>
> `$ pip install chelsa_cmip6`
> 
> ```python
> from chelsa_cmip6.GetClim import chelsa_cmip6
> chelsa_cmip6(activity_id='CMIP', 
> table_id='Amon', 
> experiment_id='historical', 
> institution_id='MPI-M', 
> source_id='MPI-ESM1-2-LR', 
> member_id='r1i1p1f1', 
> refps='1981-01-15', 
> refpe='2010-12-15', 
> fefps='2014-01-01', 
> fefpe='2014-12-31', 
> xmin=9.8, 
> xmax=10.2,
> ymin=47.0, 
> ymax=47.2,
> output='~',
> use_esgf=False)
> 
> ```

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [March 3, 2025, 8:58am UTC](https://discourse.julialang.org/t/how-to-load-a-raster-of-a-nc-file-with-multiple-monthly-bands/126487/2 "2025-03-03T08:58:12Z")

</div>

Best to make a bug report. But normally you want a `RasterStack` when there are multiple layers, or specify `Raster(path; name=:layerIwant)` or you just get the first one. Here the first one found should actually be skipped, it’s a bounds matrix.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [March 3, 2025, 9:02am UTC](https://discourse.julialang.org/t/how-to-load-a-raster-of-a-nc-file-with-multiple-monthly-bands/126487/3 "2025-03-03T09:02:53Z")

</div>

I have tried with “name” but I don’t know what to put on it… (“month” doesn’t seem to work).

I can get a single NCDataSet with, for example, `feb = NCDatasets.@select(ds, Dates.month(month) == 2)`, but then I don’t know how to convert it to a `Raster`.

Thanks, I’ll open a feature request on the `Rasters` package.

---

<div class="post-metadata">

**Author:** ![Fliks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fliks/32/2494_2.png) [@Fliks](https://discourse.julialang.org/u/Fliks)\
**Post date:** [March 3, 2025, 9:15am UTC](https://discourse.julialang.org/t/how-to-load-a-raster-of-a-nc-file-with-multiple-monthly-bands/126487/4 "2025-03-03T09:15:16Z")

</div>

From the structure of your data I think it should be `Raster(path; name="pr")`which should give you a three dimensional Raster.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [March 3, 2025, 9:18am UTC](https://discourse.julialang.org/t/how-to-load-a-raster-of-a-nc-file-with-multiple-monthly-bands/126487/5 "2025-03-03T09:18:30Z")

</div>

Thank you… I thought I tried it, instead…
