-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Matrix #203
Comments
Actually, I think JSON-Fortran can read and write a file like this (need to verify though). However, there isn't an API to automatically get/set |
Thanks for your answer. On 10 June 2016 at 15:33, Jacob Williams [email protected] wrote:
|
Here is a way to do it (assuming data is in the file program test
use json_module
use iso_fortran_env, only: output_unit
implicit none
type(json_file) :: json
integer,dimension(:),allocatable :: ivec
integer,dimension(:,:),allocatable :: imat
integer :: i,n_cols,n_rows,var_type
logical :: found
type(json_value),pointer :: WindMatrix,child
type(json_core) :: core
!load the file and print it to console:
call json%load_file('test.json')
if (json%failed()) error stop 'error loading file'
!get number of rows and columns
!assuming data stored by column
!assuming each column has the same number of elements,
!and is the same data type (integer in this case):
call json%info('ShipProfile.WindMatrix',found,var_type,n_cols)
if (.not. found) error stop 'error: ShipProfile.WindMatrix not found'
call json%info('ShipProfile.WindMatrix(1)',found,var_type,n_rows)
if (.not. found) error stop 'error: ShipProfile.WindMatrix(1) not found'
!get a pointer to the wind matrix:
call json%get('ShipProfile.WindMatrix',WindMatrix)
if (.not. associated(WindMatrix)) error stop 'error: ShipProfile.WindMatrix not found'
!size the array:
allocate(imat(n_rows,n_cols))
!grab each column of the windmatrix:
! [we need a json_core for this so we can use json_value_get_by_index]
do i=1,n_cols
call core%get_child(WindMatrix,i,child)
if (.not. associated(child)) error stop 'error: column not found'
call core%get(child,ivec) !get the vector of integers (column of the matrix)
if (.not. allocated(ivec)) error stop 'error: could not get integer column'
if (size(ivec)/=n_rows) error stop 'error: column is wrong size'
imat(:,i) = ivec
deallocate(ivec)
nullify(child)
end do
nullify(WindMatrix)
write(*,*) ''
write(*,*) 'matrix:'
do i=1,n_rows
write(*,*) imat(i,:)
end do
write(*,*) ''
end program test So, you basically read the matrix column by column. The result is:
|
Hi, Is there a procedure for writing a similar file, i.e. a nested list with the format I try the following code:
But this does not produce a nested output: { Would be great if there was a solution to this! |
Hi Jacob,
Apparently it is not possible to create a matrix (in the form arrays of arrays) with json fortran and the matrix must be reshaped as in the test_12.f90 (provided with the json fortran src code). I checked whether the code below (containing arrays of arrays ) was a valid json format and it was according to "jsonlint.com".
Is it possible to extend json fortran to take into account that kind of structure ?
Thanks a lot in advance,
Rudy
The text was updated successfully, but these errors were encountered: