m_io_mod_fld.f90 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # 0 "<stdin>"
  2. # 0 "<built-in>"
  3. # 0 "<command-line>"
  4. # 1 "/usr/include/stdc-predef.h" 1 3 4
  5. # 17 "/usr/include/stdc-predef.h" 3 4
  6. # 2 "<command-line>" 2
  7. # 1 "<stdin>"
  8. # 10 "<stdin>"
  9. module m_io_mod_fld
  10. ! Get or put one of the fields of a restart file, specified by
  11. ! ensemble number, field name and type, and vertical level. The
  12. ! time level is currently not used (restart files have only one)
  13. ! but who knows. Grid dimension is also needed, as well as if you
  14. ! want to 'get' or 'put'.
  15. !
  16. ! This replaces the two routines 'm_get_mod_fld.F90' and m_put_mod_fld.F90'.
  17. ! There was so much overlap that it became easier to merge the two. I think.
  18. !
  19. ! (c) July 2009, Christof.KonigBeatty@uclouvain.be
  20. use netcdf
  21. use qmpi
  22. private handle_err
  23. contains
  24. subroutine io_mod_fld(fld,k,enslist,cfld,type,vlevel,tlevel,nx,ny,gorp,rdate_obs)
  25. implicit none
  26. ! In/out
  27. real,dimension(nx,ny),intent(inout):: fld ! output fl
  28. integer, intent(in) :: k ! Index to enslist
  29. integer,dimension(:), intent(in) :: enslist! List of existing ensemble members
  30. character(len=*), intent(in) :: cfld ! name of fld
  31. integer, intent(in) :: type ! which file to use
  32. integer, intent(in) :: vlevel ! vertical level (ignored)
  33. integer, intent(in) :: tlevel ! time level (ignored)
  34. integer, intent(in) :: nx,ny ! Grid dimension
  35. character(len=3), intent(in) :: gorp ! 'get' or 'put' (sorry, couldn't come up with anything better)
  36. real(kind=8), intent(in) :: rdate_obs
  37. ! NetCDF vars
  38. integer :: iens ! Ensemble member to read
  39. character(len=99) :: fcfile, anafile, cfile
  40. integer :: ncid, varID, error
  41. logical :: exfc, exan
  42. ! Other
  43. character(len=3) :: cmem
  44. integer :: zvlevel ! for i/o ocean variable
  45. real(kind=8) :: rdate_mod
  46. ! Find iens withing enslist
  47. iens = enslist(k)
  48. ! Create filename dep. on type of variable/parameter requested
  49. write(cmem,'(i3.3)') 100+iens ! iens=1 gives cmem = 101
  50. select case( type )
  51. case(1) ! ice variable
  52. fcfile ='forecast_ice_'//cmem//'.nc'
  53. anafile='analysis_ice_'//cmem//'.nc'
  54. case(2) ! ocean variable
  55. fcfile ='forecast_oce_'//cmem//'.nc'
  56. anafile='analysis_oce_'//cmem//'.nc'
  57. zvlevel = max(vlevel,1)
  58. case(3) ! ice namelist parameter
  59. if (master) print *, '(io_mod_fld): ice parameter writing not implemented yet!'
  60. call stop_mpi()
  61. case(4) ! ocean namelist parameter
  62. if (master) print *, '(io_mod_fld): ocean parameter writing not implemented yet!'
  63. call stop_mpi()
  64. case default
  65. if (master) print *, '(io_mod_fld): variable type not understood!'
  66. call stop_mpi()
  67. end select
  68. ! If the fc file exists we turn it into the analysis file (unless that's already there).
  69. inquire(file=fcfile, exist=exfc)
  70. inquire(file=anafile, exist=exan)
  71. if ((.not.exfc).and.(.not.exan)) then ! Neither file is there
  72. if (master) print *, '(io_mod_fld): Restart file '//cmem//' missing!'
  73. call stop_mpi()
  74. elseif (exfc.and.(.not.exan)) then ! fcfile here but no anafile
  75. ! call system('mv '//trim(fcfile)//' '//trim(anafile) ) ! "operational" to save space
  76. call system('cp '//trim(fcfile)//' '//trim(anafile) ) ! for debugging
  77. end if
  78. ! Decide on which file to use
  79. if (gorp=='get') cfile=fcfile
  80. if (gorp=='put') cfile=anafile
  81. ! ckb prefers only one file at the time, so take care of this special case
  82. inquire(file=fcfile, exist=exfc)
  83. if (.not.exfc) cfile=anafile
  84. !!$ !XXX:
  85. !!$ write(*,*) "XXX: "
  86. !!$ write(*,*) "XXX: iens : ", iens
  87. !!$ write(*,*) "XXX: cfld : ", cfld
  88. !!$ write(*,*) "XXX: type : ", type
  89. !!$ write(*,*) "XXX: nx, ny, zvlevel: ", nx, ny, zvlevel
  90. !!$ write(*,*) "XXX: fcfile : ", trim(fcfile)
  91. !!$ write(*,*) "XXX: anafile : ", trim(anafile)
  92. !!$ write(*,*) "XXX: shape(fldIO) : ", shape(fldIO)
  93. !!$ write(*,*) "XXX: "
  94. !!$ !:XXX
  95. ! open the netCDF file
  96. error = nf90_open(trim(cfile),nf90_Write,ncid); if (error.ne.nf90_noerr) call handle_err(error, "opening")
  97. ! Find VarID of cfld
  98. error = nf90_inq_varid(ncid, trim(cfld), varID); if (error.ne.nf90_noerr) call handle_err(error, "inquiring varID")
  99. ! Put/Get
  100. select case( type )
  101. case(1, 3, 4) ! 2D
  102. if (gorp=='get') then
  103. error = nf90_get_var(ncid, varID, fld); if (error.ne.nf90_noerr) call handle_err(error, "getting 2D variable")
  104. elseif (gorp=='put') then
  105. error = nf90_put_var(ncid, varID, fld); if (error.ne.nf90_noerr) call handle_err(error, "putting 2D variable")
  106. else
  107. if (master) print *, "(io_mod_fld): Either 'put' or 'get'!"
  108. call stop_mpi()
  109. endif
  110. case(2) ! 3D ocean variable
  111. if (gorp=='get') then
  112. error = nf90_get_var(ncid, varID, fld, start=(/1,1,zvlevel/), count=(/nx,ny,1/))
  113. if (error.ne.nf90_noerr) call handle_err(error, "getting ocean variable")
  114. elseif (gorp=='put') then
  115. error = nf90_put_var(ncid, varID, fld, start=(/1,1,zvlevel/), count=(/nx,ny,1/))
  116. if (error.ne.nf90_noerr) call handle_err(error, "putting ocean variable")
  117. else
  118. if (master) print *, "(io_mod_fld): Either 'put' or 'get'!"
  119. call stop_mpi()
  120. endif
  121. end select
  122. !if (master) PRINT *, " Find VarID of cfld "
  123. error = nf90_inq_varid(ncid, 'time_counter', varID); if (error.ne.nf90_noerr) call handle_err(error, "inquiring varID")
  124. error = nf90_get_var(ncid, varID, rdate_mod); if (error.ne.nf90_noerr) call handle_err(error, "getting ocean variable")
  125. ! Close file
  126. error = nf90_close(ncid); if (error.ne.nf90_noerr) call handle_err(error, "closing")
  127. ! Check date mode and date obs
  128. IF (INT(rdate_mod) .NE. INT(rdate_obs)) THEN
  129. !PRINT *, 'date mod not egal to date obs, stop, (',INT(rdate_mod),' ',INT(rdate_obs),')'
  130. !STOP 1
  131. END IF
  132. end subroutine io_mod_fld
  133. subroutine handle_err(status, infomsg)
  134. integer, intent ( in) :: status
  135. character(len = *), intent ( in), optional :: infomsg
  136. if(status /= nf90_noerr) then
  137. if (master) then
  138. if (present(infomsg)) then
  139. print *, 'Error while '//infomsg//' - '//trim(nf90_strerror(status))
  140. else
  141. print *, trim(nf90_strerror(status))
  142. endif ! opt arg
  143. print *,'(io_mod_fld)'
  144. endif ! only master outputs
  145. call stop_mpi()
  146. end if ! check error status
  147. end subroutine handle_err
  148. end module m_io_mod_fld