# DimensionMismatch for a Raster

**URL:** <https://discourse.julialang.org/t/dimensionmismatch-for-a-raster/86677>\
**Category:** Geo\
**Created:** [September 2, 2022, 6:37am UTC](https://discourse.julialang.org/t/dimensionmismatch-for-a-raster/86677 "2022-09-02T06:37:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [September 2, 2022, 6:37am UTC](https://discourse.julialang.org/t/dimensionmismatch-for-a-raster/86677/1 "2022-09-02T06:37:33Z")

</div>

A `Raster` behaves just like an `Array` in suitable contexts, but I found the following difference in broadcast behavior. Is it 1) an intended behavior, 2) an unavoidable inconsistency, or 3) a bug?

Basically,

```julia
avr = sum(v,dims=3)/size(v,3)
dev = v .- avr

```

works when `v` is a regular 3D array but it doesn’t when `v` is a `Raster`. With a `Raster`, you need `avr[:,:,1]`. Below is a self-contained example.

I’m using Julia 1.8.0 on macOS 12.5.1 .

```julia
using Downloads
using Rasters

const url = "https://www.unidata.ucar.edu/software/netcdf/examples/sresa1b_ncar_ccsm3-example.nc"
infile = Downloads.download(url, "tmp.nc")
#infile = "tmp.nc"

ua = Raster(infile; name = "ua")

v = ua[:,:,:,1]
avr = sum(v,dims=3)/size(v,3)
#dev = v .- avr # => size mismatch!
dev = v .- avr[:,:,1] # => Fine.

v = Array{Union{Missing,Float64}}(undef, size(ua[:,:,:,1]))

v .= ua[:,:,:,1]
avr = sum(v,dims=3)/size(v,3)
dev = v .- avr

```

---

<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:** [September 2, 2022, 9:33am UTC](https://discourse.julialang.org/t/dimensionmismatch-for-a-raster/86677/2 "2022-09-02T09:33:45Z")

</div>

This is a bug in DimensionalData.jl. But a very subtle one.

For broadcast we need to compare that the dimensions are the same (DD promises not to broadcast different dimensions together or the returned dimensions are ambiguous), but we need to relax the comparison a little.

Basically we will need to allow mismatches where the length of one dimension is `1`, and make sure to keep the longer dimension.

> <https://github.com/rafaqz/DimensionalData.jl/issues/401>
