C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    M_GETLUN    GET UNIQUE LOGICAL UNIT NUMBERS
C   PRGMMR: SMITH, TRACY     ORG: FSL/PROFS  DATE: 90-06-15
C
C ABSTRACT: THIS PROGRAM GETS UNIQUE LOGICAL UNIT NUMBERS FOR OPFILE
C   OR RETURNS THEM TO THE POOL FOR CLFILE.
C
C PROGRAM HISTORY LOG:
C
C
C USAGE:    CALL M_GETLUN(LUN,OPTN)
C   INPUT ARGUMENT LIST:
C     LUN      - INTEGER  LOGICAL UNIT NUMBER
C     OPTN     - INTEGER  CNCT=1, DSCT=2.
C                IF CONNECTING A FILE(CNCT) SET THE NUMBER TO
C                NEGATIVE SO IT WON'T BE USED UNTIL AFTER
C                DSCT SETS IT POSITIVE.
C
C   OUTPUT ARGUMENT LIST:
C     LUN      - INTEGER  LOGICAL UNIT NUMBER
C
C REMARKS:
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77
C   MACHINE:  NAS-9000
C$$$

       SUBROUTINE M_GETLUN(LUN,OPTN)
C* THIS PROGRAM GETS UNIQUE LOGICAL UNIT NUMBERS FOR OPFILE
C* OR RETURNS THEM TO THE POOL FOR CLFILE
       INCLUDE 'IMPLICIT'
       INTEGER LUN,NUM(80),OPTN,CNCT,DSCT,I
       PARAMETER (CNCT=1,DSCT=2)
       SAVE NUM
CC
       DATA NUM/99,98,97,96,95,94,93,92,91,90,
     +            89,88,87,86,85,84,83,82,81,80,
     +            79,78,77,76,75,74,73,72,71,70,
     +            69,68,67,66,65,64,63,62,61,60,
     +            59,58,57,56,55,54,53,52,51,50,
     +            49,48,47,46,45,44,43,42,41,40,
     +            39,38,37,36,35,34,33,32,31,30,
     +            29,28,27,26,25,24,23,22,21,20/
C* START
       IF(OPTN.EQ.CNCT) THEN
        DO 10 I=1,80
         IF(NUM(I).GT.0) THEN
           LUN=NUM(I)
           NUM(I)=-NUM(I)
           GOTO 20
         ENDIF
10      CONTINUE
         PRINT*, 'NEED MORE THAN 80 UNIT NUMBERS'
20      CONTINUE

       ELSE IF(OPTN.EQ.DSCT) THEN
C* MAKE THE NUMBER AVAILABLE BY SETTING POSITIVE
         DO 30 I=1,80
           IF(LUN.EQ.-NUM(I)) NUM(I)=ABS(NUM(I))
30       CONTINUE
       END IF
       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**---------------------------------------------------------------------
