function [longitude, latitude, zlev, ...
    bot_dep, pressure, temp, tpot, salt, ...
    gamman, sigma0, sigma2, sigma4, valid] = get_woce_params
%FUNCTION get_woce_params
%
% This function reads the NetCDF files defining the WOCE data 
%
%
% LON = 720; LAT = 341;  ZAX = 44;
%
% Output:
%   longitude           : Longitude of data points
%   latitude            : Latitude of data points
%   zlev                : Depth of data points
%   bot_dep             : Real bottom depth (including over land), repeated
%                           over all depth
%   pressure            : pressure in dbars
%   temp                : In-situ temperature 
%   tpot                : Potential temperature
%   gamman              : Neutral Density
%   sigma0              : Potential density referenced to surface
%   sigma2              : Potential density referenced to 2000 dbar
%   sigma4              : Potenital density referenced to 4000 dbar
%
% Author: Remi Tailleux, University of Reading, 7 July 2016

%% Define global NCDF options
%-----------------------------
global nctbx_options;
nctbx_options.theAutoNaN = 1;


%% Read the temperature data from Koulterman and Gouretzki
%----------------------------------------------
%root_name = '/Users/remitailleux/Documents/DATASETS/WOCE_DATA/';
%name1 = 'wghc_params.nc';
%inputfile2 = [root_name,name1]; 
inputfile2 = 'wghc_params.nc'; 

ncid = netcdf.open(inputfile2,'NOWRITE');

varid = netcdf.inqVarID(ncid,'LON');
longitude = netcdf.getVar(ncid,varid,'double');

varid = netcdf.inqVarID(ncid,'LAT');
latitude = netcdf.getVar(ncid,varid,'double');

varid = netcdf.inqVarID(ncid,'ZAX');
zlev = netcdf.getVar(ncid,varid,'double');

varid = netcdf.inqVarID(ncid,'BOT_DEP');
bot_dep = netcdf.getVar(ncid,varid,'double');

varid = netcdf.inqVarID(ncid,'PRES');
pressure = netcdf.getVar(ncid,varid,'double');

varid = netcdf.inqVarID(ncid,'TEMP');
temp = netcdf.getVar(ncid,varid,'double');

varid = netcdf.inqVarID(ncid,'TPOTEN');
tpot = netcdf.getVar(ncid,varid,'double');

varid = netcdf.inqVarID(ncid,'SALINITY');
salt = netcdf.getVar(ncid,varid,'double');

varid = netcdf.inqVarID(ncid,'GAMMAN');
gamman = netcdf.getVar(ncid,varid,'double');

varid = netcdf.inqVarID(ncid,'SIG0');
sigma0 = netcdf.getVar(ncid,varid,'double');

varid = netcdf.inqVarID(ncid,'SIG2');
sigma2 = netcdf.getVar(ncid,varid,'double');

varid = netcdf.inqVarID(ncid,'SIG4');
sigma4 = netcdf.getVar(ncid,varid,'double');

netcdf.close(ncid);


%% Identify the valid points and set invalid ones to NaN
valid_temp = temp > -5. & temp < 45.;
valid_salt = salt > -0.1 & salt < 50.;
valid = valid_temp & valid_salt;

%% Set all non-valid points to NaN
temp(~valid) = NaN;
salt(~valid) = NaN;
tpot(~valid) = NaN;
gamman(~valid) = NaN;
sigma0(~valid) = NaN;
sigma2(~valid) = NaN;
sigma4(~valid) = NaN;
pressure(~valid) = NaN;


end

