LCOV - code coverage report
Current view: top level - cicecore/cicedynB/infrastructure/io/io_binary - ice_restart.F90 (source / functions) Hit Total Coverage
Test: 200617-180449:aec9683041:7:first,base,travis,decomp,reprosum,io,quick Lines: 367 419 87.59 %
Date: 2020-06-17 18:05:09 Functions: 5 5 100.00 %

          Line data    Source code
       1             : !=======================================================================
       2             : 
       3             : ! Read and write ice model restart files using netCDF or binary
       4             : ! interfaces.
       5             : 
       6             : ! authors David A Bailey, NCAR
       7             : 
       8             :       module ice_restart
       9             : 
      10             :       use ice_broadcast
      11             :       use ice_kinds_mod
      12             :       use ice_restart_shared, only: &
      13             :           restart, restart_ext, restart_dir, restart_file, pointer_file, &
      14             :           runid, runtype, use_restart_time, lenstr
      15             :       use ice_fileunits, only: nu_diag, nu_rst_pointer
      16             :       use ice_fileunits, only: nu_dump, nu_dump_eap, nu_dump_FY, nu_dump_age
      17             :       use ice_fileunits, only: nu_dump_lvl, nu_dump_pond, nu_dump_hbrine
      18             :       use ice_fileunits, only: nu_dump_bgc, nu_dump_aero, nu_dump_fsd, nu_dump_iso
      19             :       use ice_fileunits, only: nu_restart, nu_restart_eap, nu_restart_FY, nu_restart_age 
      20             :       use ice_fileunits, only: nu_restart_lvl, nu_restart_pond, nu_restart_hbrine
      21             :       use ice_fileunits, only: nu_restart_bgc, nu_restart_aero, nu_restart_fsd
      22             :       use ice_fileunits, only: nu_restart_iso
      23             :       use ice_exit, only: abort_ice
      24             :       use icepack_intfc, only: icepack_query_parameters
      25             :       use icepack_intfc, only: icepack_query_tracer_sizes
      26             :       use icepack_intfc, only: icepack_query_tracer_flags
      27             :       use icepack_intfc, only: icepack_warnings_flush, icepack_warnings_aborted
      28             : 
      29             :       implicit none
      30             :       private
      31             :       public :: init_restart_write, init_restart_read, &
      32             :                 read_restart_field, write_restart_field, final_restart
      33             : 
      34             : !=======================================================================
      35             : 
      36             :       contains
      37             : 
      38             : !=======================================================================
      39             : 
      40             : ! Sets up restart file for reading.
      41             : ! author David A Bailey, NCAR
      42             : 
      43         126 :       subroutine init_restart_read(ice_ic)
      44             : 
      45             :       use ice_calendar, only: istep0, istep1, time, time_forc, npt, nyr
      46             :       use ice_communicate, only: my_task, master_task
      47             :       use ice_dyn_shared, only: kdyn
      48             :       use ice_read_write, only: ice_open, ice_open_ext
      49             : 
      50             :       character(len=char_len_long), intent(in), optional :: ice_ic
      51             : 
      52             :       ! local variables
      53             : 
      54             :       logical (kind=log_kind) :: &
      55             :          solve_zsal, tr_fsd, &
      56             :          tr_iage, tr_FY, tr_lvl, tr_iso, tr_aero, tr_pond_cesm, &
      57             :          tr_pond_topo, tr_pond_lvl, tr_brine
      58             : 
      59             :       character(len=char_len_long) :: &
      60             :          filename, filename0
      61             : 
      62             :       integer (kind=int_kind) :: &
      63             :          n, &                    ! loop indices
      64             :          nbtrcr, &               ! number of bgc tracers
      65             :          iignore                 ! dummy variable
      66             : 
      67             :       real (kind=real_kind) :: &
      68           8 :          rignore                 ! dummy variable
      69             : 
      70             :       character(len=char_len_long) :: &
      71             :          string1, string2
      72             : 
      73             :       character(len=*), parameter :: subname = '(init_restart_read)'
      74             : 
      75             :       call icepack_query_parameters( &
      76         134 :          solve_zsal_out=solve_zsal)
      77             :       call icepack_query_tracer_sizes( &
      78         134 :          nbtrcr_out=nbtrcr)
      79             :       call icepack_query_tracer_flags( &
      80             :          tr_iage_out=tr_iage, tr_FY_out=tr_FY, tr_lvl_out=tr_lvl, tr_fsd_out=tr_fsd, &
      81             :          tr_iso_out=tr_iso, tr_aero_out=tr_aero, tr_pond_cesm_out=tr_pond_cesm, &
      82         134 :          tr_pond_topo_out=tr_pond_topo, tr_pond_lvl_out=tr_pond_lvl, tr_brine_out=tr_brine)
      83         134 :       call icepack_warnings_flush(nu_diag)
      84         134 :       if (icepack_warnings_aborted()) call abort_ice(error_message=subname, &
      85           0 :          file=__FILE__, line=__LINE__)
      86             : 
      87         134 :       if (present(ice_ic)) then 
      88           0 :          filename = trim(ice_ic)
      89             :       else
      90         134 :          if (my_task == master_task) then
      91          11 :             open(nu_rst_pointer,file=pointer_file)
      92          11 :             read(nu_rst_pointer,'(a)') filename0
      93          11 :             filename = trim(filename0)
      94          11 :             close(nu_rst_pointer)
      95          11 :             write(nu_diag,*) 'Read ',pointer_file(1:lenstr(pointer_file))
      96             :          endif
      97         134 :          call broadcast_scalar(filename, master_task)
      98             :       endif
      99             : 
     100         134 :       if (my_task == master_task) then
     101          11 :          write(nu_diag,*) 'Using restart dump=', trim(filename)
     102          11 :          if (restart_ext) then
     103           0 :             call ice_open_ext(nu_restart,trim(filename),0)
     104             :          else
     105          11 :             call ice_open(nu_restart,trim(filename),0)
     106             :          endif
     107          11 :          if (use_restart_time) then
     108          11 :             read (nu_restart) istep0,time,time_forc,nyr
     109             :          else
     110           0 :             read (nu_restart) iignore,rignore,rignore ! use namelist values
     111             :          endif
     112          11 :          write(nu_diag,*) 'Restart read at istep=',istep0,time,time_forc
     113             :       endif
     114             : 
     115         134 :       call broadcast_scalar(istep0,master_task)
     116         134 :       call broadcast_scalar(time,master_task)
     117         134 :       call broadcast_scalar(time_forc,master_task)
     118         134 :       call broadcast_scalar(nyr,master_task)
     119             :       
     120         134 :       istep1 = istep0
     121             : 
     122             :       ! Supplemental restart files
     123             : 
     124         134 :       if (kdyn == 2) then
     125           4 :          if (my_task == master_task) then
     126           1 :             n = index(filename0,trim(restart_file))
     127           1 :             if (n == 0) call abort_ice(subname//'ERROR: eap restart: filename discrepancy')
     128           1 :             string1 = trim(filename0(1:n-1))
     129           1 :             string2 = trim(filename0(n+lenstr(restart_file):lenstr(filename0)))
     130           0 :             write(filename,'(a,a,a,a)') &
     131           1 :                string1(1:lenstr(string1)), &
     132           1 :                restart_file(1:lenstr(restart_file)),'.eap', &
     133           2 :                string2(1:lenstr(string2))
     134           1 :             if (restart_ext) then
     135           0 :                call ice_open_ext(nu_restart_eap,filename,0)
     136             :             else
     137           1 :                call ice_open(nu_restart_eap,filename,0)
     138             :             endif
     139           1 :             read (nu_restart_eap) iignore,rignore,rignore
     140           1 :             write(nu_diag,*) 'Reading ',filename(1:lenstr(filename))
     141             :          endif
     142             :       endif
     143             : 
     144         134 :       if (tr_fsd) then
     145          16 :          if (my_task == master_task) then
     146           1 :             n = index(filename0,trim(restart_file))
     147           1 :             if (n == 0) call abort_ice(subname//'ERROR: fsd restart: filename discrepancy')
     148           1 :             string1 = trim(filename0(1:n-1))
     149           1 :             string2 = trim(filename0(n+lenstr(restart_file):lenstr(filename0)))
     150           0 :             write(filename,'(a,a,a,a)') &
     151           1 :                string1(1:lenstr(string1)), &
     152           1 :                restart_file(1:lenstr(restart_file)),'.fsd', &
     153           2 :                string2(1:lenstr(string2))
     154           1 :             if (restart_ext) then
     155           0 :                call ice_open_ext(nu_restart_fsd,filename,0)
     156             :             else
     157           1 :                call ice_open(nu_restart_fsd,filename,0)
     158             :             endif
     159           1 :             read (nu_restart_fsd) iignore,rignore,rignore
     160           1 :             write(nu_diag,*) 'Reading ',filename(1:lenstr(filename))
     161             :          endif
     162             :       endif
     163             : 
     164         134 :       if (tr_iage) then
     165         114 :          if (my_task == master_task) then
     166           8 :             n = index(filename0,trim(restart_file))
     167           8 :             if (n == 0) call abort_ice(subname//'ERROR: iage restart: filename discrepancy')
     168           8 :             string1 = trim(filename0(1:n-1))
     169           8 :             string2 = trim(filename0(n+lenstr(restart_file):lenstr(filename0)))
     170           1 :             write(filename,'(a,a,a,a)') &
     171          10 :                string1(1:lenstr(string1)), &
     172          10 :                restart_file(1:lenstr(restart_file)),'.iage', &
     173          17 :                string2(1:lenstr(string2))
     174           8 :             if (restart_ext) then
     175           0 :                call ice_open_ext(nu_restart_age,filename,0)
     176             :             else
     177           8 :                call ice_open(nu_restart_age,filename,0)
     178             :             endif
     179           8 :             read (nu_restart_age) iignore,rignore,rignore
     180           8 :             write(nu_diag,*) 'Reading ',filename(1:lenstr(filename))
     181             :          endif
     182             :       endif
     183             : 
     184         134 :       if (tr_FY) then
     185         114 :          if (my_task == master_task) then
     186           8 :             n = index(filename0,trim(restart_file))
     187           8 :             if (n == 0) call abort_ice(subname//'ERROR: FY restart: filename discrepancy')
     188           8 :             string1 = trim(filename0(1:n-1))
     189           8 :             string2 = trim(filename0(n+lenstr(restart_file):lenstr(filename0)))
     190           1 :             write(filename,'(a,a,a,a)') &
     191          10 :                string1(1:lenstr(string1)), &
     192          10 :                restart_file(1:lenstr(restart_file)),'.FY', &
     193          17 :                string2(1:lenstr(string2))
     194           8 :             if (restart_ext) then
     195           0 :                call ice_open_ext(nu_restart_FY,filename,0)
     196             :             else
     197           8 :                call ice_open(nu_restart_FY,filename,0)
     198             :             endif
     199           8 :             read (nu_restart_FY) iignore,rignore,rignore
     200           8 :             write(nu_diag,*) 'Reading ',filename(1:lenstr(filename))
     201             :          endif
     202             :       endif
     203             : 
     204         134 :       if (tr_lvl) then
     205         126 :          if (my_task == master_task) then
     206           9 :             n = index(filename0,trim(restart_file))
     207           9 :             if (n == 0) call abort_ice(subname//'ERROR: lvl restart: filename discrepancy')
     208           9 :             string1 = trim(filename0(1:n-1))
     209           9 :             string2 = trim(filename0(n+lenstr(restart_file):lenstr(filename0)))
     210           1 :             write(filename,'(a,a,a,a)') &
     211          11 :                string1(1:lenstr(string1)), &
     212          11 :                restart_file(1:lenstr(restart_file)),'.lvl', &
     213          19 :                string2(1:lenstr(string2))
     214           9 :             if (restart_ext) then
     215           0 :                call ice_open_ext(nu_restart_lvl,filename,0)
     216             :             else
     217           9 :                call ice_open(nu_restart_lvl,filename,0)
     218             :             endif
     219           9 :             read (nu_restart_lvl) iignore,rignore,rignore
     220           9 :             write(nu_diag,*) 'Reading ',filename(1:lenstr(filename))
     221             :          endif
     222             :       endif
     223             : 
     224         134 :       if (tr_pond_cesm) then
     225           4 :          if (my_task == master_task) then
     226           1 :             n = index(filename0,trim(restart_file))
     227           1 :             if (n == 0) call abort_ice(subname//'ERROR: pond_cesm restart: filename discrepancy')
     228           1 :             string1 = trim(filename0(1:n-1))
     229           1 :             string2 = trim(filename0(n+lenstr(restart_file):lenstr(filename0)))
     230           0 :             write(filename,'(a,a,a,a)') &
     231           1 :                string1(1:lenstr(string1)), &
     232           1 :                restart_file(1:lenstr(restart_file)),'.pond_cesm', &
     233           2 :                string2(1:lenstr(string2))
     234           1 :             if (restart_ext) then
     235           0 :                call ice_open_ext(nu_restart_pond,filename,0)
     236             :             else
     237           1 :                call ice_open(nu_restart_pond,filename,0)
     238             :             endif
     239           1 :             read (nu_restart_pond) iignore,rignore,rignore
     240           1 :             write(nu_diag,*) 'Reading ',filename(1:lenstr(filename))
     241             :          endif
     242             :       endif
     243             : 
     244         134 :       if (tr_pond_lvl) then
     245          98 :          if (my_task == master_task) then
     246           7 :             n = index(filename0,trim(restart_file))
     247           7 :             if (n == 0) call abort_ice(subname//'ERROR:pond_lvl restart: filename discrepancy')
     248           7 :             string1 = trim(filename0(1:n-1))
     249           7 :             string2 = trim(filename0(n+lenstr(restart_file):lenstr(filename0)))
     250           1 :             write(filename,'(a,a,a,a)') &
     251           9 :                string1(1:lenstr(string1)), &
     252           9 :                restart_file(1:lenstr(restart_file)),'.pond_lvl', &
     253          15 :                string2(1:lenstr(string2))
     254           7 :             if (restart_ext) then
     255           0 :                call ice_open_ext(nu_restart_pond,filename,0)
     256             :             else
     257           7 :                call ice_open(nu_restart_pond,filename,0)
     258             :             endif
     259           7 :             read (nu_restart_pond) iignore,rignore,rignore
     260           7 :             write(nu_diag,*) 'Reading ',filename(1:lenstr(filename))
     261             :          endif
     262             :       endif
     263             : 
     264         134 :       if (tr_pond_topo) then
     265           4 :          if (my_task == master_task) then
     266           1 :             n = index(filename0,trim(restart_file))
     267           1 :             if (n == 0) call abort_ice(subname//'ERROR: pond_topo restart: filename discrepancy')
     268           1 :             string1 = trim(filename0(1:n-1))
     269           1 :             string2 = trim(filename0(n+lenstr(restart_file):lenstr(filename0)))
     270           0 :             write(filename,'(a,a,a,a)') &
     271           1 :                string1(1:lenstr(string1)), &
     272           1 :                restart_file(1:lenstr(restart_file)),'.pond_topo', &
     273           2 :                string2(1:lenstr(string2))
     274           1 :             if (restart_ext) then
     275           0 :                call ice_open_ext(nu_restart_pond,filename,0)
     276             :             else
     277           1 :                call ice_open(nu_restart_pond,filename,0)
     278             :             endif
     279           1 :             read (nu_restart_pond) iignore,rignore,rignore
     280           1 :             write(nu_diag,*) 'Reading ',filename(1:lenstr(filename))
     281             :          endif
     282             :       endif
     283             : 
     284         134 :       if (tr_brine) then
     285          48 :          if (my_task == master_task) then
     286           2 :             n = index(filename0,trim(restart_file))
     287           2 :             if (n == 0) call abort_ice(subname//'ERROR: brine restart: filename discrepancy')
     288           2 :             string1 = trim(filename0(1:n-1))
     289           2 :             string2 = trim(filename0(n+lenstr(restart_file):lenstr(filename0)))
     290           0 :             write(filename,'(a,a,a,a)') &
     291           2 :                string1(1:lenstr(string1)), &
     292           2 :                restart_file(1:lenstr(restart_file)),'.brine', &
     293           4 :                string2(1:lenstr(string2))
     294           2 :             if (restart_ext) then
     295           0 :                call ice_open_ext(nu_restart_hbrine,filename,0)
     296             :             else
     297           2 :                call ice_open(nu_restart_hbrine,filename,0)
     298             :             endif
     299           2 :             read (nu_restart_hbrine) iignore,rignore,rignore
     300           2 :             write(nu_diag,*) 'Reading ',filename(1:lenstr(filename))
     301             :          endif
     302             :       endif
     303             : 
     304         134 :       if (solve_zsal .or. nbtrcr > 0) then
     305          48 :          if (my_task == master_task) then
     306           2 :             n = index(filename0,trim(restart_file))
     307           2 :             if (n == 0) call abort_ice(subname//'ERROR: bgc restart: filename discrepancy')
     308           2 :             string1 = trim(filename0(1:n-1))
     309           2 :             string2 = trim(filename0(n+lenstr(restart_file):lenstr(filename0)))
     310           0 :             write(filename,'(a,a,a,a)') &
     311           2 :                string1(1:lenstr(string1)), &
     312           2 :                restart_file(1:lenstr(restart_file)),'.bgc', &
     313           4 :                string2(1:lenstr(string2))
     314           2 :             if (restart_ext) then
     315           0 :                call ice_open_ext(nu_restart_bgc,filename,0)
     316             :             else
     317           2 :                call ice_open(nu_restart_bgc,filename,0)
     318             :             endif
     319           2 :             read (nu_restart_bgc) iignore,rignore,rignore
     320           2 :             write(nu_diag,*) 'Reading ',filename(1:lenstr(filename))
     321             :          endif
     322             :       endif
     323             : 
     324         134 :       if (tr_iso) then
     325          14 :          if (my_task == master_task) then
     326           1 :             n = index(filename0,trim(restart_file))
     327           1 :             if (n == 0) call abort_ice(subname//'ERROR: iso restart: filename discrepancy')
     328           1 :             string1 = trim(filename0(1:n-1))
     329           1 :             string2 = trim(filename0(n+lenstr(restart_file):lenstr(filename0)))
     330           0 :             write(filename,'(a,a,a,a)') &
     331           1 :                string1(1:lenstr(string1)), &
     332           1 :                restart_file(1:lenstr(restart_file)),'.iso', &
     333           2 :                string2(1:lenstr(string2))
     334           1 :             if (restart_ext) then
     335           0 :                call ice_open_ext(nu_restart_iso,filename,0)
     336             :             else
     337           1 :                call ice_open(nu_restart_iso,filename,0)
     338             :             endif
     339           1 :             read (nu_restart_iso) iignore,rignore,rignore
     340           1 :             write(nu_diag,*) 'Reading ',filename(1:lenstr(filename))
     341             :          endif
     342             :       endif
     343             : 
     344         134 :       if (tr_aero) then
     345          12 :          if (my_task == master_task) then
     346           2 :             n = index(filename0,trim(restart_file))
     347           2 :             if (n == 0) call abort_ice(subname//'ERROR: aero restart: filename discrepancy')
     348           2 :             string1 = trim(filename0(1:n-1))
     349           2 :             string2 = trim(filename0(n+lenstr(restart_file):lenstr(filename0)))
     350           0 :             write(filename,'(a,a,a,a)') &
     351           2 :                string1(1:lenstr(string1)), &
     352           2 :                restart_file(1:lenstr(restart_file)),'.aero', &
     353           4 :                string2(1:lenstr(string2))
     354           2 :             if (restart_ext) then
     355           0 :                call ice_open_ext(nu_restart_aero,filename,0)
     356             :             else
     357           2 :                call ice_open(nu_restart_aero,filename,0)
     358             :             endif
     359           2 :             read (nu_restart_aero) iignore,rignore,rignore
     360           2 :             write(nu_diag,*) 'Reading ',filename(1:lenstr(filename))
     361             :          endif
     362             :       endif
     363             : 
     364             :       ! if runid is bering then need to correct npt for istep0
     365         134 :       if (trim(runid) == 'bering') then
     366           0 :          npt = npt - istep0
     367             :       endif
     368             : 
     369         134 :       end subroutine init_restart_read
     370             : 
     371             : !=======================================================================
     372             : 
     373             : ! Sets up restart file for writing.
     374             : ! author David A Bailey, NCAR
     375             : 
     376         378 :       subroutine init_restart_write(filename_spec)
     377             : 
     378             :       use ice_calendar, only: sec, month, mday, nyr, istep1, &
     379           8 :                               time, time_forc, year_init
     380             :       use ice_communicate, only: my_task, master_task
     381             :       use ice_dyn_shared, only: kdyn
     382             :       use ice_read_write, only: ice_open, ice_open_ext
     383             : 
     384             :       character(len=char_len_long), intent(in), optional :: filename_spec
     385             : 
     386             :       ! local variables
     387             : 
     388             :       logical (kind=log_kind) :: &
     389             :          solve_zsal, tr_fsd, &
     390             :          tr_iage, tr_FY, tr_lvl, tr_iso, tr_aero, tr_pond_cesm, &
     391             :          tr_pond_topo, tr_pond_lvl, tr_brine
     392             : 
     393             :       integer (kind=int_kind) :: &
     394             :          nbtrcr, &               ! number of bgc tracers
     395             :          iyear, imonth, iday     ! year, month, day
     396             : 
     397             :       character(len=char_len_long) :: filename
     398             : 
     399             :       character(len=*), parameter :: subname = '(init_restart_write)'
     400             : 
     401             :       call icepack_query_parameters( &
     402         402 :          solve_zsal_out=solve_zsal)
     403             :       call icepack_query_tracer_sizes( &
     404         402 :          nbtrcr_out=nbtrcr)
     405             :       call icepack_query_tracer_flags( &
     406             :          tr_iage_out=tr_iage, tr_FY_out=tr_FY, tr_lvl_out=tr_lvl, tr_fsd_out=tr_fsd, &
     407             :          tr_iso_out=tr_iso, tr_aero_out=tr_aero, tr_pond_cesm_out=tr_pond_cesm, &
     408         402 :          tr_pond_topo_out=tr_pond_topo, tr_pond_lvl_out=tr_pond_lvl, tr_brine_out=tr_brine)
     409         402 :       call icepack_warnings_flush(nu_diag)
     410         402 :       if (icepack_warnings_aborted()) call abort_ice(error_message=subname, &
     411           0 :          file=__FILE__, line=__LINE__)
     412             : 
     413             :       ! construct path/file
     414         402 :       if (present(filename_spec)) then
     415           0 :          filename = trim(filename_spec)
     416             :       else
     417         402 :          iyear = nyr + year_init - 1
     418         402 :          imonth = month
     419         402 :          iday = mday
     420             :       
     421          24 :          write(filename,'(a,a,a,i4.4,a,i2.2,a,i2.2,a,i5.5)') &
     422         450 :               restart_dir(1:lenstr(restart_dir)), &
     423         426 :               restart_file(1:lenstr(restart_file)),'.', &
     424         804 :               iyear,'-',month,'-',mday,'-',sec
     425             :       end if
     426             :         
     427             :       ! write pointer (path/file)
     428         402 :       if (my_task == master_task) then
     429          33 :          open(nu_rst_pointer,file=pointer_file)
     430          33 :          write(nu_rst_pointer,'(a)') filename
     431          33 :          close(nu_rst_pointer)
     432          33 :          if (restart_ext) then
     433           0 :             call ice_open_ext(nu_dump,filename,0)
     434             :          else
     435          33 :             call ice_open(nu_dump,filename,0)
     436             :          endif
     437          33 :          write(nu_dump) istep1,time,time_forc,nyr
     438          33 :          write(nu_diag,*) 'Writing ',filename(1:lenstr(filename))
     439             :       endif
     440             : 
     441             :       ! begin writing restart data
     442             : 
     443         402 :       if (kdyn == 2) then
     444             : 
     445           0 :          write(filename,'(a,a,a,i4.4,a,i2.2,a,i2.2,a,i5.5)') &
     446          12 :               restart_dir(1:lenstr(restart_dir)), &
     447          12 :               restart_file(1:lenstr(restart_file)),'.eap.', &
     448          24 :               iyear,'-',month,'-',mday,'-',sec
     449             : 
     450          12 :          if (restart_ext) then
     451           0 :             call ice_open_ext(nu_dump_eap,filename,0)
     452             :          else
     453          12 :             call ice_open(nu_dump_eap,filename,0)
     454             :          endif
     455             : 
     456          12 :          if (my_task == master_task) then
     457           3 :            write(nu_dump_eap) istep1,time,time_forc
     458           3 :            write(nu_diag,*) 'Writing ',filename(1:lenstr(filename))
     459             :          endif
     460             : 
     461             :       endif
     462             : 
     463         402 :       if (tr_fsd) then
     464             : 
     465           0 :          write(filename,'(a,a,a,i4.4,a,i2.2,a,i2.2,a,i5.5)') &
     466          48 :               restart_dir(1:lenstr(restart_dir)), &
     467          48 :               restart_file(1:lenstr(restart_file)),'.fsd.', &
     468          96 :               iyear,'-',month,'-',mday,'-',sec
     469             : 
     470          48 :          if (restart_ext) then
     471           0 :             call ice_open_ext(nu_dump_fsd,filename,0)
     472             :          else
     473          48 :             call ice_open(nu_dump_fsd,filename,0)
     474             :          endif
     475             : 
     476          48 :          if (my_task == master_task) then
     477           3 :            write(nu_dump_fsd) istep1,time,time_forc
     478           3 :            write(nu_diag,*) 'Writing ',filename(1:lenstr(filename))
     479             :          endif
     480             : 
     481             :       endif
     482             : 
     483         402 :       if (tr_FY) then
     484             : 
     485          24 :          write(filename,'(a,a,a,i4.4,a,i2.2,a,i2.2,a,i5.5)') &
     486         390 :               restart_dir(1:lenstr(restart_dir)), &
     487         366 :               restart_file(1:lenstr(restart_file)),'.FY.', &
     488         684 :               iyear,'-',month,'-',mday,'-',sec
     489             : 
     490         342 :          if (restart_ext) then
     491           0 :             call ice_open_ext(nu_dump_FY,filename,0)
     492             :          else
     493         342 :             call ice_open(nu_dump_FY,filename,0)
     494             :          endif
     495             : 
     496         342 :          if (my_task == master_task) then
     497          24 :            write(nu_dump_FY) istep1,time,time_forc
     498          24 :            write(nu_diag,*) 'Writing ',filename(1:lenstr(filename))
     499             :          endif
     500             : 
     501             :       endif
     502             : 
     503         402 :       if (tr_iage) then
     504             : 
     505          24 :          write(filename,'(a,a,a,i4.4,a,i2.2,a,i2.2,a,i5.5)') &
     506         390 :               restart_dir(1:lenstr(restart_dir)), &
     507         366 :               restart_file(1:lenstr(restart_file)),'.iage.', &
     508         684 :               iyear,'-',month,'-',mday,'-',sec
     509             : 
     510         342 :          if (restart_ext) then
     511           0 :             call ice_open_ext(nu_dump_age,filename,0)
     512             :          else
     513         342 :             call ice_open(nu_dump_age,filename,0)
     514             :          endif
     515             : 
     516         342 :          if (my_task == master_task) then
     517          24 :            write(nu_dump_age) istep1,time,time_forc
     518          24 :            write(nu_diag,*) 'Writing ',filename(1:lenstr(filename))
     519             :          endif
     520             : 
     521             :       endif
     522             : 
     523         402 :       if (tr_lvl) then
     524             : 
     525          24 :          write(filename,'(a,a,a,i4.4,a,i2.2,a,i2.2,a,i5.5)') &
     526         426 :               restart_dir(1:lenstr(restart_dir)), &
     527         402 :               restart_file(1:lenstr(restart_file)),'.lvl.', &
     528         756 :               iyear,'-',month,'-',mday,'-',sec
     529             : 
     530         378 :          if (restart_ext) then
     531           0 :             call ice_open_ext(nu_dump_lvl,filename,0)
     532             :          else
     533         378 :             call ice_open(nu_dump_lvl,filename,0)
     534             :          endif
     535             : 
     536         378 :          if (my_task == master_task) then
     537          27 :            write(nu_dump_lvl) istep1,time,time_forc
     538          27 :            write(nu_diag,*) 'Writing ',filename(1:lenstr(filename))
     539             :          endif
     540             : 
     541             :       endif
     542             : 
     543         402 :       if (tr_pond_cesm) then
     544             : 
     545           0 :          write(filename,'(a,a,a,i4.4,a,i2.2,a,i2.2,a,i5.5)') &
     546          12 :               restart_dir(1:lenstr(restart_dir)), &
     547          12 :               restart_file(1:lenstr(restart_file)),'.pond_cesm.', &
     548          24 :               iyear,'-',month,'-',mday,'-',sec
     549             : 
     550          12 :          if (restart_ext) then
     551           0 :             call ice_open_ext(nu_dump_pond,filename,0)
     552             :          else
     553          12 :             call ice_open(nu_dump_pond,filename,0)
     554             :          endif
     555             : 
     556          12 :          if (my_task == master_task) then
     557           3 :            write(nu_dump_pond) istep1,time,time_forc
     558           3 :            write(nu_diag,*) 'Writing ',filename(1:lenstr(filename))
     559             :          endif
     560             : 
     561             :       endif
     562             : 
     563         402 :       if (tr_pond_lvl) then
     564             : 
     565          24 :          write(filename,'(a,a,a,i4.4,a,i2.2,a,i2.2,a,i5.5)') &
     566         342 :               restart_dir(1:lenstr(restart_dir)), &
     567         318 :               restart_file(1:lenstr(restart_file)),'.pond_lvl.', &
     568         588 :               iyear,'-',month,'-',mday,'-',sec
     569             : 
     570         294 :          if (restart_ext) then
     571           0 :             call ice_open_ext(nu_dump_pond,filename,0)
     572             :          else
     573         294 :             call ice_open(nu_dump_pond,filename,0)
     574             :          endif
     575             : 
     576         294 :          if (my_task == master_task) then
     577          21 :            write(nu_dump_pond) istep1,time,time_forc
     578          21 :            write(nu_diag,*) 'Writing ',filename(1:lenstr(filename))
     579             :          endif
     580             : 
     581             :       endif
     582             : 
     583         402 :       if (tr_pond_topo) then
     584             : 
     585           0 :          write(filename,'(a,a,a,i4.4,a,i2.2,a,i2.2,a,i5.5)') &
     586          12 :               restart_dir(1:lenstr(restart_dir)), &
     587          12 :               restart_file(1:lenstr(restart_file)),'.pond_topo.', &
     588          24 :               iyear,'-',month,'-',mday,'-',sec
     589             : 
     590          12 :          if (restart_ext) then
     591           0 :             call ice_open_ext(nu_dump_pond,filename,0)
     592             :          else
     593          12 :             call ice_open(nu_dump_pond,filename,0)
     594             :          endif
     595             : 
     596          12 :          if (my_task == master_task) then
     597           3 :            write(nu_dump_pond) istep1,time,time_forc
     598           3 :            write(nu_diag,*) 'Writing ',filename(1:lenstr(filename))
     599             :          endif
     600             : 
     601             :       endif
     602             : 
     603         402 :       if (tr_brine) then
     604             : 
     605           0 :          write(filename,'(a,a,a,i4.4,a,i2.2,a,i2.2,a,i5.5)') &
     606         144 :               restart_dir(1:lenstr(restart_dir)), &
     607         144 :               restart_file(1:lenstr(restart_file)),'.brine.', &
     608         288 :               iyear,'-',month,'-',mday,'-',sec
     609             : 
     610         144 :          if (restart_ext) then
     611           0 :             call ice_open_ext(nu_dump_hbrine,filename,0)
     612             :          else
     613         144 :             call ice_open(nu_dump_hbrine,filename,0)
     614             :          endif
     615             : 
     616         144 :          if (my_task == master_task) then
     617           6 :            write(nu_dump_hbrine) istep1,time,time_forc
     618           6 :            write(nu_diag,*) 'Writing ',filename(1:lenstr(filename))
     619             :          endif
     620             : 
     621             :       endif
     622             : 
     623         402 :       if (solve_zsal .or. nbtrcr > 0) then
     624             : 
     625           0 :          write(filename,'(a,a,a,i4.4,a,i2.2,a,i2.2,a,i5.5)') &
     626         144 :               restart_dir(1:lenstr(restart_dir)), &
     627         144 :               restart_file(1:lenstr(restart_file)),'.bgc.', &
     628         288 :               iyear,'-',month,'-',mday,'-',sec
     629             : 
     630         144 :          if (restart_ext) then
     631           0 :             call ice_open_ext(nu_dump_bgc,filename,0)
     632             :          else
     633         144 :             call ice_open(nu_dump_bgc,filename,0)
     634             :          endif
     635             : 
     636         144 :          if (my_task == master_task) then
     637           6 :            write(nu_dump_bgc) istep1,time,time_forc
     638           6 :            write(nu_diag,*) 'Writing ',filename(1:lenstr(filename))
     639             :          endif
     640             :       endif
     641             : 
     642         402 :       if (tr_iso) then
     643             : 
     644           0 :          write(filename,'(a,a,a,i4.4,a,i2.2,a,i2.2,a,i5.5)') &
     645          42 :               restart_dir(1:lenstr(restart_dir)), &
     646          42 :               restart_file(1:lenstr(restart_file)),'.iso.', &
     647          84 :               iyear,'-',month,'-',mday,'-',sec
     648             : 
     649          42 :          if (restart_ext) then
     650           0 :             call ice_open_ext(nu_dump_iso,filename,0)
     651             :          else
     652          42 :             call ice_open(nu_dump_iso,filename,0)
     653             :          endif
     654             : 
     655          42 :          if (my_task == master_task) then
     656           3 :            write(nu_dump_iso) istep1,time,time_forc
     657           3 :            write(nu_diag,*) 'Writing ',filename(1:lenstr(filename))
     658             :          endif
     659             : 
     660             :       endif
     661             : 
     662         402 :       if (tr_aero) then
     663             : 
     664           0 :          write(filename,'(a,a,a,i4.4,a,i2.2,a,i2.2,a,i5.5)') &
     665          36 :               restart_dir(1:lenstr(restart_dir)), &
     666          36 :               restart_file(1:lenstr(restart_file)),'.aero.', &
     667          72 :               iyear,'-',month,'-',mday,'-',sec
     668             : 
     669          36 :          if (restart_ext) then
     670           0 :             call ice_open_ext(nu_dump_aero,filename,0)
     671             :          else
     672          36 :             call ice_open(nu_dump_aero,filename,0)
     673             :          endif
     674             : 
     675          36 :          if (my_task == master_task) then
     676           6 :            write(nu_dump_aero) istep1,time,time_forc
     677           6 :            write(nu_diag,*) 'Writing ',filename(1:lenstr(filename))
     678             :          endif
     679             : 
     680             :       endif
     681             : 
     682         402 :       end subroutine init_restart_write
     683             : 
     684             : !=======================================================================
     685             : 
     686             : ! Reads a single restart field
     687             : ! author David A Bailey, NCAR
     688             : 
     689       29104 :       subroutine read_restart_field(nu,nrec,work,atype,vname,ndim3, &
     690             :                                     diag, field_loc, field_type)
     691             : 
     692          24 :       use ice_blocks, only: nx_block, ny_block
     693             :       use ice_domain_size, only: max_blocks
     694             :       use ice_read_write, only: ice_read, ice_read_ext
     695             : 
     696             :       integer (kind=int_kind), intent(in) :: &
     697             :            nu            , & ! unit number
     698             :            ndim3         , & ! third dimension
     699             :            nrec              ! record number (0 for sequential access)
     700             : 
     701             :       real (kind=dbl_kind), dimension(nx_block,ny_block,ndim3,max_blocks), intent(inout) :: &
     702             :            work              ! input array (real, 8-byte)
     703             : 
     704             :       character (len=4), intent(in) :: &
     705             :            atype             ! format for output array
     706             :                              ! (real/integer, 4-byte/8-byte)
     707             : 
     708             :       logical (kind=log_kind), intent(in) :: &
     709             :            diag              ! if true, write diagnostic output
     710             : 
     711             :       character (len=*), intent(in) :: vname
     712             : 
     713             :       integer (kind=int_kind), optional, intent(in) :: &
     714             :            field_loc, &      ! location of field on staggered grid
     715             :            field_type        ! type of field (scalar, vector, angle)
     716             : 
     717             :       ! local variables
     718             : 
     719             :       integer (kind=int_kind) :: &
     720             :            n                 ! number of dimensions for variable
     721             : 
     722             :       real (kind=dbl_kind), dimension(nx_block,ny_block,max_blocks) :: &
     723     1105216 :            work2             ! input array (real, 8-byte)
     724             : 
     725             :       character(len=*), parameter :: subname = '(read_restart_field)'
     726             : 
     727       14984 :          write(nu_diag,*) 'vname ',trim(vname)
     728       14984 :          if (present(field_loc)) then
     729       65288 :             do n=1,ndim3
     730       51744 :                if (restart_ext) then
     731           0 :                   call ice_read_ext(nu,nrec,work2,atype,diag)
     732             :                else
     733       51744 :                   call ice_read(nu,nrec,work2,atype,diag,field_loc,field_type)
     734             :                endif
     735    57165420 :                work(:,:,n,:) = work2(:,:,:)
     736             :             enddo
     737             :          else
     738        6336 :             do n=1,ndim3
     739        4896 :                if (restart_ext) then
     740           0 :                   call ice_read_ext(nu,nrec,work2,atype,diag)
     741             :                else
     742        4896 :                   call ice_read(nu,nrec,work2,atype,diag)
     743             :                endif
     744     4380768 :                work(:,:,n,:) = work2(:,:,:)
     745             :             enddo
     746             :          endif
     747             : 
     748       14984 :       end subroutine read_restart_field
     749             :       
     750             : !=======================================================================
     751             : 
     752             : ! Writes a single restart field.
     753             : ! author David A Bailey, NCAR
     754             : 
     755       87312 :       subroutine write_restart_field(nu,nrec,work,atype,vname,ndim3,diag)
     756             : 
     757             :       use ice_blocks, only: nx_block, ny_block
     758             :       use ice_domain_size, only: max_blocks
     759             :       use ice_read_write, only: ice_write, ice_write_ext
     760             : 
     761             :       integer (kind=int_kind), intent(in) :: &
     762             :            nu            , & ! unit number
     763             :            ndim3         , & ! third dimension
     764             :            nrec              ! record number (0 for sequential access)
     765             : 
     766             :       real (kind=dbl_kind), dimension(nx_block,ny_block,ndim3,max_blocks), intent(in) :: &
     767             :            work              ! input array (real, 8-byte)
     768             : 
     769             :       character (len=4), intent(in) :: &
     770             :            atype             ! format for output array
     771             :                              ! (real/integer, 4-byte/8-byte)
     772             : 
     773             :       logical (kind=log_kind), intent(in) :: &
     774             :            diag              ! if true, write diagnostic output
     775             : 
     776             :       character (len=*), intent(in)  :: vname
     777             : 
     778             :       ! local variables
     779             : 
     780             :       integer (kind=int_kind) :: &
     781             :            n                 ! dimension counter
     782             : 
     783             :       real (kind=dbl_kind), dimension(nx_block,ny_block,max_blocks) :: &
     784     3315648 :            work2             ! input array (real, 8-byte)
     785             : 
     786             :       character(len=*), parameter :: subname = '(write_restart_field)'
     787             : 
     788      214872 :          do n=1,ndim3
     789   184593612 :             work2(:,:,:) = work(:,:,n,:)
     790      214872 :             if (restart_ext) then
     791           0 :                call ice_write_ext(nu,nrec,work2,atype,diag)
     792             :             else
     793      169920 :                call ice_write(nu,nrec,work2,atype,diag)
     794             :             endif
     795             :          enddo
     796             : 
     797       44952 :       end subroutine write_restart_field
     798             : 
     799             : !=======================================================================
     800             : 
     801             : ! Finalize the restart file.
     802             : ! author David A Bailey, NCAR
     803             : 
     804         402 :       subroutine final_restart()
     805             : 
     806             :       use ice_calendar, only: istep1, time, time_forc
     807             :       use ice_communicate, only: my_task, master_task
     808             : 
     809             :       logical (kind=log_kind) :: &
     810             :          solve_zsal, &
     811             :          tr_iage, tr_FY, tr_lvl, tr_iso, tr_aero, tr_pond_cesm, &
     812             :          tr_pond_topo, tr_pond_lvl, tr_brine
     813             : 
     814             :       integer (kind=int_kind) :: &
     815             :          nbtrcr               ! number of bgc tracers
     816             : 
     817             :       character(len=*), parameter :: subname = '(final_restart)'
     818             : 
     819             :       call icepack_query_parameters( &
     820         402 :          solve_zsal_out=solve_zsal)
     821             :       call icepack_query_tracer_sizes( &
     822         402 :          nbtrcr_out=nbtrcr)
     823             :       call icepack_query_tracer_flags( &
     824             :          tr_iage_out=tr_iage, tr_FY_out=tr_FY, tr_lvl_out=tr_lvl, &
     825             :          tr_iso_out=tr_iso, tr_aero_out=tr_aero, tr_pond_cesm_out=tr_pond_cesm, &
     826         402 :          tr_pond_topo_out=tr_pond_topo, tr_pond_lvl_out=tr_pond_lvl, tr_brine_out=tr_brine)
     827         402 :       call icepack_warnings_flush(nu_diag)
     828         402 :       if (icepack_warnings_aborted()) call abort_ice(error_message=subname, &
     829           0 :          file=__FILE__, line=__LINE__)
     830             : 
     831         402 :       if (my_task == master_task) then
     832          33 :          close(nu_dump)
     833             : 
     834          33 :          if (tr_iso)       close(nu_dump_iso)
     835          33 :          if (tr_aero)      close(nu_dump_aero)
     836          33 :          if (tr_iage)      close(nu_dump_age)
     837          33 :          if (tr_FY)        close(nu_dump_FY)
     838          33 :          if (tr_lvl)       close(nu_dump_lvl)
     839          33 :          if (tr_pond_cesm) close(nu_dump_pond)
     840          33 :          if (tr_pond_lvl)  close(nu_dump_pond)
     841          33 :          if (tr_pond_topo) close(nu_dump_pond)
     842          33 :          if (tr_brine)     close(nu_dump_hbrine)
     843          33 :          if (solve_zsal .or. nbtrcr > 0) &
     844           6 :                            close(nu_dump_bgc)
     845             : 
     846          33 :          write(nu_diag,*) 'Restart read/written ',istep1,time,time_forc
     847             :       endif
     848             : 
     849         402 :       end subroutine final_restart
     850             : 
     851             : !=======================================================================
     852             : 
     853             :       end module ice_restart
     854             : 
     855             : !=======================================================================

Generated by: LCOV version 1.14-6-g40580cd