% get_cm_from_dm.m
% J Grelet US191 - IRD Brest - Dec 2010
%
% get_cm_from_dm: get cycle mesure from delayed mode listing file
% input: 
%    local file list with unix cmd: ls */* > dm_files_list.txt
% output:
%    dm_files_list_cm.txt:  cycle mesure listing file
%    dm_files_list_dm.txt:  delayed mode listing file
%
% Use these 2 output files for updating Excel processing file:
%    Delayed_Mode_Processing_TSG_GOSUD.xlsx
%
% Use Filezilla and Synchronized Browsing mode to get an updated directory structure
% ftp.legos.obs-mip.fr/pub/soa/salinite/sss_delayed_mode/dm_data_2003-ongoing/
%
% If you have an identical directory structure on the local machine and the server, 
% you can enable synchronized browsing. This means that any directory navigation
% on one machine is duplicated on the other.
%
% To enable synchronized browsing, create an entry in the Site Manager, and on 
% the Advanced tab, ensure that the Default local directory and the Default 
% remote directory have the same structure. 
% Then check "use synchronized browsing," save your settings, and connect.
%
% Directory Comparison
% To quickly see differences between files on the local machine and the server, 
% choose View > Directory Comparison, and choose either "compare file size" or
% "compare modification time." (You also hide identical files by checking that option.
% Then choose "Enable."
%
% You will now see color-coded differences between copies of the same file on the 
% different machines. See their meanings here. 


% set your local directory
dir = 'dm_data_2003-ongoing';

% initialize current plateforme name 
current_pf = '';

% create local file list with unix cmd: ls */* > dm_files_list.txt
fid = fopen(strcat(dir,'/dm_files_list.txt'), 'rt'); 

% open files descriptors
fid_cm = fopen(strcat(dir,'/dm_files_list_cm.txt'), 'wt'); 
fid_dm = fopen(strcat(dir,'/dm_files_list_dm.txt'), 'wt'); 

while ~feof(fid)
  
  % get fileName
  fileName = fgetl(fid);
  
  % create netcdf instance
  nc = us191.netcdf(strcat(dir, filesep, fileName));
  
  % get globals attributes
  cm = nc.ATTRIBUTES.CYCLE_MESURE;
  pf = nc.ATTRIBUTES.PLATFORM_NAME;
  
  % if plateforme name change, write it in file
  if ~strcmp(current_pf, pf)
     fprintf(fid_cm, '\n%s :\n\n', upper(pf)); 
     fprintf(fid_dm, '\n%s :\n\n', upper(pf)); 
     current_pf = pf;
  end
    
  % write result in two files
  fprintf(fid_cm, '%s\n', cm); 
  
  match = regexp( fileName, '.*?/(.*)', 'tokens');
  
  % build tsg struct
  % ----------------
  if ~isempty(match)
    fprintf(fid_dm, '%s\n', match{1}{1});
  end
  
  % close netcdf file
  nc.close;
  
end

fclose(fid_cm);
fclose(fid_dm);
