	LOGICAL FUNCTION NC_GET_ATTRIB_STRING ( dset, varid, attrib,
     .                                   do_warn, vname, maxlen,
     .                                   attlen, attoutflag, string)

*
*
*  This software was developed by the Thermal Modeling and Analysis
*  Project(TMAP) of the National Oceanographic and Atmospheric
*  Administration's (NOAA) Pacific Marine Environmental Lab(PMEL),
*  hereafter referred to as NOAA/PMEL/TMAP.
*
*  Access and use of this software shall impose the following
*  obligations and understandings on the user. The user is granted the
*  right, without any fee or cost, to use, copy, modify, alter, enhance
*  and distribute this software, and any derivative works thereof, and
*  its supporting documentation for any purpose whatsoever, provided
*  that this entire notice appears in all copies of the software,
*  derivative works and supporting documentation.  Further, the user
*  agrees to credit NOAA/PMEL/TMAP in any publications that result from
*  the use of this software or in any product that includes this
*  software. The names TMAP, NOAA and/or PMEL, however, may not be used
*  in any advertising or publicity to endorse or promote any products
*  or commercial entity unless specific written permission is obtained
*  from NOAA/PMEL/TMAP. The user also understands that NOAA/PMEL/TMAP
*  is not obligated to provide the user with any support, consulting,
*  training or assistance of any kind with regard to the use, operation
*  and performance of this software nor to provide the user with any
*  updates, revisions, new versions or "bug fixes".
*
*  THIS SOFTWARE IS PROVIDED BY NOAA/PMEL/TMAP "AS IS" AND ANY EXPRESS
*  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
*  ARE DISCLAIMED. IN NO EVENT SHALL NOAA/PMEL/TMAP BE LIABLE FOR ANY SPECIAL,
*  INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
*  RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
*  CONTRACT, NEGLIGENCE OR OTHER TORTUOUS ACTION, ARISING OUT OF OR IN
*  CONNECTION WITH THE ACCESS, USE OR PERFORMANCE OF THIS SOFTWARE. 
*
*
* get a string attribute from a netCDF data structure if the attribute exists
* 
* Based on CD_GET_ATTRIB
* V600 -  5/05 *acm*  Attribute handling upgrade. Rework to read information 
*                     from data strucure rather than the netCDF file.
*                     Replace NCAINQ with CD_GET_VAR_ATT_INFO
*                     Replace NCAGTC with CD_GET_VAR_ATTR
*                     This routine now returns both character and real results,
*                     so it replaces CD_GET_ATTVAL.
*         6/06 *acm*  replace istat with status
*         8/06 *acm*  test for too_long should be .GT. IF (attlen .GT. maxlen) THEN
* V602  2/07 *acm* Fix bug 1492, changing attributes of coordinate variables 
* V602 *acm* make sure lena=length of attr name is defined before writing warning
* V65  *acm* 1/10  Change all netcdf2-style calls and parameter names to 
*                  netcdf3-style calls, which are also consistent with netcdf4.
* V65  *acm* 1/10  Make sure parameters values are consistent with 
*                              whats in netcdf.inc from netcdf4.
* V6.74 3/12 *acm* cleanup ifdefs and unnecessary include files
* V6.85 2/13 *acm* Fix ticket 2038: making warning message for too-long string
* V697 12/15 *acm* Increased length of command-line string and risc_buff buffer
* V697  2/16 *acm* DO NOT increase length of command-line string and risc_buff buffer

* argument definitions:
*	dset	 - dataset number
*       varid    - variable id 
*       attrib   - attribute to search for
*	do_warn	 - logical flag to issue warning if invalid attribute
*                - warnings are never issued if attribute doesn't exit
*       vname    - name of variable owning attribute (used in error msgs)
*       maxlen   - maximum allowable return string
*       attlen   - length of output (string length or number of values)
*       attoutflag- 1 if attribute to be written to output files, 0 if not
*       string   - array in which to return result if string type

* argument declarations
      LOGICAL       do_warn
      INTEGER	    dset, varid, maxlen, attlen, attoutflag
      CHARACTER*(*) attrib, vname, string
      real*8        dval(40)

#include "gt_lib.parm"
      include 'netcdf.inc'
      include 'xio.cmn_text'
      include 'tmap_errors.parm'
      include 'xrisc_buff.cmn'

* internal variable declarations:
      LOGICAL too_long
      INTEGER TM_LENSTR1, NCF_GET_VAR_ATTR,
     .        lena, lenb, attype, status, i, dset_num, attid
      CHARACTER*128 aname
      CHARACTER*500 buff

      INTEGER tt  ! nice short name
      EQUIVALENCE (tt,lunit_errors)

      INTEGER slen, flen
      PARAMETER (flen=10240)        ! size of attribute string, match size_rbuff
#ifdef sun
      BYTE      fhol(flen), ghol(flen)      ! c-type Hollerith string buffer
#else
      INTEGER*1 fhol(flen), ghol(flen)     ! c-type Hollerith string buffer
#endif 

       NC_GET_ATTRIB_STRING = .FALSE.
       IF (dset .EQ. unspecified_int4) RETURN
       IF (varid .LT. 0) RETURN

* initialize
       string(:maxlen) = ' '
       too_long = .FALSE.

* inquire about the attribute by name, using its id number

      CALL CD_GET_VAR_ATT_ID (dset, varid, attrib, attid, status)
      IF (attid .GT. 0) CALL CD_GET_VAR_ATT_INFO (dset, varid, attid,
     .                   aname, attype, attlen, attoutflag, status )

      IF ( status .NE. merr_ok ) THEN
         NC_GET_ATTRIB_STRING = .FALSE.
         RETURN  ! doesn't exist
      ENDIF

      lena = TM_LENSTR1( aname )
      CALL TM_FTOC_STRNG (aname(:lena), ghol, flen)

* Get attribute length and value

      dset_num = dset
      IF (dset_num .LT. -2) dset_num = -2
      attlen = maxlen  ! for strings dont want to overrun the length
      status = NCF_GET_VAR_ATTR(dset_num, varid, ghol, fhol, attlen, dval)

      IF ( status .NE. merr_ok ) THEN
         NC_GET_ATTRIB_STRING = .FALSE.
         attlen = 0
         attoutflag = 0
         RETURN  
      ENDIF

      IF ( attype.EQ.NF_CHAR ) THEN

         lena = attlen
         IF (attlen .GT. maxlen) THEN
            fhol(maxlen) = 0
            lena = maxlen
            too_long = .TRUE.
         ENDIF

         CALL TM_CTOF_STRNG ( fhol, string, lena )

         NC_GET_ATTRIB_STRING = .TRUE.
         IF ( too_long ) GOTO 5000

      ELSE

         NC_GET_ATTRIB_STRING = .TRUE.
         WRITE (string, *) (dval(i), i=1,attlen)
         GOTO 5010
      ENDIF

      RETURN

* warning exit

 5000 lenb = TM_LENSTR1( string )
      lenb = MIN(lenb, maxlen)
      lenb = MIN(lenb, 128)
 
      lena = TM_LENSTR1( attrib )

      buff = string(:lenb)//'"'//attrib(:lena)//
     .             '" in netCDF variable: '//vname
      lenb = TM_LENSTR1( buff )
      IF ( do_warn ) CALL TM_NOTE( buff(:lenb), tt )
      IF ( do_warn .AND. too_long ) THEN
         IF (maxlen .GE. 3) WRITE (buff, 1000) maxlen, attlen
         IF (maxlen .LT. 3) WRITE (buff, 1001) maxlen, attlen
1000     FORMAT ('exceeds expected length. Maximum characters: ',I4, ' Length is', I8)
1001     FORMAT ('exceeds expected length. Maximum characters: ',I2, ' Length is', I8)
         lenb = TM_LENSTR1( buff )
         CALL TM_NOTE( buff(:lenb), tt )
         WRITE (buff, 1010) maxlen
1010     FORMAT ('--TRUNCATED TO LENGTH ',I4, '--')
         lenb = TM_LENSTR1( buff )
	 CALL TM_CTOF_STRNG( fhol, buff(lenb+2:maxlen), (maxlen-lenb))
	 
	 attlen = maxlen
      ENDIF
      RETURN

* Note about unexpected attribute type
 5010 lenb = TM_LENSTR1( string )
 
      lena = TM_LENSTR1( attrib )
      buff = 'Unexpected attribute type: attribute "'//attrib(:lena)//
     .       '" float instead of string, in netCDF variable: '//vname
      lenb = TM_LENSTR1( buff )
      IF ( do_warn ) CALL TM_NOTE( buff(:lenb), tt )
      RETURN

      END
