function cdf_var_atts,id0,var,attname,default=default,zvar=zvar,names_only=names_only, $
attributes=att, $
convert_int1_to_int2=convert_int1_to_int2
if size(/type,id0) eq 7 then id=cdf_open(id0) else id=id0
t0=systime(1)
attstr = n_elements(default) ne 0 ? default : 0
quiet = !quiet
!quiet = keyword_set(convert_int1_to_int2)
inq = cdf_inquire(id)
if n_elements(var) eq 0 then begin
cdf_control,id,get_numattrs=na
for a=0,na[0]+na[1]-1 do begin
cdf_attinq,id,a,name,scope,maxent
if strmid(scope,0,1) ne 'G' then continue
gentry=0
if cdf_attexists(id,a,gentry) then begin
if keyword_set(names_only) then begin
if keyword_set(attstr) then attstr=[attstr,name] $
else attstr=name
endif else begin
cdf_attget,id,a,gentry,value
while(cdf_attexists(id, a, gentry+1)) do begin
gentry = gentry+1
cdf_attget, id, a, gentry, value_new
value = [temporary(value), temporary(value_new)]
endwhile
name = idl_validname(name,/convert_all)
if keyword_set(attstr) then attstr = create_struct(attstr,name,value) $
else attstr = create_struct(name,value)
endelse
endif
endfor
goto, done
endif else begin
varn = size(/type,var) eq 7 ? cdf_varnum(id,var,zvar) : var
if keyword_set(attname) then begin
if cdf_attexists(id,attname,varn,zvar=zvar) then cdf_attget,id,attname,varn,zvar=zvar,attstr
endif else begin
for a=0,inq.natts-1 do begin
cdf_attinq,id,a,name,scope,maxent
if strmid(scope,0,1) ne 'V' then continue
if cdf_attexists(id,a,varn,zvar=zvar) then begin
if keyword_set(names_only) then begin
if keyword_set(attstr) then attstr=[attstr,name] $
else attstr=name
endif else begin
cdf_attget,id,a,varn,zvar=zvar,value,cdf_type=cdf_type
if keyword_set(convert_int1_to_int2) and (cdf_type eq 'CDF_INT1') then begin
dprint,dlevel=4,'Warning converting attribute from INT1 to INT2'
value = value - (value ge 128) * 256
endif
if size(/type,value) eq 7 then if strpos(value,'>$<') ge 1 then $
value = strsplit(/extract,value,'>$<')
name = idl_validname(name,/convert_all)
if keyword_set(attstr) then attstr = create_struct(attstr,name,value) $
else attstr = create_struct(name,value)
endelse
endif
endfor
endelse
endelse
done:
!quiet = quiet
dprint,dlevel=8,format='(f8.3," secs to read attribute: ",a)',systime(1)-t0,n_elements(var) ne 0 ? string(var) : ''
if size(/type,id0) eq 7 then cdf_close,id
return,attstr
end