Skip to content

Instantly share code, notes, and snippets.

@aidanheerdegen
Last active August 12, 2019 04:02
Show Gist options
  • Save aidanheerdegen/203af6f6e0a87d1d82704eae9608f099 to your computer and use it in GitHub Desktop.
Save aidanheerdegen/203af6f6e0a87d1d82704eae9608f099 to your computer and use it in GitHub Desktop.
Edit the time header in a CICE iced restart file
! Open an iced restart file, report times. Write new iced restart with modified time header
! single processor (not parallelized)
! intel compiler:
! ifort edit_time.f90 -o edit_time -i4 -r8 -convert big_endian -assume byterecl
program edit_time
implicit none
integer, parameter :: char_len_long = 256, &
log_kind = kind(.true.), &
int_kind = selected_int_kind(6), &
dbl_kind = selected_real_kind(13)
! file names
character (len=char_len_long) :: filename, newfile, tmpfile = '.edit_time_tmp'
character (len=1) :: c
character (len=1024) :: cbuf
! time info
integer (kind=int_kind) :: &
istep0, & ! counter, number of steps at current timestep
nu=0, orig=1, tmp=2, new=3, mypos
real (kind=dbl_kind) :: &
time , & ! total elapsed time (s)
time_forc ! time of last forcing update (s)
! Filename for the iced restart
write(*,'(a)',advance='no') "Filename? "
read(*,*) filename
! Open restart, report time info and close
call ice_open(nu, filename)
read (nu) istep0,time,time_forc
write(*,*) 'Restart header data (istep0, time, time_forc) :',istep0,time,time_forc
close(nu)
! Filename for new iced restart (must be different to original)
write(*,'(a)',advance='no') "New filename? "
read(*,*) newfile
! New timing information
write(*,'(a)',advance='no') "new istep0, time, time_forc? "
read(*,*) istep0,time,time_forc
! Make a temporary file containing the new timing header
call ice_open(nu, tmpfile)
write (nu) istep0,time,time_forc
close(nu)
! Open original, temporary header, and new files with stream access, just
! pure binary
open(orig, file=filename, status='old', form='unformatted', access='stream')
open(tmp, file=tmpfile, status='old', form='unformatted', access='stream')
open(new, file=newfile, status='replace', form='unformatted', access='stream')
! Copy header from temporary to new file, also read from original
! so it is at the same position as the other files
do while (.true.)
read(tmp,end=1) c
write(new) c
read(orig) c
end do
1 print *,'Finished writing header'
close(tmp)
! Copy the rest of the file
do while (.true.)
inquire(orig, pos=mypos)
read(orig,end=3) cbuf
write(new) cbuf
end do
3 print *,'finish copying one byte at a time'
! Copy the rest of the file
do while (.true.)
read(orig,pos=mypos,end=2) c
write(new,pos=mypos) c
mypos = mypos + 1
end do
2 print *,'Finished writing file',mypos
close(orig)
close(new)
contains
subroutine ice_open(nu, filename)
integer (kind=int_kind), intent(in) :: &
nu ! unit number
character (*) :: filename
open(nu,file=filename,form='unformatted')
end subroutine ice_open
end program edit_time
@aidanheerdegen
Copy link
Author

Some info on how to run this:

The program will ask for an original file name, a new filename, and the values for the three quantities you want to change. They are an integer (step) and two floating point (real) values (elapsed time and forcing time which seems to always be zero).

You can determine what you want the values to be by running the program on a restart with the values you wish to have, then using ctrl-C to abort the program

For example, I have a restart file iced.4

% ./edit_time 
Filename? iced.4
Restart header data (istep0, time, time_forc) :     1376232
  63303206400.0000       0.000000000000000E+000
^C

So the three values in this case are 1376232 63303206400. 0.

Or you can figure out what they should be based on the difference between two restarts that are 1 year apart.

Always make copies of restarts in another directory in case you end up writing the wrong value or to the wrong file.

You can make a file with the values the program wants to read in and use input redirection, to save typing.

% cat input
iced.4
iced.5
1000 99999 0.0

% ./edit_time < input 
Filename?  Restart header data (istep0, time, time_forc) :     1376232
  63303206400.0000       0.000000000000000E+000
New filename? new istep0, time, time_forc?  Finished writing header
finish copying one byte at a time
Finished writing file    44928445

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment