You say you are modifying an existing old project. In that case it makes sense to keep changes minimal.
However, you can't define the size of a static array at run time. If you want, you can keep the array as it currently is and make sure you don't use the 4th element when data->nDataVar() != 1.
Then:
const size_t dataSize = data->primType().getTypeSize() * dimsOut[0] * dimsOut[1] * dimsOut[2] * (data->nDataVar() != 1 ? 1 : dimsOut[3]);
It's worth mentioning the dimsOut array seems completely unnecessary to calculate the value of dataSize, but who knows what else your code is doing with it. If it is only used inside a single function/method then you could easily replace it with something else, such as std::vector.