C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MMADISCLS1  CLOSE AN OPEN NETCDF FILE 
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 04-08-20
C
C ABSTRACT: CLOSE AN OPEN NETCDF FILE AND, IF IT'S A TEMPORARY FILE
C   ON THE MADIS WEB SERVER, DELETE IT.
C
C PROGRAM HISTORY LOG:
C   04-08-20  M. BARTH    ORIGINAL VERSION
C
C USAGE:    CALL MMADISCLS1(NCID,DNAME,DNLEN,FNAME)
C
C   INPUT ARGUMENT LIST:
C     NCID     - I       NETCDF ID OF THE OPEN FILE
C     DNAME    - C       NAME OF THE REAL-TIME DATA DIRECTORY       
C     DNLEN    - I       LENGTH OF DNAME
C     FNAME    - C       YYYYDDMM_HHMM FILENAME
C
C SUBPROGRAMS CALLED:
C     UNIQUE:  M_LSTCHR
C     LIBRARY:   - NETCDF:  NCCLOS
C                - SYSTEM:  SYSTEM
C
C REMARKS:
C   NONE
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$

      SUBROUTINE MMADISCLS1 (NCID,DNAME,DNLEN,FNAME)

C     ------------------------------------------------------------------
C     INCLUDES AND COMMONS
C     ------------------------------------------------------------------
      INCLUDE 'IMPLICIT'
      INCLUDE 'MADISCON'
      INCLUDE 'MADISCOM'
      INCLUDE 'NETCDFI'

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      INTEGER NCID,DNLEN
      CHARACTER*(*) DNAME,FNAME

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      CHARACTER*255 LDNAME
      CHARACTER*13 LCLFNAME
      INTEGER RCODE,M_LSTCHR,I

C     ------------------------------------------------------------------
C     COPY THE CHARACTER ARGUMENTS TO LOCAL VERSIONS -- THIS IS TO GET
C     AROUND G77 LIMITATIONS...
C     ------------------------------------------------------------------
      LDNAME = DNAME(1:DNLEN)
      LCLFNAME = FNAME

C     ------------------------------------------------------------------
C     HERE'S THE DEAL.  THERE ARE TWO BASIC CASES:  NORMAL USAGE AND
C     MADIS WEB SERVER USAGE.  THE NORMAL USAGE CASE CAN EITHER BE 
C     READING LOCAL FILES OR READING GZIPPED FILES VIA OPENDAP/DODS.
C     FOR THE WEB SERVER CASE THE NETCDF FILE IS ONLY KEPT TEMPORARILY
C     WHILE IT'S BEING READ BY THIS PROCESS, SO WE'LL DELETE IT AFTER
C     IT'S BEEN CLOSED.
C
C     THE WEB SERVER CASE IS IDENTIFIED BY HAVING ".tmp.<pid>" 
C     AS FILLED IN BY MINIT INCLUDED IN THE FEXT.  
C
C     MORE DETAILS ON WHAT'S BEING ASSUMED TO MAKE THIS WORK:
C
C     FNAME STRUCTURE:  
C     - YYYYMMDD_HHMM
C
C     REAL-TIME DIRECTORIES (SAMPLE):
C     - $MADIS_DATA/public/LDAD/mesonet/netCDF
C
C     ARCHIVE DIRECTORIES:
C     - $MADIS_DATA/public/archive/YYYY/MM/DD/LDAD/mesonet/netCDF
C
C     TEMPORARY DIRECTORIES:
C     - $MADIS_SERVER_ROOT/LDAD/mesonet/netCDF
C     ------------------------------------------------------------------
      CALL NCCLOS(NCID,RCODE)
      
      IF(RCODE.EQ.0.AND.FEXT(2:4).EQ.'tmp')THEN

C     ------------------------------------------------------------------
C     NOTE THAT WITH THE DODS NETCDF LIBRARY YOU GET RCODE = 0 EVEN
C     IF NCID ISN'T A VALID ID!  THEREFORE, WE'LL MAKE SURE WE ALSO
C     HAVE A FILENAME.
C     ------------------------------------------------------------------
         I = M_LSTCHR(LCLFNAME,13)
         IF(I.NE.0)CALL SYSTEM('rm -f '//
     2               TMP_DIR(1:TLEN)//LDNAME(1:DNLEN)//LCLFNAME//FEXT)

      ENDIF

      RETURN
      END
C**---------------------------------------------------------------------
C**REQUIRED STANDARD FSL DISCLAIMER (NOVEMBER 2000)
C**---------------------------------------------------------------------
C**  OPEN SOURCE LICENSE/DISCLAIMER,  FORECAST SYSTEMS LABORATORY
C**  NOAA/OAR/FSL, 325 BROADWAY BOULDER, CO 80305
C**
C**  THIS SOFTWARE IS DISTRIBUTED UNDER THE OPEN SOURCE DEFINITION, 
C**  WHICH MAY BE FOUND AT http://www.opensource.org/osd.html.
C**
C**  IN PARTICULAR, REDISTRIBUTION AND USE IN SOURCE AND BINARY FORMS,
C**  WITH OR WITHOUT MODIFICATION, ARE PERMITTED PROVIDED THAT THE 
C**  FOLLOWING CONDITIONS ARE MET:
C**
C**  - REDISTRIBUTIONS OF SOURCE CODE MUST RETAIN THIS NOTICE, THIS LIST
C**    OF CONDITIONS AND THE FOLLOWING DISCLAIMER.
C**
C**  - REDISTRIBUTIONS IN BINARY FORM MUST PROVIDE ACCESS TO THIS 
C**    NOTICE, THIS LIST OF CONDITIONS AND THE FOLLOWING DISCLAIMER, AND
C**    THE UNDERLYING SOURCE CODE.
C**
C**  - ALL MODIFICATIONS TO THIS SOFTWARE MUST BE CLEARLY DOCUMENTED, 
C**    AND ARE SOLELY THE RESPONSIBILITY OF THE AGENT MAKING THE 
C**    MODIFICATIONS.
C**
C**  - IF SIGNIFICANT MODIFICATIONS OR ENHANCEMENTS ARE MADE TO THIS
C**    SOFTWARE, THE FSL SOFTWARE POLICY MANAGER 
C**    (softwaremgr.fsl@noaa.gov) BE NOTIFIED.
C**
C**  THIS SOFTWARE AND ITS DOCUMENTATION ARE IN THE PUBLIC DOMAIN AND 
C**  ARE FURNISHED "AS IS."  THE AUTHORS, THE UNITED STATES GOVERNMENT,
C**  ITS INSTRUMENTALITIES, OFFICERS, EMPLOYEES, AND AGENTS MAKE NO 
C**  WARRANTY, EXPRESS OR IMPLIED, AS TO THE USEFULNESS OF THE SOFTWARE
C**  AND DOCUMENTATION FOR ANY PURPOSE.  THEY ASSUME NO RESPONSIBILITY 
C**  (1) FOR THE USE OF THE SOFTWARE AND DOCUMENTATION; OR (2) TO 
C**  PROVIDE TECHNICAL SUPPORT TO USERS.
C**---------------------------------------------------------------------
