-
Notifications
You must be signed in to change notification settings - Fork 54
/
module_hrldas_netcdf_io.F
4030 lines (3326 loc) · 163 KB
/
module_hrldas_netcdf_io.F
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 module_hrldas_netcdf_io
use module_date_utilities
use netcdf
#ifdef MPP_LAND
use module_mpp_land, only:mpp_land_bcast_int1, decompose_data_real, mpp_land_bcast_real1, decompose_data_int, &
io_id, global_nx, global_ny, my_id, write_io_real, write_io_int, write_io_real3d,decompose_data_real3d, &
mpp_land_sync,mpp_land_bcast_char,mpp_land_bcast
#endif
#ifdef _PARALLEL_
use mpi
#endif
implicit none
logical, parameter :: FATAL = .TRUE.
logical, parameter :: NOT_FATAL = .FALSE.
type inputstruct
character(len=19) :: read_date
real, pointer, dimension(:,:) :: t
real, pointer, dimension(:,:) :: q
real, pointer, dimension(:,:) :: u
real, pointer, dimension(:,:) :: v
real, pointer, dimension(:,:) :: p
real, pointer, dimension(:,:) :: lw
real, pointer, dimension(:,:) :: sw
real, pointer, dimension(:,:) :: pcp
real, pointer, dimension(:,:) :: snow
real, pointer, dimension(:,:) :: vegfra
real, pointer, dimension(:,:) :: lai
end type inputstruct
character(len=256), private :: restart_filename_remember
integer, private :: iswater_remember
integer, private :: xstartpar_remember
integer, private, allocatable, dimension(:,:) :: vegtyp_remember
integer, private :: ncid_remember
integer, private :: output_count_remember = 0
logical, private :: define_mode_remember
integer, private :: dimid_ix_remember
integer, private :: dimid_jx_remember
integer, private :: dimid_times_remember
integer, private :: dimid_layers_remember
integer, private :: dimid_snow_layers_remember
interface prepare_output_file
#ifdef MPP_LAND
module procedure prepare_output_file_mpp
#else
module procedure prepare_output_file_seq
#endif
end interface
interface prepare_restart_file
#ifdef MPP_LAND
module procedure prepare_restart_file_mpp
#else
module procedure prepare_restart_file_seq
#endif
end interface
interface add_to_restart
#ifdef MPP_LAND
module procedure add_to_restart_2d_float_mpp, add_to_restart_2d_integer_mpp, add_to_restart_3d_mpp
#else
module procedure add_to_restart_2d_float, add_to_restart_2d_integer, add_to_restart_3d
#endif
end interface
interface get_from_restart
#ifdef MPP_LAND
module procedure get_from_restart_2d_float_mpp, get_from_restart_2d_integer_mpp, get_from_restart_3d_mpp, &
get_from_restart_att
#else
module procedure get_from_restart_2d_float, get_from_restart_2d_integer, get_from_restart_3d, get_from_restart_att
#endif
end interface
interface add_to_output
#ifdef MPP_LAND
module procedure add_to_output_2d_float_mpp, add_to_output_2d_integer_mpp, add_to_output_3d_mpp
#else
module procedure add_to_output_2d_float, add_to_output_2d_integer, add_to_output_3d
#endif
end interface
contains
!-------------------------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------------------------
subroutine check_outdir(rank, outdir)
implicit none
! Check that output directory OUTDIR exists and is writable, by
! trying to open a test file in that directory. Include a random
! number in the test file name, to greatly reduce the chance of
! collision with existing file names. This assumes that the
! intrinsic random_seed routine, called without argument, will seed
! the random number generator based on something like system time
! or hardware noise.
integer, intent(in) :: rank
character(len=*), intent(in) :: outdir
real :: xrand
character(len=256) :: testfile
integer :: ierr
if (rank == 0) then
call random_seed()
call random_number(xrand)
write(testfile, '(A,"/scratch.",I4.4,".",I6.6,".scratch")') trim(outdir), rank, int(xrand*1.E6)
open(unit=30, file=trim(testfile), status='unknown', iostat=ierr)
if (ierr /= 0) then
write(*, '(/)')
write(*, '(" ***** Namelist error: ******************************************************")')
write(*, '(" ***** ")')
write(*, '(" ***** We cannot write a file to the directory specified in namelist option OUTDIR.")')
write(*, '(" ***** Check namelist option OUTDIR (currently set to ''", A, "'')")') trim(outdir)
write(*, '(" ***** Check that the directory exists, is a directory, and is writable.")')
write(*, '(/)')
stop "OUTDIR Problem"
endif
close(unit=30, iostat=ierr, status='delete')
if (ierr /= 0) then
print*, "TESTFILE = " // trim(testfile) // '"'
stop "Much confusion. Problem closing test file."
endif
endif
end subroutine check_outdir
!-------------------------------------------------------------------------------------------------------
!-------------------------------------------------------------------------------------------------------
subroutine find_restart_file(rank, restart_filename_requested, startdate, khour, olddate, restart_flnm)
implicit none
!
! If the user has requested the latest restart file, find the latest restart file.
! If the user has requested a specific restart file, check that the file exists.
!
! Return the restart file name in string RESTART_FLNM.
! Update the OLDDATE string (in case the latest restart file was requested).
!
integer, intent(in) :: rank
character(len=*), intent(in) :: restart_filename_requested
character(len=19), intent(in) :: startdate
integer, intent(in) :: khour
character(len=19), intent(in) :: olddate
character(len=256), intent(out) :: restart_flnm
character(len=19) :: locdate
integer :: ribeg
integer :: riend
logical :: lexist
#ifdef MPP_LAND
if(my_id .ne. IO_id) return
#endif
ribeg = index(restart_filename_requested, "<LATEST>")-1
if ( ribeg > 0 ) then
riend = ribeg + 9
! Find the latest RESTART file
call geth_newdate(locdate(1:13), olddate(1:13), khour+24)
RLOOP : do
restart_flnm = restart_filename_requested(1:ribeg) // &
locdate(1:4)//locdate(6:7)//locdate(9:10)//locdate(12:13) // &
restart_filename_requested(riend:)
inquire (file=trim(restart_flnm), exist=lexist)
if ( .not. lexist ) then
call geth_newdate(locdate(1:13), locdate(1:13), -1)
if (locdate(1:13) < startdate(1:13) ) then
write(*, *)
write(*, '(" ***** RESTART error: **************************************************")')
write(*, '(" ***** ")')
write(*, '(" ***** You have requested to restart from the latest restart file,")')
write(*, '(" ***** but no restart file was found.")')
write(*, '(" ***** ")')
write(*, *)
stop " ***** ERROR EXIT: Cannot find a restart file"
endif
else
exit RLOOP
endif
enddo RLOOP
else
restart_flnm = restart_filename_requested
inquire (file=trim(restart_flnm), exist=lexist)
if ( .not. lexist ) then
write(*, *)
write(*, '(" ***** RESTART error: **************************************************")')
write(*, '(" ***** ")')
write(*, '(" ***** You have requested to restart from a file that cannot be found.")')
write(*, '(" ***** Specified restart file = ''", A, "''")') trim(restart_flnm)
write(*, '(" ***** ")')
write(*, *)
stop " ***** ERROR EXIT: Cannot find restart file"
endif
endif
if (rank == 0) then
write(*, '("Found restart file: ''", A, "''")') trim(restart_flnm)
endif
end subroutine find_restart_file
!---------------------------------------------------------------------------------------------------------
!---------------------------------------------------------------------------------------------------------
subroutine read_dim(wrfinput_flnm, ix, jx)
implicit none
character(len=*), intent(in) :: wrfinput_flnm
integer, intent(out) :: ix, jx ! dimensions
integer :: ncid, dimid, ierr
ierr = nf90_open(wrfinput_flnm, NF90_NOWRITE, ncid)
ierr = nf90_inq_dimid(ncid, "west_east", dimid)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding dimension 'west_east'")
ierr = nf90_inquire_dimension(ncid, dimid, len=ix)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding dimension length for 'west_east'")
ierr = nf90_inq_dimid(ncid, "south_north", dimid)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding dimension 'south_north'")
ierr = nf90_inquire_dimension(ncid, dimid, len=jx)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding dimension length for 'south_north'")
end subroutine read_dim
subroutine read_hrldas_hdrinfo(wrfinput_flnm, ix, jx, &
xstart, xend, ystart, yend, &
iswater, islake, isurban, isice, llanduse, dx, dy, truelat1, truelat2, cen_lon, lat1, lon1, &
#ifdef _PARALLEL_
igrid, mapproj, dum2d_ptr)
#else
igrid, mapproj)
#endif
! Return the dimensions of the grid and some map information.
implicit none
character(len=*), intent(in) :: wrfinput_flnm
integer, intent(out) :: mapproj
integer, intent(out) :: igrid
integer, intent(out) :: ix, jx ! dimensions
integer, intent(in) :: xstart, ystart ! Subwindow definition
#ifdef MPP_LAND
integer, intent(in) :: xend, yend ! Subwindow definition
#else
integer, intent(inout) :: xend, yend ! Subwindow definition
#endif
integer, intent(out) :: iswater ! vegetation category corresponding to water bodies
integer, intent(out) :: islake ! vegetation category corresponding to lakes
integer, intent(out) :: isurban ! vegetation category corresponding to urban areas
integer, intent(out) :: isice ! vegetation category corresponding to ice areas
character(len=256), intent(out) :: llanduse ! Landuse dataset (USGS or MODI)
real, intent(out) :: dx
real, intent(out) :: dy
real, intent(out) :: truelat1
real, intent(out) :: truelat2
real, intent(out) :: cen_lon
real, intent(out) :: lat1
real, intent(out) :: lon1
#ifdef _PARALLEL_
real, pointer, dimension(:,:) :: dum2d_ptr
#endif
integer :: ncid, dimid, varid, ierr
real, allocatable, dimension(:,:) :: dum2d
character(len=256) :: units
integer :: i
integer :: rank
#ifdef _PARALLEL_
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
if (ierr /= MPI_SUCCESS) stop "MPI_COMM_RANK"
#else
rank = 0
#endif
! Open the NetCDF file.
if (rank == 0) write(*,'("wrfinput_flnm: ''", A, "''")') trim(wrfinput_flnm)
!KWM#ifdef _PARALLEL_
!KWM ierr = nf90_open_par(wrfinput_flnm, NF90_NOWRITE, MPI_COMM_WORLD, MPI_INFO_NULL, ncid)
!KWM#else
ierr = nf90_open(wrfinput_flnm, NF90_NOWRITE, ncid)
!KWM#endif
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problem opening wrfinput file: "//trim(wrfinput_flnm))
ierr = nf90_inq_dimid(ncid, "west_east", dimid)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding dimension 'west_east'")
ierr = nf90_inquire_dimension(ncid, dimid, len=ix)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding dimension length for 'west_east'")
ierr = nf90_inq_dimid(ncid, "south_north", dimid)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding dimension 'south_north'")
ierr = nf90_inquire_dimension(ncid, dimid, len=jx)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding dimension length for 'south_north'")
ierr = nf90_get_att(ncid, NF90_GLOBAL, "DX", dx)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding global attribute 'DX'")
ierr = nf90_get_att(ncid, NF90_GLOBAL, "DY", dy)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding global attribute 'DY'")
ierr = nf90_get_att(ncid, NF90_GLOBAL, "TRUELAT1", truelat1)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding global attribute 'TRUELAT1'")
ierr = nf90_get_att(ncid, NF90_GLOBAL, "TRUELAT2", truelat2)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding global attribute 'TRUELAT2'")
ierr = nf90_get_att(ncid, NF90_GLOBAL, "STAND_LON", cen_lon)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding global attribute 'STAND_LON'")
ierr = nf90_get_att(ncid, NF90_GLOBAL, "MAP_PROJ", mapproj)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding global attribute 'MAP_PROJ'")
ierr = nf90_get_att(ncid, NF90_GLOBAL, "GRID_ID", igrid)
if (ierr /= 0) then
ierr = nf90_get_att(ncid, NF90_GLOBAL, "grid_id", igrid)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding global attribute 'GRID_ID' or 'grid_id'")
endif
ierr = nf90_get_att(ncid, NF90_GLOBAL, "ISWATER", iswater)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding global attribute 'ISWATER'")
ierr = nf90_get_att(ncid, NF90_GLOBAL, "ISLAKE", islake)
if(ierr /= 0) then
write(*,*) "Problems finding global attribute: ISLAKE; setting to -1"
islake = -1
end if
ierr = nf90_get_att(ncid, NF90_GLOBAL, "ISURBAN", isurban)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding global attribute 'ISURBAN'")
ierr = nf90_get_att(ncid, NF90_GLOBAL, "ISICE", isice)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding global attribute 'ISICE'")
ierr = nf90_get_att(ncid, NF90_GLOBAL, "MMINLU", llanduse)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems finding global attribute 'MMINLU'")
! IBM XLF seems to need something like this:
do i = 1, 256
if (ichar(llanduse(i:i)) == 0) llanduse(i:i) = " "
enddo
#ifndef MPP_LAND
if (xend == 0) then
xend = ix
endif
if (yend == 0) then
yend = jx
endif
#endif
!
! This section is for reading the information from the wrfinput file
!
! We only need to read the one starting point.
#ifdef MPP_LAND
allocate(dum2d(xstart:xend,ystart:yend))
call get_2d_netcdf("XLAT", ncid, dum2d, units, xstart, xend , ystart, yend , FATAL, ierr)
#else
allocate(dum2d(xstart:xstart,ystart:ystart))
call get_2d_netcdf("XLAT", ncid, dum2d, units, xstart, xstart, ystart, ystart, FATAL, ierr)
#endif
lat1 = dum2d(xstart,ystart)
#ifdef MPP_LAND
call get_2d_netcdf("XLONG", ncid, dum2d, units, xstart, xend , ystart, yend , FATAL, ierr)
#else
call get_2d_netcdf("XLONG", ncid, dum2d, units, xstart, xstart, ystart, ystart, FATAL, ierr)
#endif
lon1 = dum2d(xstart,ystart)
deallocate (dum2d)
#ifdef MPP_LAND
call mpp_land_bcast_real1(lon1)
call mpp_land_bcast_real1(lat1)
#endif
#ifdef _PARALLEL_
allocate(dum2d_ptr(xstart:xend,ystart:yend))
call get_2d_netcdf("IVGTYP", ncid, dum2d_ptr, units, xstart, xend, ystart, yend, FATAL, ierr)
#endif
ierr = nf90_close(ncid)
call error_handler(ierr, failure="READ_HRLDAS_HDRINFO: Problems closing NetCDF file.")
end subroutine read_hrldas_hdrinfo
!---------------------------------------------------------------------------------------------------------
!---------------------------------------------------------------------------------------------------------
subroutine readland_hrldas(wrfinput_flnm, &
xstart, xend, &
ystart, yend, &
iswater, islake, vegtyp, soltyp, terrain, tbot_2d, latitude, longitude,xland,seaice,msftx,msfty)
implicit none
character(len=*), intent(in) :: wrfinput_flnm
integer, intent(in) :: xstart, xend, ystart, yend
integer, intent(in) :: iswater
integer, intent(in) :: islake
integer, dimension(xstart:xend,ystart:yend), intent(out) :: vegtyp, soltyp
real, dimension(xstart:xend,ystart:yend), intent(out) :: terrain
real, dimension(xstart:xend,ystart:yend), intent(out) :: tbot_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: latitude
real, dimension(xstart:xend,ystart:yend), intent(out) :: longitude
real, dimension(xstart:xend,ystart:yend), intent(out) :: xland
real, dimension(xstart:xend,ystart:yend), intent(out) :: seaice
real, dimension(xstart:xend,ystart:yend), intent(out) :: msftx
real, dimension(xstart:xend,ystart:yend), intent(out) :: msfty
character(len=256) :: units
integer :: ierr
integer :: ncid
real, dimension(xstart:xend,ystart:yend) :: xdum
integer :: rank
#ifdef _PARALLEL_
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
if (ierr /= MPI_SUCCESS) stop "MPI_COMM_RANK"
#else
rank = 0
#endif
! Open the NetCDF file.
if (rank == 0) write(*,'("wrfinput_flnm: ''", A, "''")') trim(wrfinput_flnm)
#ifdef _PARALLEL_
ierr = nf90_open_par(wrfinput_flnm, NF90_NOWRITE, MPI_COMM_WORLD, MPI_INFO_NULL, ncid)
#else
ierr = nf90_open(wrfinput_flnm, NF90_NOWRITE, ncid)
#endif
if (ierr /= 0) then
write(*,'("READLAND_HRLDAS: Problem opening wrfinput file: ''", A, "''")') trim(wrfinput_flnm)
#ifdef _PARALLEL_
call mpi_finalize(ierr)
if (ierr /= 0) write(*, '("Problem with MPI_finalize.")')
#endif
stop
endif
! Get Latitude (lat)
call get_2d_netcdf("XLAT", ncid, latitude, units, xstart, xend, ystart, yend, FATAL, ierr)
! print*, 'latitude(xstart,ystart) = ', latitude(xstart,ystart)
! Get Longitude (lon)
call get_2d_netcdf("XLONG", ncid, longitude, units, xstart, xend, ystart, yend, FATAL, ierr)
! print*, 'longitude(xstart,ystart) = ', longitude(xstart,ystart)
! Get land mask (xland)
call get_2d_netcdf("XLAND", ncid, xland, units, xstart, xend, ystart, yend, NOT_FATAL, ierr)
! print*, 'xland(xstart,ystart) = ', xland(xstart,ystart)
! Get seaice (seaice)
call get_2d_netcdf("SEAICE", ncid, seaice, units, xstart, xend, ystart, yend, NOT_FATAL, ierr)
! print*, 'seaice(xstart,ystart) = ', seaice(xstart,ystart)
! Get Terrain (avg)
call get_2d_netcdf("HGT", ncid, terrain, units, xstart, xend, ystart, yend, FATAL, ierr)
! print*, 'terrain(xstart,ystart) = ', terrain(xstart,ystart)
! Get Deep layer temperature (TMN)
call get_2d_netcdf("TMN", ncid, tbot_2d, units, xstart, xend, ystart, yend, FATAL, ierr)
! print*, 'terrain(xstart,ystart) = ', terrain(xstart,ystart)
! Get Map Factors (MAPFAC_MX)
call get_2d_netcdf("MAPFAC_MX", ncid, msftx, units, xstart, xend, ystart, yend, NOT_FATAL, ierr)
! print*, 'msftx(xstart,ystart) = ', msftx(xstart,ystart)
if (ierr /= 0) print*, 'Did not find MAPFAC_MX, only needed for iopt_run=5'
! Get Map Factors (MAPFAC_MY)
call get_2d_netcdf("MAPFAC_MY", ncid, msfty, units, xstart, xend, ystart, yend, NOT_FATAL, ierr)
! print*, 'msfty(xstart,ystart) = ', msfty(xstart,ystart)
if (ierr /= 0) print*, 'Did not find MAPFAC_MY, only needed for iopt_run=5'
! Get Dominant Land Use categories (use)
call get_landuse_netcdf(ncid, xdum , units, xstart, xend, ystart, yend)
vegtyp = nint(xdum)
! print*, 'vegtyp(xstart,ystart) = ', vegtyp(xstart,ystart)
! Get Dominant Soil Type categories in the top layer (stl)
call get_soilcat_netcdf(ncid, xdum , units, xstart, xend, ystart, yend)
soltyp = nint(xdum)
! print*, 'soltyp(xstart,ystart) = ', soltyp(xstart,ystart)
! Close the NetCDF file
ierr = nf90_close(ncid)
if (ierr /= 0) stop "MODULE_NOAHLSM_HRLDAS_INPUT: READLAND_HRLDAS: NF90_CLOSE"
! Make sure vegtyp and soltyp are consistent when it comes to water points,
! by setting soil category to water when vegetation category is water, and
! vice-versa.
!where (vegtyp == ISWATER .or. vegtyp == islake) soltyp = 14
!where (soltyp == 14) vegtyp = ISWATER
end subroutine readland_hrldas
!---------------------------------------------------------------------------------------------------------
subroutine read_mmf_runoff(wrfinput_flnm, &
xstart, xend, &
ystart, yend, &
fdepth,eqzwt,rechclim,riverbed)
implicit none
character(len=*), intent(in) :: wrfinput_flnm
integer, intent(in) :: xstart, xend, ystart, yend
real, dimension(xstart:xend,ystart:yend), intent(out) :: fdepth
real, dimension(xstart:xend,ystart:yend), intent(out) :: eqzwt
real, dimension(xstart:xend,ystart:yend), intent(out) :: rechclim
real, dimension(xstart:xend,ystart:yend), intent(out) :: riverbed
character(len=256) :: units
integer :: ierr
integer :: ncid
real, dimension(xstart:xend,ystart:yend) :: xdum
integer :: rank
#ifdef _PARALLEL_
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
if (ierr /= MPI_SUCCESS) stop "MPI_COMM_RANK"
#else
rank = 0
#endif
! Open the NetCDF file.
if (rank == 0) write(*,'("wrfinput_flnm: ''", A, "''")') trim(wrfinput_flnm)
#ifdef _PARALLEL_
ierr = nf90_open_par(wrfinput_flnm, NF90_NOWRITE, MPI_COMM_WORLD, MPI_INFO_NULL, ncid)
#else
ierr = nf90_open(wrfinput_flnm, NF90_NOWRITE, ncid)
#endif
if (ierr /= 0) then
write(*,'("read_mmf_runoff: Problem opening wrfinput file: ''", A, "''")') trim(wrfinput_flnm)
#ifdef _PARALLEL_
call mpi_finalize(ierr)
if (ierr /= 0) write(*, '("Problem with MPI_finalize.")')
#endif
stop
endif
! Get equilibrium water table depth (FDEPTH)
call get_2d_netcdf("FDEPTH", ncid, fdepth, units, xstart, xend, ystart, yend, FATAL, ierr)
! Get equilibrium water table depth (EQZWT)
call get_2d_netcdf("EQZWT", ncid, eqzwt, units, xstart, xend, ystart, yend, FATAL, ierr)
! Get water table depth (RECHCLIM)
call get_2d_netcdf("RECHCLIM", ncid, rechclim, units, xstart, xend, ystart, yend, FATAL, ierr)
! Get equilibrium water table depth (RIVERBED)
call get_2d_netcdf("RIVERBED", ncid, riverbed, units, xstart, xend, ystart, yend, FATAL, ierr)
! Close the NetCDF file
ierr = nf90_close(ncid)
if (ierr /= 0) stop "MODULE_HRLDAS_NETCDF_IO: READ_MMF_RUNOFF: NF90_CLOSE"
end subroutine read_mmf_runoff
!---------------------------------------------------------------------------------------------------------
subroutine read_agriculture_data(agdata_flnm, &
xstart, xend, &
ystart, yend, &
irfract, &
sifract, &
mifract, &
fifract)
implicit none
character(len=*), intent(in) :: agdata_flnm
integer, intent(in) :: xstart, xend, ystart, yend
real, dimension(xstart:xend,ystart:yend), intent(out) :: irfract
real, dimension(xstart:xend,ystart:yend), intent(out) :: sifract
real, dimension(xstart:xend,ystart:yend), intent(out) :: mifract
real, dimension(xstart:xend,ystart:yend), intent(out) :: fifract
character(len=24) :: name
integer :: ierr,iret
integer :: ncid, varid
integer :: rank
#ifdef _PARALLEL_
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
if (ierr /= MPI_SUCCESS) stop "MPI_COMM_RANK"
#else
rank = 0
#endif
! Open the NetCDF file.
if (rank == 0) write(*,'("agdata_flnm: ''", A, "''")') trim(agdata_flnm)
#ifdef _PARALLEL_
ierr = nf90_open_par(agdata_flnm, NF90_NOWRITE, MPI_COMM_WORLD, MPI_INFO_NULL, ncid)
#else
ierr = nf90_open(agdata_flnm, NF90_NOWRITE, ncid)
#endif
if (ierr /= 0) then
write(*,'("read_agriculture data: Problem opening agdata file: ''", A, "''")') trim(agdata_flnm)
#ifdef _PARALLEL_
call mpi_finalize(ierr)
if (ierr /= 0) write(*, '("Problem with MPI_finalize.")')
#endif
stop
endif
! Get irrigation fraction
name = "IRFRACT"
iret = nf90_inq_varid(ncid, name, varid)
if (iret == 0) then
ierr = nf90_get_var(ncid, varid, irfract, start=(/xstart,ystart/), count=(/xend-xstart+1,yend-ystart+1/))
else
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file. Using default values."
endif
! Get sprinkler irrigation fraction
name = "SIFRACT"
iret = nf90_inq_varid(ncid, name, varid)
if (iret == 0) then
ierr = nf90_get_var(ncid, varid, sifract, start=(/xstart,ystart/), count=(/xend-xstart+1,yend-ystart+1/))
else
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file. Using default values."
endif
! Get sprinkler irrigation fraction
name = "MIFRACT"
iret = nf90_inq_varid(ncid, name, varid)
if (iret == 0) then
ierr = nf90_get_var(ncid, varid, mifract, start=(/xstart,ystart/), count=(/xend-xstart+1,yend-ystart+1/))
else
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file. Using default values."
endif
! Get sprinkler irrigation fraction
name = "FIFRACT"
iret = nf90_inq_varid(ncid, name, varid)
if (iret == 0) then
ierr = nf90_get_var(ncid, varid, fifract, start=(/xstart,ystart/), count=(/xend-xstart+1,yend-ystart+1/))
else
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file. Using default values."
endif
! Close the NetCDF file
ierr = nf90_close(ncid)
if (ierr /= 0) stop "MODULE_NOAHLSM_HRLDAS_INPUT: read_agriculture_data: NF90_CLOSE"
end subroutine read_agriculture_data
!---------------------------------------------------------------------------------------------------------
subroutine read_crop_input(wrfinput_flnm, &
xstart, xend, &
ystart, yend, &
croptype,planting,harvest,season_gdd)
implicit none
character(len=*), intent(in) :: wrfinput_flnm
integer, intent(in) :: xstart, xend, ystart, yend
real, dimension(xstart:xend,5,ystart:yend), intent(out) :: croptype
real, dimension(xstart:xend, ystart:yend), intent(out) :: planting
real, dimension(xstart:xend, ystart:yend), intent(out) :: harvest
real, dimension(xstart:xend, ystart:yend), intent(out) :: season_gdd
character(len=256) :: units
character(len=24) :: name
integer :: ierr,iret
integer :: ncid, varid, icrop
real, dimension(xstart:xend,ystart:yend,5) :: xdum
integer :: rank
#ifdef _PARALLEL_
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
if (ierr /= MPI_SUCCESS) stop "MPI_COMM_RANK"
#else
rank = 0
#endif
! Open the NetCDF file.
if (rank == 0) write(*,'("wrfinput_flnm: ''", A, "''")') trim(wrfinput_flnm)
#ifdef _PARALLEL_
ierr = nf90_open_par(wrfinput_flnm, NF90_NOWRITE, MPI_COMM_WORLD, MPI_INFO_NULL, ncid)
#else
ierr = nf90_open(wrfinput_flnm, NF90_NOWRITE, ncid)
#endif
if (ierr /= 0) then
write(*,'("read_mmf_runoff: Problem opening wrfinput file: ''", A, "''")') trim(wrfinput_flnm)
#ifdef _PARALLEL_
call mpi_finalize(ierr)
if (ierr /= 0) write(*, '("Problem with MPI_finalize.")')
#endif
stop
endif
! Get crop type data (CROPTYPE)
name = "CROPTYPE"
iret = nf90_inq_varid(ncid, name, varid)
if (iret == 0) then
ierr = nf90_get_var(ncid, varid, xdum, start=(/xstart,ystart,1/), count=(/xend-xstart+1,yend-ystart+1,5/))
do icrop = 1,5
croptype(:,icrop,:) = xdum(:,:,icrop)
end do
else
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file. Using default values."
endif
! Get planting date (PLANTING)
name = "PLANTING"
iret = nf90_inq_varid(ncid, name, varid)
if (iret == 0) then
ierr = nf90_get_var(ncid, varid, planting, start=(/xstart,ystart/), count=(/xend-xstart+1,yend-ystart+1/))
else
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file. Using default values."
endif
! Get harvest date (HARVEST)
name = "HARVEST"
iret = nf90_inq_varid(ncid, name, varid)
if (iret == 0) then
ierr = nf90_get_var(ncid, varid, harvest, start=(/xstart,ystart/), count=(/xend-xstart+1,yend-ystart+1/))
else
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file. Using default values."
endif
! Get seasonal growing degree days (SEASON_GDD)
name = "SEASON_GDD"
iret = nf90_inq_varid(ncid, name, varid)
if (iret == 0) then
ierr = nf90_get_var(ncid, varid, season_gdd, start=(/xstart,ystart/), count=(/xend-xstart+1,yend-ystart+1/))
else
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file. Using default values."
endif
! Close the NetCDF file
ierr = nf90_close(ncid)
if (ierr /= 0) stop "MODULE_NOAHLSM_HRLDAS_INPUT: read_crop_input: NF90_CLOSE"
end subroutine read_crop_input
!---------------------------------------------------------------------------------------------------------
subroutine read_tile_drain_map(tdinput_flnm, &
xstart, xend, &
ystart, yend, &
td_fraction)
implicit none
character(len=*), intent(in) :: tdinput_flnm
integer, intent(in) :: xstart, xend, ystart, yend
real, dimension(xstart:xend,ystart:yend), intent(out) :: td_fraction
character(len=24) :: name
integer :: ierr,iret
integer :: ncid, varid
integer :: rank
#ifdef _PARALLEL_
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
if (ierr /= MPI_SUCCESS) stop "MPI_COMM_RANK"
#else
rank = 0
#endif
! Open the NetCDF file.
if (rank == 0) write(*,'("tdinput_flnm: ''", A, "''")') trim(tdinput_flnm)
#ifdef _PARALLEL_
ierr = nf90_open_par(tdinput_flnm, NF90_NOWRITE, MPI_COMM_WORLD, MPI_INFO_NULL, ncid)
#else
ierr = nf90_open(tdinput_flnm, NF90_NOWRITE, ncid)
#endif
if (ierr /= 0) then
write(*,'("read_tile_drain_map: Problem opening tdinput file: ''", A, "''")') trim(tdinput_flnm)
#ifdef _PARALLEL_
call mpi_finalize(ierr)
if (ierr /= 0) write(*, '("Problem with MPI_finalize.")')
#endif
stop
endif
! Get Tile Drain Fraction
name = "TD_FRACTION"
iret = nf90_inq_varid(ncid, name, varid)
if (iret == 0) then
ierr = nf90_get_var(ncid, varid, td_fraction, start=(/xstart,ystart/), count=(/xend-xstart+1,yend-ystart+1/))
else
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file. Using default values."
endif
! Close the NetCDF file
ierr = nf90_close(ncid)
if (ierr /= 0) stop "MODULE_NOAHLSM_HRLDAS_INPUT: read_tile_drain_map: NF90_CLOSE"
end subroutine read_tile_drain_map
!---------------------------------------------------------------------------------------------------------
subroutine read_urban_map(wrfinput_flnm, &
xstart, xend, ystart, yend, &
urban_fraction)
implicit none
character(len=*), intent(in) :: wrfinput_flnm
integer, intent(in) :: xstart, xend, ystart, yend
real, dimension(xstart:xend,ystart:yend), intent(out) :: urban_fraction
character(len=24) :: name
integer :: ierr,iret
integer :: ncid, varid
integer :: rank
#ifdef _PARALLEL_
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
if (ierr /= MPI_SUCCESS) stop "MPI_COMM_RANK"
#else
rank = 0
#endif
! Open the NetCDF file.
if (rank == 0) write(*,'("wrfinput_flnm: ''", A, "''")') trim(wrfinput_flnm)
#ifdef _PARALLEL_
ierr = nf90_open_par(wrfinput_flnm, NF90_NOWRITE, MPI_COMM_WORLD, MPI_INFO_NULL, ncid)
#else
ierr = nf90_open(wrfinput_flnm, NF90_NOWRITE, ncid)
#endif
if (ierr /= 0) then
write(*,'("read_urban_map: Problem opening wrfinput file: ''", A, "''")') trim(wrfinput_flnm)
#ifdef _PARALLEL_
call mpi_finalize(ierr)
if (ierr /= 0) write(*, '("Problem with MPI_finalize.")')
#endif
stop
endif
! Get Urban Fraction
name = "FRC_URB2D"
iret = nf90_inq_varid(ncid, name, varid)
if (iret == 0) then
ierr = nf90_get_var(ncid, varid, urban_fraction, start=(/xstart,ystart/), count=(/xend-xstart+1,yend-ystart+1/))
else
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file. Using default table values."
endif
! Close the NetCDF file
ierr = nf90_close(ncid)
if (ierr /= 0) stop "MODULE_NOAHLSM_HRLDAS_INPUT: read_urban_map: NF90_CLOSE"
end subroutine read_urban_map
!---------------------------------------------------------------------------------------------------------
subroutine read_3d_soil(spatial_filename,xstart, xend,ystart, yend, &
nsoil,bexp_3d,smcdry_3d,smcwlt_3d,smcref_3d,smcmax_3d, &
dksat_3d,dwsat_3d,psisat_3d,quartz_3d,refdk_2d,refkdt_2d,&
irr_frac_2d,irr_har_2d,irr_lai_2d,irr_mad_2d,filoss_2d,sprir_rate_2d,&
micir_rate_2d,firtfac_2d,ir_rain_2d,bvic_2d,axaj_2d,bxaj_2d,xxaj_2d,&
bdvic_2d,gdvic_2d,bbvic_2d,klatfac,tdsmcfac,tddc,tddcoef,tdddrain,tdradi,tdspac)
implicit none
character(len=*), intent(in) :: spatial_filename
integer, intent(in) :: xstart, xend, ystart, yend
integer, intent(in) :: nsoil
real, dimension(xstart:xend,nsoil,ystart:yend), intent(out) :: bexp_3d
real, dimension(xstart:xend,nsoil,ystart:yend), intent(out) :: smcdry_3d
real, dimension(xstart:xend,nsoil,ystart:yend), intent(out) :: smcwlt_3d
real, dimension(xstart:xend,nsoil,ystart:yend), intent(out) :: smcref_3d
real, dimension(xstart:xend,nsoil,ystart:yend), intent(out) :: smcmax_3d
real, dimension(xstart:xend,nsoil,ystart:yend), intent(out) :: dksat_3d
real, dimension(xstart:xend,nsoil,ystart:yend), intent(out) :: dwsat_3d
real, dimension(xstart:xend,nsoil,ystart:yend), intent(out) :: psisat_3d
real, dimension(xstart:xend,nsoil,ystart:yend), intent(out) :: quartz_3d
real, dimension(xstart:xend,ystart:yend), intent(out) :: refdk_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: refkdt_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: irr_frac_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: irr_har_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: irr_lai_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: irr_mad_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: filoss_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: sprir_rate_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: micir_rate_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: firtfac_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: ir_rain_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: bvic_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: axaj_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: bxaj_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: xxaj_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: bdvic_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: gdvic_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: bbvic_2d
real, dimension(xstart:xend,ystart:yend), intent(out) :: klatfac
real, dimension(xstart:xend,ystart:yend), intent(out) :: tdsmcfac
real, dimension(xstart:xend,ystart:yend), intent(out) :: tddc
real, dimension(xstart:xend,ystart:yend), intent(out) :: tddcoef
real, dimension(xstart:xend,ystart:yend), intent(out) :: tdddrain
real, dimension(xstart:xend,ystart:yend), intent(out) :: tdradi
real, dimension(xstart:xend,ystart:yend), intent(out) :: tdspac
character(len=24) :: name
character(len=256) :: units
integer :: ierr,iret, varid,isoil
integer :: ncid
real, dimension(xstart:xend,ystart:yend,nsoil) :: xdum
ierr = nf90_open(spatial_filename, NF90_NOWRITE, ncid)
if (ierr /= 0) then
write(*,'("read_3d_soil: Problem opening 3d soil file: ''", A, "''")') trim(spatial_filename)
stop
endif
name = "bexp"
iret = nf90_inq_varid(ncid, trim(name), varid)
if (iret /= 0) then
print*, 'ncid = ', ncid
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file."
stop
endif
iret = nf90_get_var(ncid, varid, xdum, start=(/xstart,ystart,1/), count=(/xend-xstart+1,yend-ystart+1,nsoil/))
do isoil = 1,nsoil
bexp_3d(:,isoil,:) = xdum(:,:,isoil)
end do
name = "smcdry"
iret = nf90_inq_varid(ncid, trim(name), varid)
if (iret /= 0) then
print*, 'ncid = ', ncid
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file."
stop
endif
iret = nf90_get_var(ncid, varid, xdum, start=(/xstart,ystart,1/), count=(/xend-xstart+1,yend-ystart+1,nsoil/))
do isoil = 1,nsoil
smcdry_3d(:,isoil,:) = xdum(:,:,isoil)
end do
name = "smcwlt"
iret = nf90_inq_varid(ncid, trim(name), varid)
if (iret /= 0) then
print*, 'ncid = ', ncid
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file."
stop
endif
iret = nf90_get_var(ncid, varid, xdum, start=(/xstart,ystart,1/), count=(/xend-xstart+1,yend-ystart+1,nsoil/))
do isoil = 1,nsoil
smcwlt_3d(:,isoil,:) = xdum(:,:,isoil)
end do
name = "smcref"
iret = nf90_inq_varid(ncid, trim(name), varid)
if (iret /= 0) then
print*, 'ncid = ', ncid
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file."
stop
endif
iret = nf90_get_var(ncid, varid, xdum, start=(/xstart,ystart,1/), count=(/xend-xstart+1,yend-ystart+1,nsoil/))
do isoil = 1,nsoil
smcref_3d(:,isoil,:) = xdum(:,:,isoil)
end do
name = "smcmax"
iret = nf90_inq_varid(ncid, trim(name), varid)
if (iret /= 0) then
print*, 'ncid = ', ncid
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file."
stop
endif
iret = nf90_get_var(ncid, varid, xdum, start=(/xstart,ystart,1/), count=(/xend-xstart+1,yend-ystart+1,nsoil/))
do isoil = 1,nsoil
smcmax_3d(:,isoil,:) = xdum(:,:,isoil)
end do
name = "dksat"
iret = nf90_inq_varid(ncid, trim(name), varid)
if (iret /= 0) then
print*, 'ncid = ', ncid
write(*,*) "MODULE_HRLDAS_NETCDF_IO: Problem finding variable '"//trim(name)//"' in NetCDF file."
stop
endif