forked from GEOS-ESM/FVdycoreCubed_GridComp
-
Notifications
You must be signed in to change notification settings - Fork 5
/
c2c.F90
464 lines (379 loc) · 12.7 KB
/
c2c.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
#define I_AM_MAIN
#include "MAPL_Generic.h"
#if defined(__INTEL_COMPILER)
# define _FTELL ftelli8
#elif defined(__PGI)
# define _FTELL ftell64
#else
# define _FTELL ftell
#endif
program gmao_regrid
use ESMF
use MAPL
implicit none
integer, parameter :: GridType_Unknown = 0
integer, parameter :: GridType_LatLon = 1
integer, parameter :: GridType_CubedSphere = 2
character(ESMF_MAXSTR) :: str
character(ESMF_MAXSTR) :: f_in
type(ESMF_VM) :: VM
integer :: filetype
logical :: changeResolution
#ifndef __GFORTRAN__
integer*4 :: iargc
external :: iargc
integer(kind=8) :: _FTELL
external :: _FTELL
#endif
! ErrLog variables
!-----------------
integer :: STATUS
character(len=ESMF_MAXSTR) :: Iam="GMAO_Regrid"
integer :: comm
type Regrid_GridInfo
character(len=ESMF_MAXSTR) :: filename
integer :: IM
integer :: JM
integer :: gridtype
end type Regrid_GridInfo
type(Regrid_GridInfo) :: gi
type(Regrid_GridInfo) :: gout
type(ESMF_Config) :: config
class (AbstractRegridder), pointer :: regridder
integer :: nargs
integer :: unit_r, unit_w
integer :: i,j,k,n
integer :: ic
integer :: im
integer :: ndes, myid
integer(kind=8) :: RecStart, RecEnd
integer, parameter :: LM = 72
real, allocatable :: var_in(:,:)
real, allocatable :: var_out(:,:)
real :: pref(LM+1)
integer, parameter :: iA=ichar('a')
integer, parameter :: mA=ichar('A')
integer, parameter :: iZ=ichar('z')
integer, parameter :: i0=ichar('0')
integer, parameter :: i9=ichar('9')
integer, parameter :: LatLonRes(2,8) = RESHAPE( SOURCE =&
[ 72, 46, & ! A - 4 degree
144, 91, & ! B - 2 degree
288, 181, & ! C - 1 degree
540, 361, & ! D - 1/2 degree MERRA
576, 361, & ! D - 1/2 degree
1152, 721, & ! E - 1/4 degree
1440, 721, & ! F - 1/4 degree rectangular
2880, 1441], & ! G - 1/8 degree rectangular
SHAPE = [2,8] )
!type(MAPL_NCIO) :: inNCIO,outNCIO
integer :: nDims, dimSizes(4),nSpatialDims
type (ESMF_Grid) :: gridIn, gridOut
integer :: rc
! Begin
nargs = iargc() ! get command line argument info
if (nargs /= 3) then
call getarg(0,str)
write(*,*) "Usage:",trim(str)," <file_in> <file_out> <Resolution(i.e. C180)>"
call exit(2)
end if
call getarg(1,f_in)
call ESMF_Initialize (vm=vm, logKindFlag=ESMF_LOGKIND_NONE, rc=status)
VERIFY_(STATUS)
call ESMF_VmGet(VM, localPet=myid, petCount=ndes, rc=status)
VERIFY_(STATUS)
if (ndes /= 1) then
print *,''
print *,'ERROR: currently PARALLEL jobs not supported'
print *,''
_ASSERT(.false.,'needs informative message')
end if
if (MAPL_AM_I_Root(vm)) then
call GuessFileType(f_in, filetype, rc=status)
VERIFY_(STATUS)
end if
call MAPL_CommsBcast(vm, DATA=filetype, N=1, ROOT=0, RC=status)
VERIFY_(STATUS)
! print *, filetype
gi%filename = f_in
! determine grid type, and compute/guess im,im
if (filetype ==0) then
!InNCIO = MAPL_NCIOOpen(f_in,rc=status)
!VERIFY_(STATUS)
!call GetGridInfo(gi, filetype, ncinfo=InNCIO, rc=status)
!VERIFY_(STATUS)
else
call GetGridInfo(gi, filetype, rc=status)
VERIFY_(STATUS)
end if
call getarg(2,str)
gout%filename = str
call getarg(3,str)
! convert STR to upprecase
do i = 1, len_trim(str)
ic=ichar(str(i:i))
if(ic >= iA .and. ic <= iZ) ic=ic+(mA-iA)
str(i:i)=char(ic)
end do
if (str(1:1) == 'B') then
gout%gridtype = GridType_LatLon
gout%IM = 144
gout%JM = 91
else if (str(1:1) == 'C') then
if (str(2:2) /= ' ') then
ic = ichar(str(2:2))
_ASSERT(ic >= i0 .and. ic <= i9,'needs informative message') ! Must be a number
read(str(2:),*) im
gout%gridtype = GridType_CubedSphere
gout%IM = im
gout%JM = 6*im
else
gout%gridtype = GridType_LatLon
gout%IM = 288
gout%JM = 181
end if
else if (str(1:1) == 'D') then
gout%gridtype = GridType_LatLon
gout%IM = 576
gout%JM = 361
else if (str(1:1) == 'E') then
gout%gridtype = GridType_LatLon
gout%IM = 1152
gout%JM = 721
else if (str(1:1) == 'F') then
if (str(2:2) /= ' ') then
ic = ichar(str(2:2))
_ASSERT(ic >= i0 .and. ic <= i9,'needs informative message') ! Must be a number
read(str(2:),*) im
gout%gridtype = GridType_CubedSphere
gout%IM = im
gout%JM = 6*im
else
gout%gridtype = GridType_LatLon
gout%IM = 1440
gout%JM = 721
end if
else if (str(1:1) == 'G') then
if (str(2:2) /= ' ') then
ic = ichar(str(2:2))
_ASSERT(ic >= i0 .and. ic <= i9,'needs informative message') ! Must be a number
read(str(2:),*) im
gout%gridtype = GridType_CubedSphere
gout%IM = im
gout%JM = 6*im
else
gout%gridtype = GridType_LatLon
gout%IM = 2880
gout%JM = 1441
end if
else
VERIFY_(999)
end if
changeResolution = gi%im /= gout%im .or. gi%jm /= gout%jm
if (changeResolution) then
! create horz regridder
gridIn = grid_manager%make_grid(CubedSphereGridFactory(im_world=gi%im,lm=1,nx=1,ny=1))
gridOut = grid_manager%make_grid(LatLonGridFactory(im_world=gout%im, jm_world=gout%jm, lm=1, nx=1, ny=1))
regridder => regridder_manager%make_regridder(gridIn, gridOut, REGRID_METHOD_BILINEAR, rc=status)
VERIFY_(STATUS)
! allocate buffers
allocate(var_in(gi%im, gi%jm), stat=status)
VERIFY_(STATUS)
allocate(var_out(gout%im, gout%jm), stat=status)
VERIFY_(STATUS)
if (filetype ==0) then
!call MAPL_NCIOChangeRes(InNCIO,OutNCIO,latSize=gout%jm,lonSize=gout%im,rc=status)
!VERIFY_(STATUS)
!call MAPL_NCIOSet(OutNCIO,filename=gout%filename)
!call MAPL_NCIOCreateFile(OutNCIO)
!do n=1,InNCIO%nVars
!call MAPL_NCIOVarGetDims(InNCIO,InNCIO%vars(n)%name,nDims,dimSizes,nSpatialDims=nSpatialDims)
!if (nSpatialDims == 2) then
!call MAPL_VarRead(InNCIO,InNCIO%vars(n)%name,var_in)
!call regridder%regrid(var_in, var_out, rc=status)
!VERIFY_(STATUS)
!call MAPL_VarWrite(OutNCIO,InNCIO%vars(n)%name,var_out)
!else if (nSpatialDims ==3) then
!do i=1,dimSizes(3)
!call MAPL_VarRead(InNCIO,InNCIO%vars(n)%name,var_in,lev=i)
!call regridder%regrid(var_in, var_out, rc=status)
!VERIFY_(STATUS)
!call MAPL_VarWrite(OutNCIO,InNCIO%vars(n)%name,var_out,lev=i)
!end do
!end if
!enddo
!call MAPL_NCIOClose(OutNCIO)
else
! open files
UNIT_R = GetFile(gi%filename, rc=status)
VERIFY_(STATUS)
UNIT_W = GetFile(gout%filename, rc=status)
VERIFY_(STATUS)
i=0
! do until EOF
do while (.true.)
! read record (level, slice, etc)
read(unit_r, err=100, end=200) var_in
i = i+1
! if not VertOnly
! transform
! write
call regridder%regrid(var_in, var_out, rc=status)
VERIFY_(STATUS)
write(unit_w) var_out
! print *,'record ',i
cycle
100 continue
RecEnd = _FTELL(unit_r)
backspace(unit_r)
RecStart = _FTELL(unit_r)
_ASSERT(4*((LM+1)+2) == RecEnd-RecStart,'needs informative message')
print *,'WARNING: encoutered shorter record, assuming PREF'
read (unit_r) pref
write(unit_w) pref
end do
200 continue
! end do
! close files
call FREE_FILE(UNIT_R)
call FREE_FILE(UNIT_W)
end if
deallocate(var_out, var_in)
else
print *, 'No change in resolution! Nothing to be done. Copy input to output yourself!'
end if
call ESMF_Finalize (RC=status)
VERIFY_(STATUS)
contains
! ================================================================
#undef I_AM_MAIN
subroutine GuessFileType(filename, filetype, rc)
use ESMF
use MAPL
implicit none
! Arguments
!----------
character(len=*), intent(IN ) :: filename
integer, intent(INOUT) :: filetype
integer, optional, intent( OUT) :: RC
! ErrLog variables
!-----------------
integer :: STATUS
character(len=ESMF_MAXSTR) :: Iam="GMAO_Regrid"
character(len=1) :: word(4)
character(len=1) :: TwoWords(8)
integer, parameter :: hdf5(8) = (/137, 72, 68, 70, &
13, 10, 26, 10 /)
integer :: irec
integer :: unit
integer :: i, nx, cwrd
logical :: typehdf5
UNIT = GETFILE(FILENAME, DO_OPEN=0, ALL_PES=.false., RC=STATUS)
VERIFY_(STATUS)
INQUIRE(IOLENGTH=IREC) WORD
open (UNIT=UNIT, FILE=FILENAME, FORM='unformatted', ACCESS='DIRECT', RECL=IREC, IOSTAT=status)
VERIFY_(STATUS)
! Read first 8 characters and compare with HDF5 signature
read (UNIT, REC=1, ERR=100) TwoWords(1:4)
read (UNIT, REC=2, ERR=100) TwoWords(5:8)
call FREE_FILE(UNIT)
typehdf5 = .true.
filetype = -1 ! Unknown
do i = 1, 8
if (iachar(TwoWords(i)) /= hdf5(i)) then
typehdf5 = .false.
exit
end if
end do
if (typehdf5) then
print *, 'HDF5 file'
filetype = 0 ! HDF5
RETURN_(ESMF_SUCCESS)
end if
! Attempt to identify as fortran binary
cwrd = transfer(TwoWords(1:4), irec)
! check if divisible by 4
irec = cwrd/4
filetype = irec
if (cwrd /= 4*irec) then
print *, "ERROR: not a Fortran binary"
RETURN_(ESMF_FAILURE)
end if
RETURN_(ESMF_SUCCESS)
100 continue
RETURN_(ESMF_FAILURE)
end subroutine GuessFileType
!subroutine GetGridInfo(gi, filetype, ncinfo, rc)
subroutine GetGridInfo(gi, filetype, rc)
use ESMF
use MAPL
implicit none
type(Regrid_GridInfo) :: gi
integer :: filetype
!type(MAPL_NCIO), optional, intent(in) :: ncinfo
integer, optional, intent(OUT):: RC
integer :: i6, im, jm
integer :: i
logical :: found
gi%gridtype = GridType_Unknown
if (filetype == 0) then
!gi%im=-1
!gi%jm=-1
!do i=1,ncinfo%ndims
!if ( trim(ncinfo%dims(i)%name) == "lon" ) then
!gi%im = ncinfo%dims(i)%len
!else if (trim(ncinfo%dims(i)%name) == "lat" ) then
!gi%jm = ncinfo%dims(i)%len
!end if
!enddo
!_ASSERT(gi%im /= -1,'needs informative message')
!_ASSERT(gi%jm /= -1,'needs informative message')
!if (gi%jm == gi%im*6) then
!gi%gridtype = GridType_CubedSphere
!else
!gi%gridtype = GridType_LatLon
!end if
!RETURN_(ESMF_SUCCESS)
end if
if (filetype == 6) then
print *, ""
print *,"FV binary not supported yet"
print *, ""
RETURN_(ESMF_FAILURE)
end if
! check for cubed-sphere
i6 = filetype/6
if (filetype == 6*i6) then
im = nint(sqrt(real(i6)))
if (i6 == im*im) then
! cubed-sphere
print *, 'cubed sphere C',im
gi%gridtype = GridType_CubedSphere
gi%im = im
gi%jm = 6*im
RETURN_(ESMF_SUCCESS)
end if
end if
! check for "known" lat-lon gridsizes
found = .false.
do i = 1, size(LatLonRes,2)
im = LatLonRes(1,i)
jm = LatLonRes(2,i)
if (filetype == im*jm) then
gi%gridtype = GridType_LatLon
gi%im = im
gi%jm = jm
found = .true.
exit
end if
end do
if (.not. found) then
gi%gridtype = GridType_Unknown
print *, ""
print *, 'Unknown input grid type, assumming TILE, currently not supported'
print *, ""
RETURN_(ESMF_FAILURE)
end if
RETURN_(ESMF_SUCCESS)
end subroutine GetGridInfo
end program gmao_regrid