function [data] = readCASAnetcdf(varargin)
% READ X-BAND XSCV RADAR IN NETCDF FORMAT
%
% reader_XSCV_radar(filename)
%
% filename: e.g., san_jose.ca-20160306-115519.qc.netcdf
%
% Copyright(C)
% Robert Cifelli and Haonan Chen
% Physical Sciences Division (PSD) 
% NOAA Earth System Research Laboratory
% For any question, please contact Rob.Cifelli@noaa.gov or
% Haonan.Chen@noaa.gov
% Oct, 2016

switch nargin
    case 0
        [filename pathname] = uigetfile('*.netcdf', 'Select the netcdf-file');
        inputfile = [pathname filename];
    case 1
        inputfile  = varargin{1};
    otherwise
        error('Invalid arguments.');
end
%open netcdf file
try
    ncid  = netcdf.open(inputfile, 'NC_NOWRITE');
    
catch ME
    inputfile = inputfile(1:end)
    ncid = netcdf.open(inputfile, 'NC_NOWRITE');
end

%Get information about netcdf file
[numdims, numvars, numglobalatts, unlimdimID] = netcdf.inq(ncid);

%Get variable data
for kk = 0:numvars-1
    %get variable name
    varname = netcdf.inqVar(ncid, kk);
    switch(lower(varname))
        case 'reflectivity'
            data.Z=double(netcdf.getVar(ncid,kk));
        case 'correctedreflectivity'
            data.CZ=double(netcdf.getVar(ncid,kk));
        case 'velocity'
            data.VE=double(netcdf.getVar(ncid,kk));
        case 'spectralwidth'
            data.SW=double(netcdf.getVar(ncid,kk));
        case 'differentialreflectivity'
            data.ZDR=double(netcdf.getVar(ncid,kk));
        case 'correcteddifferentialreflectivity'
            data.CZDR=double(netcdf.getVar(ncid,kk));
        case 'differentialphase'
            data.DP=double(netcdf.getVar(ncid,kk));
        case 'filtereddifferentialphase'
            data.PDP=double(netcdf.getVar(ncid,kk));
        case 'crosspolcorrelation'
            data.RH=double(netcdf.getVar(ncid,kk));
        case 'normalizedcoherentpower'
            data.NC=double(netcdf.getVar(ncid,kk));
        case 'specificdifferentialphase'
            data.KDP=double(netcdf.getVar(ncid,kk));
        case 'rainrate'
            data.RR = double(netcdf.getVar(ncid, kk));
        case 'signaltonoiseratio'
            data.SNR = double(netcdf.getVar(ncid, kk));
        case 'azimuth'
            data.az=double(netcdf.getVar(ncid,kk));
        case 'elevation'
            data.el=double(netcdf.getVar(ncid,kk));
            data.info.el = data.el(1);
        case 'start_azimuth'
            data.startaz=double(netcdf.getVar(ncid,kk));
        case 'start_elevation'
            data.startel=double(netcdf.getVar(ncid,kk));
        case 'stop_azimuth'
            data.stopaz=double(netcdf.getVar(ncid,kk));
        case 'stop_elevation'
            data.stopel=double(netcdf.getVar(ncid,kk));
        case 'gatewidth'
            data.gate=netcdf.getVar(ncid,kk);
        case 'startrange'
            data.startrange=netcdf.getVar(ncid,kk);
        case 'time'
            data.time=netcdf.getVar(ncid,kk);
        case 'txfrequency'
            data.tx_freq=netcdf.getVar(ncid,kk);
            data.info.lambda = (3e8/data.tx_freq(1));
        case 'txlength'
            data.tx_len=netcdf.getVar(ncid,kk);
            data.info.res=data.tx_len(1)*3e8/2;
        case 'txpower'
            data.pow_tx=netcdf.getVar(ncid,kk);
            data.info.pow_tx=data.pow_tx(1);
    end
end

% get metadata
for kk = 0:numglobalatts-1
    attname = netcdf.inqAttName(ncid, netcdf.getConstant('NC_GLOBAL'),kk);
    switch(lower(attname))
        case 'netcdfrevision'
            data.netcdfrev = netcdf.getAtt(ncid, netcdf.getConstant('NC_GLOBAL'), attname);
        case 'radarname'
            data.info.name = netcdf.getAtt(ncid, netcdf.getConstant('NC_GLOBAL'), attname);
        case 'latitude'
            data.info.lat = netcdf.getAtt(ncid, netcdf.getConstant('NC_GLOBAL'), attname);
        case 'longitude'
            data.info.lon = netcdf.getAtt(ncid, netcdf.getConstant('NC_GLOBAL'), attname);
        case 'height'
            data.info.alt = netcdf.getAtt(ncid, netcdf.getConstant('NC_GLOBAL'), attname);
        case 'numgates'
            data.numgate = double(netcdf.getAtt(ncid, netcdf.getConstant('NC_GLOBAL'), attname));
        case 'antennagain'
            data.info.G = netcdf.getAtt(ncid, netcdf.getConstant('NC_GLOBAL'), attname);
        case 'antennabeamwidth'
            data.info.bw = netcdf.getAtt(ncid, netcdf.getConstant('NC_GLOBAL'), attname);
    end
end
netcdf.close(ncid);
data.R = ((mean(data.startrange))+(0:data.numgate(1)-1)*mean(data.gate))*1e-6;
%For display
data.info.prf(1) = 2.4;
data.info.prf(2) = 1.6;
