Fix UnboundLocalError when a datasource of the wrong shape is looked for

We should raise a proper exception when the shape isn't correct as opposed to let an UnboundLocalError occur.