C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    MMADISOPEN  OPEN A MADIS DATA FILE
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 03-10-06
C
C ABSTRACT: OPEN A SINGLE MADIS DATA FILE, USING WHATEVER TECHNOLOGY
C   IS CURRENTLY AVAILABLE FOR USE AND HAS BEEN SELECTED BY THE USER
C
C PROGRAM HISTORY LOG:
C   03-10-06  M. BARTH    ORIGINAL VERSION
C   04-08-19  M. BARTH    FOR MADIS WEB SERVER USAGE, INSTEAD OF
C                         PUTTING THE TEMPORARY UNZIPPED FILE INTO
C                         THE $MADIS_DATA DIRECTORY, PUT IT INTO
C                         A PRIVATE DIRECTORY FOR SOLE USE BY THE
C                         SERVER PROCESSING.  THIS IS SO WE DON'T
C                         INTERFERE WITH PEOPLE DOWNLOADING FILES
C                         FROM $MADIS_DATA.
C
C USAGE:    CALL MMADISOPEN (DNAME,DNLEN,FNAME,ID,STATUS)
C
C   INPUT ARGUMENT LIST:
C     DNAME    - C       NAME OF THE REAL-TIME DATA DIRECTORY       
C     DNLEN    - I       LENGTH OF DNAME
C     FNAME    - C       YYYYDDMM_HHMM OF DATA FILE TO BE OPENED
C
C   OUTPUT ARGUMENT LIST:
C     ID       - I       ID NUMBER OF THE FILE THAT'S OPENED
C     STATUS   - I       SUCCESS_P OR ERROR CODE
C
C SUBPROGRAMS CALLED:
C     UNIQUE:    NCOPN
C     LIBARRY:   - SYSTEM LIBRARY:  SYSTEM
C
C REMARKS:
C   CURRENTLY THE ONLY TYPE OF FILES THAT ARE SUPPORTED ARE NETCDF 
C   FILES, BUT THESE MAY COME IN DIFFERENT FLAVORS...THUS THE NEED FOR
C   THIS ROUTINE.  SEE WHERE THIS IS DONE BELOW FOR MORE EXPLANATION.
C
C   NOTE THAT GZIP MUST BE IN THE PATH, AND THAT THE SYNTAX USED IN
C   THE SYSTEM CALL FOR GZIP THAT REDIRECTS STDERR TO /dev/null IS
C   BASED ON BASH OR KORN SHELL SYNTAX.  IF YOU NEED TO USE CSH OR
C   TCSH, YOU'LL NEED TO CHANGE IT.
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$

      SUBROUTINE MMADISOPEN (DNAME,DNLEN,FNAME,ID,STATUS)

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

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

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      CHARACTER*255 ANAME,LDNAME
      CHARACTER*13 LCLFNAME
      INTEGER ALEN

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 WE HAVE TO GUNZIP TO A TEMPORARY FILE
C     BEFORE OPENING THE FILE.  FOR BOTH CASES WE NEED TO TRY FIRST IN
C     THE "REAL-TIME", OR PRIMARY DIRECTORY FOR THE PARTICULAR DATASET,
C     AND IF THE DATA AREN'T THERE, TRY THE ARCHIVE DIRECTORY.  ANY USER
C     WHO EITHER PUTS ARCHIVED DATA INTO THE PRIMARY DIRECTORIES OR
C     TO AN ARCHIVE DIRECTORY STRUCTURE THAT MATCHES THE ONE USED ON
C     THE MADIS FILE SERVER, CAN USE THIS FEATURE.
C
C     WE'LL TRY BOTH THE REAL-TIME AND THE ARCHIVE BEFORE RETURNING.
C     WE DON'T BOTHER CHECKING THE STATUS OF THE SYSTEM CALL, OR IF
C     THE ARCHIVE DIRECTORY EVEN EXISTS -- NOT KNOWING WHAT CASE WE'RE
C     REALLY DEALING WITH HERE, WE CAN'T REJECT ANY CASES AS ERRONEOUS.
C
C     THE CASE CAN BE DETERMINED BY LOOKING AT THE FEXT, WHICH CONTAINS
C     ".tmp.<pid>" AS FILLED IN BY MINIT.  THE PID IS USED TO KEEP 
C     ANY SIMULTANEOUS REQUESTS FROM CAUSING ANY PROBLEMS.
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     ------------------------------------------------------------------
      IF(FEXT(2:4).EQ.'tmp')THEN

         CALL SYSTEM('gzip -dc '//DATA_DIR(1:DLEN)//LDNAME(1:DNLEN)//
     1               LCLFNAME//'.gz > '//
     1               TMP_DIR(1:TLEN)//LDNAME(1:DNLEN)//LCLFNAME//FEXT//
     2               ' 2> /dev/null')
         ID = NCOPN(TMP_DIR(1:TLEN)//LDNAME(1:DNLEN)//LCLFNAME//FEXT,
     1              NCNOWRIT,STATUS)

      ELSE

         ID = NCOPN(DATA_DIR(1:DLEN)//LDNAME(1:DNLEN)//LCLFNAME//FEXT,
     1              NCNOWRIT,STATUS)

      ENDIF

      IF(STATUS.NE.SUCCESS_P)THEN

         ANAME = DATA_DIR(1:DLEN)//'/archive/'//LCLFNAME(1:4)//'/'//
     1           LCLFNAME(5:6)//'/'//LCLFNAME(7:8)//LDNAME(1:DNLEN)
         ALEN = DLEN + 19 + DNLEN

         IF(FEXT(2:4).EQ.'tmp')THEN

            CALL SYSTEM('gzip -dc '//ANAME(1:ALEN)//LCLFNAME//'.gz > '//
     1                  TMP_DIR(1:TLEN)//LDNAME(1:DNLEN)//LCLFNAME//
     2                  FEXT//' 2> /dev/null')
            ID = NCOPN(TMP_DIR(1:TLEN)//LDNAME(1:DNLEN)//LCLFNAME//FEXT,
     1                 NCNOWRIT,STATUS)

         ELSE

            ID = NCOPN(ANAME(1:ALEN)//LCLFNAME//FEXT,NCNOWRIT,STATUS)

         ENDIF

      ENDIF

C     ------------------------------------------------------------------
C     IF WE COULDN'T OPEN THE TEMPORARY FILE, THAT MEANS THAT WE TRIED
C     TO UNZIP A FILE THAT WASN'T THERE, SO WE NOW HAVE AN EMPTY FILE.
C     DELETE IT.
C     ------------------------------------------------------------------
      IF(STATUS.NE.0.AND.FEXT(2:4).EQ.'tmp')CALL SYSTEM('rm -f '//
     2     TMP_DIR(1:TLEN)//LDNAME(1:DNLEN)//LCLFNAME//FEXT)

      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**---------------------------------------------------------------------
