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
      implicit none
c
c     POSTMINI postprocessor program
c
c     A utility for plotting, printing and othewise manipulating data
c     from circuit, device and process simulators and external files.
c
c     Programmer: John Faricelli
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 invalid, err, overwrite
      integer temp_ws_id, lookup_ws_id,istatus
      character*1  ch
      character*10 answer
c
c     Start usage statistics
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, .TRUE.)
      call menu_set_logical_units(luttyi, luttyo)
c
      call pm_date(date_string)
c
      call get_command_line_args(err)
      if( err ) then
         call usage
         goto 10100
      endif
c
c     Exit if we are in batch (hardcopy) plot mode
      if( lbatch ) goto 10100
c
c     Exit if we are in "plot from PMI file to screen and exit" mode
      if( lrestore ) then
c        Read from stdin, then exit
         call get_char(ch,'Press return to exit Postmini: ',0)
         goto 10100
      endif
c
c     OK. Let's do something...
c
400   call clear_screen
      write(luttyo,300) version_string
300   format(' ','POSTMINI (',a,'), Copyright (C) 2000 Compaq ',
     1           'Computer Corp'//
     1    ' ','1d        - Plot X-Y graph, or along a 1D cut line'/
     1    ' ','2d        - Plot contours in 2D'/
     1    ' ','3d        - Plot surface in 3D perspective'/
     1    ' ','Compare   - Plot X-Y graph with multiple curves'/
     1    ' ','            (line chart, bar chart)'/
     1    ' ','Exit/Quit - Exit'/
     1    ' ','Find      - Find where value occurs along a 1D cut line'/
     1    ' ','Integrate - Integrate quantity'/
     1    ' ','Line      - Print internal quantity along a 1D cut',
     1        ' line to a file'/
     1    ' ','Minmax    - Find min/max of quantity; derivatives'/
     1    ' ','Overlay   - Plot contours of multiple datasets'/
     1    ' ','Print     - Print internal quantity in formatted array',
     1        ' to a file'/
     1    ' ','Read      - Read a data file or create analytical ',
     1        'function'/
     1    ' ','Show      - Description of currently loaded data'//
     1    ' ','Default      - Modify default plot characteristics'/
     1    ' ','Save         - Save plot commands in a file'/
     1    ' ','Shell        - Run operating system commands'/
     1    ' ','Restore      - Restore plot from command file'/
     1    ' ','Window       - Manage windows on workstation')
410   write(luttyo,305)
305   format(/' ','Request? ',$)
      read(luttyi,310,end=10100) answer
310   format(a)
      call upcase(answer)
c
c     switch on answer
c
      invalid = .FALSE.
      if( answer(1:1) .eq. '1' ) then
          call plot1d
      else if( answer(1:1) .eq. '2' ) then
          call plot2d
      else if( answer(1:1) .eq. '3' ) then
          call plot3d
      else if( answer(1:1) .eq. 'C' ) then
          call compare
      else if( answer(1:1) .eq. 'D' ) then
          call change_defaults
      else if( answer(1:1) .eq. 'E' ) then
         goto 10100
      else if( answer(1:1) .eq. 'F' ) then
          call find_value
      else if( answer(1:1) .eq. 'I' ) then
          call integrate
      else if( answer(1:1) .eq. 'L' ) then
         call print_line
      else if( answer(1:1) .eq. 'M' ) then
          call pm_minmax
      else if( answer(1:1) .eq. 'O' ) then
          call compare_2d
      else if( answer(1:1) .eq. 'P' ) then
         call print_array
      else if( answer(1:1) .eq. 'Q' ) then
         goto 10100
      else if( answer(1:1) .eq. 'R' ) then
         if( answer(1:3) .eq. 'RES' ) then
            call restore_plot
         else
            call readbinfile
         endif
      else if( answer(1:1) .eq. 'S' ) then
         if( answer(1:2) .eq. 'SH' ) then
            if( answer(3:3) .eq. 'O' ) then
               call runinfo
            elseif ( answer(3:3) .eq. 'E' ) then
               call spawn_subprocess('POSTMINI', 'Postmini> ')
            else
               invalid = .TRUE.
            endif
         else
            call save_plot
         endif
      else if( answer(1:1) .eq. 'W' ) then
          call vs_window_manager
      else
         invalid = .TRUE.
      endif
c
      if( invalid ) then
         write(luttyo,390) answer
390      format(' ','Invalid request: ''',a,'''')
         goto 410
      else
         goto 400
      endif
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 .and. .not. lbatch .and. .not. lrestore .and.
     2    l_write_pmifile ) then
c
c        Look up window id to make sure we have a valid window id
         temp_ws_id = lookup_ws_id(current_window_id)
         if( temp_ws_id .ne. PLT$EMPTY_WINDOW_SLOT ) then
c           Third argument says to overwrite any existing file (VMS only)
            overwrite = .FALSE.
            call write_savefile('POSTMINI.PMI',current_window_id,
     1                          overwrite,err)
         endif
      endif
c
      if( gks_is_open ) call close_gks
c
      end
