m_io_mod_fld.F90 6.2 KB

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