[Dart-dev] [3719] DART/trunk: Finally got around to adding the input. nml contents to the

nancy at ucar.edu nancy at ucar.edu
Tue Dec 30 11:26:06 MST 2008


An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/dart-dev/attachments/20081230/b7bb53a3/attachment.html
-------------- next part --------------
Modified: DART/trunk/assim_model/assim_model_mod.f90
===================================================================
--- DART/trunk/assim_model/assim_model_mod.f90	2008-12-15 22:42:48 UTC (rev 3718)
+++ DART/trunk/assim_model/assim_model_mod.f90	2008-12-30 18:26:05 UTC (rev 3719)
@@ -25,7 +25,8 @@
 use utilities_mod, only : get_unit, close_file, register_module, error_handler,    &
                           E_ERR, E_WARN, E_MSG, E_DBG, logfileunit, nmlfileunit,   &
                           do_output, dump_unit_attributes, find_namelist_in_file,  &
-                          check_namelist_read, nc_check
+                          check_namelist_read, nc_check, &
+                          find_textfile_dims, file_to_text
 use     model_mod, only : get_model_size, static_init_model, get_state_meta_data,  &
                           get_model_time_step, model_interpolate, init_conditions, &
                           init_time, adv_1step, end_model, nc_write_model_atts,    &
@@ -206,13 +207,15 @@
 integer, OPTIONAL,intent(in) :: lagID
 type(netcdf_file_type)       :: ncFileID
 
-integer             :: i, metadata_length
+integer :: i, metadata_length, nlines, linelen
 
 integer ::   MemberDimID,   MemberVarID     ! for each "copy" or ensemble member
 integer ::     TimeDimID,     TimeVarID
 integer :: LocationDimID
 integer :: MetadataDimID, MetadataVarID
+integer ::   nlinesDimID,  linelenDimID, nmlVarID
 
+character(len=129), allocatable, dimension(:) :: textblock
 
 if(.not. byteSizesOK()) then
     call error_handler(E_ERR,'init_diag_output', &
@@ -246,7 +249,23 @@
               name="time",           len = nf90_unlimited,         dimid = TimeDimID), &
               'init_diag_output', 'def_dim time '//trim(ncFileID%fname))
 
+!-------------------------------------------------------------------------------
+! Find dimensions of namelist file ... will save it as a variable.
+!-------------------------------------------------------------------------------
 
+call find_textfile_dims("input.nml", nlines, linelen)
+
+allocate(textblock(nlines))
+textblock = ''
+
+call nc_check(nf90_def_dim(ncid=ncFileID%ncid, &
+              name="NMLlinelen", len = LEN(textblock(1)), dimid = linelenDimID), &
+              'init_diag_output', 'def_dim NMLlinelen '//trim(ncFileID%fname))
+
+call nc_check(nf90_def_dim(ncid=ncFileID%ncid, &
+              name="NMLnlines", len = nlines, dimid = nlinesDimID), &
+              'init_diag_output', 'def_dim NMLnlines '//trim(ncFileID%fname))
+
 !-------------------------------------------------------------------------------
 ! Write Global Attributes 
 !-------------------------------------------------------------------------------
@@ -292,6 +311,12 @@
 call nc_check(nf90_put_att(ncFileID%ncid, metadataVarID, "long_name",       &
               "Metadata for each copy/member"), 'init_diag_output', 'put_att long_name')
 
+!    input namelist 
+call nc_check(nf90_def_var(ncid=ncFileID%ncid,name="inputnml", xtype=nf90_char,    &
+              dimids = (/ linelenDimID, nlinesDimID /),  varid=nmlVarID), &
+              'init_diag_output', 'def_var inputnml')
+call nc_check(nf90_put_att(ncFileID%ncid, nmlVarID, "long_name",       &
+              "input.nml contents"), 'init_diag_output', 'put_att input.nml')
 
 !    Time -- the unlimited dimension
 call nc_check(nf90_def_var(ncFileID%ncid, name="time", xtype=nf90_double, dimids=TimeDimID, &
@@ -302,7 +327,6 @@
    call error_handler(E_MSG,'init_diag_output',msgstring,source,revision,revdate)
 endif
 
-
 ! Create the time "mirror" with a static length. There is another routine
 ! to increase it if need be. For now, just pick something.
 ncFileID%Ntimes    = 0
@@ -316,6 +340,7 @@
 
 !-------------------------------------------------------------------------------
 ! Fill the coordinate variables.
+! Write the input namelist as a netCDF variable.
 ! The time variable is filled as time progresses.
 !-------------------------------------------------------------------------------
 
@@ -323,7 +348,14 @@
               'init_diag_output', 'put_var MemberVarID')
 call nc_check(nf90_put_var(ncFileID%ncid, metadataVarID, meta_data_per_copy ), &
               'init_diag_output', 'put_var metadataVarID')
+ 
+call file_to_text("input.nml", textblock)
 
+call nc_check(nf90_put_var(ncFileID%ncid, nmlVarID, textblock ), &
+              'init_diag_output', 'put_var nmlVarID')
+
+deallocate(textblock)
+
 !-------------------------------------------------------------------------------
 ! sync to disk, but leave open
 !-------------------------------------------------------------------------------

Modified: DART/trunk/utilities/utilities_mod.f90
===================================================================
--- DART/trunk/utilities/utilities_mod.f90	2008-12-15 22:42:48 UTC (rev 3718)
+++ DART/trunk/utilities/utilities_mod.f90	2008-12-30 18:26:05 UTC (rev 3719)
@@ -60,6 +60,14 @@
 !
 !      to_upper         converts a character string to uppercase
 !
+!      find_textfile_dims    finds number of lines and max line length in a 
+!                            text file. Used so we can record the namelist files 
+!                            in the netcdf output files.
+!
+!      file_to_text     converts the contents of a (hopefully small) file to
+!                       a single text variable ... to record in the
+!                       netcdf output files.
+!
 ! nsc start 31jan07
 !   idea - add some unit number routine here?
 !   you can extract the filename associated with a fortran unit number
@@ -98,6 +106,7 @@
 public :: file_exist, get_unit, open_file, close_file, timestamp, &
        register_module, error_handler, to_upper, &
        nc_check, logfileunit, nmlfileunit, &
+       find_textfile_dims, file_to_text, &
        initialize_utilities, finalize_utilities, dump_unit_attributes, &
        find_namelist_in_file, check_namelist_read, &
        set_tasknum, set_output, do_output,  &
@@ -1142,6 +1151,82 @@
 
 end subroutine to_upper
 
+!#######################################################################
+
+subroutine find_textfile_dims( fname, nlines, linelen )
+! Determines the number of lines and maximum line length
+! of the file. Sometimes you need to know this stuff.
+character(len=*), intent(IN)  :: fname
+integer,          intent(OUT) :: nlines, linelen
+
+integer :: i, mylen, ios, funit
+
+character(len=1024) :: oneline
+character(len=129)  :: error_msg
+
+nlines  = 0
+linelen = 0
+funit   = open_file(fname, form="FORMATTED", action="READ")
+
+READLOOP : do i = 1,100000
+
+   read(funit, '(A)', iostat=ios) oneline
+   if (ios < 0) exit READLOOP  ! end of file
+   if (ios > 0) then
+      write(error_msg,'(A,'' read around line '',i8)')trim(fname),nlines
+      call error_handler(E_ERR,'find_textfile_dims', error_msg)
+   endif
+
+   nlines = nlines + 1
+   mylen  = len_trim(oneline)
+
+   if (mylen > linelen) linelen = mylen
+
+enddo READLOOP
+
+call close_file(funit)
+
+end subroutine find_textfile_dims
+
+!#######################################################################
+
+subroutine file_to_text( fname, textblock )
+!
+!
+character(len=*),               intent(IN)  :: fname
+character(len=*), dimension(:), intent(OUT) :: textblock
+
+integer :: i, ios, funit
+integer :: mynlines, mylinelen
+
+character(len=129)  :: error_msg
+
+call find_textfile_dims(fname, mynlines, mylinelen)
+
+if ( ( mynlines /= size(textblock) ) .or. &
+     (mylinelen /= len(textblock)) ) then
+   write(error_msg,'(A, '' file shape is '',i6,'' by '',i4, &
+                       &''  textblock is '',i6,'' by '',i4)') &
+   trim(fname),mynlines,mylinelen,size(textblock),len(textblock)
+   call error_handler(E_MSG,'file_to_text', error_msg)
+endif
+
+funit   = open_file(fname, form="FORMATTED", action="READ")
+
+PARSELOOP : do i = 1,mynlines
+
+   read(funit, '(A)', iostat=ios) textblock(i)
+   if ( ios /= 0 ) then
+      write(error_msg,'(A,'' read around line '',i8)')trim(fname),i
+      call error_handler(E_ERR,'file_to_text', error_msg)
+   endif
+
+enddo PARSELOOP
+
+call close_file(funit)
+
+end subroutine file_to_text
+
 !=======================================================================
 ! End of utilities_mod
 !=======================================================================


More information about the Dart-dev mailing list