c
c     Copyright (c) 1999,2000, Compaq Computer Corporation
c
c     See the file "license.terms" for information on usage and redistribution
c     of this file, and for a DISCLAIMER OF ALL WARRANTIES.
c
      subroutine pmcont(fw, fposy, fposx,
     1                  inxm, inym, inptx, inpty,
     2                  ctitle, ctext, cunits,
     3                  llinea, nferrl, linter,
     4                  pmfile)
      implicit none
c
c     Callable interface to the 2D contour plot code of Postmini
c
c     Programmer: John Faricelli
c     Callable interface
c     Date:     5-Mar-93
c
c     Input arguments:
c
c     FW     (real)   Data values to be plotted, arranged in FORTRAN
c                     two dimensional array format (column major order).
c                     Array FW is of declared size (INYM,INXM)
c                     Data occurs within the bounds (1:INPTY,1:INPTX)
c     FPOSY  (real)   Lateral  coordinates, in units of microns, 1:INPTY
c     FPOSX  (real)   Vertical coordinates, in units of microns, 1:INPTX
c     INYM  (integer) Size of 1st dimension of array fw as declared
c     INXM  (integer) Size of 2nd dimension of array fw as declared
c     INPTY (integer) Size of 1st dimension of array fw as used
c     INPTX (integer) Size of 2nd dimension of array fw as used
c     PMFILE (character) Name of Postmini command file (optional, see below)
c     CTITLE (character) Title for plot
c     CTEXT  (character) Name of plot quantity, e.g. 'POTENTIAL'
c     CUNITS (character) Units of plot quantity, e.g. 'cm^{-3}'
c     LLINEA (logical) If .TRUE. -> data varies on a linear scale
c                      Else      -> data varies on a log scale
c     NFERRL (integer) FORTRAN logical unit number to be used for all
c                      output messages if Postmini is run non-interactively
c     LINTER (logical) If .TRUE., Postmini is assumed to be called
c                      from an interactive program.
c                      If .FALSE., Postmini is assumed to be called from
c                      a program running without user intervention (batch)
c
c     Output parameters:
c
c     PMFILE (character) Name of the Postmini save file that was last explicitly
c                        written by the user with a "SAVE" command.
c                        If no file was explicitly saved, the default file
c                        POSTMINI.PMI is returned.
c
c     Interaction of PMFILE with LINTER:
c
c     If LINTER is .TRUE.
c        If PMFILE is all blanks, then Postmini runs in interactive mode.
c        Else, interpret PMFILE, execute plot to screen, and return.
c
c     If LINTER is .FALSE.
c        If PMFILE is all blanks, then output a 2D contour plot
c           to a file given by parameter CTEXT (with extension .PS) and return.
c           The default 'hardcopy' device is used (hardcoded to PostScript)
c        Else, interpret PMFILE, output plot to a file given by parameter
c           CTEXT, and return. The 'hardcopy' device may be specified in
c           PMFILE. An appropriate extension is added depending on the hardcopy
c           device.
c
      integer inxm, inym, inptx, inpty, nferrl
      logical  linter, llinea
      character*(*) pmfile, ctitle, ctext, cunits
      real fposy(inym), fposx(inxm), fw(inym,inxm)
c
      include 'const.h'
      include 'sysdep.cmn'
      include 'mesh.cmn'
      include 'data.cmn'
      include 'io.cmn'
      include 'plot.cmn'
      include 'compare.cmn'
      include 'units.cmn'
      include 'annotations.cmn'
      include 'defaults.cmn'
      include 'save.cmn'
      include 'callable.cmn'
      include 'version.cmn'
c
c     local variables
c
      logical err, batchmode
      integer string_len
      character*1  ch
      character*10 ext
c
c     init_common will only allow one intialization, so it's safe to
c     call anywhere.
      call init_common
c
c     init_defaults will only allow one initialization, so it's safe to
c     call anywhere.
      call init_defaults(luttyo, linter)
      call menu_set_logical_units(luttyi, luttyo)
c
      call date(date_string)
c
c
c     Set batch mode (non-interactive) if asked by calling procedure.
      lrestore = .FALSE.
      if( linter ) then
         lbatch = .FALSE.
c        If we are passed in a PMI file, then execute and return
         if( string_len(pmfile) .ne. 0 ) then
            lrestore = .TRUE.
         endif
      else
         lbatch = .TRUE.
         luttyo = nferrl
         call mathmode_set_lun(luttyo)
      endif
c
c     Load data passed in to private storage so we can re-load it later on
      err = .FALSE.
      callable_loaded = .FALSE.      
      call store_callable_data(fw,fposy,fposx,inym,inxm,inpty,inptx,
     1                         llinea,ctext,cunits,err)
c     Only error possible is that data was truncated to fit array size
      if( err ) then
         if( linter ) then
            call get_char(ch,'Enter any character to continue: ',0)
         endif
         err = .FALSE.
      endif
c     Load data passed in to Postmini arrays
      batchmode = .FALSE.
      call load_callable_data(batchmode,err)
      if( err ) return
c     Set filename for use with making hardcopy files
      binfilestem = ctext
c
c     Reset autoscaling flags (callable only)
      oned_lfirst   = .TRUE.
      twod_lfirst   = .TRUE.
      threed_lfirst = .TRUE.
      comp_lfirst   = .TRUE.
      lfirst_read   = .TRUE.
c     Set title
      callable_num_titles = 1
      callable_titles(1)  = ctitle
c
c     If non-interactive run, call restore and then return
c
      if( lbatch ) then
         lplotfile = .TRUE.
         call get_plotfile_extension(default_hc_name,ext)
         call make_plotfile_name(given_plotfilename,ctext,ext)
         if( string_len(pmfile) .eq. 0) then
c           Perform default plot
            call ship_contours(arrdat(arrind(1)), ni, nj,
     1                        data_name(1), data_units(1),
     2                        data_nickname(1), is_data_log(1),
     3                        is_data_in_oxide(1), .FALSE.)
         else
            call do_restore_plot(pmfile,err)
         endif
         return
      else
         lplotfile = .FALSE.
      endif
c
c     If 'lrestore' mode run, call restore and then return
c
      if( lrestore ) then
         call do_restore_plot(pmfile,err)
         return
      endif
c
c     Else it's full interative. Call up 2D plot routine
      call ship_contours(arrdat(arrind(1)), ni, nj,
     1                   data_name(1), data_units(1),
     2                   data_nickname(1), is_data_log(1),
     3                   is_data_in_oxide(1), .FALSE.)
c
c     the end of the road...
c
10100 continue
c
c     Write save file
      if( save_plot_type .ne. PLOT_NULL_COMMAND .and.
     1    gks_is_open ) then
c        Third argument says to create a new file (VMS only)
         call write_savefile('POSTMINI.PMI',current_window_id,
     1                       .FALSE.,err)
         pmfile = savefilename
      else
         pmfile = ' '
      endif
c
CC      if( gks_is_open ) call close_gks
c
      return
      end
