% do_buoy_to_OTS.m
% set buoy to each buoy deployment
clear all;
diary on

%experiment='stratus';
%experiment='whots';

experiment='ntas';
% change this index value for each deployment such as 1,2, etc
index=2; 

OTSHOME = ['/Users/kelan/uop/oceansites/buoy'];

% set up for individual buoy deployment
strI=    { '1';     '2';     '3';     '4';     '5';     '6';     '7';   '8'};
strYear={'2000';   '2001'; '2002';   '2003';  '2004';  '2005';  '2006'; '2007'};
strDate={'200010';'200110';'200210';'200311';'200412';'200510';'200610';'200710'};

ntasI=    { '1';      '2';    '3';     '4';     '5'};
ntasYear={'2001';   '2002';  '2003';  '2004';  '2005'};
ntasDate={'200103';'200203';'200302';'200402';'200502'};

whotsI=    { '1';      '2';    '3';     '4' };
whotsYear={'2004';   '2005';  '2006';  '2007'};
whotsDate={'200408';'200507';'200606';'200706'};

if (strcmp(experiment,'stratus')==1)
   di=strI{index}; % such as str1, ... str8 etc
   dYear=strYear{index};
   dDate=strDate{index};
elseif (strcmp(experiment,'ntas')==1)
   di=ntasI{index}; % such as ntas1, ... ntas5 etc
   dYear=ntasYear{index};
   dDate=ntasDate{index};
elseif (strcmp(experiment,'whots')==1)
   di=whotsI{index}; % such as whots1, ... whots4 etc
   dYear=whotsYear{index};
   dDate=whotsDate{index};
end

dType='met'; % for 'met' for imet, 'flux' for flux output

% change this for individule buoy
if (strcmp(experiment,'stratus')==1)
   expDir = ['/Users/kelan/uop/' experiment di '/met/'];
elseif (strcmp(experiment,'ntas')==1)
   expDir = ['/Users/kelan/uop/' experiment '/nt' di '/met/'];
elseif (strcmp(experiment,'whots')==1)
   expDir = ['/Users/kelan/uop/' experiment '/wh' di '/met/'];
end
infile=[experiment di '_' dType]; % such as stratus8_met

OTS_file = ['OS_' experiment '_' dDate '_M.nc' ];
% such as OS_stratus_200710_M.nc
% such as OS_ntas_200103_M.nc

meta.global.platform_code = [upper(experiment) '_' dYear]; % such as STRATUS_2007

addpath(expDir);
addpath(OTSHOME);
addpath([matlabroot '/toolbox/netcdf']);
addpath([matlabroot '/toolbox/netcdf/ncutility']);
addpath([matlabroot '/toolbox/netcdf/nctype']);

%addpath('/Users/kelan/matlabtools/seawater']);
%addpath('/Users/kelan/matlabtools/main']);

%addpath('/Applications/netcdf-4.0');

disp(['do_buoy_to_OTS.m for ' infile '; deployed in year ' dYear '; deployed yyyymm ' dDate]);

% get the global metadata that is shared by all our sites
% this starts to populate meta.global
get_uop_meta; % meta.global for all uop data;
disp(['get_uop_meta']);

% get buoy metadata;
get_buoy_meta; % meta.global for buoy data_mode etc;
disp(['get_buoy_meta']);

% get the metadata for each deployment site - includes geospatial_mins & maxs
eval(['get_' experiment '_meta']); % such as get_stratus_meta.m for all stratus experiment
disp(['get_' experiment '_meta']); 
   
% get surface met sensor metadata, sensor SNs and heighs for particular deployment
% 
if (strcmp(experiment,'stratus')==1) 
   eval(['get_str' di '_met_meta']); % such as get_str8_met_meta 
   disp(['get_str' di '_met_meta']); 
elseif (strcmp(experiment,'ntas')==1)
   eval(['get_ntas' di '_met_meta']); % such as get_ntas1_met_meta 
   disp(['get_ntas' di '_met_meta']); 
elseif (strcmp(experiment,'whots')==1)
   eval(['get_whots' di '_met_meta']); % such as get_ntas1_met_meta 
   disp(['get_whots' di '_met_meta']); 
end

eval(['cd ' expDir]);
met=load([infile '.mat']); % get processed met file, such as stratus8_met.mat
disp(['load ' infile]);
       
% load the data from this system into the output struct
[dstruct, meta] = get_met_vars(met, meta); % assign dstruct with met.variables
disp('get_meta_vars');

% write out the ots file for met files

eval(['cd ' OTSHOME ]);

if (strcmp(dType,'met')==1) % for surface met sensors
   status = OTS_met_out(OTS_file,meta,dstruct);
   disp('OTS_met_out');
end

