diff --git a/.gitignore b/.gitignore
index f4a479b9dcecc37bc559cf7179b9afc39fa375ce..a0130cfd4953dca0a06d9ece5cee3621001aac4d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,3 +7,4 @@ IDMX*/
 old_data/
 doc/*.pdf
 *.jnl
+ADCP_*
diff --git a/PROTOCOLE_TRAITEMENT_ADCP_MOUILLAGE.pdf b/PROTOCOLE_TRAITEMENT_ADCP_MOUILLAGE.pdf
index 92565b40d084cd03e6bc52aba97da5b2b3be61b2..8716de82d2ef3dedfe8cc4ac0c6c8e5abd2a59a3 100644
Binary files a/PROTOCOLE_TRAITEMENT_ADCP_MOUILLAGE.pdf and b/PROTOCOLE_TRAITEMENT_ADCP_MOUILLAGE.pdf differ
diff --git a/merge_data.m b/merge_data.m
index 4df55b5e61a4dc4054dbadb15e9e76f93b66af0f..f4e2a4cb7e1eca1009c273db95eb215fccda0024 100644
--- a/merge_data.m
+++ b/merge_data.m
@@ -5,14 +5,15 @@
 clear all; close all;
 
 %% Variables
-FileToMerge1     = '/home/proussel/Documents/OUTILS/ADCP/ADCP_mooring_data_processing/FR28/ADCP_0N0W_2016_2018_1d.nc'; %.nc file to merge with
-FileToMerge2     = '/home/proussel/Documents/OUTILS/ADCP/ADCP_mooring_data_processing/FR30/ADCP_0N0W_2018_2020_1d.nc'; %.nc file to merge
+FileToMerge1     = '/home/proussel/Documents/OUTILS/ADCP/ADCP_mooring_data_processing/IDMX2/ADCP_0N130E_2010_2010_1d.nc'; %.nc file to merge with
+FileToMerge2     = '/home/proussel/Documents/OUTILS/ADCP/ADCP_mooring_data_processing/IDMX/ADCP_0N130E_2013_2016_1d.nc'; %.nc file to merge
+FileToMerge3     = '/home/proussel/Documents/OUTILS/ADCP/ADCP_mooring_data_processing/IDMX2/ADCP_0N130E_2010_2012_1d.nc'; %.nc file to merge
 % FileToMerge1     = '/home/proussel/Documents/OUTILS/ADCP/ADCP_mooring_data_processing/IDMX/ADCP_0N130E_2013_2013_1d.nc'; %.nc file to merge with
 % FileToMerge2     = '/home/proussel/Documents/OUTILS/ADCP/ADCP_mooring_data_processing/IDMX/ADCP_0N130E_2013_2016_1d.nc'; %.nc file to merge
 step_subsampling = 1; % 1=daily
-plot_data        = 0;
-mooring.name     = '0N0E';
-freq             = '150';
+plot_data        = 1;
+mooring.name     = '0N130E';
+freq             = '75';
 % mooring.name     = '0N130E';
 % freq             = '75';
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -27,13 +28,19 @@ ncfile2.time  = ncread(FileToMerge2,'time');
 ncfile2.depth = ncread(FileToMerge2,'depth');
 ncfile2.u     = ncread(FileToMerge2,'u');
 ncfile2.v     = ncread(FileToMerge2,'v');
+
+%% Read third .nc file
+ncfile3.time  = ncread(FileToMerge3,'time');
+ncfile3.depth = ncread(FileToMerge3,'depth');
+ncfile3.u     = ncread(FileToMerge3,'u');
+ncfile3.v     = ncread(FileToMerge3,'v');
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %% Create depth matrix
 depth = max(vertcat(ncfile1.depth,ncfile2.depth)):ncfile1.depth(2)-ncfile1.depth(1):min(vertcat(ncfile1.depth,ncfile2.depth));
 depth = fliplr(depth);
 
 %% Create time matrix
-time  = vertcat(ncfile1.time,ncfile2.time);
+time  = vertcat(ncfile1.time,ncfile3.time,ncfile2.time);
 time  = time*ones(1,length(depth));
 
 %% Create u/v matrix
@@ -45,8 +52,12 @@ for ii = 1:length(ncfile2.time)
     u2(ii,:) = interp1(ncfile2.depth,ncfile2.u(ii,:),depth);
     v2(ii,:) = interp1(ncfile2.depth,ncfile2.v(ii,:),depth);
 end
-u     = vertcat(u1,u2);
-v     = vertcat(v1,v2);
+for ii = 1:length(ncfile3.time)
+    u3(ii,:) = interp1(ncfile3.depth,ncfile3.u(ii,:),depth);
+    v3(ii,:) = interp1(ncfile3.depth,ncfile3.v(ii,:),depth);
+end
+u     = vertcat(u1,u3,u2);
+v     = vertcat(v1,v3,v2);
 
 %% Plot data
 if plot_data
@@ -106,7 +117,7 @@ yr_end   = datestr(time(end)+datenum(1950,01,01), 'yyyy');
 ncid     = netcdf.create(['ADCP_',mooring.name,'_',num2str(yr_start),'_',num2str(yr_end),'.nc'],'NC_CLOBBER');
 
 %create dimension
-dimidz   = netcdf.defDim(ncid, 'DEPTH', length(depth));
+dimidz   = netcdf.defDim(ncid, 'DEPTH', size(depth,2));
 dimidt   = netcdf.defDim(ncid, 'TIME', netcdf.getConstant('NC_UNLIMITED'));
 %Define IDs for the dimension variables (pressure,time,latitude,...)
 time_ID  = netcdf.defVar(ncid,'TIME','double',dimidt);
@@ -128,7 +139,7 @@ netcdf.putAtt(ncid, v_ID, 'long_name', 'Meridional velocities');
 %We are done defining the NetCdf
 netcdf.endDef(ncid);
 %Then store the dimension variables in
-netcdf.putVar(ncid, depth_ID, depth);
+netcdf.putVar(ncid, depth_ID, depth(1,:));
 netcdf.putVar(ncid, time_ID, 0, length(time), time);
 %Then store my main variable
 netcdf.putVar(ncid, u_ID, u');
diff --git a/moored_adcp_proc/uvrot.m b/moored_adcp_proc/uvrot.m
index 701ec5ea2814e3e4f39260c5d90ab0c60629e175..9081914310fabb314c9b9de1d186730ce94558db 100644
--- a/moored_adcp_proc/uvrot.m
+++ b/moored_adcp_proc/uvrot.m
@@ -4,3 +4,29 @@ function [ru,rv]=uvrot(u,v,ang)
 ang=ang*pi/180;
 ru=u.*cos(ang)-v.*sin(ang);
 rv=u.*sin(ang)+v.*cos(ang);
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/template_get_adcp_data.m b/template_get_adcp_data.m
index f29400b035a05424013ab992e6535c3a92bb5ebf..3bc09df2d91921c623b462f31df4884c69d5cd9a 100644
--- a/template_get_adcp_data.m
+++ b/template_get_adcp_data.m
@@ -16,21 +16,21 @@ addpath('/home/proussel/Documents/OUTILS/TOOLS/nansuite'); % NaNSuitePath
 
 %% META information:
 % Location rawfile
-rawfile          = '/home/proussel/Bureau/piratafr30/MOUILLAGE_ADCP/FR28_000.000';        % binary file with .000 extension
-fpath_output     = './FR30/';                                                             % Output directory
+rawfile          = 'IDMX2/_RDI_000.000';        % binary file with .000 extension
+fpath_output     = './IDMX2/';                                                             % Output directory
 
 % Cruise/mooring info
-cruise.name      = 'PIRATA-FR30';                                                         % cruise name
-mooring.name     = '0N0W';                                                                % '0N10W'
-mooring.lat      = '00°00.287';                                                           % latitude [°']
-mooring.lon      = '000°04.07';                                                           % longitude [°']
-clock_drift      = 276;                                                                   % [seconds]
+cruise.name      = 'INDOMIX';                                                         % cruise name
+mooring.name     = '0N130E';                                                                % '0N10W'
+mooring.lat      = '00°04.066';                                                           % latitude [°']
+mooring.lon      = '129°12.41';                                                           % longitude [°']
+clock_drift      = 0;                                                                   % [seconds]
 
 % ADCP info
-adcp.sn          = 8237;                                                                  % ADCP serial number
-adcp.type        = '150 khz Quartermaster';                                               % Type : Quartermaster, longranger
+adcp.sn          = 13952;                                                                  % ADCP serial number
+adcp.type        = '75 khz Quartermaster';                                               % Type : Quartermaster, longranger
 adcp.direction   = 'up';                                                                  % upward-looking 'up', downward-looking 'dn'
-adcp.instr_depth = 300;                                                                   % nominal instrument depth
+adcp.instr_depth = 632;                                                                   % nominal instrument depth
 instr            = 1;                                                                     % this is just for name convention and sorting of all mooring instruments
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -121,9 +121,9 @@ EA0                 = round(mean(ea(nbin,:)));
 
 %% Calculate Magnetic deviation values
 [a,~]                   = gregorian(raw.juliandate(1));
-magnetic_deviation_ini  = magdev(mooring.lat,mooring.lon,0,a+(raw.juliandate(1)-julian(a,1,1,0,0,0))/365.25);
+magnetic_deviation_ini  = -magdev(mooring.lat,mooring.lon,0,a+(raw.juliandate(1)-julian(a,1,1,0,0,0))/365.25);
 [a,~]                   = gregorian(raw.juliandate(end));
-magnetic_deviation_end  = magdev(mooring.lat,mooring.lon,0,a+(raw.juliandate(end)-julian(a,1,1,0,0,0))/365.25);
+magnetic_deviation_end  = -magdev(mooring.lat,mooring.lon,0,a+(raw.juliandate(end)-julian(a,1,1,0,0,0))/365.25);
 rot                     = (magnetic_deviation_ini+magnetic_deviation_end)/2;
 mag_dev                 = linspace(magnetic_deviation_ini, magnetic_deviation_end, length(time0));
 mag_dev                 = mag_dev(first:last);
@@ -131,10 +131,24 @@ mag_dev                 = mag_dev(first:last);
 %% Correction of magnetic deviation
 disp('****')
 disp('Correct magnetic deviation')
+% for ii = 1 : length(mag_dev)
+%     [u(:,ii),v(:,ii)] = uvrot(u2(:,ii), v2(:,ii), -mag_dev(ii));
+% end
+mag_dev = -mag_dev * pi/180;
 for ii = 1 : length(mag_dev)
-    [u(:,ii),v(:,ii)] = uvrot(u2(:,ii), v2(:,ii), -mag_dev(ii));
+    M(1,1) = cos(mag_dev(ii));
+    M(1,2) = -sin(mag_dev(ii));
+    M(2,1) = sin(mag_dev(ii));
+    M(2,2) = cos(mag_dev(ii));
+    vvel(1,:) = u2(:,ii);
+    vvel(2,:) = v2(:,ii);
+    vvvel = M * vvel;
+    u(:,ii) = vvvel(1,:);
+    v(:,ii) = vvvel(2,:);
 end
 
+
+
 %% Correct percent good: Exclude data with percent good below prct_good
 figure;
 colormap jet;
@@ -279,6 +293,21 @@ else
         offset           = 0;
     end
     
+    binmat              = repmat((1:nbin)',1,length(dpt1));
+    
+    if strcmp(adcp.direction,'up')
+        z(1,:) = dpt1(1,:)-(tlen+blen+lag)*0.5-blnk;
+        for ii = 2:length(binmat(:,1))
+            z(ii,:) = z(1,:)-(binmat(ii,:)-1.5)*blen;
+        end
+    elseif strcmp(adcp.direction,'dn')
+        z(1,:) = dpt1(1,:)+(tlen+blen+lag)*0.5+blnk;
+        for ii = 2:length(binmat(:,1))
+            z(ii,:) = z(1,:)+(binmat(ii,:)-1.5)*blen;
+        end
+    else
+        error('Bin depth calculation: unknown direction!');
+    end
     
 end