[Dart-dev] [4909] DART/trunk/models/cam: Finish the updates that Tim started - add the dart_to_cam

nancy at ucar.edu nancy at ucar.edu
Fri May 13 14:33:46 MDT 2011


Revision: 4909
Author:   nancy
Date:     2011-05-13 14:33:46 -0600 (Fri, 13 May 2011)
Log Message:
-----------
Finish the updates that Tim started - add the dart_to_cam
converter.  This and cam_to_dart should replace all the
trans_* programs.  update shell scripts to use new converters;
add example caminput.nc files for both spectral and finite vol
versions of CAM.  Minor updates to model_mod to add optional
args to the read/write routines, and move the write_times
routine into the model mod so internal arrays won't have to
be public.

Modified Paths:
--------------
    DART/trunk/models/cam/cam_to_dart.f90
    DART/trunk/models/cam/model_mod.f90
    DART/trunk/models/cam/shell_scripts/advance_model.csh
    DART/trunk/models/cam/shell_scripts/job.simple.csh
    DART/trunk/models/cam/shell_scripts/run-cam.csh

Added Paths:
-----------
    DART/trunk/models/cam/cam_to_dart.nml
    DART/trunk/models/cam/dart_to_cam.f90
    DART/trunk/models/cam/dart_to_cam.html
    DART/trunk/models/cam/dart_to_cam.nml
    DART/trunk/models/cam/finite_volume/
    DART/trunk/models/cam/finite_volume/cam_phis.nc
    DART/trunk/models/cam/finite_volume/caminput.nc
    DART/trunk/models/cam/shell_scripts/input.normal.nml
    DART/trunk/models/cam/shell_scripts/input.pert0.nml
    DART/trunk/models/cam/spectral/
    DART/trunk/models/cam/spectral/caminput.nc
    DART/trunk/models/cam/spectral/topog_file.nc
    DART/trunk/models/cam/work/mkmf_closest_member_tool
    DART/trunk/models/cam/work/mkmf_dart_to_cam
    DART/trunk/models/cam/work/mkmf_fill_inflation_restart
    DART/trunk/models/cam/work/path_names_closest_member_tool
    DART/trunk/models/cam/work/path_names_dart_to_cam
    DART/trunk/models/cam/work/path_names_fill_inflation_restart

Removed Paths:
-------------
    DART/trunk/models/cam/finite_volume/cam_phis.nc
    DART/trunk/models/cam/finite_volume/caminput.nc
    DART/trunk/models/cam/spectral/caminput.nc
    DART/trunk/models/cam/spectral/topog_file.nc
    DART/trunk/models/cam/work/topog_file.nc

-------------- next part --------------
Modified: DART/trunk/models/cam/cam_to_dart.f90
===================================================================
--- DART/trunk/models/cam/cam_to_dart.f90	2011-05-13 20:02:57 UTC (rev 4908)
+++ DART/trunk/models/cam/cam_to_dart.f90	2011-05-13 20:33:46 UTC (rev 4909)
@@ -23,7 +23,8 @@
 
 use        types_mod, only : r8
 use    utilities_mod, only : initialize_utilities, finalize_utilities, do_output, &
-                             check_namelist_read, find_namelist_in_file, nmlfileunit
+                             check_namelist_read, find_namelist_in_file, nmlfileunit, &
+                             do_nml_file, do_nml_term
 use        model_mod, only : model_type, init_model_instance, end_model_instance, &
                              prog_var_to_vector, read_cam_init
 use  assim_model_mod, only : static_init_assim_model, get_model_size, &
@@ -62,8 +63,8 @@
 call check_namelist_read(iunit, io, "cam_to_dart_nml")
 
 ! Record the namelist values 
-if (do_output()) write(nmlfileunit, nml=cam_to_dart_nml)
-if (do_output()) write(     *     , nml=cam_to_dart_nml)
+if (do_nml_file()) write(nmlfileunit, nml=cam_to_dart_nml)
+if (do_nml_term()) write(     *     , nml=cam_to_dart_nml)
 
 ! Static init assim model sets the output file format (binary/ascii)
 ! and calls static_init_model

Added: DART/trunk/models/cam/cam_to_dart.nml
===================================================================
--- DART/trunk/models/cam/cam_to_dart.nml	                        (rev 0)
+++ DART/trunk/models/cam/cam_to_dart.nml	2011-05-13 20:33:46 UTC (rev 4909)
@@ -0,0 +1,5 @@
+
+&cam_to_dart_nml
+   cam_to_dart_input_file  = 'caminput.nc',
+   cam_to_dart_output_file = 'dart_ics', 
+   /


Property changes on: DART/trunk/models/cam/cam_to_dart.nml
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

Copied: DART/trunk/models/cam/dart_to_cam.f90 (from rev 4908, DART/branches/cam-update/dart_to_cam.f90)
===================================================================
--- DART/trunk/models/cam/dart_to_cam.f90	                        (rev 0)
+++ DART/trunk/models/cam/dart_to_cam.f90	2011-05-13 20:33:46 UTC (rev 4909)
@@ -0,0 +1,127 @@
+! DART software - Copyright \xA9 2004 - 2010 UCAR. This open source software is
+! provided by UCAR, "as is", without charge, subject to all terms of use at
+! http://www.image.ucar.edu/DAReS/DART/DART_download
+
+program dart_to_cam
+
+!----------------------------------------------------------------------
+! purpose: interface between CAM and DART
+!
+! method: Read DART state vector ("proprietary" format), including time(s).
+!         Reform state vector back into CAM fields.
+!         Replace those fields on the CAM initial file with the new values,
+!         preserving all other information on the file.
+!         Write a 'times' file containing the time information needed to
+!         update the CAM namelist start/stop times.
+!
+!----------------------------------------------------------------------
+
+use       types_mod, only : r8
+use   utilities_mod, only : open_file, close_file, &
+                            initialize_utilities, finalize_utilities, &
+                            logfileunit, nmlfileunit, do_nml_file, do_nml_term, &
+                            check_namelist_read, find_namelist_in_file
+use       model_mod, only : model_type, init_model_instance, write_cam_init, &
+                            vector_to_prog_var, static_init_model, get_model_size, &
+                            write_cam_times
+use assim_model_mod, only : aread_state_restart, open_restart_read, close_restart
+use time_manager_mod, only : time_type, print_time, print_date
+
+implicit none
+
+! version controlled file description for error handling, do not edit
+character(len=128), parameter :: &
+   source   = "$URL$", &
+   revision = "$Revision$", &
+   revdate  = "$Date$"
+
+!------------------------------------------------------------------
+! The namelist variables
+!------------------------------------------------------------------
+
+character (len = 128) :: dart_to_cam_input_file  = 'dart_ics'
+character (len = 128) :: dart_to_cam_output_file = 'caminput.nc'
+logical               :: advance_time_present    = .true.
+
+namelist /dart_to_cam_nml/ dart_to_cam_input_file,  &
+                           dart_to_cam_output_file, &
+                           advance_time_present
+
+!----------------------------------------------------------------------
+
+type(model_type)       :: var
+type(time_type)        :: model_time, adv_to_time
+real(r8), allocatable  :: statevector(:)
+integer                :: file_unit, vecsize, iunit, io
+
+! NSC needs namelist to say if model advance or not
+
+!-----------------------------------------------------------------------------
+! start of program
+!-----------------------------------------------------------------------------
+
+call initialize_utilities('dart_to_cam')
+
+! Read the namelist to get the input filename and whether
+! a simple restart/ic file or a model advance file.
+call find_namelist_in_file("input.nml", "dart_to_cam_nml", iunit)
+read(iunit, nml = dart_to_cam_nml, iostat = io)
+call check_namelist_read(iunit, io, "dart_to_cam_nml")
+
+! Record the namelist values used for the run 
+if (do_nml_file()) write(nmlfileunit, nml=dart_to_cam_nml)
+if (do_nml_term()) write(     *     , nml=dart_to_cam_nml)
+
+! initialize model
+call static_init_model()
+
+vecsize = get_model_size()
+allocate(statevector(vecsize))
+
+! Allocate the instance of the cam model type for storage
+call init_model_instance(var)
+
+! Get file for DART vector input
+file_unit = open_restart_read(dart_to_cam_input_file)
+! read in model time and state vector from DART
+if (advance_time_present) then
+   call aread_state_restart(model_time, statevector, file_unit, adv_to_time)
+else
+   call aread_state_restart(model_time, statevector, file_unit)
+endif
+call close_restart(file_unit)
+
+! decompose vector back into CAM fields
+call vector_to_prog_var (statevector, var)
+deallocate (statevector)
+
+! write fields to the netCDF initial file.
+call write_cam_init(dart_to_cam_output_file, var, model_time)
+
+! write cam times to a separate 'times' support file.  used to
+! update cam namelist start/stop times.
+if (advance_time_present) call write_cam_times(model_time, adv_to_time)
+
+call print_date( model_time,'dart_to_cam:CAM  model date')
+call print_time( model_time,'dart_to_cam:DART model time')
+call print_date( model_time,'dart_to_cam:CAM  model date',logfileunit)
+call print_time( model_time,'dart_to_cam:DART model time',logfileunit)
+
+if ( advance_time_present ) then
+call print_time(adv_to_time,'dart_to_cam:advance_to time')
+call print_date(adv_to_time,'dart_to_cam:advance_to date')
+call print_time(adv_to_time,'dart_to_cam:advance_to time',logfileunit)
+call print_date(adv_to_time,'dart_to_cam:advance_to date',logfileunit)
+endif
+
+
+call finalize_utilities()
+
+end program dart_to_cam
+
+! <next few lines under version control, do not edit>
+! $URL$
+! $Id$
+! $Revision$
+! $Date$
+

Copied: DART/trunk/models/cam/dart_to_cam.html (from rev 4908, DART/branches/cam-update/dart_to_cam.html)
===================================================================
--- DART/trunk/models/cam/dart_to_cam.html	                        (rev 0)
+++ DART/trunk/models/cam/dart_to_cam.html	2011-05-13 20:33:46 UTC (rev 4909)
@@ -0,0 +1,233 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<HTML>
+<HEAD>
+<TITLE>program dart_to_cam</TITLE>
+<link rel="stylesheet" type="text/css" href="../../doc/html/doc.css" />
+</HEAD>
+<BODY>
+<A NAME="TOP"></A>
+
+<H1>PROGRAM <em class=program>dart_to_cam</em></H1>
+
+<table border=0 summary="" cellpadding=5>
+<tr>
+    <td valign=middle>
+    <img src="../../doc/html/Dartboard9.png" alt="DART project logo" height=70 />
+    </td>
+    <td>
+       <P>Jump to <a href="../../index.html">DART Documentation Main Index</a><br />
+          <small><small>version information for this file: <br />
+          <!-- version tag follows, do not edit -->
+          $Id$</small></small>
+       </P></td>
+</tr>
+</table>
+
+<A HREF="#Namelist">NAMELIST</A> /
+<A HREF="#Modules">MODULES</A> /
+<A HREF="#FilesUsed">FILES</A> /
+<A HREF="#References">REFERENCES</A> /
+<A HREF="#Errors">ERRORS</A> /
+<A HREF="#FuturePlans">PLANS</A> /
+<A HREF="#Legalese">TERMS OF USE</A>
+
+<H2>Overview</H2>
+
+<P>
+   <em class=program>dart_to_cam</em> is the program that reads a DART
+   restart or model advance file 
+   (e.g. <em class=file>perfect_ics, filter_ics, assim_model_state_id ... </em>). 
+   and overwrites the part of the CAM data in a single CAM restart file 
+   (usually <em class=file>caminput.nc</em>) which is in the DART state vector.
+   If you have
+   multiple input files, you will need to rename the output files as you create them.
+</P><P>
+   The list of variables extracted from the DART state vector
+   and exported to the CAM netCDF file
+   is controlled by the set of <em class=file>input.nml</em>
+   <em class=code>&amp;model_nml:state_names_*</em> variables.
+</P><P>
+   If the input is a model advance file, containing 2 timestamps (the current model
+   time and a future time the model should run until), this program also creates
+   a separate file named <em class="file">times</em> that contains three lines:
+   the advance-to time, the current model time, and the number of hours to advance.
+   These will need to be extracted and inserted in a CAM namelist to indicate to
+   CAM how long to run.
+</P><P>
+   This program also updates the <em class=code>date</em> and
+   <em class=code>datesec</em> variables in the CAM netcdf file.
+   Generally these are identical times since the assimilation doesn't
+   change the time of the data, but in case the original file had a
+   different time that was overwritten in the state vector, it will
+   update the time for consistency.
+</P><P>
+   Conditions required for successful execution of <em class=program>dart_to_cam</em>:
+</P>
+
+<UL>
+   <LI>a valid <em class=file>input.nml</em> namelist file for DART</LI>
+   <LI>a CAM 'phis' netCDF file [default: <em class=file>cam_phis.nc</em>]</LI>
+   <LI>a DART restart file [default: <em class=file>dart_ics</em>].</LI>
+   <LI>a CAM restart file [default: <em class=file>caminput.nc</em>] (read and written).</LI>
+</UL>
+
+<P>
+Since this program is called repeatedly for every ensemble member,
+we have found it convenient to link the DART input and CAM restart files
+to the default filenames <em class=file>dart_ics</em> and
+<em class=file>caminput.nc</em>).  The output files may be moved
+or relinked as necessary.
+</P>
+
+<!--==================================================================-->
+<!--=================== DESCRIPTION OF A NAMELIST ====================-->
+<!--==================================================================-->
+
+<A NAME="Namelist"></A>
+<hr />
+<H2>NAMELIST</H2>
+<P>We adhere to the F90 standard of starting a namelist with an ampersand
+'&amp;' and terminating with a slash '/' for all our namelist input.
+Character strings that contain a '/' must be
+enclosed in quotes to prevent them from prematurely terminating the namelist.
+The namelist declaration is:
+</P>
+<div class=namelist>
+<pre>
+<em class=call>namelist / dart_to_cam_nml / </em> 
+   dart_to_cam_input_file, dart_to_cam_output_file
+</pre>
+</div>
+
+<div class=indent1>
+<!-- Description -->
+
+<P>
+This namelist is always read from a file called <em class=file>input.nml</em>.
+</P>
+
+<TABLE border=0 cellpadding=3 width=100%>
+<TR><TH align=left>Contents    </TH>
+    <TH align=left>Type        </TH>
+    <TH align=left>Description </TH></TR>
+
+<TR><!--contents--><TD valign=top>dart_to_cam_input_file&nbsp;&nbsp;&nbsp;</TD>
+    <!--  type  --><TD valign=top>character(len=128)&nbsp;&nbsp;&nbsp;</TD>
+    <!--descript--><TD>The name of the DART restart file containing the CAM state. 
+                       <em class=units>[default: 'dart_ics']</em></TD></TR>
+
+<TR><!--contents--><TD valign=top>dart_to_cam_output_file&nbsp;&nbsp;&nbsp;</TD>
+    <!--  type  --><TD valign=top>character(len=128)&nbsp;&nbsp;&nbsp;</TD>
+    <!--descript--><TD>The name of the CAM restart netcdf file.
+                       <em class=units>[default: 'caminput.nc']</em></TD></TR>
+
+</TABLE>
+
+</div>
+<br />
+
+<!--==================================================================-->
+
+<A NAME="Modules"></A>
+<hr />
+<H2>MODULES USED</H2>
+<PRE>
+assim_model_mod.f90
+types_mod.f90
+threed_sphere/location_mod.f90
+model_mod.f90
+null_mpi_utilities_mod.f90
+obs_kind_mod.f90
+random_nr_mod.f90
+random_seq_mod.f90
+time_manager_mod.f90
+utilities_mod.f90
+</PRE>
+
+<!--==================================================================-->
+<!-- Describe the Files Used by this module.                          -->
+<!--==================================================================-->
+
+<A NAME="FilesUsed"></A>
+<hr />
+<H2>FILES Read</H2>
+<UL>
+    <LI>DART namelist file; <em class=file>input.nml</em></LI>
+    <LI>DART initial conditions/restart file; e.g. <em class=file>dart_ics</em></LI>
+    <LI>CAM "phis" file specified in <em class=code>model_nml&cam_phis</em> 
+        (normally <em class=file>cam_phis.nc</em>)</LI>
+</UL>
+
+<H2>FILES Written</H2>
+<UL><LI>CAM restart file; <em class=file>caminput.nc</em> (read and written)</LI>
+</UL>
+
+<!--==================================================================-->
+<!-- Cite references, if need be.                                     -->
+<!--==================================================================-->
+
+<A NAME="References"></A>
+<hr />
+<H2>REFERENCES</H2>
+<P>
+none
+</P>
+
+<!--==================================================================-->
+<!-- Describe all the error conditions and codes.                     -->
+<!--==================================================================-->
+
+<A NAME="Errors"></A>
+<hr />
+<H2>ERROR CODES and CONDITIONS</H2>
+<P>
+none - all error messages come from modules that have their own documentation.
+</P>
+
+<A NAME="Bugs"></A>
+<hr />
+<H2>KNOWN BUGS</H2>
+<P>
+none
+</P>
+
+<!--==================================================================-->
+<!-- Describe Future Plans.                                           -->
+<!--==================================================================-->
+
+<A NAME="FuturePlans"></A>
+<hr />
+<H2>FUTURE PLANS</H2>
+<P>
+None.
+</P>
+
+<!--==================================================================-->
+<!-- Legalese & Metadata                                              -->
+<!--==================================================================-->
+
+<A NAME="Legalese"></A>
+<hr />
+<H2>Terms of Use</H2>
+
+<P>
+DART software - Copyright &copy; 2004 - 2010 UCAR.<br />
+This open source software is provided by UCAR, "as is",<br />
+without charge, subject to all terms of use at<br />
+<a href="http://www.image.ucar.edu/DAReS/DART/DART_download">
+http://www.image.ucar.edu/DAReS/DART/DART_download</a>
+</P>
+
+<TABLE border=0 cellpadding=0 width=100% summary="">
+<TR><TD valign=top>Contact:       </TD><TD> DART core group   </TD></TR>
+<TR><TD valign=top>Revision:      </TD><TD> $Revision$ </TD></TR>
+<TR><TD valign=top>Source:        </TD><TD> $URL$ </TD></TR>
+<TR><TD valign=top>Change Date:   </TD><TD> $Date$ </TD></TR>
+<TR><TD valign=top>Change&nbsp;history:&nbsp;</TD><TD> try "svn&nbsp;log" or "svn&nbsp;diff" </TD></TR>
+</TABLE>
+
+<!--==================================================================-->
+
+</BODY>
+</HTML>

Added: DART/trunk/models/cam/dart_to_cam.nml
===================================================================
--- DART/trunk/models/cam/dart_to_cam.nml	                        (rev 0)
+++ DART/trunk/models/cam/dart_to_cam.nml	2011-05-13 20:33:46 UTC (rev 4909)
@@ -0,0 +1,5 @@
+& dart_to_cam_nml
+   dart_to_cam_input_file  = 'dart_ics',
+   dart_to_cam_output_file = 'caminput.nc',
+   advance_time_present    = .true.,
+   /


Property changes on: DART/trunk/models/cam/dart_to_cam.nml
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:eol-style
   + native

Deleted: DART/trunk/models/cam/finite_volume/cam_phis.nc
===================================================================
(Binary files differ)

Copied: DART/trunk/models/cam/finite_volume/cam_phis.nc (from rev 4908, DART/branches/cam-update/finite_volume/cam_phis.nc)
===================================================================
(Binary files differ)

Deleted: DART/trunk/models/cam/finite_volume/caminput.nc
===================================================================
(Binary files differ)

Copied: DART/trunk/models/cam/finite_volume/caminput.nc (from rev 4908, DART/branches/cam-update/finite_volume/caminput.nc)
===================================================================
(Binary files differ)

Modified: DART/trunk/models/cam/model_mod.f90
===================================================================
--- DART/trunk/models/cam/model_mod.f90	2011-05-13 20:02:57 UTC (rev 4908)
+++ DART/trunk/models/cam/model_mod.f90	2011-05-13 20:33:46 UTC (rev 4909)
@@ -4,109 +4,47 @@
 
 module model_mod
 
-! <next few lines under version control, do not edit>
-! $URL$
-! $Id$
-! $Revision$
-! $Date$
 
-
-
-
-! kdr This came from Nancy for doing comparisons with Pincus runs,
-!     where we want to limit the impact of cloud liquid water to only cloud liquid water.
-!     There are some minor changes in my model_mod.f90 which I'd want to incorporate into
-!     this version, but I'm leaving them out for now.
-
-!     I've put in an error check in model_interpolate because of Patrick's trouble (1/10/10)
-
-
  
 !----------------------------------------------------------------------
-! purpose: interface between CAM and DART
-!              Translate to/from state_vector and caminput.nc,
-!              Initialize model,
-!              Write out CAM fields to Prior and Posterior_Diag.nc,
-!              Generate expected obs from model state (model_interpolate)
-!              Find state variables (or obs) that are close to a given base observation.
-!                  (get_close_obs)
-!              Provide randomly perturbed fields for initial ensemble in filter.(pert_model_state)
-
+! Interface code between CAM and DART.  Contains the required 16 interfaces
+!  as specified by DART.  Also contains several utility routines which help
+!  translate between CAM and DART formats, and deal with time.
 !
-! method: trans_pv_sv reads CAM 'initial' file (netCDF format),
-!         reforms fields into a state vector.
-!         DART assimilates obs, modifying the values of the state vector.
-!         This requires get_close_obs to find state variables to be modified by each ob.
-!         Reform state vector back into CAM fields via trans_sv_pv.
-!         Replace those fields on the CAM initial file with the new values,
-!         preserving all other information on the file.
-!         Also read hybrid coordinate coefficients from CAM input file (for plevs_cam)
+!  Contains a perturb routine for generating initial ensembles.  Does not
+!  provide adv_1step or init_conditions because CAM is a separate executable
+!  and cannot be called as a subroutine.
 !
-! author: Kevin Raeder 2/14/03  and 8/1/03
-!         based on prog_var_to_vector and vector_to_prog_var by Jeff Anderson
+!  This module intercepts the get_close_obs() calls and alters the distances
+!  for obs at the top of the model so they do not impact the state.
+! 
+!  This module keeps a copy of the ensemble mean in module global storage and
+!  uses it for computing the pressure-to-height conversions.
 !
-! modified: Tim Hoar 02 Sep 03 
-!         nc_write_model_atts, nc_write_model_vars now write out "prognostic"
-!         files instead of a nondescript state variable vector glom
+!  See the subversion code logs for history of this module.
 !
-! augmented; Kevin Raeder 7/1/04 for CAM3.0 and to use namelist input for
-!         lists of fields to include in state vector.
-!         'CAM3' marks changes
-!         Later;?
-!         Also; read field attributes from netcdf file and write them out 
-!         from nc_write_model_atts, instead of hard-coded there.
-!
-! augmented; Kevin Raeder (code from Hui Liu and CAM) 4/2006 to add vertical interpolation
-!         in height.  Also add checks of requested fields for interpolation.
-!
-! mostly rewritten: Kevin Raeder  9-10/2006 to merge FV and eulerian versions,
-!         and lay groundwork for future, non-rectangular coordinate systems.
-!         Also adapt to MPI mode of DART.
-
-!     OVERVIEW; 
+!  During the assimilation stage, only a piece of the state vector is available to each 
+!  process, and each process calls parts of model_mod.  In order to handle the conversion 
+!  of vertical coordinates of obs and/or state variables into a consistent coordinate, 
+!  an entire state vector is needed, so the ensemble mean is passed to model_mod before 
+!  the assimilation starts.  This is NOT done for model_interpolate; the whole vector is
+!  available, and should be used.  All locations are now converted to a standard coordinate 
+!  (pressure), instead of always converting the state vertical location to that of the ob.
+!  The highest_obs_level and ..._height_m variables are derived from highest_obs_pressure_mb
+!  namelist variable.
 !     
-!     This module interfaces with the MPI version of DART.  
-!
-!        model_get_close_states has been replaced with new interfaces, including get_close_obs, 
-!     which uses location_mod:get_close_obs.  This allows model_mod to use the 
-!     "generic efficient search algorithm" in DART to find the obs/state variables that
-!     are close to the given base obs, and to modify the distances according to any desired 
-!     criteria (obs_kinds, height above a threshold, etc).  
-!
-!     During the assimilation stage, only a piece of the state vector is available to each 
-!     process, and each process calls parts of model_mod.  In order to handle the conversion 
-!     of vertical coordinates of obs and/or state variables into a consistent coordinate, 
-!     an entire state vector is needed, so the ensemble mean is passed to model_mod before 
-!     the assimilation starts.  This is NOT done for model_interpolate; the whole vector is
-!     available, and should be used.  All locations are now converted to a standard coordinate 
-!     (pressure), instead of always converting the state vertical location to that of the ob.
-!     The highest_obs_level and ..._height_m variables are derived from highest_obs_pressure_mb
-!     namelist variable.
+!  This module has been rewritten to handle both the eulerian and finite volume core versions 
+!  of CAM (they have different grids), and hopefully the semi-lagrangian dynamics core, 
+!  and even lay some groundwork for future dynamical cores (such as HOMME) which are column 
+!  oriented, with irregular horizontal grids.
 !     
-!     This module has been rewritten to handle both the eulerian and finite volume core versions 
-!     of CAM (they have different grids), and hopefully the semi-lagrangian dynamics core, 
-!     and even lay some groundwork for future dynamical cores (such as HOMME) which are column 
-!     oriented, with irregular horizontal grids.
+!  The coordinate orders of fields stored in various forms have also been simplified.
+!  For example; various vintages of CAM 3D fields may be read in with (lon, lat, lev) or 
+!  (lon, lev, lat).  These are uniformly converted to (lev, lon, lat) for use in model_mod.
+!  This latter form is different than pre MPI model_mods.  Then such fields are stored in
+!  the state vector with the same coordinate order.  They are converted back to the modern
+!  CAM coordinate order when written to caminput.nc files.
 !     
-!     The coordinate orders of fields stored in various forms have also been simplified.
-!     For example; various vintages of CAM 3D fields may be read in with (lon, lat, lev) or 
-!     (lon, lev, lat).  These are uniformly converted to (lev, lon, lat) for use in model_mod.
-!     This latter form is different than pre MPI model_mods.  Then such fields are stored in
-!     the state vector with the same coordinate order.  They are converted back to the modern
-!     CAM coordinate order when written to caminput.nc files.
-!     
-!     The organization of the state vector has been changed from the B-grid style to a field 
-!     and column oriented form; see prog_var_to_vector and vector_to_prog_var.  This allows 
-!     each field to have different dimensions than all the others.  The fields are still grouped 
-!     by the rank of the array used to store them in CAM form; 0d, 1d, 2d, and 3d.  In the 3d case 
-!     all the values of a field for one column are stored contiguously, to speed up vertical 
-!     interpolations and calculations of heights on model levels.  The dimensions of each field 
-!     are stored in globally available arrays ([sf]_dim_#d) and there are functions and subroutines 
-!     for accessing the needed information (see ./model_mod.html).
-!     
-!     Observations on pressure levels or heights require various pieces of the state vector
-!     in order to calculate the expected observation.  Just the surface pressure is needed for 
-!     the former, while PS plus the temperature and moisture profiles are needed for the heights.  
 !     These may be needed on the regular A-grid (thermodynamic variables) and grids staggered 
 !     relative to the A-grid.   Currently, PS for the A-grid and the 2 staggered grids is 
 !     stored for global access and pressures and heights on model levels are (re)calculated
@@ -244,10 +182,13 @@
 use types_mod,         only : r8, MISSING_I, MISSING_R8, gravity_const => gravity
 !          add after verification against Hui's tests;  gas_constant_v,gas_constant,ps0,PI,DEG2RAD
 
-use time_manager_mod,  only : time_type, set_time, set_date, print_time, print_date, set_calendar_type
+use time_manager_mod,  only : time_type, set_time, set_date, print_time, print_date,  &
+                              set_calendar_type, get_calendar_type, operator(-),      &
+                              get_time, get_date
 use utilities_mod,     only : open_file, close_file, find_namelist_in_file, check_namelist_read, &
                               register_module, error_handler, file_exist, E_ERR, E_WARN, E_MSG,  &
-                              logfileunit, nmlfileunit, do_output, nc_check, get_unit
+                              logfileunit, nmlfileunit, do_output, nc_check, get_unit, do_nml_file, &
+                              do_nml_term
 use mpi_utilities_mod, only : my_task_id, task_count
 
 !-------------------------------------------------------------------------
@@ -327,16 +268,21 @@
 implicit none
 private
 
-! The last three lines are for interfaces to CAM specific programs; transXXX, etc.
+! The first block are the 16 required interfaces.  The following block
+! are additional useful interfaces that utility programs can call.
 public ::                                                            &
    static_init_model, get_model_size, get_model_time_step,           &
    pert_model_state, get_state_meta_data, model_interpolate,         &
    nc_write_model_atts, nc_write_model_vars,                         &
    init_conditions, init_time, adv_1step, end_model,                 &
+   get_close_maxdist_init, get_close_obs_init, get_close_obs,        &
+   ens_mean_for_model
+
+public ::                                                            &
    model_type, prog_var_to_vector, vector_to_prog_var,               &
    read_cam_init, read_cam_init_size,                                &
    init_model_instance, end_model_instance, write_cam_init,          &
-   get_close_maxdist_init, get_close_obs_init, get_close_obs, ens_mean_for_model
+   write_cam_times
 
 
 !-----------------------------------------------------------------------
@@ -351,8 +297,7 @@
 ! DART form of ensemble mean, global storage for use by get_close_obs:convert_vert 
 ! Ensemble mean is used so that the same "state" will be used for the height calculations
 ! on all processors, for all ensemble members.
-! Nancy; need allocatable here if it's an argument passed into a subroutine, with space
-! allocated in another module?
+! This is allocated in static_init_model().
 real(r8), allocatable :: ens_mean(:)      
 
 !----------------------------------------------------------------------
@@ -481,7 +426,8 @@
 integer :: state_num_2d = 1              ! # of 2d fields to read from file
 integer :: state_num_3d = 4              ! # of 3d fields to read from file
 
-! make these allocatable?  Where would I define the default values?
+! These can't be allocatable since they are namelist items.  They have to
+! have a fixed size at compile time.
 integer, parameter :: MAX_STATE_NAMES = 100
 character(len=8),dimension(MAX_STATE_NAMES) :: state_names_0d = (/('        ',iii=1,MAX_STATE_NAMES)/)
 character(len=8),dimension(MAX_STATE_NAMES) :: state_names_1d = (/('        ',iii=1,MAX_STATE_NAMES)/)
@@ -525,20 +471,28 @@
 ! by numerical stability concerns for repeated restarting in leapfrog.
 integer :: Time_step_seconds = 21600, Time_step_days = 0
 
-namelist /model_nml/ output_state_vector , model_version , cam_phis, model_config_file & 
-                     ,state_num_0d   ,state_num_1d   ,state_num_2d   ,state_num_3d &
-                     ,state_names_0d ,state_names_1d ,state_names_2d ,state_names_3d &
-                     ,                which_vert_1d  ,which_vert_2d  ,which_vert_3d &
-                     ,pert_names ,pert_sd ,pert_base_vals &
-                     ,highest_obs_pressure_mb , highest_state_pressure_mb  &
-                     ,max_obs_lat_degree ,Time_step_seconds ,Time_step_days &
-                     ,impact_only_same_kind
+! set to .true. to get more details about the state vector and the
+! CAM fields and sizes in the init code.
+logical :: print_details = .false.
+
+
+namelist /model_nml/ output_state_vector, model_version, cam_phis, model_config_file, & 
+                       state_num_0d,   state_num_1d,   state_num_2d,   state_num_3d,  &
+                     state_names_0d, state_names_1d, state_names_2d, state_names_3d,  &
+                                      which_vert_1d,  which_vert_2d,  which_vert_3d,  &
+                     pert_names, pert_sd, pert_base_vals,                             &
+                     highest_obs_pressure_mb, highest_state_pressure_mb,              &
+                     max_obs_lat_degree, Time_step_seconds, Time_step_days,           &
+                     impact_only_same_kind, print_details
                      
 
 !---- end of namelist (found in file input.nml) ----
 !----------------------------------------------------------------------
 ! Derived parameters
 
+! make sure static init code only called once
+logical :: module_initialized = .false.
+
 ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 type(time_type) :: Time_step_atmos
 
@@ -551,8 +505,7 @@
 ! for a cold start.
 integer                 :: num_tasks
 integer                 :: my_task
-integer                 :: ens_member
-data ens_member /0/
+integer                 :: ens_member = 0
 logical                 :: do_out
 
 ! common message string used by many subroutines
@@ -658,6 +611,10 @@
 integer            :: iunit, io, topog_lons, topog_lats, i, num_lons, num_lats, ncfileid
 integer            :: max_levs
 
+! only execute this code once
+if (module_initialized) return
+
+
 ! Register the module
 call register_module(source, revision, revdate)
 
@@ -695,15 +652,16 @@
 end if
 
 ! Record the namelist values 
-if (do_out) write(nmlfileunit, nml=model_nml)
+if (do_nml_file()) write(nmlfileunit, nml=model_nml)
+if (do_nml_term()) write(    *      , nml=model_nml)
 
 ! Set the model minimum time step from the namelist seconds and days input
 Time_step_atmos = set_time(Time_step_seconds, Time_step_days)
 ! kdr debug
-if (do_out) call print_time(Time_step_atmos)
+if (print_details .and. do_out) call print_time(Time_step_atmos)
 
 ! read CAM 'initial' file domain info
-call nc_check(nf90_open(path = trim(model_config_file), mode = nf90_write, ncid = ncfileid), &
+call nc_check(nf90_open(path = trim(model_config_file), mode = nf90_nowrite, ncid = ncfileid), &
              'static_init_model', 'opening '//trim(model_config_file))
 
 ! Get sizes of dimensions/coordinates from netcdf and put in global storage.
@@ -721,7 +679,10 @@
 do i=1,state_num_3d 
    model_size = model_size + s_dim_3d(1,i) * s_dim_3d(2,i) * s_dim_3d(3,i)
 end do
-if (do_out) write(*, *) 'CAM size initialized as ', model_size
+if (do_out) then
+   write(msgstring, '(A,I9)') 'CAM state vector size: ', model_size
+   call error_handler(E_MSG, '', msgstring)
+endif
 
 ! Allocate space for longitude and latitude global arrays
 ! and Allocate space for hybrid vertical coord coef arrays
@@ -754,8 +715,8 @@
 !------------------------------------------------------------------------
 ! # fields to read
 nflds = state_num_0d + state_num_1d + state_num_2d + state_num_3d      
-if (do_out) write(*, '(A,I3,A,4I3)') '# of fields in state vector =  ', nflds,' = sum of ', &
-                 state_num_0d ,state_num_1d ,state_num_2d ,state_num_3d
+if (print_details .and. do_out) write(*, '(A,I3,A,4I3)') '# of fields in state vector =  ', nflds, &
+                 ' = sum of ', state_num_0d ,state_num_1d ,state_num_2d ,state_num_3d
 
 ! CAM3 subroutine to order the state vector parts into cflds 
 ! cflds is needed for reading attributes
@@ -780,11 +741,11 @@
 if (file_exist(cam_phis)) then
    call nc_check(nf90_open(path = trim(cam_phis), mode = nf90_nowrite, ncid = ncfileid), &
               'static_init_model', 'opening '//trim(cam_phis))
-   if (do_out) write(*, *) 'file_name for surface geopotential height is ', trim(cam_phis)
+   if (print_details .and. do_out) write(*, *) 'file_name for surface geopotential height is ', trim(cam_phis)
 
    call read_topog_size(ncfileid, topog_lons, topog_lats)
 
-   if (do_out) write(*,*) 'topog_lons, _lats = ',topog_lons, topog_lats
+   if (print_details .and. do_out) write(*,*) 'topog_lons, _lats = ',topog_lons, topog_lats
 
    num_lons = dim_sizes(find_name('lon     ',dim_names))
    num_lats = dim_sizes(find_name('lat     ',dim_names))
@@ -828,6 +789,9 @@
    impact_kind_index = get_raw_obs_kind_index(impact_only_same_kind)
 endif
 
+! make sure we only come through here once
+module_initialized = .true.
+
 end subroutine static_init_model
 
 
@@ -848,6 +812,11 @@
 ! learn how many dimensions are defined in this file.
 call nc_check(nf90_inquire(ncfileid, num_dims), 'read_cam_init_size', 'inquire num_dims')
 
+if (allocated(dim_ids) .or. allocated(dim_names) .or. allocated(dim_sizes)) then
+   write(msgstring, *) 'dim_ids, dim_names, and/or dim_sizes already allocated'
+   call error_handler(E_ERR, 'read_cam_init_size', msgstring, source, revision, revdate)
+endif
+
 ! where to deallocate?
 allocate (dim_ids(num_dims), dim_names(num_dims), dim_sizes(num_dims))
 
@@ -857,7 +826,7 @@
    dim_ids(i) = i
    call nc_check(nf90_inquire_dimension(ncfileid, i, dim_names(i), dim_sizes(i)), &
                  'read_cam_init_size', 'inquire for '//trim(dim_names(i)))
-   if (do_out) write(*,*) 'Dims info = ',i, trim(dim_names(i)), dim_sizes(i)
+   if (print_details .and. do_out) write(*,*) 'Dims info = ',i, trim(dim_names(i)), dim_sizes(i)
 end do
 
 ! Find and store shapes of all the state vector fields.  Grouped by rank of fields into 
@@ -897,7 +866,7 @@
 endif
 
 !debug   where will this be written?
-if (do_out .and. .false.) then
+if (print_details .and. do_out .and. .false.) then
    if (state_num_1d > 0) then
       write(*,*) 's_dim_1d = ',s_dim_1d
       write(*,*) (s_dim_max(i,1),i=1,3)
@@ -954,8 +923,14 @@
       char_version(nchars:nchars) = model_version(i:i)
    end if
 end do
-if (do_out) WRITE(*,'(A,A10,4(I3,2X))') 'model_version, version(1:4) = ' &
-                            ,model_version,(int_version(i),i=1,4)
+if (do_out) then
+   if (print_details) then
+      write(*,'(A,A10,4(I3,2X))') 'model_version, version(1:4) = ' &
+                                  ,model_version,(int_version(i),i=1,4)
+   else
+      call error_handler(E_MSG, '', 'CAM model version: '//trim(model_version))
+   endif
+endif
    
 ! assume cam3.0.7 format to start
 ! test on version cam3.0.3
@@ -1158,13 +1133,13 @@
 integer :: ncfldid
 integer :: n,m, slon_index, slat_index, lat_index, lon_index
 
-!if (do_out) PRINT*,'read_cam_horiz; reading ',cfield
+!if (print_details .and. do_out) PRINT*,'read_cam_horiz; reading ',cfield
 call nc_check(nf90_inq_varid(ncfileid, trim(cfield), ncfldid), &
               'read_cam_horiz', 'inq_varid '//trim(cfield))
 call nc_check(nf90_get_var(ncfileid, ncfldid, var, start=(/1,1,1/), &
            count=(/dim1, dim2, 1/)), 'read_cam_horiz', trim(cfield))
 
-!if (do_out) PRINT*,'read_cam_horiz; reading ',cfield,' using id ',ncfldid, dim1, dim2
+!if (print_details .and. do_out) PRINT*,'read_cam_horiz; reading ',cfield,' using id ',ncfldid, dim1, dim2
 
 ! assign values to phis grids for use by the rest of the module.
 if (cfield == 'PHIS    ') then
@@ -1226,7 +1201,7 @@
 !           ncid = ncfileid))
 
 ! read CAM 'initial' file attribute desired
-if (do_out) PRINT*,'reading ',trim(att)
+if (print_details .and. do_out) PRINT*,'reading ',trim(att)
 do i = 1,nflds
    call nc_check(nf90_inq_varid(ncfileid, trim(cflds(i)), ncfldid), 'nc_read_model_atts', &
                  'inq_varid '//trim(cflds(i)))
@@ -1239,7 +1214,7 @@
       call nc_check(nf90_get_att(ncfileid, ncfldid, trim(att) ,att_vals(i) ), &
                     'nc_read_model_atts', 'get_att '//trim(att))
       att_vals(i)(nchars+1:128) = ' '
-      if (do_out) WRITE(*,'(A,1X,I6,I6,1X,A,1X,A)') att, ncfldid, nchars, cflds(i), trim(att_vals(i))
+      if (print_details .and. do_out) WRITE(*,'(A,1X,I6,I6,1X,A,1X,A)') att, ncfldid, nchars, cflds(i), trim(att_vals(i))
    else
       WRITE(*,*) ncfldid, cflds(i), 'NOT AVAILABLE'
    end if
@@ -1365,7 +1340,7 @@
    end do Res
 endif
 
-if (do_out) then
+if (print_details .and. do_out) then
    PRINT*,'reading ',cfield,' using id ',ncfldid,' size ',coord_size,' resolution ', &
           var%resolution
    WRITE(*,*) 'first, last val: ', var%vals(1),var%vals(coord_size)
@@ -1481,25 +1456,32 @@
    write(msgstring, *) 'nfld = ',nfld,', nflds = ',nflds,' must be equal '
    call error_handler(E_ERR, 'order_state_fields', msgstring, source, revision, revdate)
 elseif (do_out) then
-   write(logfileunit,'(/A/)') 'State vector is composed of '
-!   write(logfileunit,'((8(A8,1X)))') (cflds(i),i=1,nflds)
-   do i=1,state_num_0d
-      write(logfileunit,'(/A,I4)') cflds(i), TYPE_1D(i)
-   end do
-   i1 = state_num_0d
-   do i=1,state_num_1d
-      write(logfileunit,'(/A,I4)') cflds(i1+i), TYPE_1D(i)
-   end do
-   i1 = i1 + state_num_1d
-   do i=1,state_num_2d
-      write(logfileunit,'(/A,I4)') cflds(i1+i), TYPE_2D(i)
-   end do
-   i1 = i1 + state_num_2d
-   do i=1,state_num_3d
-      write(logfileunit,'(/A,I4)') cflds(i1+i), TYPE_3D(i)
-   end do
-   write(logfileunit,'(/A)') 'TYPE_PS, TYPE_T, TYPE_U, TYPE_V, TYPE_Q = ' 
-   write(logfileunit,'((8(I8,1X)))') TYPE_PS, TYPE_T, TYPE_U, TYPE_V, TYPE_Q
+   if (print_details) then
+      write(logfileunit,'(/A/)') 'State vector is composed of these fields: '
+   !   write(logfileunit,'((8(A8,1X)))') (cflds(i),i=1,nflds)
+      do i=1,state_num_0d
+         write(logfileunit,'(/A,I4)') cflds(i), TYPE_1D(i)
+      end do
+      i1 = state_num_0d
+      do i=1,state_num_1d
+         write(logfileunit,'(/A,I4)') cflds(i1+i), TYPE_1D(i)
+      end do
+      i1 = i1 + state_num_1d
+      do i=1,state_num_2d
+         write(logfileunit,'(/A,I4)') cflds(i1+i), TYPE_2D(i)
+      end do
+      i1 = i1 + state_num_2d
+      do i=1,state_num_3d
+         write(logfileunit,'(/A,I4)') cflds(i1+i), TYPE_3D(i)
+      end do
+      write(logfileunit,'(/A)') 'TYPE_PS, TYPE_T, TYPE_U, TYPE_V, TYPE_Q = ' 
+      write(logfileunit,'((8(I8,1X)))') TYPE_PS, TYPE_T, TYPE_U, TYPE_V, TYPE_Q
+   else
+      call error_handler(E_MSG, '', 'State vector is composed of these fields: ')
+      do i = 1,nflds
+         call error_handler(E_MSG, '', trim(cflds(i)))
+      enddo
+   endif
 end if
 
 return
@@ -1561,7 +1543,7 @@
 ! cam_to_dart_kinds(TYPE_LCWAT) = KIND_CLOUD_WATER  ?  
 
 
-if (do_out) then
+if (print_details .and. do_out) then

@@ Diff output truncated at 40000 characters. @@


More information about the Dart-dev mailing list