-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_ldas_routines.F90
3370 lines (2464 loc) · 112 KB
/
preprocess_ldas_routines.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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module preprocess_ldas_routines
! collection of subroutines and functions needed for GEOSldas pre-processing
!
! The *.F90 module i was created as follows:
!
! 1.) git mv preprocess_ldas.F90 preprocess_ldas_routines.F90 (for best possible git diff)
! 2.) removed main program from file and put into new file preprocess_ldas.F90
! 3.) moved additional helper subroutines and functions to here:
! - LDAS_read_til_file() [from LDAS_TileCoordRoutines.F90]
! - read_grid_elev() [from LDAS_TileCoordRoutines.F90]
! - fix_dateline_bug_in_tilecoord() [from LDAS_TileCoordRoutines.F90]
! - read_catchment_def() [from LDAS_TileCoordRoutines.F90]
! - is_cat_in_box() [from LDAS_TileCoordRoutines.F90]
! - domain_setup() [from LDAS_ensdrv_init_routines.F90]
! - read_exclude_or_includelist() [from LDAS_ensdrv_init_routines.F90]
! - read_cat_param() [from LDAS_ensdrv_init_routines.F90]
! - is_in_list() [from LDAS_ensdrv_functions.F90]
! - is_in_domain() [from LDAS_ensdrv_functions.F90]
! - word_count() [from LDAS_ensdrv_functions.F90]
! - open_land_param_file() [from LDAS_ensdrv_functions.F90]
use netcdf
use MAPL
use MAPL_BaseMod, ONLY: &
NTYPS => MAPL_NumVegTypes, &
MAPL_Land
use MAPL_ConstantsMod, ONLY: &
MAPL_RADIUS ! Earth radius
use LDAS_ensdrv_Globals, ONLY: &
logit, &
logunit, &
nodata_generic, &
nodata_tol_generic
use LDAS_TileCoordType, ONLY: &
tile_coord_type, &
grid_def_type, &
operator (==), &
io_grid_def_type
use LDAS_TileCoordRoutines, ONLY: &
LDAS_create_grid_g, &
get_minExtent_grid, &
io_domain_files
use nr_ran2_gasdev, ONLY: &
NRANDSEED
use LDAS_DateTimeMod, ONLY: &
date_time_type
use force_and_cat_progn_pert_types, ONLY: &
N_progn_pert_max, &
N_force_pert_max
use catch_types, ONLY: &
cat_param_type
use catch_constants, ONLY: &
N_gt => CATCH_N_GT
use LDAS_ensdrv_functions, ONLY: &
get_io_filename
use LDAS_ExceptionsMod, ONLY: &
ldas_abort, &
LDAS_GENERIC_ERROR
use gFTL_StringVector
use pFIO
implicit none
private
public :: createf2g
public :: createLocalTilefile
public :: createLocalBC
public :: createLocalCatchRestart
public :: createLocalVegRestart
public :: createLocalmwRTMRestart
public :: correctEase
public :: optimize_latlon
public :: convert_pert_rst
character(10), private :: tmpstring10
character(40), private :: tmpstring40
! Tile type for land that is to be excluded from the simulation domain.
! (GEOSldas allows for non-global simulations and repeated "zooming"
! of the domain while MAPL generally assumes a complete (global) tile
! space. The *_ExcludeFromDomain tile type makes it possible to work
! with complete (global) tile files (ie, make use of MAPL functionality)
! and also maintain GEOSldas functionality.
integer, parameter :: MAPL_Land_ExcludeFromDomain = 1100
contains
! ********************************************************************
subroutine createf2g(orig_tile,domain_def,out_path,catch_def_file,exp_id,ymdhm, SURFLAY, f2g_file)
implicit none
character(*) :: orig_tile
character(*) :: domain_def
character(*) :: out_path
character(*) :: catch_def_file
character(*) :: exp_id
character(*) :: ymdhm
character(*) :: SURFLAY
character(*) :: f2g_file
real :: minlon,maxlon,minlat,maxlat
character(len=512):: exclude_file,include_file
character(len=512):: bcs_path
logical :: file_exist
logical :: d_exist,c_exist
integer :: n
type(grid_def_type) :: tile_grid_g,tile_grid_d
type(tile_coord_type), dimension(:), pointer :: tile_coord_g => null()
type(tile_coord_type), dimension(:), pointer :: tile_coord_d => null()
integer, dimension(:), pointer :: f2g => null()
integer, dimension(:), pointer :: d2g => null()
integer, dimension(:), pointer :: d2f => null()
integer :: N_catg, N_catd,n1,n2,N_catf
type(cat_param_type), dimension(:), allocatable :: cp
real :: dzsf
namelist / domain_inputs / &
minlon, maxlon,minlat,maxlat, &
exclude_file,include_file
inquire(file=trim(orig_tile),exist=file_exist)
if( .not. file_exist) stop ("original tile file not exist")
inquire(file=trim(domain_def),exist=d_exist)
if( .not. d_exist) then
print*,"no domain definition file"
endif
inquire(file=trim(catch_def_file),exist=c_exist)
if( .not. c_exist) then
print*,"no catchment definition file:" , catch_def_file
endif
if(d_exist) then
open (10, file=trim(domain_def), delim='apostrophe', action='read', status='old')
read (10, nml= domain_inputs)
close(10)
else
minlon = -180.
maxlon = 180.
minlat = -90.
maxlat = 90.
exclude_file = ' '
include_file = ' '
endif
call LDAS_read_til_file(orig_tile,catch_def_file,tile_grid_g,tile_coord_g,f2g)
N_catg=size(tile_coord_g)
! include and exclude files are absolute
call domain_setup( &
N_catg, tile_coord_g, &
tile_grid_g, &
' ', exclude_file, ' ', include_file, &
trim(out_path), 'exp_domain ', trim(exp_id), &
minlon, minlat, maxlon, maxlat, &
N_catd, d2g, tile_coord_d, &
tile_grid_d )
allocate(cp(N_catd))
read(SURFLAY,*) dzsf
print*, "SURFLAY: ", dzsf
n1 = index(catch_def_file,'/clsm/')
bcs_path(1:n1-1) = catch_def_file(1:n1-1)
call read_cat_param( N_catg, N_catd, d2g, tile_coord_d, dzsf, bcs_path(1:n1-1), bcs_path(1:n1-1),bcs_path(1:n1-1), &
cp )
call write_cat_param(cp,N_catd)
allocate(d2f(N_catd))
d2f = 0
N_catf = size(f2g)
if( N_catf /= N_catg) then
n = 1
do n1 = 1,N_catd
do n2 = n, N_catf
if (d2g(n1) == f2g(n2)) then
d2f(n1) = n2
n = n2+1
exit
endif
enddo
enddo
if(any(d2f == 0)) stop " Domain includes those excluded tiles"
print*," f2g now is d2f "
else
d2f = d2g
endif
open(40,file=f2g_file,form='formatted',action='write')
write(40,*)N_catf
write(40,*)N_catd
do n=1,N_catd
write(40,*)d2f(n)
enddo
do n=1,N_catd
write(40,*)d2g(n)
enddo
close(40)
if (associated(f2g)) deallocate(f2g)
if (associated(d2g)) deallocate(d2g)
if (associated(d2f)) deallocate(d2f)
contains
! ********************************************************************
logical function is_in_list(N_list, list, this_one)
! checks whether "this_one" is element of list
! reichle, 2 May 2003
implicit none
integer :: N_list, this_one
integer, dimension(N_list) :: list
integer :: n
! ------------------------------------
is_in_list = .false.
do n=1,N_list
if (list(n)==this_one) then
is_in_list = .true.
exit
end if
end do
end function is_in_list
! ******************************************************************
logical function is_in_domain( &
this_cat_exclude, this_cat_include, this_cat_in_box )
! determine whether catchment is in domain
!
! The domain is set up using (if present) an "ExcludeList" of catchments
! to be excluded, an "IncludeList" (if present) of catchments to be included,
! and the bounding box of a rectangular "zoomed" area (as specified
! in the "exeinp" file used in ldas_setup).
!
! order of precedence:
! 1. exclude catchments on ExcludeList
! 2. include catchments on IncludeList or catchments within rectangular domain
! (i.e., catchments in ExcludeList are *always* excluded)
!
! reichle, 7 May 2003
! reichle, 9 May 2005 - redesign (no more continents)
!
! ----------------------------------------------------------------
implicit none
logical :: this_cat_include, this_cat_exclude, this_cat_in_box
is_in_domain = .false.
! if catchment is NOT in ExcludeList
if (.not. this_cat_exclude) then
! if catchment is within bounding box OR in IncludeList
if ((this_cat_in_box) .or. (this_cat_include)) then
is_in_domain = .true.
end if
end if
end function is_in_domain
! *******************************************************************
logical function is_cat_in_box( &
this_minlon, this_minlat, this_maxlon, this_maxlat, &
minlon, minlat, maxlon, maxlat )
! determine whether catchment is within bounding box - reichle, 7 May 2003
implicit none
real :: this_minlon, this_minlat, this_maxlon, this_maxlat
real :: minlon, minlat, maxlon, maxlat
if ( (this_minlon >= minlon) .and. &
(this_maxlon <= maxlon) .and. &
(this_minlat >= minlat) .and. &
(this_maxlat <= maxlat) ) then
is_cat_in_box = .true.
else
is_cat_in_box = .false.
end if
end function is_cat_in_box
! ********************************************************************
subroutine domain_setup( &
N_cat_global, tile_coord_global, &
tile_grid_g, &
exclude_path, exclude_file, include_path, include_file, &
work_path, exp_domain, exp_id, &
minlon, minlat, maxlon, maxlat, &
N_cat_domain, d2g, tile_coord, tile_grid_d )
! Set up modeling domain and determine index vectors mapping from the
! domain to global catchment space.
! Determine actual bounding box for domain.
! Also return tile_coord for domain and tile_grid_d for domain.
!
! -----------------------
!
! The domain is set up using (if present) an "ExcludeList" of catchments
! to be excluded, an "IncludeList" (if present) of catchments to be included,
! and the bounding box of a rectangular "zoomed" area (as specified
! in the "exeinp" file used in ldas_setup).
!
! order of precedence:
! 1. exclude catchments in ExcludeList
! 2. include catchments in IncludeList or catchments within rectangular domain
! (i.e., catchments in ExcludeList are *always* excluded)
!
! input:
!
! input/output:
! tile_grid_g : def of global tile definition grid
! minlon, maxlon, etc: coordinates of bounding box of domain units as
! in tile_coord file, that is longitude -180:180,
! latitude -90:90
!
! output:
! N_cat_domain = number of catchments in zoomed domain
! (for which model integration is conducted)
! d2g = index from domain to global tiles
! tile_coord_d = tile_coord vector for domain
! tile_grid_d = def of smallest subgrid of global tile_grid_g that contains
! all catchments (or tiles) in the domain (tile_grid_d%i_offg,
! tile_grid_d%j_offg are offsets in indices between tile_grid_g
! and tile_grid_d)
! N_catd_cont = number of catchments of (full) domain on each continent
!
!
! - reichle, May 7, 2003
! - reichle, Nov 7, 2003 - computation of bounding box of actual domain
! - reichle, Jul 20, 2004 - fixed initialization of min_min_lon etc
! - reichle, May 11, 2005 - minor output path changes for redesign
! - reichle, May 16, 2005 - add output of tile_grid_d
! - reichle, Aug 18, 2005 - reinstated minlon, maxlon, minlat, maxlat
! - reichle, Jul 23, 2010 - major overhaul
!
! ----------------------------------------------------------
implicit none
integer, intent(in) :: N_cat_global
type(tile_coord_type), dimension(:), pointer :: tile_coord_global ! input
type(grid_def_type), intent(in) :: tile_grid_g
character(*), intent(in) :: exclude_path, include_path
character(*), intent(in) :: exclude_file, include_file
character(*), intent(in) :: work_path
character(*), intent(in) :: exp_domain, exp_id
real, intent(in) :: minlon, minlat ! from nml inputs
real, intent(in) :: maxlon, maxlat ! from nml inputs
integer, intent(out) :: N_cat_domain
integer, dimension(:), pointer :: d2g ! output
type(tile_coord_type), dimension(:), pointer :: tile_coord ! output
type(grid_def_type), intent(out) :: tile_grid_d
! locals
integer :: n, this_tileid, this_catpfaf, N_exclude, N_include, indomain, rc
integer, dimension(N_cat_global) :: ExcludeList, IncludeList, tmp_d2g
real :: this_minlon, this_minlat, this_maxlon, this_maxlat
logical :: this_cat_exclude, this_cat_include, this_cat_in_box
integer :: this_i_indg, this_j_indg
type(grid_def_type) :: tmp_grid_def
logical :: c3_grid
character(512) :: fname
character(len=*), parameter :: Iam = 'domain_setup'
character(len=400) :: err_msg
! ------------------------------------------------------------
if (logit) write (logunit,*) 'Setting up domain: '
if (logit) write (logunit,*)
! ------------------------------------------------------------
!
! try reading *domain.txt, *tilecoord.txt, and *tilegrids.txt files
call io_domain_files( 'r', work_path, exp_id, &
N_cat_domain, d2g, tile_coord, tmp_grid_def, tile_grid_d, rc )
if (rc==0) then ! read was successful
! minimal consistency check
if (.not. tile_grid_g==tmp_grid_def) then
err_msg = 'existing domain files inconsistent with ' // &
'global tile_grid_g from tile_coord_file'
call ldas_abort(LDAS_GENERIC_ERROR, Iam, err_msg)
end if
write (logunit,*) 'Domain successfully defined from existing files above.'
write (logunit,*)
else
print*, "Creating domain..., reading IncludeList and ExludeList if present..."
! ------------------------------------------------------------
!
! load ExcludeList: catchments listed in this file will *always* be excluded
fname = trim(exclude_path) // '/' // trim(exclude_file)
call read_exclude_or_includelist(N_cat_global, fname, ExcludeList, N_exclude)
! load IncludeList: catchments listed in this file will be included
! (unless excluded via ExcludeList)
fname = trim(include_path) // '/' // trim(include_file)
call read_exclude_or_includelist(N_cat_global, fname, IncludeList, N_include)
! -----------------
!
! find and count catchments that are in the domain
c3_grid = .false.
if(index(tile_grid_g%gridtype,"c3")/=0) c3_grid = .true.
indomain = 0 ! initialize
do n=1,N_cat_global
this_tileid = tile_coord_global(n)%tile_id
if( .not. c3_grid) then
this_minlon = tile_coord_global(n)%min_lon
this_minlat = tile_coord_global(n)%min_lat
this_maxlon = tile_coord_global(n)%max_lon
this_maxlat = tile_coord_global(n)%max_lat
else ! c3 grid can straddle the lat-lon
this_minlon = tile_coord_global(n)%com_lon
this_minlat = tile_coord_global(n)%com_lat
this_maxlon = tile_coord_global(n)%com_lon
this_maxlat = tile_coord_global(n)%com_lat
endif
this_cat_exclude = is_in_list( N_exclude, ExcludeList(1:N_exclude), this_tileid )
this_cat_include = is_in_list( N_include, IncludeList(1:N_include), this_tileid )
this_cat_in_box = &
is_cat_in_box(this_minlon,this_minlat,this_maxlon,this_maxlat, &
minlon, minlat, maxlon, maxlat )
if (is_in_domain( &
this_cat_exclude, this_cat_include, this_cat_in_box )) then
indomain = indomain + 1
tmp_d2g(indomain) = n
end if
end do
N_cat_domain = indomain
if (N_cat_domain .eq. 0) then
err_msg = 'No catchments found in domain'
call ldas_abort(LDAS_GENERIC_ERROR, Iam, err_msg)
else
if (logit) then
write (logunit,*) 'Number of catchments in domain = ', N_cat_domain
write (logunit,*)
end if
end if
! -------------------------------------------------------------------
!
! assemble d2g, tile_coord, tile_grid_d
allocate(d2g( N_cat_domain))
allocate(tile_coord(N_cat_domain))
d2g(1:N_cat_domain) = tmp_d2g(1:N_cat_domain)
tile_coord = tile_coord_global(d2g)
! finalize extent of actual domain:
! determine smallest subgrid of tile_grid_d that contains all
! catchments/tiles in domain
tile_grid_d = get_minExtent_grid( N_cat_domain, tile_coord%i_indg, tile_coord%j_indg, &
tile_coord%min_lon, tile_coord%min_lat, tile_coord%max_lon, tile_coord%max_lat, &
tile_grid_g)
! output domain files
tmp_grid_def = tile_grid_g ! cannot use intent(in) tile_grid_g w/ io_domain_files
call io_domain_files( 'w', work_path, exp_id, &
N_cat_domain, d2g, tile_coord, tmp_grid_def, tile_grid_d, rc )
end if ! domain/tilecoord/tilegrids files exist
! output extent of domain and tile_grid_d to logunit
if (logit) write (logunit,*) 'Actual extent of domain grid:'
if (logit) write (logunit,*) 'min lon = ', tile_grid_d%ll_lon
if (logit) write (logunit,*) 'max lon = ', tile_grid_d%ur_lon
if (logit) write (logunit,*) 'min lat = ', tile_grid_d%ll_lat
if (logit) write (logunit,*) 'max lat = ', tile_grid_d%ur_lat
if (logit) write (logunit,*)
tmpstring40 = 'tile_grid_d'
if (logit) call io_grid_def_type('w', logunit, tile_grid_d, tmpstring40)
print*, "Done with " // trim(Iam)
end subroutine domain_setup
! *************************************************************************
subroutine read_exclude_or_includelist(N_cat, fname, MyList, N_list)
! read numbers/IDs of catchments in MyList (ExcludeList or IncludeList)
!
! format of MyList file: ASCII list of tile IDs
!
! N_list = number of catchments in MyList
!
! reichle, 2 May 2003
!
! --------------------------------------------------------------
implicit none
! N_cat = max number of catchments allowed in list
! (use N_cat_global when calling this subroutine)
integer, intent(in) :: N_cat
character(*), intent(in) :: fname
integer, intent(out) :: N_list
integer, dimension(N_cat), intent(out) :: MyList
! locals
integer :: istat, tmpint
logical :: file_exists
character(len=*), parameter :: Iam = 'read_exclude_or_includelist'
character(len=400) :: err_msg
! -----------------------------------------------------------
N_list = 0
inquire( file=fname, exist=file_exists)
if (file_exists) then
open(10, file=fname, form='formatted', action='read', &
status='old', iostat=istat)
if (istat==0) then
if (logit) write (logunit,*) &
'reading ExcludeList or IncludeList from ', trim(fname)
if (logit) write (logunit,*)
do
read(10,*,iostat=istat) tmpint
if (istat==-1) then
if (logit) write (logunit,*) ' found ', N_list, ' catchments on list'
exit
else if (istat/=0) then
err_msg = 'read error other than end-of-file'
call ldas_abort(LDAS_GENERIC_ERROR, Iam, err_msg)
else
N_list = N_list+1
MyList(N_list) = tmpint
end if
if (N_list>N_cat) then
write (tmpstring10,*) N_cat
write (tmpstring40,*) N_list
err_msg = 'N_list=' // trim(tmpstring40) &
// ' > N_cat=' // trim(tmpstring10)
call ldas_abort(LDAS_GENERIC_ERROR, Iam, err_msg)
end if
end do
close(10,status='keep')
else
if (logit) write (logunit,*) &
'could not open ExcludeList or IncludeList file ', trim(fname)
end if
else
if (logit) write (logunit,*) &
'ExcludeList or IncludeList file does not exist: ', trim(fname)
end if
if (logit) write (logunit,*)
end subroutine read_exclude_or_includelist
! ****************************************************************
integer function word_count( mystring )
! count number of words in "mystring" (delimited by space)
!
! - reichle, 31 Mar 2015
implicit none
character(len=*) :: mystring
integer :: N_words, N_string, ii
logical :: current_is_space, next_is_space
N_words = 0
current_is_space = (mystring(1:1)==' ')
if (.not. current_is_space) N_words = N_words + 1
do ii=2,len(mystring)
next_is_space = (mystring(ii:ii)==' ')
if (current_is_space .and. .not. next_is_space) N_words = N_words + 1
current_is_space = next_is_space
end do
word_count = N_words
end function word_count
! ***********************************************************************
integer function open_land_param_file( unitnumber, formatted_file, is_big_endian, &
N_search_dir, fname, pathname, search_dir, ignore_stop )
! reichle, 13 Dec 2010
! reichle, 21 Oct 2011 - added optional output "istat"
! reichle, 11 Dec 2013 - moved from "clsm_ensdrv_drv_routines.F90"
! and converted to function
! try reading land or mwRTM parameter files from various sub-dirs for
! compatibility with old and new parameter directory structures
! fname = file name (without path) of parameter file
! pathname = path to parameter file
! search_dir = vector (length N_search_dir) of subdirectories to search
! for file fname
! ignore_stop = optional input, if present and .true., skip call to "stop_it()"
implicit none
integer :: unitnumber, N_search_dir
logical :: formatted_file
logical :: is_big_endian
character(*) :: fname
character(*) :: pathname
character(*), dimension(:) :: search_dir
logical, optional :: ignore_stop
! local variables
character(len=512) :: filename
integer :: i, istat
logical :: ignore_stop_tmp
character(len=*), parameter :: Iam = 'open_land_param_file'
character(len=400) :: err_msg
! ------------------------------------------------------------------
!
! try opening file
do i=1,N_search_dir
filename = trim(pathname) // '/' // trim(search_dir(i)) // '/' // trim(fname)
if (formatted_file) then
open(unitnumber, file=filename, form='formatted', &
action='read', status='old', iostat=istat)
else
if (is_big_endian) then
open(unitnumber, file=filename, form='unformatted', &
convert='big_endian', &
action='read', status='old', iostat=istat)
else
open(unitnumber, file=filename, form='unformatted', &
convert='little_endian', &
action='read', status='old', iostat=istat)
end if
end if
if (istat==0) exit ! exit loop when first successful
end do
! report back opened filename or stop (unless requested otherwise)
if (istat==0) then
if (logit) write (logunit,'(400A)') 'Reading from: ' // trim(filename)
else
if (logit) then
write (logunit,*) 'Cannot find file ', trim(fname), ' in: '
do i=1,N_search_dir
write (logunit,*) trim(pathname) // '/' // trim(search_dir(i))
end do
end if
! figure out whether to stop
ignore_stop_tmp = .false. ! default: stop if file not opened successfully
if (present(ignore_stop)) ignore_stop_tmp = ignore_stop
if (.not. ignore_stop_tmp) then
err_msg = 'ERROR opening file'
call ldas_abort(LDAS_GENERIC_ERROR, Iam, err_msg)
end if
end if
if (logit) write (logunit,*)
open_land_param_file = istat
end function open_land_param_file
! *****************************************************************************************
subroutine read_cat_param( &
N_catg, N_catf, f2g, tile_coord_f, dzsf, veg_path, soil_path, top_path, &
cp )
! Reads soil properties and topographic parameters from global files
! and extracts data for the (full) domain.
!
! Additional parameters are derived from the ones that have been read
! from files.
!
! cp = cat_param_f
!
! reichle, 12 May 2003
! reichle, 6 Jun 2005 - adapted to read "SiB2_V2" parameters
! reichle, 5 Apr 2013 - removed alb_param_type fields "sc_albvr", "sc_albnr"
! reichle, 25 Jul 2013 - removed LAI, GRN, and albedo inputs, renamed subroutine
! from "read_land_parameters()" to "read_cat_param()"
! reichle, 16 Nov 2015 - read static (JPL) veg height from boundary condition file
! reichle, 14 Jul 2020 - work around for new "peat fraction" column in Icarus-NLv4 (ignore for now)
!
! -------------------------------------------------------------------
implicit none
integer, intent(in) :: N_catg, N_catf
type(tile_coord_type), dimension(:), pointer :: tile_coord_f ! intent(in)
real, intent(in) :: dzsf
integer, dimension(N_catf), intent(in) :: f2g
character(*), intent(in) :: veg_path
character(*), intent(in) :: soil_path
character(*), intent(in) :: top_path
type(cat_param_type), dimension(N_catf), intent(out) :: cp
! local variables
integer, parameter :: N_search_dir_max = 5
integer, parameter :: N_col_real_max = 18 ! "v15" soil_param.dat had 22 columns (incl. first 4 columns with integers)
character( 80) :: fname
character(999) :: tmpstr999
character(100), dimension(N_search_dir_max) :: search_dir
integer :: n, k, m, dummy_int, dummy_int2, istat, N_search_dir, N_col
integer, dimension(N_catg) :: tmpint, tmpint2, tmptileid
real, dimension(N_catg,N_col_real_max) :: tmpreal
real :: dummy_real, dummy_real2, z_in_m, term1, term2
logical :: dummy_logical
character(len=*), parameter :: Iam = 'read_cat_param'
character(len=400) :: err_msg
real, dimension(NTYPS) :: VGZ2
! legacy vegetation height look-up table (for backward compatibility)
!
DATA VGZ2 /35.0, 20.0, 17.0, 0.6, 0.5, 0.6/ ! Dorman and Sellers (1989)
! ---------------------------------------------------------------------
if (logit) write (logunit,*) 'reading Catchment model parameters'
if (logit) write (logunit,*)
! -----------------------------
! Vegetation class
if (logit) write (logunit,*) 'Reading vegetation class and, if available, height'
fname = '/mosaic_veg_typs_fracs'
N_search_dir = 2 ! specify sub-dirs of veg_path to search for file "fname"
search_dir(1) = 'clsm'
search_dir(2) = 'VEGETATION-GSWP2'
! find out how many columns are in the (formatted) file
istat = open_land_param_file( &
10, .true., dummy_logical, N_search_dir, fname, trim(veg_path), search_dir)
read(10,'(a)') tmpstr999 ! read first line
close(10, status='keep')
! count words in first line (delimited by space)
N_col = word_count( tmpstr999 )
! read parameters
istat = open_land_param_file( &
10, .true., dummy_logical, N_search_dir, fname, trim(veg_path), search_dir)
tmptileid = 0
tmpreal = nodata_generic
select case (N_col)
case (6)
! legacy vegetation height from look-up table
if (logit) write (logunit,*) 'Using vegetation height look-up table'
do n=1,N_catg
read (10,*) tmptileid(n), dummy_int, tmpint(n)
end do
if ( (any(tmpint<1)) .or. (any(tmpint>NTYPS)) ) then
err_msg = 'veg type (class) exceeds allowed min/max'
call ldas_abort(LDAS_GENERIC_ERROR, Iam, err_msg)
end if
do n=1,N_catg
tmpreal(n,1) = VGZ2( tmpint(n) )
end do
case (7,8)
! vegetation height from boundary condition file
if (logit) write (logunit,*) 'reading vegetation height from file'
do n=1,N_catg
! 7-th column contains veg height in m
! 8-th column contains ASCAT z0 values (IGNORED for now, reichle, 31 Oct 2017)
read (10,*) tmptileid(n), dummy_int, tmpint(n), &
dummy_int2, dummy_real, dummy_real2, tmpreal(n,1)
end do
case default
err_msg = 'unknown number of columns in ' // trim(fname)
call ldas_abort(LDAS_GENERIC_ERROR, Iam, err_msg)
end select
close (10,status='keep')
if (logit) write (logunit,*) 'done reading'
if (logit) write (logunit,*)
do k=1,N_catf
! this check works only for "SiB2_V2" and newer versions
if (tile_coord_f(k)%tile_id/=tmptileid(f2g(k))) then
err_msg = 'something wrong with veg parameters'
call ldas_abort(LDAS_GENERIC_ERROR, Iam, err_msg)
end if
cp(k)%vegcls = tmpint( f2g(k) )
cp(k)%veghght = tmpreal(f2g(k),1)