csdms / bmi-fortran

Basic Model Interface for Fortran
https://bmi.readthedocs.io
MIT License
6 stars 10 forks source link

Output arrays, not pointers, from getters #18

Closed mdpiper closed 5 years ago

mdpiper commented 5 years ago

A BMI should not allocate memory. Instead, when using, e.g., get_value, the caller should create a variable, allocating memory to hold an array output. The variable is passed as a parameter into get_value, where values are copied into it.

Fortran arrays actually make this easy. For example, in get_grid_shape, these statements

shape(1) = self%model%n_y
shape(2) = self%model%n_x

and this statement

shape = [self%model%n_y, self%model%n_x]

are equivalent, element-wise copies into the variable shape.

In this PR, I've used this concept to rework the the get_value and get_value_at_indices methods, outputting an array, not a pointer, with the values of the specified variable.

(Thank you to @mcflugen for recognizing this problem on his way out the door yesterday.)

cc: @sc0tts