-
Notifications
You must be signed in to change notification settings - Fork 3
/
load_mosaic_old.f90
375 lines (319 loc) · 9.83 KB
/
load_mosaic_old.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
program load_mosaic
!
! Create a file with all the wet points that we wish to map to.
!
! Mosaic input.
!
! Usage: load_mosaic
!
! Assumes there's a file 'mosaic.nc' lurking about....
!
! Output. A file called ...
!
!
use iso_fortran_env
use netcdf
implicit none
integer(int32) :: i,j,k
integer(int32) :: nx,ny ! Size of model grid
integer(int32) :: nxp,nyp ! Size of model supergrid
integer(int32) :: ncid,vid,did ! NetCDF ids
real(real64),allocatable,dimension(:,:) :: wrk,wrk_super
real(real64),allocatable,dimension(:,:) :: x_c,y_c,area
character(len=128) :: odir='',ofile='' ! for ocean_mosaic.nc
character(len=128) :: gdir='',gfile='' ! for hgrid file
character(len=256) :: dirfile='' ! concatenation
logical :: fexist = .false.
! Get info on the grid from input
write(*,*) 'Getting model grid info'
! Get mosaic info
inquire(file=trim('mosaic.nc'),exist=fexist)
if ( .not. fexist ) then
write(*,*) 'mosaic.nc does not exist. Bailing out'
stop 1
endif
call handle_error(nf90_open('mosaic.nc',nf90_nowrite,ncid))
call handle_error(nf90_inq_varid(ncid,'ocn_mosaic_dir',vid))
call handle_error(nf90_get_var(ncid,vid,odir))
call handle_error(nf90_inq_varid(ncid,'ocn_mosaic_file',vid))
call handle_error(nf90_get_var(ncid,vid,ofile))
call handle_error(nf90_close(ncid))
! Get horizontal grid
dirfile=odir(1:scan(odir,'/',back=.true.)) // ofile(1:scan(ofile,'c',back=.true.))
write(*,*) len_trim(dirfile),dirfile
inquire(file=trim(dirfile),exist=fexist)
if ( .not. fexist ) then
write(*,*) 'ocn_mosaic_dir/ocn_mosaic_file =',trim(dirfile), ' does not exist. Bailing out'
stop 1
endif
call handle_error(nf90_open(trim(dirfile),nf90_nowrite,ncid))
call handle_error(nf90_inq_varid(ncid,'gridlocation',vid))
call handle_error(nf90_get_var(ncid,vid,gdir))
call handle_error(nf90_inq_varid(ncid,'gridfiles',vid))
call handle_error(nf90_get_var(ncid,vid,gfile))
call handle_error(nf90_close(ncid))
!
! On mosaic "supergrid" we need to get every second point
!
write(*,*) 'Reading supergrid info'
! Read xt
dirfile=gdir(1:scan(gdir,'/',back=.true.)) // gfile(1:scan(gfile,'c',back=.true.))
inquire(file=trim(dirfile),exist=fexist)
if ( .not. fexist ) then
write(*,*) 'gridlocation/gridfiles =',trim(dirfile), ' does not exist. Bailing out'
stop 1
endif
call handle_error(nf90_open(trim(dirfile),nf90_nowrite,ncid))
call handle_error(nf90_inq_dimid(ncid,'nx',did))
call handle_error(nf90_inquire_dimension(ncid,did,nx)
nxp=nx+1
nxt=nx/2
nxc=nx/2+1
call handle_error(nf90_inq_dimid(ncid,'ny',did))
call handle_error(nf90_inquire_dimension(ncid,did,ny)
nyp=ny+1
nyt=ny/2
nxy=ny/2+1
allocate(x_c(nxc,nyc),y_c(nxc,nyc))
allocate(wrk_super(nxp,nyp)
! Get x corners
call handle_error(nf90_inq_varid(ncid,'x',vid))
call handle_error(nf90_get_var(ncid,vid,wrk_super))
do j=1,nyc
do i = 1,nxc
x_c(i,j)= wrk_super(2*i-1,2*j-1)
enddo
enddo
! Get y corners
call handle_error(nf90_inq_varid(ncid,'y',vid))
call handle_error(nf90_get_var(ncid,vid,wrk_super))
do j=1,nyc
do i = 1,nxc
y_c(i,j)= wrk_super(2*i-1,2*j-1)
enddo
enddo
j_lat = nyc
if ( istripolar ) then
j_lat =0
do j = 1,1,nyc
if( y_c(1,j) /= y_c(2,j) ) then
jlat = j-1
exit
endif
enddo
if ( j_lat == 0 ) then
write(*,*) 'FATAL: unable to locate j_lat for tripolar grid'
stop 1
endif
endif
! Area, probably should do dxt*dyt correctly but I think this is ok.
deallocate(wrk_super)
allocate(wrk_super(nx,ny))
call handle_error(nf90_inq_varid(ncid,'area',vid))
call handle_error(nf90_get_var(ncid,vid,wrk_super))
call handle_error(nf90_close(ncid))
do j = 1, ny
do i = 1,nx
area(i,j) = wrk_super(2*i-1,2*j-1)+wrk_super(2*i,2*j-1)+wrk_super(2*i-1,2*j)+wrk_super(2*i,2*j)
enddo
enddo
deallocate(wrk_super)
! Now load up spherical grid
topo_file='/home/datalib/bathymetry/GEBCO_2008/gebco_08_2d.nc'
call handle_error(nf90_open(trim(topo_file),ncid))
call handle_error(nf90_inq_varid(ncid,'height',hid))
call handle_error(nf90_inquire_variable(ncid,hid,dimids=dids))
call handle_error(nf90_inq_dimension(ncid,dids(1),xname,xlen))
call handle_error(nf90_inq_dimension(ncid,dids(2),yname,ylen))
call handle_error(nf90_inq_varid(ncid,trim(xname),xid)
call handle_error(nf90_inq_varid(ncid,trim(yname),yid)
allocate(xtopo(xlen),ytopo(ylen),weight(ylen))
call handle_error(nf90_get_var(ncid,xid,xtopo))
call handle_error(nf90_get_var(ncid,yid,ytopo))
x_cyc = 0
x_shift=x_c(1,1)
if(x_shift < 0.0) then
x_cyc=count(x_c(:,1) < 0.0)
else
x_cyc=count(x_c(:,1) > 360.0)
endif
x_rot=mod(cshift(x_c(:,1),x_cyc),360.0)
write(*,*) 'Cycling ',x_cyc,' spaces x_c_start = ',x_c(1+x_cyc),'xtopo(1),xtopo(1),xtopo(2)
! Weights
ys=D2R*max(-90.0,1.5*ytopo(1)-0.5*ytopo(2))
yn=D2R*0.5*(ytopo(2)-ytopo(1)
weight(1)=sin(yn)-sin(ys)
do j = 2,ylen-1
ys=yn
yn=D2R*0.5*(ytopo(j)-ytopo(j-1)
weight(i)=sin(yn)-sin(ys)
enddo
ys=y2
yn=D2R*min(90.0,1.5*ytopo(ylen)-0.5*ytopo(n-1))
weight(ylen)=sin(yn)-sin(ys)
! X weights
allocate(xw(xlen),dest(xlen))
ioff=1
xw=0
do i=2,nxp
do itop = ioff,xlen
if(xtopo(ioff) > x_rot(i) ) then
exit
endif
xw(itop)=xw(itop)+1
dest(itop)=i-1
enddo
enddo
! Do by latitude
rows=0
do j = 1, jlat-1
call get_rows(y_c(j:j+1),weight,ytopo,rows)
call process_row(x_rot,xtopo,rows,topo_out)
enddo
do js = 1,ny,jblock
je = min(js+jblock,ny)
do is = 1,nx,iblock
ie = min(is+iblock,nx)
xmin=minval(x_c(is,js:je+1))
xmax=maxval(x_c(ie+1,js:je+1))
ymin=minval(y_c(is:ie+1,js))
ymax=maxval(y_c(is:ie+1,je+1))
ist=-1
do i=1,xlen
if(xtopo(i) >= xmin .and. xtopo(i) < xmax ) then
ist=i
exit
endif
enddo
iet=ist
do i=ist,xlen
if(xtopo(i) >= xmax ) then
exit
endif
iet=i
enddo
jst=-1
do j=1,ylen
if(ytopo(j) >= ymin .and. ytopo(j) < ymax ) then
jst=j
exit
endif
enddo
jet=jst
do j=jst,ylen
if(ytopo(j) >= ymax ) then
exit
endif
jet=j
enddo
call load_topo(topo_in,ist,iet,jst,jet)
call process_2d(topo_in,xtopo(ist:iet),ytopo(jst:jet),
contains
subroutine get_rows(edges,lat,rows)
real(real32),intent(in) :: edges(2)
real(real32),dimension(:),intent(in) :: lat
integer(int32),intent(inout) :: rows(2)
integer :: j
rows=0
do j=1,size(lat)
if(lat(j)) >= edges(1) .and. lat(j)) < edges(2))
rows = j
exit
else
if(lat(j) >= edges(2)) exit
endif
enddo
do j=rows(1)+1,size(lat)
if(lat(j)) < edges(2))
rows(2) = j
else
exit
endif
enddo
end subroutine get_rows
subroutine process_rows(x,cyc,rows,top,xtopo)
real,dimension(:) :: x
integer :: cyc
integer,dimension(2) :: rows
real,dimension(:) :: top
integer :: nrows
real,dimension(xlen) :: top_in
nrows=rows(2)-rows(1)+1
topx=0.0
sumj=0.0
do j=1,nrows
jtop=j+rows(1)-1
call handle_error(nf90_get_var(ncid,hid,top_in,start=[1,jtop],count=[xlen,1])
top_in=-top_in
wgt=weight(jtop)
mask=top_in > 0.0_real64
where(mask)
sumj=sumj+wgt
topx=topx+wgt*top_in
cnt=cnt+1
top_max=max(top_max,top_in)
top_min=min(top_min,top_in)
end where
enddo
is=0
ie=1
do it=1,xlen
i=dest(it)
sumi(i)=sum(i)+sumj(it)
top(i)=top(i)+topx(it)
cont(i)=cont(i)+cnt(it)
topmax(i)=max(topmax(i),top_max(it))
if(cnt(it) > 0 ) then topmin(i)=min(topmin(i),top_min(it))
enddo
where(cnt > 0 ) top=top/sumi
end subroutine process_rows
subroutine create_wet_file(wet)
type(wet_type), intent(in) :: wet
integer(int32) :: ncid, did_ic
integer(int32) :: wet_i_id,wet_j_id
integer(int32) :: wet_x_id,wet_y_id
integer(int32) :: wet_area_id
call handle_error(nf90_create('model_wet.nc',ior(NF90_CLOBBER,NF90_NETCDF4),ncid))
call handle_error(nf90_def_dim(ncid,'iw',wet%npts,did_ic))
!
! These are for ALL the model wetal points
!
call handle_error(nf90_def_var(ncid,'wet_i',nf90_int,did_ic,wet_i_id))
call handle_error(nf90_put_att(ncid,wet_i_id,'long_name','model i index'))
call handle_error(nf90_def_var(ncid,'wet_j',nf90_int,did_ic,wet_j_id))
call handle_error(nf90_put_att(ncid,wet_j_id,'long_name','model j index'))
call handle_error(nf90_def_var(ncid,'wet_x',nf90_double,did_ic,wet_x_id))
call handle_error(nf90_put_att(ncid,wet_x_id,'long_name','model longitude'))
call handle_error(nf90_put_att(ncid,wet_x_id,'units','degrees_E'))
call handle_error(nf90_def_var(ncid,'wet_y',nf90_double,did_ic,wet_y_id))
call handle_error(nf90_put_att(ncid,wet_y_id,'long_name','model latitude'))
call handle_error(nf90_put_att(ncid,wet_y_id,'units','degrees_N'))
call handle_error(nf90_def_var(ncid,'wet_area',nf90_double,did_ic,wet_area_id))
call handle_error(nf90_put_att(ncid,wet_area_id,'long_name','model area'))
call handle_error(nf90_put_att(ncid,wet_area_id,'units','m^2'))
call handle_error(nf90_enddef(ncid,h_minfree=4096))
! Put it there
call handle_error(nf90_put_var(ncid,wet_i_id,wet%i))
call handle_error(nf90_put_var(ncid,wet_j_id,wet%j))
call handle_error(nf90_put_var(ncid,wet_x_id,wet%x))
call handle_error(nf90_put_var(ncid,wet_y_id,wet%y))
call handle_error(nf90_put_var(ncid,wet_area_id,wet%area))
call handle_error(nf90_close(ncid))
end subroutine create_wet_file
subroutine handle_error(error_flag,isfatal,err_string)
! Simple error handle for NetCDF
integer(int32),intent(in) :: error_flag
logical, intent(in),optional :: isfatal
character(*), intent(in),optional :: err_string
logical :: fatal
fatal = .true.
if(present(isfatal)) fatal=isfatal
if ( error_flag /= nf90_noerr ) then
if ( fatal ) then
write(*,*) 'FATAL ERROR:',nf90_strerror(error_flag)
if (present(err_string)) write(*,*) trim(err_string)
stop
endif
endif
end subroutine handle_error
end program create_model_wet