[wspeeds,wdirs,ambiguity_select,number_ambguities,L2B_ATindex,L2B_CTindex,L2B_speed,L2B_dir,L2B_Mspeed,L2B_Mdir,longitude,latitude,land] = read_L2H_UHR_cdf(infile);

%**********************************************************************
% Filename:   read_L2H_UHF_cdf.m
% --------
% Program Usage:
%
%   This procedure/subroutine is called by user
%
% --------
% Program Description:
%  
%   Reads ASCAT UHR L2H (high res winds) NetCDF files produced by 
%   BYU UHR processing code.  Uses various netcdf file access routines.
%
% --------
% Glossary of parameters passed into routine
%
%   1. "infile" - name of avewr netcdf file to read
%
% --------
% Glossary of parameters returned by routine
%   1. "wspeeds"            - UHR wind speed ambiguities
%   2. "wdirs"              - UHR wind dir amgiguities
%   3. "ambiguity_select"   - UHR selected ambiguity, 0=none
%   4. "number_ambiguities" - UHR number of ambiguities [1..4],
%   0=invalid wind
%   5. "L2B_ATindex"        - along track index in EPS file for UHR WVC 
%   6. "L2B_CTindex"        - cross track index in EPS file for UHR WVC 
%   7. "L2B_speed"          - low res EPS file selected wind speed
%   8. "L2B_dir"            - low res EPS file selected wind directoin
%   9. "L2B_Mspeed"         - low res EPS file model wind speed
%   10. "L2B_Mdir"          - low res EPS file model wind direction
%   13. "longitude"         - UHR wvc longitude
%   14. "latitude"          - UHR wvc latitude
%   15. "land"              - UHR land flag (0=land, 1=ocean or invalid)
%

%
% --------
% Program Language: Matlab
%  note: code requires netcdf package in matlab 2008a and above
%  if you don't have this, use the mexcdf packaage and modificy
%  the netcdf calls below  
% --------
%
% Program Author:	David G. Long
%
%==================================================================
%   Copyright (c) 2010, Brigham Young University
%==================================================================		
% Notes:
%
% 1. This code is freely modifiable for customized and unlimited use.
%    Please acknowledge original source and author
%
% --------
% Revisions: 		Date		Author		Description
%*********************************************************************

%% when using mexcdf, make sure path includes path to mexcdf directory

% open input netcdf file
ncid = netcdf.open(infile,'NOWRITE'); % builtin matlab necdf
%%[ncid,status] = mexnc('OPEN',infile,'nowrite');  % mexcdef

GLOBAL = 1;       % read and print global attributes if set to 1

if GLOBAL == 1 % (optionally) read and print selected GLOBAL variables
  NC_GLOBAL=-1;
  % define global attributes to display
  gnames={'long_name', 'rev', 'year', 'day', 'ngrid','SZF_start_time',...
	'SZF_stop_time','L2B_filename'};
  disp(sprintf('Selected netcdf Global Attributes: %s',infile));
  for i=1:length(gnames)
    value=netcdf.getAtt(ncid,NC_GLOBAL,char(gnames(i)));
    if isinteger(value)
      disp(sprintf('%s = %d',char(gnames(i)),value))
    elseif isfloat(value)
      disp(sprintf('%s = %f',char(gnames(i)),value))
    else % must be a string
      disp(sprintf('%s = %s',char(gnames(i)),value))
    end
    
    %% mexcdf code to read and decode global attribute type
    %% [value, status] = mexnc('GET_ATT_TEXT',ncid,NC_GLOBAL, char(gnames(i)));
    %% if status == 0
    %%  disp(sprintf('%s = %s',char(gnames(i)),value))
    %% else
    %%  [value, status] = mexnc('GET_ATT_INT',ncid,NC_GLOBAL, char(gnames(i)));
    %%  if status == 0
    %%	disp(sprintf('%s = %d',char(gnames(i)),value))
    %%  else
    %%	[value, status] = mexnc('GET_ATT_FLOAT',ncid,NC_GLOBAL, char(gnames(i)));
    %%	if status == 0
    %%	  disp(sprintf('%s = %f',char(gnames(i)),value))
    %%	else
    %%	  [value, status] = mexnc('GET_ATT_DOUBLE',ncid,NC_GLOBAL, char(gnames(i)));
    %%	  if status == 0
    %%	    disp(sprintf('%s = %lf',char(gnames(i)),value))
    %%	  end
    %%	end	  
    %%  end
    %% end
    
  end
end

% variables to extract
varnames={'longitude','wspeeds','wdirs',...
      'ambiguity_select','number_ambiguities','L2B_ATindex',...
      'L2B_CTindex','L2B_speed','L2B_dir','L2B_Mspeed',...
      'L2B_Mdir'};
% reac each variable and scale as needed
for i=1:length(varnames)
  varid=netcdf.inqVarID(ncid,char(varnames(i)));  
  %%[varid,status] = mexnc('INQ_VARID',ncid,char(varnames(i)));
  var=netcdf.getVar(ncid,varid);  
  %%[var,status] = mexnc('GET_VAR_SHORT', ncid, varid);
  scale_factor=netcdf.getAtt(ncid,varid,'scale_factor');
  %%[scale_factor,status] = mexnc('GET_ATT_FLOAT', ncid, varid,'scale_factor');
  offset=netcdf.getAtt(ncid,varid,'offset');
  %%[offset,status] = mexnc('GET_ATT_SHORT', ncid, varid,'offset');

  str=sprintf('%s=double(var+offset)*scale_factor;',char(varnames(i)));
  eval(str);
end

% latitude and land are handled separately since the land flag
% is encoded as part of the latitude variable
varid=netcdf.inqVarID(ncid,'latitude');  
%%[varid,status] = mexnc('INQ_VARID',ncid,'latitude');
var=netcdf.getVar(ncid,varid);  
%%[var,status] = mexnc('GET_VAR_SHORT', ncid, varid);
scale_factor=netcdf.getAtt(ncid,varid,'scale_factor');
%%[scale_factor,status] = mexnc('GET_ATT_FLOAT', ncid, varid,'scale_factor');
offset=netcdf.getAtt(ncid,varid,'offset');
%%[offset,status] = mexnc('GET_ATT_SHORT', ncid, varid,'offset');
land=sign(var);
latitude=double(abs(var)+offset)*scale_factor;

% close input file
netcdf.close(ncid);
%%status = mexnc('CLOSE',ncid);

