-
Notifications
You must be signed in to change notification settings - Fork 7
/
mkSMAPTilesPara_v2.F90
864 lines (658 loc) · 31 KB
/
mkSMAPTilesPara_v2.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
#include "Raster.h"
PROGRAM mkSMAPTilesPara_v2
! This program constructs land and lake tiles for the SMAP-EASE-M09 and M36 grids (just set MGRID)
! for CLSM implementation.
! f90 -c create_smap_tiles.f90
! f90 -c smapconv.f
! f90 -o create_smap_tiles create_smap_tiles.o smapconv.o
!
use easeV2_conv
use rmTinyCatchParaMod
use process_hres_data
use MAPL_SortMod
use MAPL_ConstantsMod
use LogRectRasterizeMod
implicit none
character*4 :: LBSV = 'DEF'
integer i,j,ig,jg,i0,iop,n,d1,d2,j1,j2,i1,i2,ix, jx,icount,pcount
integer :: NC = i_raster, NR = j_raster, NT = 16330000, ND = 10000, ND_raster = 10000
integer, parameter :: nc_esa = 129600, nr_esa = 64800
! For regridding
integer, allocatable, target, dimension (:,:) &
:: geos_msk
REAL, allocatable, DIMENSION (:) :: loc_val
INTEGER, ALLOCATABLE, DIMENSION (:) :: density, loc_int
logical, dimension (:), allocatable :: unq_mask
integer, pointer , dimension (:,:) :: subset
integer, pointer , dimension (:) :: subset1, subset_smap
real, pointer , dimension (:) :: subset2
integer :: dx_esa, dy_esa, NBINS, NPLUS
integer*8, allocatable, dimension (:) :: SRTM_catid
integer,allocatable, dimension (:,:), target :: tileid_index,catid_index
integer,allocatable, dimension (:,:) :: catid, iaster
integer,allocatable, dimension (:) :: land_id,water_id,ice_id
integer,allocatable, dimension (:) :: my_land, all_id
real, allocatable, dimension (:) :: smap_grid_area,tile_area,SRTM_CatchArea
integer*1,allocatable, dimension (:,:) :: veg, i2aster
real*4, dimension (:,:), allocatable :: q0,raster
REAL, dimension (:), allocatable :: tile_ele, tile_area_land
INTEGER*8 :: PFAF_CODE
integer l,imn,imx,jmn,jmx,mval,l_index,i_index,w_index,typ,pfaf,cindex
integer :: LakeType, IceType, OceanType
character(3) :: easegrid
real :: clat, clon, r_smap, s_smap, smap_convert, da
real :: fr_gcm
integer :: ind_col, ind_row, status, ncid, nciv,nland_cells, DOM_INDX
REAL (kind=8), PARAMETER :: RADIUS=6378137.0,pi=3.14159265358979323846
character*100 :: veg_class (12)
character*5 :: MGRID
character*100 :: gfile,gtopo30
integer :: nc_smap,nr_smap, N_args, iargc
real :: EASE_grid_area, CELL_km
REAL :: dx,dy,d2r,lats,mnx,mxx,mny,mxy,sum1,sum2,jgv, VDUM,pix_area
character(40) :: arg, EASElabel
character*200 :: tmpstring, tmpstring1, tmpstring2
logical :: regrid = .false.
character*128 :: MaskFile
logical :: pfaf_til = .false.
character*1 :: PF
include 'netcdf.inc'
N_args = iargc()
if(N_args < 1) then
print *,'USAGE : bin/mkSMAPTiles -smap_grid MXX'
print *,'Allowed SMAP grids are: M01 M03 M09 M25 M36'
stop
end if
i=0
do while ( i < N_args )
i = i+1
call getarg(i,arg)
if ( trim(arg) == '-smap_grid' ) then
i = i+1
call getarg(i,MGRID)
elseif ( trim(arg) == '-pfaf_til' ) then
i = i+1
call getarg(i,PF)
if (PF == 'T') pfaf_til = .true.
elseif ( trim(arg) == '-v' ) then
i = i+1
call getarg(i,LBSV)
else ! stop for any other arguments
print *,'USAGE : bin/mkSMAPTiles -smap_grid MXX -pfaf_til T'
print *,'Allowed SMAP grids are: M09 M36 Ml'
stop
endif
end do
call system('cd data/ ; ln -s /discover/nobackup/projects/gmao/ssd/land/l_data/LandBCs_files_for_mkCatchParam/V001/ CATCH')
call system('cd ..')
! Setting SMAP Grid specifications
! --------------------------------
if (trim(MGRID) == 'M09') then
CELL_km = 9.008055210146 ! nominal cell size in kilometers
nc_smap = 3856
nr_smap = 1624
gfile = 'SMAP_EASEv2_'//trim(MGRID)//'_3856x1624'
EASE_grid_area = CELL_km*CELL_km
EASElabel = 'SMAP-EASEv2-M09'
elseif(trim(MGRID) == 'M36') then
CELL_km = 36.032220840584 ! nominal cell size in kilometers
nc_smap = 964
nr_smap = 406
gfile = 'SMAP_EASEv2_'//trim(MGRID)//'_964x406'
EASE_grid_area = CELL_km*CELL_km
EASElabel = 'SMAP-EASEv2-M36'
elseif(trim(MGRID) == 'M25') then
CELL_km = 25.0252600081 ! nominal cell size in kilometers
nc_smap = 1388
nr_smap = 584
gfile = 'SMAP_EASEv2_M25_1388x584'
EASE_grid_area = CELL_km*CELL_km
else if (trim(MGRID) .eq. 'M03') then ! SMAP 3 km grid
CELL_km = 3.0026850700487 ! nominal cell size in kilometers
nc_smap = 11568
nr_smap = 4872
gfile = 'SMAP_EASEv2_M03_11568x4872'
EASE_grid_area = CELL_km*CELL_km
regrid = .true.
NC = 21600
NR = 10800
NT = 500000000
else if (trim(MGRID) .eq. 'M01') then ! SMAP 1 km grid
CELL_km = 1.00089502334956 ! nominal cell size in kilometers
nc_smap = 34704
nr_smap = 14616
gfile = 'SMAP_EASEv2_M01_34704x14616'
EASE_grid_area = CELL_km*CELL_km
regrid = .true.
NC = 43200
NR = 21600
NT = 1500000000
else !
print *,'Unknown SMAP Grid stopping..'
stop
endif
allocate(land_id (1:NT))
allocate(water_id (1:NT))
allocate(ice_id (1:NT))
land_id = 0
water_id = 0
ice_id = 0
OceanType = 0
IceType =11
LakeType =10
ND = 10*10**(nint(log10(1.*nr_smap)))
! Check for the 10 arc-sec MaskFile
! -----------------------------------
call getenv ("MASKFILE" ,MaskFile )
print *, 'Using MaskFile ', trim(MaskFile)
if(pfaf_til) then
nc = 43200 ! Number of rows in raster file
nr = 21600
call mkEASEv2Raster
else
if((trim(MGRID) == 'M09').or.(trim(MGRID) == 'M36'))call write_tilfile
endif
if (index(MaskFile,'GEOS5_10arcsec_mask') /= 0) then
! New ESA (Veg) + SRTM (catchments) based mask file
! is overlaid on SMAP
! -------------------------------------------------
nc = 43200 ! Number of rows in raster file
nr = 21600 ! Number of columns in raster file
regrid = .true.
dx_esa = nc_esa / nc ! x-dimension (or # of ESA columns within the raster grid cell)
dy_esa = nr_esa / nr ! y-dimension (or # of ESA rows within the raster grid cell)
allocate(tileid_index(1:nc,1:nr))
allocate(SRTM_catid (1:SRTM_maxcat+2))
allocate(catid_index (1:nc,1:nr))
allocate(veg (1:nc,1:nr))
allocate(geos_msk (1:nc_esa,1:dy_esa))
allocate(SRTM_CatchArea (1:SRTM_maxcat))
OPEN (10, FILE = 'data/CATCH/SRTM-TopoData/Pfafcatch-routing.dat', &
FORM = 'FORMATTED',STATUS='OLD',ACTION='READ')
READ (10,*) I
DO N = 1, I
READ (10, '(i8,i15,4(1x,f9.4),1x,e10.3,4(1x,e9.3),I8,6(1x,f9.4))') &
DOM_INDX,PFAF_CODE,VDUM,VDUM,VDUM,VDUM,VDUM, &
SRTM_CatchArea (N)
END DO
CLOSE (10, STATUS='KEEP')
dx = 360._8/nc
dy = 180._8/nr
d2r = PI/180._8
da = MAPL_radius*MAPL_radius*pi*pi*dx*dy/180./180./1000000.
tileid_index = 0
catid_index = 0
veg = 0
status = NF_OPEN ('data/CATCH/GEOS5_10arcsec_mask.nc', NF_NOWRITE, ncid)
status = NF_GET_VARA_INT64 (ncid,3,(/1/),(/SRTM_maxcat/),SRTM_catid(1:SRTM_maxcat)) ! Read pfafstetter IDs
if(status /=0) then
PRINT *, NF_STRERROR(STATUS)
print *, 'Problem with NF_OPEN',trim(MaskFile)
endif
SRTM_catid (SRTM_maxcat + 1) = 190000000
SRTM_catid (SRTM_maxcat + 2) = 200000000
i1 = 0 ! count # of 30-arcsec pixels
do j=1,nr
clat = -90. + float(j-1)*dy + dy/2.
status = NF_GET_VARA_INT (ncid,4,(/1,(j-1)*dy_esa +1/),(/nc_esa,dy_esa/),geos_msk) ! Read 10-arcsec rows that lie within the raster row 'j'
if(status /=0) then
PRINT *, NF_STRERROR(STATUS)
print *, 'Problem with NF_GET_VARA_INT',trim(MaskFile),status
endif
do i = 1,nc
clon = -180. + float(i-1)*dx + dx/2.
if (associated (subset)) NULLIFY (subset)
subset => geos_msk ((i-1)*dx_esa + 1 : i*dx_esa, 1:dy_esa) ! rectangular array contains ESA pixels that lie within the raster grid cell at i,j
if(maxval (subset) > SRTM_maxcat) then
where (subset == 190000000) subset = SRTM_maxcat + 1
where (subset == 200000000) subset = SRTM_maxcat + 2
endif
if (maxval (subset) > 0) then ! check whether there are Non-ocean ESA pixels
! catID of the middle pixel
veg (i,j) = 1 ! veg is set to land
NPLUS = count(subset >= 1 .and. subset <= SRTM_maxcat + 2) ! Count non-ocean ESA pixels within
allocate (loc_int (1:NPLUS))
allocate (unq_mask(1:NPLUS))
loc_int = pack(subset,mask = (subset >= 1 .and. subset <= SRTM_maxcat + 2)) ! loc_int contains catch_indices of non-ocean ESA pixels
call MAPL_Sort (loc_int)
unq_mask = .true.
do n = 2,NPLUS
unq_mask(n) = .not.(loc_int(n) == loc_int(n-1)) ! count number of unique numbers in loc_int for binning
end do
NBINS = count(unq_mask)
if (NBINS > 1) then
allocate(loc_val (1:NBINS))
allocate(density (1:NBINS))
loc_val = 1.*pack(loc_int,mask =unq_mask) ! loc_val contains available non-ocean catch_indices within the i,j grid cell,
! Those numbers will be used as bin values
call histogram (dx_esa*dy_esa, NBINS, density, loc_val, real(subset)) ! density is the pixel count for each bin value
catid_index (i,j) = loc_val (maxloc(density,1)) ! picks maximum density as the dominant catchment_index at i,j
deallocate (loc_val, density)
else
catid_index (i,j) = loc_int (1)
endif
deallocate (loc_int, unq_mask)
if(catid_index (i,j) == SRTM_maxcat + 1) veg (i,j) = LakeType
if(catid_index (i,j) == SRTM_maxcat + 2) veg (i,j) = IceType
if((catid_index(i,j) >= 1).and.(catid_index (i,j) <= SRTM_maxcat)) i1 = i1 + 1
! count in if this is i,j pixel is a land, lake or ice within ind_col,ind_row SMAP grid cell
call easeV2_convert(trim(MGRID), clat, clon, r_smap, s_smap)
ind_col = nint(r_smap) + 1
ind_row = nint(s_smap) + 1
if((ind_row.ge.1).and.(veg(i,j).ne.OceanType).and.(ind_row.le.nr_smap)) then
l= ind_row*ND + ind_col
if(veg(i,j)==LakeType) then
water_id(l) = 1
else if(veg(i,j)==IceType) then
ice_id (l) = 1
else
land_id (l) = 1
endif
endif
endif
end do
enddo
status = NF_CLOSE (ncid)
deallocate (geos_msk)
print *,'Read ', trim (MaskFile)
print *,'Min and Max of tile indices:',minval(catid_index),maxval(catid_index)
else
! Old IGBP (Veg) + HYDRO1k (catchments) based mask will
! Overlaid on SMAP mask
! -----------------------------------------------------
allocate(iaster (i_raster,j_raster))
allocate(i2aster (i_raster,j_raster))
allocate(veg (1:nc,1:nr))
allocate(catid (1:nc,1:nr))
allocate(catid_index (1:nc,1:nr))
allocate(tileid_index(1:nc,1:nr))
dx = 360._8/nc
dy = 180._8/nr
d2r = PI/180._8
da = MAPL_radius*MAPL_radius*pi*pi*dx*dy/180./180./1000000.
tileid_index = 0
! Simple Biosphere 2 Model Legend
! Value Class Name
! (ftp://edcftp.cr.usgs.gov/pub/data/glcc/globe/latlon/sib22_0.leg)
! the types vary 0-11 (array index minus 1)
veg_class(1) = 'Ocean'
veg_class(2) = 'Broadleaf Evergreen Trees'
veg_class(3) = 'Broadleaf Deciduous Trees'
veg_class(4) = 'Broadleaf and Needleleaf Trees'
veg_class(5) = 'Needleleaf Evergreen Trees'
veg_class(6) = 'Needleleaf Deciduous Trees'
veg_class(7) = 'Short Vegetation/C4 Grassland'
veg_class(8) = 'Shrubs with Bare Soil'
veg_class(9) = 'Dwarf Trees and Shrubs'
veg_class(10) = 'Agriculture or C3 Grassland'
veg_class(11) = 'Water, Wetlands'
veg_class(12) = 'Ice/Snow'
! reading SiB2 land cover classification data - the origin of the
! 2.5'x2.5' vegetation raster file is global 1min IGBP data
! (ftp://edcftp.cr.usgs.gov/pub/data/glcc/globe/latlon/sib22_0.leg)
open (10,file='data/CATCH/sib22.5_v2.0.dat', &
form='unformatted', &
action='read', convert='big_endian',status='old')
READ(10)i2aster
close (10,status='keep')
if(regrid) then
call RegridRaster1 (i2aster,veg)
else
veg = i2aster
endif
deallocate (i2aster)
! reading 2.5'x2.5' global raster file of Pfafstetter Catchment IDs
! In this version, the dateline has been overlaid over the catchments those straddle
! across. The numbers contain for
! 1 global ocean catchment : Pfafstetter ID 0
! 36716 global land catchments : Pfafstetter IDs 1000-5999900
! 1 global inland water (lakes) catchment : Pfafstetter ID 6190000
! 1 global ice catchment : Pfafstetter ID 6200000
open (10,file='data/CATCH/global.cat_id.catch.DL', form='formatted', &
action='read', status='old')!
do j=1,j_raster
read(10,*)(iaster(i,j),i=1,i_raster)
end do
close (10,status='keep')
if(regrid) then
call RegridRaster(iaster,catid)
else
catid = iaster
endif
print *,'Read global.cat_id.catch.DL'
print *,'Min and Max of Pfafstetter IDs:', minval(catid),maxval(catid)
! reading the 2.5'x2.5' global raster file of tile indices for the
! above Pfafstetter Catchments
! 1 global ocean catchment : tile_index 36719
! 36716 global land catchments : tile_index 1-36716
! 1 global inland water (lakes) catchment : tile_index 36717
! 1 global ice catchment : tile_index 36718
! ------------------------------------------------------------
open (10,file='data/CATCH/' &
//'PfafstatterDL.rst', form='unformatted', &
action='read',convert='little_endian', status='old')
do j=1,j_raster
read(10)(iaster(i,j),i=1,i_raster)
end do
close (10,status='keep')
if(regrid) then
call RegridRaster(iaster,catid_index)
else
catid_index = iaster
endif
deallocate (iaster)
print *,'Read PfafstatterDL.rst'
print *,'Min and Max of tile indices:',minval(catid_index),maxval(catid_index)
! While looping through the nc x nr grid (tile raster), this section counts # of
! SMAP grid cells that contain land, ice or water, seperately.
! Each SMAP grid cell is assigned with an ID = ind_row*ND + ind_col.
! This is just the prelimiminery assessment in the process of assigning separate
! tiles for land, water and ice fractions within the SMAP Grid cell
! The program checks each nc x nr pixels whether there is a SMAP grid cell underneath, and counts
! number of water, land and ice pixels as seen on veg raster.
! -----------------------------------------------------------------------------------------------
do i = 1 ,nc
clon = -180. + float(i-1)*dx + dx/2.
do j =nr ,1 ,-1
clat = -90. + float(j-1)*dy + dy/2.
call easeV2_convert(trim(MGRID), clat, clon, r_smap, s_smap)
ind_col = nint(r_smap) + 1
ind_row = nint(s_smap) + 1
if((ind_row.ge.1).and.(veg(i,j).ne.OceanType).and.(ind_row.le.nr_smap)) then
l= ind_row*ND + ind_col
if(veg(i,j)==LakeType) then
water_id(l) = 1
else if(veg(i,j)==IceType) then
ice_id (l) = 1
else
land_id (l) = 1
endif
endif
end do
end do
endif
! Reading SRTM elevation data - to be consistent with AGCM
! --------------------------------------------------------
allocate(raster (i_raster,j_raster))
allocate(q0(nc,nr))
gtopo30 = 'data/CATCH/srtm30_withKMS_2.5x2.5min.data'
open (10,file=trim(gtopo30),form='unformatted',status='old',convert='little_endian')
read (10) raster
close (10,status='keep')
if(regrid) then
call RegridRasterReal(raster,q0)
else
q0 = raster
endif
deallocate (raster)
print *,'# of Land pixels in SMAP: ',sum (land_id)
print *,'# of water pixels in SMAP: ',sum (water_id)
print *,'# of ice pixels in SMAP: ',sum (ice_id)
l_index=0
w_index=sum (land_id)
i_index=sum (land_id) + sum (water_id)
nland_cells = w_index
allocate(tile_area (1:i_index + sum (ice_id)))
allocate(smap_grid_area (1:NT))
allocate(tile_ele (1:w_index))
allocate(tile_area_land(1:w_index))
allocate(my_land (1:i_index + sum (ice_id)))
allocate(all_id (1:i_index + sum (ice_id)))
land_id = 0
water_id= 0
ice_id = 0
my_land = 0
all_id = 0
smap_grid_area = 0.
tile_area_land = 0.
tile_ele = 0.
tile_area = 0.
! While looping through the nc x nr grid, this section derives land, ice and water tiles.
! Each SMAP grid cell is assigned with an ID = ind_row*ND + ind_col
! ind_col, ind_row are overlying SMAP grid cell indices
! Based on the above sums:
! l_index Grid cells have land fractions (sum(land_id))
! w_index SMAP Grid cells have inland water fractions (sum(water_id))
! i_index SMAP Grid cells have ice fractions (sum(ice_id))
! hence, tile_index 1 to l_index represent land tiles
! tile_index l_index +1 to l_index + w_index represent water (lakes) tiles
! tile_index l_index + w_index +1 to l_index + w_index + i_index represent ice tiles
! global nc x nr array of tileid_index(nc,nr) contains corresponding tile_index values which
! is derived in the below loop
ND_raster = 10*10**(nint(log10(1.*NR)))
i2 = 1
do i = 1 ,nc
clon = -180. + float(i-1)*dx + dx/2.
do j =nr ,1 ,-1
lats = -90._8 + (j - 0.5_8)*dy
clat = -90. + float(j-1)*dy + dy/2.
call easeV2_convert(trim(MGRID), clat, clon, r_smap, s_smap)
ind_col = nint(r_smap) + 1
ind_row = nint(s_smap) + 1
l= ind_row*ND + ind_col
pix_area =(sin(d2r*(lats+0.5*dy)) -sin(d2r*(lats-0.5*dy)))*(dx*d2r)
if((ind_row.ge.1).and.(veg(i,j).ge.1).and.(ind_row.le.nr_smap)) then
if(veg(i,j)==LakeType) then
if(water_id(l)==0) then
w_index = w_index + 1
water_id(l) = w_index
tileid_index(i,j)= water_id(l)
else
tileid_index(i,j)= water_id(l)
endif
endif
if(veg(i,j)==IceType) then
if(ice_id(l)==0) then
i_index = i_index + 1
ice_id (l) = i_index
tileid_index(i,j)= ice_id (l) !i_index
else
tileid_index(i,j)= ice_id (l) !i_index
endif
endif
if(veg(i,j).lt.LakeType) then
if(land_id(l)==0) then
l_index = l_index + 1
land_id (l) = l_index
tileid_index(i,j)= land_id (l) !1-l_index
else
tileid_index(i,j)= land_id (l) !1-l_index
endif
endif
tile_area(tileid_index(i,j))= tile_area(tileid_index(i,j)) + &
pix_area
my_land(tileid_index(i,j)) = l
all_id (tileid_index(i,j)) = j*ND_raster + i
endif
if((ind_row.ge.1).and.(ind_row.le.nr_smap)) then
smap_grid_area(l) = smap_grid_area(l) + &
pix_area
endif
! computing tile area/elevation
! -----------------------------
if((tileid_index(i,j) > 0).and.(tileid_index(i,j) <= nland_cells))then
tile_ele(tileid_index(i,j)) = tile_ele(tileid_index(i,j)) + q0(i,j) * &
pix_area
tile_area_land(tileid_index(i,j)) = tile_area_land(tileid_index(i,j)) + &
pix_area
endif
end do
end do
deallocate(land_id, q0)
deallocate(water_id)
deallocate(ice_id )
tile_ele = tile_ele/tile_area_land
! adjustment Global Mean Topography to 614.649 (615.662 GTOPO 30) m
! -----------------------------------------------------------------
sum1=0.
sum2=0.
do j=1,l_index
sum1 = sum1 + tile_ele(j)*tile_area(j)
enddo
if(sum1/sum(tile_area(1:l_index)).ne. 614.649D0 ) then
print *,'Global Mean Elevation (over land): ', sum1/sum(tile_area(1:l_index))
tile_ele =tile_ele*(614.649D0 / (sum1/sum(tile_area(1:l_index))))
sum1=0.
sum2=0.
do j=1,l_index
sum1 = sum1 + tile_ele(j)*tile_area(j)
enddo
print *,'Global Mean Elevation after scaling to SRTM : ',sum1/sum(tile_area(1:l_index))
endif
print *,'Total Land Area :', sum(tile_area(1:l_index))* MAPL_RADIUS * MAPL_RADIUS/1000./1000., &
sum(tile_area_land(1:l_index))* MAPL_RADIUS * MAPL_RADIUS/1000./1000.
print *,'Creating ... ', trim(gfile)//'rst'
!-------------------------------------------
open (10, file ='rst/'//trim(gfile)//'.rst',form='unformatted',status='unknown', &
action='write')
do j=1,nr
write(10)(tileid_index(i,j),i=1,nc)
end do
close (10,status='keep')
print *,'Creating ... ', trim(gfile)//'til ,catchment.def'
!-----------------------------------------------------------
open (11,file='clsm/catchment.def', &
form='formatted',status='unknown')
write(11,*)l_index
open (10, file ='til/'//trim(gfile)//'.til',form='formatted',status='unknown',action='write')
write (10,*)i_index,SRTM_maxcat, nc, nr
write (10,*)1
write (10,*)'SMAP-EASEv2-'//trim(MGRID)
write (10,*)nc_smap
write (10,*)nr_smap
! write (10,*)'NO-OCEAN'
! write (10,*) -9999
! write (10,*) -9999
do l=1,i_index
ig = my_land(l)-ND*(my_land(l)/ND)
jg = my_land(l)/ND
cindex= catid_index(all_id(l)-ND_raster*(all_id(l)/ND_raster),all_id(l)/ND_raster)
if (index(MaskFile,'GEOS5_10arcsec_mask') /= 0) then
pfaf = cindex
else
pfaf = catid(all_id(l)-ND_raster*(all_id(l)/ND_raster),all_id(l)/ND_raster)
endif
if ((l > l_index).and.(l <= w_index)) typ =19
if (l > w_index) typ = 20
if (l <= l_index) then
typ = 100
call easeV2_inverse (trim(MGRID), real(ig-1),real(jg-1), clat, clon)
mnx = clon - 180./real(nc_smap)
mxx = clon + 180./real(nc_smap)
jgv = real(jg-1) + 0.5
call easeV2_inverse (trim(MGRID), real(ig-1),jgv, clat, clon)
mny = clat
jgv = real(jg-1) - 0.5
call easeV2_inverse (trim(MGRID), real(ig-1),jgv, clat, clon)
mxy = clat
write (11,'(i10,i8,5(2x,f9.4), i4)')l,pfaf,mnx,mxx,mny,mxy,tile_ele(l)
endif
call easeV2_inverse (trim(MGRID), real(ig-1), real(jg-1), clat, clon)
fr_gcm= tile_area(l)/smap_grid_area(jg*ND + ig)
if (index(MaskFile,'GEOS5_10arcsec_mask') /= 0) then
write(10,'(i10,i9,2f10.4,2i6,f19.12,i10,i15,e13.4)') &
typ,pfaf,clon,clat,ig-1,jg-1,fr_gcm ,pfaf,SRTM_catid(cindex)
else
write(10,'(i10,i9,2f10.4,2i5,f19.12,i10,e13.4,i8)') &
typ,pfaf,clon,clat,ig-1,jg-1,fr_gcm ,cindex
endif
end do
close (10,status='keep')
close (11,status='keep')
deallocate (tileid_index,catid_index,veg)
deallocate (tile_area, smap_grid_area, tile_ele, tile_area_land, my_land, all_id)
if (index(MaskFile,'GEOS5_10arcsec_mask') /= 0) then
print *,'Creating SMAP-Catch_TransferData.nc files.'
!---------------------------------------------------
deallocate (SRTM_CatchArea, SRTM_catid)
endif
! create Grid2Catch transfer file
! -------------------------------
! CALL CREATE_ROUT_PARA_FILE (NC, NR, trim(gfile), MGRID=MGRID)
call system (tmpstring)
! now run mkCatchParam
! --------------------
tmpstring1 = '-e EASE -g '//trim(gfile)//' -v '//trim(LBSV)
write(tmpstring2,'(2(a2,x,i5,x))')'-x',nc,'-y',nr
tmpstring = 'bin/mkCatchParam.x '//trim(tmpstring2)//' '//trim(tmpstring1)
print *,trim(tmpstring)
call system (tmpstring)
contains
! -------------------------------------------------------------------------------
SUBROUTINE mkEASEv2Raster
implicit none
integer :: i, j, i_ease, j_ease
real*8, allocatable :: xs(:,:), ys(:,:)
real :: x,y, xout, yout
allocate (xs ( nc_smap+1, nr_smap+1))
allocate (ys ( nc_smap+1, nr_smap+1))
do j = 1, nr_smap
do i = 1, nc_smap
x = real(i-1) -0.5
y = real(nr_smap - j)+0.5
call easeV2_inverse(MGRID, x, y, yout, xout)
ys (i,j) = dble(yout)
xs (i,j) = dble(xout)
end do
end do
do j = nr_smap + 1, nr_smap + 1
do i = nc_smap + 1, nc_smap + 1
x = real(i-1) -0.5
y = -0.5
call easeV2_inverse(MGRID, x, y, yout, xout)
ys (i,j) = dble(yout)
xs (i,j) = dble(xout)
end do
end do
where (ys > 90.)
ys = 90.D0
endwhere
where (ys < -90.)
ys = -90.D0
endwhere
where (xs > 180.)
xs = 180.D0
endwhere
where (xs < -180.)
xs = -180.D0
endwhere
call LRRasterize(EASElabel,xs,ys,nc=nc,nr=nr,Here=.false., Verb=.false.)
stop
end SUBROUTINE mkEASEv2Raster
! ------------------------------------------------------------
SUBROUTINE write_tilfile
implicit none
character*200 :: infile
integer :: NT, NF, NC, NR, NPF, NG, IDUM, i, N, icol, rcol
character*20 :: cdum
integer, dimension (:,:), allocatable :: iRtable
real, dimension (:,:), allocatable :: rRtable
infile = 'til/'//trim(EASElabel)//'_'//trim(EASElabel)//'-Pfafstetter.'
open (10,file = trim(infile)//'ind', form = 'formatted', action = 'read', status = 'old')
open (11,file = trim(infile)//'TIL', form = 'formatted', action = 'write', status = 'unknown')
read (10, *) NT, NF, NC, NR
write (11,'(4I10)')NT, NF, NC, NR
read (10, *) NG
write(11, *) NG
do n = 1, NG
read (10, '(a)') cdum
write(11, '(a)') trim (cdum)
read (10, *) IDUM
write(11, '(I10)') IDUM
read (10, *) IDUM
write(11, '(I10)') IDUM
end do
icol = 7
rcol = 5
allocate (iRtable (1, 1:icol))
allocate (rRtable (1, 1:rcol))
do n = 1, nt
read(10,'(I10,3E20.12,9(2I10,E20.12,I10))') iRtable (1,1),rRtable(1,1),rRtable(1,2),rRtable(1,3),iRtable (1,2),iRtable (1,3),rRtable(1,4),iRtable (1,4),&
iRtable (1,5),iRtable (1,6),rRtable(1,5),iRtable (1,7)
write(11,'(I10,3E20.12,9(2I10,E20.12,I10))') iRtable (1,1),rRtable(1,1),rRtable(1,2),rRtable(1,3),iRtable (1,2)-1,nr_smap - iRtable (1,3),rRtable(1,4),iRtable (1,4),&
iRtable (1,5),iRtable (1,6),rRtable(1,5),iRtable (1,7)
end do
close (10, status = 'keep')
close (11, status = 'keep')
END SUBROUTINE write_tilfile
END PROGRAM