; Axel 960925:
; below definition is necessary since all these functions
; are in one file. It is still necessary to compile these
; functions:
;     .run readpathp
; alternatively the functions maybe saved in seperate files

forward_function readpathp, ppgetattr, ppgetsds_info,ppgetsds
; begin function.
function readpathp,filename,var=var,verbose=verbose
; Rudimentary IDL function to access individual variables from the
; TOVS PathP data set.
;tovs-pdaily-l3-19880723-v1
; usage exmaple:
;    data=readpathp('tovs-pdaily-l3-19880723-v1',var='SKTEMP',[/verbose])
;    setting the verbose switch prints out a list of meta data.
;
; This is a subset of valid attributes for the TOVS PathP data
; set.
  
  attribute_list=$
    ['PROJECT','DATASET_NAME','SOURCE_NAME','PROCESSING_LEVEL',$
     'REFERENCE_DATE','START_DATE','STOP_DATE','POSITION_TYPE',$
     'GRID_TYPE','PROJECTION','Temporal_Res','Spatial_X_Res',$
     'Spatial_Y_Res','LATITUDE','LONGITUDE']

; this is a list of valid scientific data sets in the the TOVS
; PathP data set

  sds_names=$
    ['LATITUDE_GRID','LONGITUDE_GRID','TEMP','WVAPOR','SKTEMP',$
     'FCLD','CLPRESS','CLTEMP','ZANGLE','EMISS','PBLSTRAT',$
     'Cg','ALPHA','IIIreject','OBS','TEMP-SD','WVAPOR-SD','SKTEMP-SD',$
     'FCLD-SD','CLPRESS-SD','CLTEMP-SD','ZANGLE-SD','EMISS-SD',$
     'PBLSTRAT-SD','Cg-SD','ALPHA-SD']

  varidx=where(sds_names eq var,nmatch)
  if nmatch eq 0 then begin
      print,'No such scientific data set : '+var
      return,-1
  endif

  hdf_id = hdf_sd_start(filename,/read)
;  hdf_sd_fileinfo,hdf_id,ndatasets,nglobal_attr
  if keyword_set(verbose) then begin
      for i=0,n_elements(attribute_list)-1 do begin
          ret=ppgetattr(hdf_id,attribute_list(i),attribute)
          print,attribute_list(i)+': ',attribute
      endfor
  end
  if keyword_set(verbose) then begin
      ret=ppgetsds_info(hdf_id,var)
  endif
  sds=ppgetsds(hdf_id,var)  
  hdf_sd_end,hdf_id
return,sds
end

function ppgetattr,hdf_id,attribute_name,attribute
; This function obtains a single global character attribute from 
; a TOVS Path-P hdf file pointed to by hdf_id


  index=hdf_sd_attrfind(hdf_id,attribute_name)
  if index lt 0 then $
    return,-1
  hdf_sd_attrinfo,hdf_id,index,NAME=attribute_name,TYPE=number_type,$
    COUNT=length,DATA=attribute
  return,0
; Pressure levels for temperature TEMP
;  10,30,50,70,100,300,500,700,850
; Presure layers mb for water vapor
;  300-400,400-500,500-700,700-850,850-1000
  end

function ppgetsds,hdf_id,sdsname
; this function obtains a single scientific data set in the file
; pointed to by hdf_id. The name of the scientific data set is given
; by sdsname

  index = hdf_sd_nametoindex(hdf_id,sdsname)
  if index lt 0 then $
    return,-1
  sds_id = hdf_sd_select(hdf_id,index)
  hdf_sd_getdata,sds_id,data
  hdf_sd_getinfo,sds_id,LABEL=l,UNIT=u,FORMAT=f,COORDSYS=c,FILL=fill,RANGE=r,$
    CALDATA=cd,NDIMS=ndims,DIMS=dims,TYPE=ty
  
  hdf_sd_endaccess,sds_id
  return,data
  end

function ppgetsds_info,hdf_id,sdsname
; get meta data about individual SDS 

  index = hdf_sd_nametoindex(hdf_id,sdsname)
  if index lt 0 then $
    return,-1
  sds_id = hdf_sd_select(hdf_id,index)
  hdf_sd_getinfo,sds_id,LABEL=l,UNIT=u,FORMAT=f,$
    COORDSYS=c,FILL=fill,RANGE=r,$
    CALDATA=cd,NDIMS=ndims,DIMS=dims,TYPE=ty
  
  help,l,u,f,c,fill,r,cd,ndims,dims,ty
  for i =0,ndims-1 do begin
      dim_id=hdf_sd_dimgetid(sds_id,i)
      hdf_sd_dimget,dim_id,COUNT=c,format=f,label=l,$
                        name=name,nattr=nattr,scale=scale,$
                       type=type,unit=unit
      print,'Dimension data for dimension : ',i
      help,c,f,l,name,nattr,scale,type,unit
      print,scale
  endfor
  hdf_sd_endaccess,sds_id
  return,0
  end







