[sig_fore,sig_aft,sig_mid,inc_fore,inc_mid,inc_aft,az_fore,az_mid,az_aft,longitude,latitude,land] = read_avewr_cdf(infile);
%
%**********************************************************************
% Filename:   read_avewr_cdf.m
% --------
% Program Usage:
%
%   This procedure/subroutine is called by user
%
% --------
% Program Description:
%  
%   Reads ASCAT UHR avewr (high res sigma) 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. "sig_fore"   - UHR fore beam sigma-0
%   2. "sig_mid"    - UHR mid beam sigma-0
%   3. "sig_aft"    - UHR aft beam sigma-0
%   4. "inc_fore"   - UHR fore beam incidence angle 
%   5. "inc_mid"    - UHR mid beam incidence angle 
%   6. "inc_aft"    - UHR aft beam incidence angle 
%   7. "az_fore"    - UHR fore beam azimuth angle
%   8. "az_mid"     - UHR mid beam azimuth angle
%   9. "az_aft"     - UHR aft beam azimuth angle
%   10. "longitude" - UHR wvc longitude
%   11. "latitude"  - UHR wvc latitude
%   12. "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'};
  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','sig_fore','sig_mid','sig_aft',...
      'inc_fore','inc_mid','inc_aft',...
      'az_fore','az_mid','az_aft'};
% 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);

