function [speed,dir,speeds,dirs,lat,lon,land,mle,mles,choices,namb,speedL2B,dirL2B,speedNCEP,dirNCEP,rainFlag,speed2,dir2,mle2,choices2,HurrParams,speedMAP,dirMAP,mleMAP,choicesMAP,modelsp,modeldr,MAPspds,MAPdrs,numambigsmap,MAPvalues]=read_l2h(filename,inpath);
% 
% function read_l2h(filename)
%
% read_l2h reads a QuikScat L2H HDF file
%
% [speed,dir,speeds,dirs,lat,lon,land,mle,mles,choices,namb,speedL2B,dirL2B,speedNCEP,dirNCEP,rainFlag,speed2,dir2,mle2,choices2,HurrParams,speedMAP,dirMAP,mleMAP,choicesMAP,modelsp,modeldr,MAPspds,MAPdrs,numambigsmap,MAPvalues]=read_l2h(filename,inpath);
%
% 'inpath' is the path to the L2H file
% 'filename' is the L2H file name
%
% if the L2H file does not contain certain information (ie. MAP estimation)
% those data sets are output as zeros
%
% Does not read simultaneous wind/rain stuff yet

if nargin<2,
    QSSourcePath = '/auto/users/williams/';
else
    QSSourcePath =inpath;
end;

QSDestPath = './'; 
SourceFilename = [QSSourcePath,filename]

S = hdfinfo(SourceFilename);
hdfinfo(SourceFilename)

%%standard stuff
ascnode=double(hdfread(SourceFilename, 'ascnode'))/180.0


swath_ind=hdfread(SourceFilename, 'swath_indicies')
atstart=swath_ind(1);
atend=swath_ind(2);
ctstart=swath_ind(3);
ctend=swath_ind(4);

lat1 = hdfread(SourceFilename, 'latitude');
lat=zeros(ctend-ctstart+1,atend-atstart+1);
lat(:,:)=double(lat1.')./180.0;
lon1 = hdfread(SourceFilename, 'longitude');
lon=zeros(ctend-ctstart+1,atend-atstart+1);
lon(:,:)=double(lon1.')./180.0;
land1 = hdfread(SourceFilename, 'land_mask');
land=zeros(ctend-ctstart+1,atend-atstart+1).*1.0;
land=double(land1.');

rain = hdfread(SourceFilename, 'rain_flag');
rainFlag=double(rain.');

wind_speed_ambigs = hdfread(SourceFilename, 'wind_speed');
wind_speed_ambigs=double(wind_speed_ambigs);

wind_dir_ambigs = hdfread(SourceFilename,'wind_dir');
wind_dir_ambigs=double(wind_dir_ambigs);

wvc_selection = hdfread(SourceFilename, 'wvc_selection');
wvc_selection=int16(wvc_selection);
choices=wvc_selection.'.*1.0;
    
ambig_select2a=hdfread(SourceFilename, 'ambig_select2');
ambig_select2=ambig_select2a{1};
ambig_select2(8:end)'
%size(ambig_select2(8:11))
%size('None')

if ambig_select2(8:11)'=='None'
    wvc_selection2=wvc_selection;
    choices2=choices;
else,
    wvc_selection2 = hdfread(SourceFilename, 'wvc_selection2');
    wvc_selection2=int16(wvc_selection2);
    choices2=wvc_selection2.'.*1.0;
end

mapfilea=hdfread(SourceFilename, 'map_file');
mapfile=mapfilea{1};
modelsp=zeros(ctend-ctstart+1,atend-atstart+1);
modeldr=zeros(ctend-ctstart+1,atend-atstart+1);
MAPspds=zeros(ctend-ctstart+1,atend-atstart+1,4);
MAPdrs=zeros(ctend-ctstart+1,atend-atstart+1,4);
MAPvalues=zeros(ctend-ctstart+1,atend-atstart+1,4);
numambigsmap=zeros(ctend-ctstart+1,atend-atstart+1);
HurrParams=zeros(5,1);
mapfile(8:11)'
%mapfile(8:14)'
if mapfile(8:11)'=='None',
    wvc_selectionMAP=wvc_selection;
    choicesMAP=choices;
%elseif mapfile(8:23+7)'=='MAP Ambiguity Selection',
elseif mapfile(8:14)'=='MAP Amb'%iguity Selection',
    disp('MAP AMBIGUITY SELECTION FILE')
    HurrParams=double(hdfread(SourceFilename, 'hurricane_model_params'))';
    HurrParams=HurrParams./100
    
    wvc_selectionMAP = hdfread(SourceFilename, 'wvc_selection_map');
    wvc_selectionMAP=int16(wvc_selectionMAP);
    choicesMAP=wvc_selectionMAP.'.*1.0;
    
    map_model_speed = hdfread(SourceFilename, 'map_model_speed');
    map_model_dir = hdfread(SourceFilename, 'map_model_dir');
    modelsp(:,:)=double(map_model_speed.')./200.0;  
    modeldr(:,:)=double(map_model_dir.')./180.0;
else
    disp('MAP ESTIMATION FILE')
    HurrParams=double(hdfread(SourceFilename, 'hurricane_model_params'))';
    HurrParams=HurrParams./100
    
    wvc_selectionMAP = hdfread(SourceFilename, 'wvc_selection_map');
    wvc_selectionMAP=int16(wvc_selectionMAP);
    choicesMAP=wvc_selectionMAP.'.*1.0;
    
    
    numambigsmap= hdfread(SourceFilename, 'num_ambigs_map');
    numambigsmap=int16(numambigsmap)*1.0;
    
    
    map_model_speed = hdfread(SourceFilename, 'map_model_speed');
    map_model_dir = hdfread(SourceFilename, 'map_model_dir');
    modelsp(:,:)=double(map_model_speed.')./200.0;
    modeldr(:,:)=double(map_model_dir.')./180.0;
    
    map_speeds = hdfread(SourceFilename, 'wind_speed_map');
    map_dirs = hdfread(SourceFilename, 'wind_dir_map');
    map_values = hdfread(SourceFilename, 'max_aposteriori_prob_est');
    for j=1:4,
        clear temp
        temp(:,:)=double(map_speeds(j,:,:));
        MAPspds(:,:,j)=temp'./200;
        temp(:,:)=double(map_dirs(j,:,:));
        MAPdrs(:,:,j)=temp'./180;
        temp(:,:)=double(map_values(j,:,:));
        MAPvalues(:,:,j)=temp'./1000.0;
    end;
end

namb = hdfread(SourceFilename, 'num_ambigs');
namb=double(namb');

wind_speed_L2B = hdfread(SourceFilename, 'wind_speed_L2B');
wind_dir_L2B = hdfread(SourceFilename, 'wind_dir_L2B');

speedL2B=zeros(ctend-ctstart+1,atend-atstart+1);
dirL2B=zeros(ctend-ctstart+1,atend-atstart+1);
speedL2B(:,:)=wind_speed_L2B.'./200.0;
dirL2B(:,:)=wind_dir_L2B.'./180.0;

wind_speed_ncep = hdfread(SourceFilename, 'wind_speed_ncep');
wind_dir_ncep = hdfread(SourceFilename, 'wind_dir_ncep');
size(wind_speed_ncep)
speedNCEP=zeros(ctend-ctstart+1,atend-atstart+1);
dirNCEP=zeros(ctend-ctstart+1,atend-atstart+1);
size(speedNCEP)
speedNCEP(:,:)=wind_speed_ncep.'./200.0;
dirNCEP(:,:)=wind_dir_ncep.'./180.0;

mle1=hdfread(SourceFilename, 'max_likelihood_est');
mle1=double(mle1);

speed=zeros(ctend-ctstart+1,atend-atstart+1);
dir=zeros(ctend-ctstart+1,atend-atstart+1);
speeds=zeros(ctend-ctstart+1,atend-atstart+1,4);
dirs=zeros(ctend-ctstart+1,atend-atstart+1,4);

speed2=zeros(ctend-ctstart+1,atend-atstart+1);
dir2=zeros(ctend-ctstart+1,atend-atstart+1);
mle2=zeros(ctend-ctstart+1,atend-atstart+1);
speedMAP=zeros(ctend-ctstart+1,atend-atstart+1);
dirMAP=zeros(ctend-ctstart+1,atend-atstart+1);
mleMAP=zeros(ctend-ctstart+1,atend-atstart+1);
for ia=1:atend-atstart+1,
    for ic=1:ctend-ctstart+1,
        temp=wvc_selection(ia,ic);
        temp2=wvc_selection2(ia,ic);
        tempMAP=wvc_selectionMAP(ia,ic);
        if temp>0,
            speed(ic,ia)=wind_speed_ambigs(temp,ia,ic)./200.0;
            dir(ic,ia)=wind_dir_ambigs(temp,ia,ic)./180.0;
            mle(ic,ia)=mle1(temp,ia,ic)./1000.0;
        else
            speed(ic,ia)=wind_speed_ambigs(1,ia,ic)./200.0;
            dir(ic,ia)=wind_dir_ambigs(1,ia,ic)./180.0;
            mle(ic,ia)=mle1(1,ia,ic)./1000.0;
        end
        if temp2>0,
            speed2(ic,ia)=wind_speed_ambigs(temp2,ia,ic)./200.0;
            dir2(ic,ia)=wind_dir_ambigs(temp2,ia,ic)./180.0;
            mle2(ic,ia)=mle1(temp2,ia,ic)./1000.0;
        else
            speed2(ic,ia)=wind_speed_ambigs(1,ia,ic)./200.0;
            dir2(ic,ia)=wind_dir_ambigs(1,ia,ic)./180.0;
            mle2(ic,ia)=mle1(1,ia,ic)./1000.0;
        end 
        if tempMAP>0,
            speedMAP(ic,ia)=wind_speed_ambigs(tempMAP,ia,ic)./200.0;
            dirMAP(ic,ia)=wind_dir_ambigs(tempMAP,ia,ic)./180.0;
            mleMAP(ic,ia)=mle1(tempMAP,ia,ic)./1000.0;
        else
            speedMAP(ic,ia)=wind_speed_ambigs(1,ia,ic)./200.0;
            dirMAP(ic,ia)=wind_dir_ambigs(1,ia,ic)./180.0;
            mleMAP(ic,ia)=mle1(1,ia,ic)./1000.0;
        end 
        for k=1:4
            speeds(ic,ia,k)=wind_speed_ambigs(k,ia,ic)./200.0;
            dirs(ic,ia,k)=wind_dir_ambigs(k,ia,ic)./180.0;
            mles(ic,ia,k)=mle1(k,ia,ic)/1000.0;
            
        end;
    end;
end;
