clear
%  http://cf-pcmdi.llnl.gov/  <-compliances

home= '/login/jebro/';
% NETCDF
path(path,[home,'/netcdf_toolbox/mexnc']);
path(path,[home,'/netcdf_toolbox/netcdf']);
path(path,[home,'/netcdf_toolbox/netcdf/nctype']);
path(path,[home,'/netcdf_toolbox/netcdf/ncutility']);
path(path,[home,'/netcdf_toolbox/netcdf/ncsource']);


%model time series data at Formby point
load 102002_htdP1.txt; %wave data 
load 102002_zuvP1.txt; %hydrodynamic data

figure(1)
subplot(2,1,1), plot(X102002_htdP1(:,1), X102002_htdP1(:,2:6), '-'), datetick('x','dd/mm'), legend('Hs', 'Tp', 'dir', 'Tm02', 'Tm01')
subplot(2,1,2), plot(X102002_zuvP1(:,1), X102002_zuvP1(:,2:4), '-'), datetick('x','dd/mm'), legend('Elevation', 'u-velocity', 'v-velocity')

day1W=datevec(X102002_htdP1(1,1));daylenW=length(X102002_htdP1);
day1H=datevec(X102002_zuvP1(1,1));daylenH=length(X102002_zuvP1);


%Waves
 clear dims vars atts
 
 if day1W(3)<10, D=['0', int2str(day1W(3))]; else D=[int2str(day1W(3))]; end
 if day1W(2)<10, M=['0', int2str(day1W(2))]; else M=[int2str(day1W(2))]; end 
  in=['hours since ',int2str(day1W(1)),'-',M,'-',D, 0:0:0'];
 %in=['day number since', D, '/',M,'/',int2str(day1W(1)),' 01:00']; 
 
dims(1).name          = 'time' ;
dims(1).standard_name = 'time ' ;
dims(1).long_name     = in ;
dims(1).units         = in ;
dims(1).type          = 'double' ;
dims(1).length        =  daylenW;

vars(1).name = 'time' ;
vars(1).dims{1} = 'time';
vars(1).type = 'double' ;
vars(1).fillval = 999999 ;
vars(1).long_name = in;
vars(1).standard_name = 'time ' ;
vars(1).units = in ;

vars(2).name  = 'hs';
vars(2).standard_name  = 'sea_surface_wave_significant_height'; 
vars(2).long_name  = 'significant surface wave height (Hm0)';
vars(2).type  = 'float';
vars(2).fillval = 2.0e20 ;
vars(2).dims{1} = 'time';
vars(2).units = 'm';
%
vars(3).name  = 'tp';
vars(3).standard_name  = 'sea_surface_wave_period_at_variance_spectral_density_maximum'; 
vars(3).long_name  = 'peak wave period';
vars(3).type  = 'float';
vars(3).fillval = 2.0e20 ;
vars(3).dims{1} = 'time';
vars(3).units = 's';
%
vars(4).name  = 'dir';
vars(4).standard_name  = 'sea_surface_wave_to_direction'; 
vars(4).long_name  = 'nautical wave direction';
vars(4).type  = 'float';
vars(4).fillval = 2.0e20 ;
vars(4).dims{1} = 'time';
vars(4).units = 'degrees';
%
vars(5).name  = 'tm02';
vars(5).standard_name  = 'sea_surface_wave_mean_period_from_variance_spectral_density_second_frequency_moment'; 
vars(5).long_name  = 'second moment wave period (Tm02)';
vars(5).type  = 'float';
vars(5).fillval = 2.0e20 ;
vars(5).dims{1} = 'time';
vars(5).units = 's';
%

%
atts.source    = 'Hourly wave data for the 11-year POLCOMS-WAM run on the Irish Sea Model (IRS), 2.0e20 is a dry land point. The modelled wave outputs are available/stored for the full Irish Sea model domain at www.BODC.ac.uk in CF-netCDF format.';
atts.author  = 'Jenny Brown';
atts.inst = 'Proudman Oceanographic Laboratory';
atts.title = '11-year POLCOMS-WAM time series of wave parameters at Formby Point';
atts.conv = 'CF-1.4';
% 
 outfile=['POLCOMS_WAM_wave.nc'];
%                               
 nc_produce_auto(outfile,dims,vars,atts);
 eval(['nc = netcdf(''' outfile ''',''write'');'])
% 
% %%%%end of headers for netcdf file 
% 
% %load in data
nc{'time'}(:)=(1:daylenW);%60min output kept time in hours;
nc{'hs'}(:)=X102002_htdP1(:,2);
nc{'tp'}(:)=X102002_htdP1(:,3);
nc{'dir'}(:)=X102002_htdP1(:,4);
nc{'tm02'}(:)=X102002_htdP1(:,5);
% 
 close(nc)
% 

%Hydrodynamics
 clear dims vars atts
 
 if day1H(3)<10, D=['0', int2str(day1H(3))]; else D=[int2str(day1H(3))]; end
 if day1H(2)<10, M=['0', int2str(day1H(2))]; else M=[int2str(day1H(2))]; end 
 in=['hours since ',int2str(day1H(1)),'-',M,'-',D, 0:0:0'];
 %in=['day number since', D, '/',M,'/',int2str(day1H(1)),' 00:30']; 
 
dims(1).name          = 'time' ;
dims(1).standard_name = 'time ' ;
dims(1).long_name     = in ;
dims(1).units         = in ;
dims(1).type          = 'double' ;
dims(1).length        =  daylenH;

vars(1).name = 'time' ;
vars(1).dims{1} = 'time';
vars(1).type = 'double' ;
vars(1).fillval = 999999 ;
vars(1).long_name = in;
vars(1).standard_name = 'time ' ;
vars(1).units = in ;

vars(2).name  = 'z';
vars(2).standard_name  = 'sea_surface_height_above_sea_level'; 
vars(2).long_name  = 'surface elevation relative to mean tidal level';
vars(2).type  = 'float';
vars(2).fillval = 2.0e20 ;
vars(2).dims{1} = 'time';
vars(2).units = 'm';
%
vars(3).name  = 'u';
vars(3).standard_name  = 'eastward_sea_water_velocity'; 
vars(3).long_name  = 'depth averaged east-velocity component';
vars(3).type  = 'float';
vars(3).fillval = 2.0e20 ;
vars(3).dims{1} = 'time';
vars(3).units = 'm s^-1';
%
vars(4).name  = 'v';
vars(4).standard_name  = 'northward_sea_water_velocity'; 
vars(4).long_name  = 'depth averaged north-velocity component';
vars(4).type  = 'float';
vars(4).fillval = 2.0e20 ;
vars(4).dims{1} = 'time';
vars(4).units = 'm s^-1';
%
atts.source    = 'Half hourly hydrodynamic data for the 11-year POLCOMS-WAM run on the Irish Sea Model (IRS), 2.0e20 is a dry land point. The modelled hydrodynamic outputs are available/stored for the full Irish Sea model domain at www.BODC.ac.uk in CF-netCDF format.';
atts.author  = 'Jenny Brown';
atts.inst = 'Proudman Oceanographic Laboratory';
atts.title = '11-year POLCOMS-WAM time series of hydrodynamic parameters at Formby Point';
atts.conv = 'CF-1.4';
% 
 outfile=['POLCOMS_WAM_hydro.nc'];
%                               
 nc_produce_auto(outfile,dims,vars,atts);
 eval(['nc = netcdf(''' outfile ''',''write'');'])
% 
% %%%%end of headers for netcdf file 
% 
% %load in data
nc{'time'}(:)=(0.5:0.5:daylenH/2);%30min output kept time in hours;
nc{'z'}(:)=X102002_zuvP1(:,2);
nc{'u'}(:)=X102002_zuvP1(:,2);
nc{'v'}(:)=X102002_zuvP1(:,2);

% 
 close(nc)
%  

