
%% Converts .mat files with current data (curr_startdate_enddate.txt) into netCDF format. 
% Data file description:
% 1. Each .mat file contains three variables named as:
%           - time: Date and time of measurement, format YYYYMMDD.HHMM;
%           - crm:  Currents magnitude [m/s];
%           - crd:  Currents direction [degrees];
%
% 2. Each row in crm and crd varibles represents hourly current
%    magnitude/direction measurements at 45 levels with increment 0.33m
%    starting from depth 15.11m, that is, the depth decrease from
%    crm(1,1)/crd(1,1) to crm(1,45)/crd(1,45);
%
%% ---------
clear all

%% Load the raw data .mat file from a source destination
load ('E:\MICORE\WP2_Data processing\DATA for REPOSITORY\ADCP\Currents\raw\curr_20100122_20100125.mat');

%% Store data in transect series
CurrDate = datenum(num2str(time,'%15.4f'), 'yyyymmdd.HHMM');
crm = crm'; crd = crd'; %--- transpose the matrix, so each column now is an hourly measurment from bottom to the surface  
stormcurrents = struct('CurrMag', crm, ...
                       'CurrDir', crd, ....
                       'Time', CurrDate);

%% Enter netCDF file name according to the name of the input raw data file or else...

%----- Create NetCDF file. 
ncid = netcdf.create('currents_20100122_20100125.nc','nc_write');  

%% Define global attributes
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'Title',             'Current parameters (magnitude & direction)');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'Institution',       'Institute of Oceanology - BAS');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'Source',            'Measurements at 45 depth horizons are taken by TeledyneRDI WH 600kHz ADCP deployed at 18m depth offshore of Shkorpilovtsi study site - Bulgaria. ');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'ADCP_Location',     '42.960716666N, 27.908016666E, WGS84, format DD.DDDDDDDDD');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'History',           'The parameters are calculated by TeledyneRDI WavesMon v.3.04');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'Project_Name',      'MICORE');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'Project_URL',       'www.micore.eu');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'E-mail',            'n.andreeva@io-bas.bg');
netcdf.putAtt(ncid, netcdf.getConstant('NC_GLOBAL'), 'Conventions',       'CF-1.4');
 
%% Define dimensions
timedim = netcdf.defDim(ncid,'Time', length(time));
leveldim = netcdf.defDim(ncid, 'Level', 45);

%% Create variables and their attributes
timevar = netcdf.defvar(ncid,'Time','double',timedim);
netcdf.putAtt(ncid,timevar,'long_name','time');
netcdf.putAtt(ncid,timevar,'units','hours');

crmvar = netcdf.defvar(ncid,'CurrMag','float',[timedim leveldim]);
netcdf.putAtt(ncid,crmvar,'long_name','current magnitude');
netcdf.putAtt(ncid,crmvar,'missing_value','-999');
netcdf.putAtt(ncid,crmvar,'units','metre per second');

crdvar = netcdf.defvar(ncid,'CurrDir','float',[timedim leveldim]);
netcdf.putAtt(ncid,crdvar,'long_name','current direction');
netcdf.putAtt(ncid,crdvar,'missing_value','-999');
netcdf.putAtt(ncid,crdvar,'units','degrees');

%% Exit define mode
netcdf.endDef(ncid);

%% Add data to the created variables
netcdf.putVar(ncid,crmvar,stormcurrents.CurrMag);
netcdf.putVar(ncid,crdvar,stormcurrents.CurrDir);
netcdf.putVar(ncid,timevar,stormcurrents.Time);

%% Close netcdf file
netcdf.close(ncid);
