-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmod_modelio.F90
978 lines (858 loc) · 42.2 KB
/
mod_modelio.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
! -*- f90 -*-
MODULE MOD_MODELIO
#if defined(USEJSONOUT) || defined(USEJSONIN)
USE JSON_MODULE
!
! JSON-Fortran: A Modern Fortran JSON API
! <https://github.com/jacobwilliams/json-fortran>
!
! Copyright (c) 2014-2021, Jacob Williams
! All rights reserved.
!
! Redistribution and use in source and binary forms, with or without modification,
! are permitted provided that the following conditions are met:
!
! * Redistributions of source code must retain the above copyright notice, this
! list of conditions and the following disclaimer.
!
! * Redistributions in binary form must reproduce the above copyright notice, this
! list of conditions and the following disclaimer in the documentation and/or
! other materials provided with the distribution.
!
! * The names of its contributors may not be used to endorse or promote products
! derived from this software without specific prior written permission.
!
! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
! ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
! WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
! DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
! ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
! (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
! ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
! (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
! SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
!
! > -----------------------------------------------------------------------------------------
! >
! > Original FSON License:
! >
! > Copyright (c) 2012 Joseph A. Levin
! >
! > Permission is hereby granted, free of charge, to any person obtaining a copy of this
! > software and associated documentation files (the "Software"), to deal in the Software
! > without restriction, including without limitation the rights to use, copy, modify, merge,
! > publish, distribute, sublicense, and/or sell copies of the Software, and to permit
! > persons to whom the Software is furnished to do so, subject to the following conditions:
! >
! > The above copyright notice and this permission notice shall be included in all copies or
! > substantial portions of the Software.
! >
! > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
! > INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
! > PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
! > LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
! > OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
! > DEALINGS IN THE SOFTWARE.
! >
! > -----------------------------------------------------------------------------------------
#endif
USE MOD_PRECISION
USE MOD_BOXES
USE MOD_COMMON
IMPLICIT NONE
! --------------------------------------------------------
! List of (PRIVATE) routines/functions
! --------------------------------------------------------
CONTAINS
!=======================================================================
SUBROUTINE MODELIO_FMT(fmt)
CHARACTER*64, INTENT(OUT) :: fmt
CHARACTER*64 :: inifmt, varfmt, frep
! Construct fortran format string
! Output the time and nutrient limitation code
inifmt='1x, i10.1, 1x, i14.0,'
! Each variable then is a space and a 10 position float with 5 decimal places
varfmt='1x, f10.5'
! This is the number of repeats (10 variables of nbox dimensions plus pstar and atmpco2)
write(frep ,'(I4)') 10*nbox+2
! Combine everything together
! fmt='('//trim(fmt)//trim(frep)//'('//trim(varfmt)//'))'
write(fmt,'(6A)') '(',trim(inifmt),trim(frep),'(',trim(varfmt),'))'
RETURN
END SUBROUTINE MODELIO_FMT
!=======================================================================
SUBROUTINE MODELIO_HEADER(filename_avg &
)
CHARACTER*64, INTENT(IN) :: filename_avg
INTEGER :: iounit
#if defined(WRITESTOUTFILE)
CHARACTER*64 :: avg_fname
write (avg_fname , '(a,a)') trim(filename_avg) ,'.dat'
iounit = 14
! open an output file and write initial values to file
open(iounit, file=avg_fname, status="unknown")
! write column header output
write(iounit,*)' t(yr) Limits ', &
repeat(' THETA ',nbox), &
repeat(' SALT ',nbox), &
repeat('DIC ',nbox), &
repeat('ALK ',nbox), &
repeat(' PO4 ',nbox), &
repeat(' NO3 ',nbox), &
repeat(' FET ',nbox), &
repeat(' LIG ',nbox), &
repeat(' EXPORT ',nbox), &
' P* ', &
repeat(' OCPCO2 ',nbox), &
' ATPCO2 '
! close the output file
close(iounit)
#endif
RETURN
END SUBROUTINE MODELIO_HEADER
!=======================================================================
SUBROUTINE MODELIO_OUTPUT( &
filename_avg, &
outstep, &
outstepmax, &
timeM, &
lim, &
thetaM, &
saltM, &
dicM, &
alkM, &
po4M, &
no3M, &
fetM, &
ltM, &
exportM, &
pstarM, &
pco2M, &
pco2A, &
thout, &
sout, &
cout, &
aout, &
pout, &
nout, &
fout, &
lout, &
expout, &
ocpco2out, &
tout, &
nlout, &
psout, &
atpco2out &
)
REAL(KIND=wp), INTENT(IN), DIMENSION(nbox) :: &
thetaM, saltM, exportM, pco2M, &
dicM, alkM, po4M, no3M, fetM, ltM
REAL(KIND=wp), INTENT(IN) :: &
timeM, pco2A, pstarM
CHARACTER*64, INTENT(IN) :: filename_avg
INTEGER, INTENT(IN) :: outstep, outstepmax
INTEGER(kind=ip), INTENT(IN) :: lim
REAL(KIND=wp), intent(inout), dimension (outstepmax,nbox) :: &
thout, &
sout, &
cout, &
aout, &
pout, &
nout, &
fout, &
lout, &
expout, &
ocpco2out
REAL(KIND=wp), intent(inout), dimension (outstepmax) :: &
tout, &
psout, &
atpco2out
INTEGER(KIND=ip), intent(inout), dimension (outstepmax) :: &
nlout
! Always fill the arrays
CALL MODELIO_ARRAY_OUTPUT( &
outstep, &
outstepmax, &
timeM, &
lim, &
thetaM, &
saltM, &
dicM, &
alkM, &
po4M, &
no3M, &
fetM, &
ltM, &
exportM, &
pstarM, &
pco2M, &
pco2A, &
thout, &
sout, &
cout, &
aout, &
pout, &
nout, &
fout, &
lout, &
expout, &
ocpco2out, &
tout, &
nlout, &
psout, &
atpco2out &
)
! Only write out to textfile if needed
#if defined(WRITESTOUTFILE)
CALL MODELIO_STOUT_OUTPUT( &
filename_avg, &
outstep, &
outstepmax, &
timeM, &
lim, &
thetaM, &
saltM, &
dicM, &
alkM, &
po4M, &
no3M, &
fetM, &
ltM, &
exportM, &
pstarM, &
pco2M, &
pco2A &
)
#endif
#if defined(USEJSONOUT)
! Only write to JSON file at the end of the run, if requested
if (mod(outstep,outstepmax).eq.zero) then
call MODELIO_JSON_OUTPUT( &
filename_avg, &
thout, &
sout, &
cout, &
aout, &
pout, &
nout, &
fout, &
lout, &
expout, &
ocpco2out, &
tout, &
nlout, &
psout, &
atpco2out &
)
endif ! output time check
#endif
RETURN
END SUBROUTINE MODELIO_OUTPUT
!=======================================================================
SUBROUTINE MODELIO_STOUT_OUTPUT( &
filename_avg, &
outstep, &
outstepmax, &
timeM, &
lim, &
thetaM, &
saltM, &
dicM, &
alkM, &
po4M, &
no3M, &
fetM, &
ltM, &
exportM, &
pstarM, &
pco2M, &
pco2A &
)
REAL(KIND=wp), INTENT(IN), DIMENSION(nbox) :: &
thetaM, saltM, exportM, pco2M, &
dicM, alkM, po4M, no3M, fetM, ltM
REAL(KIND=wp), INTENT(IN) :: &
timeM, pco2A, pstarM
CHARACTER*64, INTENT(IN) :: filename_avg
INTEGER, INTENT(IN) :: outstep, outstepmax
INTEGER(kind=ip), INTENT(IN) :: lim
INTEGER :: iounit
CHARACTER*64 :: fmt, avg_fname
iounit = 14
write (avg_fname , '(a,a)') trim(filename_avg) ,'.dat'
! Set up the I/O numerical format for files
call modelio_fmt(fmt)
#if defined(WRITESTOUTFILE)
! Write out the file header if necessary
if ( timeM .eq.zero) then
call modelio_header(filename_avg &
)
endif
! Append model state to file
open(iounit, file=avg_fname, status="old", &
position="append")
write(iounit,fmt) int(timeM), &
lim, &
thetaM, &
saltM, &
dicM, &
alkM, &
po4M, &
no3M, &
fetM, &
ltM, &
-exportM, &
pstarM, &
pco2M, &
pco2A
close(iounit)
#endif
RETURN
END SUBROUTINE MODELIO_STOUT_OUTPUT
!=======================================================================
SUBROUTINE MODELIO_ARRAY_OUTPUT( &
outstep, &
outstepmax, &
timeM, &
lim, &
thetaM, &
saltM, &
dicM, &
alkM, &
po4M, &
no3M, &
fetM, &
ltM, &
exportM, &
pstarM, &
pco2M, &
pco2A, &
thout, &
sout, &
cout, &
aout, &
pout, &
nout, &
fout, &
lout, &
expout, &
ocpco2out, &
tout, &
nlout, &
psout, &
atpco2out &
)
REAL(KIND=wp), INTENT(IN), DIMENSION(nbox) :: &
thetaM, saltM, exportM, pco2M, &
dicM, alkM, po4M, no3M, fetM, ltM
REAL(KIND=wp), INTENT(IN) :: &
timeM, pco2A, pstarM
INTEGER, INTENT(IN) :: outstep, outstepmax
INTEGER(kind=ip), INTENT(IN) :: lim
REAL(KIND=wp), intent(inout), dimension (outstepmax,nbox) :: &
thout, &
sout, &
cout, &
aout, &
pout, &
nout, &
fout, &
lout, &
expout, &
ocpco2out
REAL(KIND=wp), intent(inout), dimension (outstepmax) :: &
tout, &
psout, &
atpco2out
INTEGER(KIND=ip), intent(inout), dimension (outstepmax) :: &
nlout
! output to array
thout (outstep,1:nbox) = thetaM
sout (outstep,1:nbox) = saltM
cout (outstep,1:nbox) = dicM
aout (outstep,1:nbox) = alkM
pout (outstep,1:nbox) = po4M
nout (outstep,1:nbox) = no3M
fout (outstep,1:nbox) = fetM
lout (outstep,1:nbox) = ltM
expout (outstep,1:nbox) =-exportM
ocpco2out (outstep,1:nbox) = pco2M
tout (outstep) = timeM
nlout (outstep) = lim
psout (outstep) = pstarM
atpco2out (outstep) = pco2A
RETURN
END SUBROUTINE MODELIO_ARRAY_OUTPUT
!=======================================================================
SUBROUTINE MODELIO_JSON_OUTPUT( &
filename_avg, &
! outstep, &
! outstepmax, &
thout, &
sout, &
cout, &
aout, &
pout, &
nout, &
fout, &
lout, &
expout, &
ocpco2out, &
tout, &
nlout, &
psout, &
atpco2out &
)
! Write output using the json-fortran library
CHARACTER*64, INTENT(IN) :: filename_avg
! INTEGER, intent(in) :: outstep, outstepmax
REAL(KIND=wp), intent(in), dimension (:,:) :: &
thout, &
sout, &
cout, &
aout, &
pout, &
nout, &
fout, &
lout, &
expout, &
ocpco2out
REAL(KIND=wp), intent(in), dimension (:) :: &
tout, &
psout, &
atpco2out
INTEGER(KIND=ip), intent(in), dimension (:) :: &
nlout
#if defined(USEJSONOUT)
TYPE(json_core) :: json
TYPE(json_value),POINTER :: root
LOGICAL :: is_valid
INTEGER :: iunit
CHARACTER(KIND=json_CK,LEN=:),ALLOCATABLE :: error_msg
CHARACTER*64 :: avg_fname
! Write filename to a variable
write (avg_fname , '(a,a)') trim(filename_avg) ,'.json'
! Initialize the JSON object
call json%initialize(verbose=.true.,compress_vectors=.true.); &
if (json%failed()) stop
! Create the root ouput object to store all the data
call json%create_object(root,avg_fname); if (json%failed()) stop
!input structure:
! Would be great to write the parameters with the output, but struggling to
! find a way to write out 2-d matrices for P, K, and R.
! call json%create_object(inp,'inputs'); if (json%failed()) stop
! call json%add(p, inp); if (json%failed()) stop
!
! call json%add(inp, 't0', 0.1_wp); if (json%failed()) stop
! call json%add(inp, 'tf', 1.1_wp); if (json%failed()) stop
! call json%add(inp, 'x0', 9999.000_wp); if (json%failed()) stop
! nullify(inp)
!model output structure:
! call json%create_object(outp,'output'); if (json%failed()) stop
! call json%add(root, outp); if (json%failed()) stop
! Can just output directly with vectors
call json%add(root, 'time' , int(tout) ); if (json%failed()) stop
call json%add(root, 'lim' , int(nlout)); if (json%failed()) stop
call json%add(root, 'pstar' , psout ); if (json%failed()) stop
call json%add(root, 'atmpco2', atpco2out ); if (json%failed()) stop
! Easier to output arrays with a function
call modelio_json_add_arr(json, root, 'theta' , thout )
call modelio_json_add_arr(json, root, 'salt' , sout )
call modelio_json_add_arr(json, root, 'carbon' , cout )
call modelio_json_add_arr(json, root, 'alkalinity', aout )
call modelio_json_add_arr(json, root, 'phosphate' , pout )
call modelio_json_add_arr(json, root, 'nitrate' , nout )
call modelio_json_add_arr(json, root, 'iron' , fout )
call modelio_json_add_arr(json, root, 'ligand' , lout )
call modelio_json_add_arr(json, root, 'export' , expout )
call modelio_json_add_arr(json, root, 'pco2' , ocpco2out )
! Clean up a bit
!nullify(outp)
!validate the JSON structure
call json%validate(root, is_valid, error_msg)
if (.not. is_valid) then
stop &
'Error: root is not a valid JSON linked list: '
write(*,*) error_msg
end if
! Write out to file
open(newunit=iunit, file=avg_fname, status='REPLACE')
call json%print(root,iunit); if (json%failed()) stop
close(iunit)
! Clean up the rest
call json%destroy(root); if (json%failed()) stop
RETURN
#endif
END SUBROUTINE MODELIO_JSON_OUTPUT
!=======================================================================
#if defined(USEJSONOUT)
SUBROUTINE MODELIO_JSON_ADD_ARR(json, me, variable, boxdata)
! Write array output using the json-fortran library inspired by
! https://github.com/jacobwilliams/json-fortran/pull/547
IMPLICIT NONE
TYPE(json_core),INTENT(INOUT) :: json
TYPE(json_value),POINTER :: me, var
CHARACTER(LEN=*),INTENT(IN) :: variable
REAL(KIND=WP),DIMENSION(:,:),INTENT(IN) :: boxdata
INTEGER :: i
!initialize:
nullify(var)
!create the variable array before data can be added
call json%create_array(var, trim(variable)); if (json%failed()) stop
! Add to the output object
call json%add(me, var); if (json%failed()) stop
! Cycle through the data, adding each box/vector to the variable array
do i=1,nbox
call json%add(var, '', boxdata(:,i)); if (json%failed()) stop
enddo
!cleanup:
nullify(var)
RETURN
END SUBROUTINE MODELIO_JSON_ADD_ARR
#endif
!=======================================================================
SUBROUTINE MODELIO_JSON_INPUT( &
filename, id, &
maxyears, outputyears, outstepmax, &
dx, dy, dz, depth, latitude, &
Kin, Rin, Pin, &
psi_in, dif_in, &
alpha_yr, gamma_in, lt_lifein, &
dldz_in, fe_input, wind_in, fopen_in, &
thin, sain, cain, alin, phin, niin, fein, liin, &
atpco2in &
)
! Read input using the json-fortran library
CHARACTER*64, INTENT(IN) :: filename
INTEGER, INTENT(OUT) :: outstepmax, id
REAL(KIND=wp), INTENT(OUT) :: &
maxyears, &
outputyears, &
gamma_in, &
lt_lifein, &
alpha_yr, &
atpco2in, &
psi_in, &
dif_in
! Input arrays (nbox dimensionless)
REAL(KIND=wp), DIMENSION(nbox), INTENT(OUT) :: &
dx, &
dy, &
dz, &
depth, &
latitude, &
thin, &
sain, &
cain, &
alin, &
phin, &
niin, &
fein, &
liin, &
fe_input, &
dldz_in, &
wind_in, &
fopen_in
REAL(KIND=wp), DIMENSION(nbox,nbox), INTENT(OUT) :: &
Kin, &
Rin, &
Pin
#if defined(USEJSONIN)
! Local variable definitions
TYPE(json_file) :: json
TYPE(json_value), POINTER :: p_matrix, p_child
TYPE(json_core) :: core
LOGICAL :: found
INTEGER :: i, n_cols, n_rows, var_type
REAL(KIND=wp) :: isca
REAL(KIND=wp), DIMENSION(:), ALLOCATABLE :: ivec
REAL(KIND=wp), DIMENSION(:,:), ALLOCATABLE :: imat
! initialize the class
call json%initialize(); if (json%failed()) stop &
'error initializing json-fortran'
! read the file
!json_data => fson_parse("run_microCOSM_4boxvariablelt_pickup.json")
call json%load_file(filename); if (json%failed()) stop &
'error: microcosm_pickup.json not found'
! extract scalar and vector data from the file
! [found can be used to check if the data was really there]
! Iteration ID
call json%get('niter', isca, found)
if ( .not. found ) then
stop 1
else
id=isca
endif
! Length of simulation in years
call json%get('nyrs', isca, found)
if ( .not. found ) then
stop 1
else
maxyears=isca
endif
! Output frequency
call json%get('tout', isca, found)
if ( .not. found ) then
stop 1
else
outputyears=isca
endif
! Number of outputs, including initial conditions
call json%get('nout', isca, found)
if ( .not. found ) then
stop 1
else
outstepmax=isca
endif
! Geometry and geography
! Box size in x-direction
call json%get('dx', ivec, found)
if ( .not. found ) then
stop 1
else
dx=ivec
endif
! Box size in y-direction
call json%get('dy', ivec, found)
if ( .not. found ) then
stop 1
else
dy=ivec
endif
! Box size in z-direction
call json%get('dz', ivec, found)
if ( .not. found ) then
stop 1
else
dz=ivec
endif
! Box depth
call json%get('depth', ivec, found)
if ( .not. found ) then
stop 1
else
depth=ivec
endif
! Box latitude
call json%get('latitude', ivec, found)
if ( .not. found ) then
stop 1
else
latitude=ivec
endif
! Parameter values
! Overturning circulations strength
call json%get('psi', isca, found)
if ( .not. found ) then
stop 1
else
psi_in=isca
endif
! Diffusive mixing strength
call json%get('dif', isca, found)
if ( .not. found ) then
stop 1
else
dif_in=isca
endif
! Biological productivity max rate
call json%get('alphabio', isca, found)
if ( .not. found ) then
stop 1
else
alpha_yr=isca
endif
! Fraction of biological matter that can be ligand
call json%get('gamma', isca, found)
if ( .not. found ) then
stop 1
else
gamma_in=isca
endif
! Ligand degradation timescale
call json%get('lambda', isca, found)
if ( .not. found ) then
stop 1
else
lt_lifein=isca
endif
! gaovla_opt = ((gamma_in/106._wp))/lt_lifein
! Ligand degradation rate vertical gradient
call json%get('dlambdadz', ivec, found)
if ( .not. found ) then
stop 1
else
dldz_in=ivec
endif
! Iron input rate
call json%get('sourceFe', ivec, found)
if ( .not. found ) then
stop 1
else
fe_input=ivec
endif
! Wind speed
call json%get('wind', ivec, found)
if ( .not. found ) then
stop 1
else
wind_in=ivec
endif
! Open ocean fraction (ice or subsurface)
call json%get('fopen', ivec, found)
if ( .not. found ) then
stop 1
else
fopen_in=ivec
endif
! Initial concentrations
! Initial temperatures
call json%get('theta', ivec, found)
if ( .not. found ) then
stop 1
else
thin=ivec
endif
! Initial salinities
call json%get('salt', ivec, found)
if ( .not. found ) then
stop 1
else
sain=ivec
endif
! Initial carbon concentrations
call json%get('carbon', ivec, found)
if ( .not. found ) then
stop 1
else
cain=ivec
endif
! Initial alkalinities
call json%get('alkalinity', ivec, found)
if ( .not. found ) then
stop 1
else
alin=ivec
endif
! Initial phosphate concentrations
call json%get('phosphate', ivec, found)
if ( .not. found ) then
stop 1
else
phin=ivec
endif
! Initial nitrate concentrations
call json%get('nitrate', ivec, found)
if ( .not. found ) then
stop 1
else
niin=ivec
endif
! Initial iron concentrations
call json%get('iron', ivec, found)
if ( .not. found ) then
stop 1
else
fein=ivec
endif
! Initial ligand concentrations
call json%get('ligand', ivec, found)
if ( .not. found ) then
stop 1
else
liin=ivec
endif
! Initial atmospheric pCO2
call json%get('atmpco2', isca, found)
if ( .not. found ) then
stop 1
else
atpco2in=isca
endif
! Read circulation matrix (Wish i could subroutine this, but fails compilation
! with
! "Error: Found no matching specific binding for the call to the GENERIC 'info'")
call json%info('Pcir',found,var_type,n_cols)
if (.not. found) error stop 'error: Pcir not found'
call json%info('Pcir(1)',found,var_type,n_rows)
if (.not. found) error stop 'error: Pcir(1) not found'
!get a pointer to the matrix:
call json%get('Pcir',p_matrix)
if (.not. associated(p_matrix)) error stop 'error: Pcir not found'
!size the array:
allocate(imat(n_rows,n_cols))
!grab each column of the p_matrix:
! [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(p_matrix,i,p_child)
if (.not. associated(p_child)) error stop 'error: column not found'
call core%get(p_child,ivec) !get the vector (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(p_child)
end do
nullify(p_matrix)
Pin=transpose(imat)
deallocate(imat)
!Read Mixing matrix (Wish i could subroutine this, but fails compilation with
! "Error: Found no matching specific binding for the call to the GENERIC 'info'")
call json%info('Kmix',found,var_type,n_cols)
if (.not. found) error stop 'error: Kmix not found'
call json%info('Kmix(1)',found,var_type,n_rows)
if (.not. found) error stop 'error: Kmix(1) not found'
!get a pointer to the matrix:
call json%get('Kmix',p_matrix)
if (.not. associated(p_matrix)) error stop 'error: Kmix not found'
!size the array:
allocate(imat(n_rows,n_cols))
!grab each column of the p_matrix:
! [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(p_matrix,i,p_child)
if (.not. associated(p_child)) error stop 'error: column not found'
call core%get(p_child,ivec) !get the vector (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(p_child)
end do
nullify(p_matrix)
Kin=transpose(imat)
deallocate(imat)
!Read Rrmemin matrix (Wish i could subroutine this, but fails compilation with
! "Error: Found no matching specific binding for the call to the GENERIC 'info'")
call json%info('Rremin',found,var_type,n_cols)
if (.not. found) error stop 'error: Rremin not found'
call json%info('Rremin(1)',found,var_type,n_rows)
if (.not. found) error stop 'error: Rremin(1) not found'
!get a pointer to the matrix:
call json%get('Rremin',p_matrix)
if (.not. associated(p_matrix)) error stop 'error: Rremin not found'
!size the array:
allocate(imat(n_rows,n_cols))
!grab each column of the p_matrix:
! [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(p_matrix,i,p_child)
if (.not. associated(p_child)) error stop 'error: column not found'
call core%get(p_child,ivec) !get the vector (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(p_child)
end do
nullify(p_matrix)
Rin=transpose(imat)
deallocate(imat)
! clean up
call json%destroy()
#endif
RETURN
END SUBROUTINE MODELIO_JSON_INPUT
!=======================================================================
END MODULE MOD_MODELIO