jacobwilliams / json-fortran

A Modern Fortran JSON API
https://jacobwilliams.github.io/json-fortran/
Other
333 stars 82 forks source link

Write out 2d array to JSON file #546

Closed seamanticscience closed 3 months ago

seamanticscience commented 9 months ago

Hello! I'm implementing JSON I/O into a simple Fortran box model of the ocean. I was able to use json-fortran to read in a set of parameters (including a 2d-array, thanks to the instructions here) from a JSON file, but I'm having difficulty writing out 2d arrays.

Here's an example of the JSON input that I can read to an MxN Fortran array and I want to write back out:

 "Pcir" : [
         [0.00, 0.35, 0.15, 0.00, 0.50, 0.00],
         [1.00, 0.00, 0.00, 0.25, 0.00, 0.00],
         [0.00, 0.00, 0.00, 1.00, 0.00, 0.00],
         [0.00, 0.90, 0.55, 0.00, 0.00, 0.90],
         [0.00, 0.00, 0.30, 0.00, 0.00, 0.70],
         [0.00, 0.00, 0.00, 1.10, 0.50, 0.00]
]

I realize one way to approach this would be to unroll the array and write out as an MxN-length vector. However, I'm wondering if there is a way to write the array out as an M-length vector of N-length vectors consistent with the example above?

Thanks in advance!

jacobwilliams commented 9 months ago

Yes you can do it. I can try to work up a simple example...

jacobwilliams commented 9 months ago

Look at the example here: https://github.com/jacobwilliams/json-fortran/pull/547

seamanticscience commented 9 months ago

Wonderful, works for me! Thanks, @jacobwilliams.