Skip to content
Snippets Groups Projects
Commit 710273f6 authored by jacques.grelet_ird.fr's avatar jacques.grelet_ird.fr
Browse files

move program into csv2btl directory

use csv2btl.ini configuration file
add csv test files and demo btl
parent 623e10af
No related branches found
No related tags found
No related merge requests found
%CruiseID C O L I 1 6 0 1
%WS_TYPE OSI
%HEADER YEAR MNTH DAYX hh mi ss LATX_EXT LONX_EXT SSPS_EXT SSPS_EXT_QC SSPS_EXT_TYPE SSPS_EXT_ANALDATE
2016 01 27 11 00 00 49.5833333 -3.8833333 35.367 0 WS 20160614000000
2016 01 28 11 00 00 45.7333333 -8.2666667 35.690 0 WS 20160614000000
2016 01 29 11 00 00 40.2166667 -10.1166667 36.109 0 WS 20160614000000
2016 01 30 11 00 00 36.0666667 -7.0000000 36.475 0 WS 20160614000000
2016 01 31 11 00 00 37.5500000 0.2000000 37.023 0 WS 20160614000000
2016 02 01 11 00 00 41.0333333 6.2666667 38.200 0 WS 20160614000000
2016 02 02 11 00 00 43.4666667 10.1333333 38.231 0 WS 20160614000000
2016 02 19 11 00 00 39.6500000 4.4666667 37.987 0 WS 20160614000000
2016 02 20 11 00 00 36.4166667 -2.1666667 38.090 0 WS 20160614000000
2016 02 21 11 00 00 36.7833333 -9.2000000 36.200 0 WS 20160614000000
2016 02 22 11 00 00 42.3166667 -9.7833333 35.811 0 WS 20160614000000
2016 02 23 11 00 00 47.4500000 -6.6000000 35.510 0 WS 20160614000000
2016 02 24 11 00 00 50.3333333 0.1500000 35.320 0 WS 20160614000000
2016 03 01 11 00 00 53.8000000 6.8333333 32.197 0 WS 20160614000000
2016 03 03 11 00 00 51.2000000 1.7166667 35.286 0 WS 20160614000000
;SAMPLING SEA WATER;;;;;;;;;;
;LOG SHEET;;;;;;;;;;
GOSUD;;;;;;;;;;;
;;;;;;;;;;;
;;;;;;;;;;;
Ship Name;COLIBRI;;Departure Date MM/DD/YYYY;01/26/2016;;;;;;;
Call sign;FNHO;;Arrival Date MM/DD/YYYY;03/04/2016;;;;;;;
Cruise Id;COLI1601;;Date of Analysis MM/DD/YYYY;06/14/2016;;;;;;;
Bottle Type;OSI;;Laboratory;SHOM;;;;;;;
;;;;;;;;;;;
To be filled in by the operator;;;;;Do not fill;;;;;;
Bottle N-;Gmt DATE MM/DD/YYYY;Gmt TIME HH:MM;Latitude DDMM' N/S;Longitude DDDMM' E/W;"Analysed
Salinity";;;;;;
1;01/27/2016;11:00;4935' N;00353' W;35.367
2;01/28/2016;11:00;4544' N;00816' W;35.690
3;01/29/2016;11:00;4013' N;01007' W;36.109
4;01/30/2016;11:00;3604' N;00700' W;36.475
5;01/31/2016;11:00;3733' N;00012' E;37.023
6;02/01/2016;11:00;4102' N;00616' E;38.200
7;02/02/2016;11:00;4328' N;01008' E;38.231
8;02/19/2016;11:00;3939' N;00428' E;37.987
9;02/20/2016;11:00;3625' N;00210' W;38.090
10;02/21/2016;11:00;3647' N;00912' W;36.200
11;02/22/2016;11:00;4219' N;00947' W;35.811
12;02/23/2016;11:00;4727' N;00636' W;35.510
13;02/24/2016;11:00;5020' N;00009' E;35.320
14;03/01/2016;11:00;5348' N;00650' E;32.197
15;03/03/2016;11:00;5112' N;00143' E;35.286
;SAMPLING SEA WATER;;;;;;;;;;
;LOG SHEET;;;;;;;;;;
GOSUD;;;;;;;;;;;
;;;;;;;;;;;
;;;;;;;;;;;
Ship Name;COLIBRI;;Departure Date MM/DD/YYYY;03/04/2016;;;;;;;
Call sign;FNHO;;Arrival Date MM/DD/YYYY;03/30/2016;;;;;;;
Cruise Id;COLI1602;;Date of Analysis MM/DD/YYYY;06/15/2016;;;;;;;
Bottle Type;OSI;;Laboratory;SHOM;;;;;;;
;;;;;;;;;;;
To be filled in by the operator;;;;;Do not fill;;;;;;
Bottle N-;Gmt DATE MM/DD/YYYY;Gmt TIME HH:MM;Latitude DDMM' N/S;Longitude DDDMM' E/W;"Analysed
Salinity";;;;;;
1;03/05/2016;11:00;4927' N;00418' W;34.622
2;03/06/2016;11:00;4558' N;01052' W;35.365
3;03/07/2016;12:00;4214' N;01728' W;35.833
4;03/08/2016;12:00;3829' N;02344' W;36.000
5;03/09/2016;13:00;3338' N;02854' W;36.586
6;03/10/2016;13:00;2855' N;03310' W;36.957
7;03/11/2016;13:00;2416' N;03713' W;37.340
8;03/12/2016;14:00;1907' N;04131' W;37.190
9;03/13/2016;14:00;1413' N;04530' W;36.342
10;03/14/2016;15:00;0920' N;04923' W;36.365
11;03/19/2016;14:00;0923' N;04920' W;36.340
12;03/20/2016;14:00;1347' N;04552' W;36.667
13;03/21/2016;14:00;1810' N;04223' W;37.087
14;03/22/2016;13:00;2238' N;03916' W;37.120
15;03/23/2016;13:00;2718' N;03522' W;37.180
16;03/24/2016;12:00;3141' N;03104' W;36.776
17;03/25/2016;12:00;3620' N;02619' W;36.352
18;03/26/2016;11:00;3956' N;02049' W;36.025
19;03/27/2016;11:00;4318' N;01452' W;35.689
20;03/28/2016;11:00;4654' N;00851' W;35.504
21;03/29/2016;10:00;4954' N;00211' W;35.373
repIn = C:\svn\tsg-qc\trunk\tsg_tools\csv2btl
repOut = C:\svn\tsg-qc\trunk\tsg_tools\csv2btl
function csv2btl
% Programme de lecture des fichiers bouteilles au format CSV
% et conversion au format des fichiers BTL du programme TSG-QC
%
% ------------------------------------------------------------
clear
clc
% Repertoire en entree
% --------------------
% repIn = 'D:\0-US191\UR-US Clientes\UR065 - LEGOS\UR065 - ORE SSS\Donnees\bouteilles\Fichiers CSV\2007';
% repOut = 'D:\0-US191\UR-US Clientes\UR065 - LEGOS\UR065 - ORE SSS\Donnees\bouteilles\fichier BTL_fmtTSG\2007\';
repIn = 'F:\salinite\bouteilles\csv\2010\';
repOut = 'F:\salinite\bouteilles\btl\2010\';
% Repertoire courant par default
% ------------------------------
repIn = '.';
repOut = repIn;
% Constantes
% ----------
DELIMITER = ';'; % Chaque colonne du fichier est separee par DELIMITER
NBELEMENT = 6; % Nombre d'élément de chaque ligne du fichier en entree
% define configuration file name as csv2btl.ini
% ---------------------------------------------
file_ini = strcat(mfilename,'.ini');
fid_ini = fopen(file_ini, 'r');
% if file exist, read it
% ----------------------
if (fid_ini ~= -1)
% display information on command window
% --------------------------------------
fprintf('\nreading %s ...\n', file_ini);
% test the end-of-file
% --------------------
while ~feof(fid_ini)
% read next line
% --------------
inputText = textscan(fid_ini, '%s', 1, 'delimiter', '\n');
for ii = {'repIn', 'repOut'}
% get key, use char because i is cell
% -----------------------------------
clef = char(ii);
% use string instead cell
% -----------------------
str = inputText{1}{1};
regex = strcat('^\s*', clef, '\s*=\s*["]*(.+?)["]*$');
match = regexp( str, regex, 'tokens');
% build tsg struct
% ----------------
if ~isempty(match)
cfg.(clef) = match{1}{1};
% for debbuging only
% ------------------
fprintf('%s -> %s\n', clef, cfg.(clef));
continue
end
end
end % end of while loop
end
% close .ini file
% ---------------
fclose(fid_ini);
% Ouverture du fichier CSV
% ------------------------
eval( ['cd ''' repIn ' '' ']);
cd(cfg.repIn);
[file, pathname, filterIndex] = uigetfile( {'*.csv'}, 'Pick a file');
pathnameOut = repOut;
if ~isequal(file, 0)
% Lecture des fichiers Labviews
% -----------------------------
fileIn = [pathname file];
fileIn = fullfile(pathname, file);
file = strrep(file, '.csv', '.btl');
fileOut = [pathnameOut file];
disp( file );
disp( fileIn );
disp( fileOut );
fileOut = fullfile(cfg.repOut, file);
fprintf('\ninput file: %s\n', fileIn);
fprintf('ouput file: %s\n', fileOut);
% Parametres en sortie vides
% --------------------------
nblig = 0;
DataIn = [];
% Le nombre d'elements de l'entete determine le nombre de chaine a lire.
% Je cree ici la chaine de caractere decrivant le format utilise par
% textscan
% ----------------------------------------------------------------------
theFormat = [];
for i = 1:NBELEMENT
theFormat = [theFormat ' %s'];
end
theFormat = repmat(' %s', 1, NBELEMENT);
% Ouverture du fichier en lecture '.CSV'
% --------------------------------------
fid = fopen( fileIn, 'r');
% Teste si le fichier existe
% --------------------------
if fid ~= -1
% Read the first 8 (3) lines
% ----------------------
for i = 1:8
line = fgetl( fid );
end
% Lecture de la date d'analyse des bouteilles
% -------------------------------------------
C = textscan(line, '%*s %s %*s %*s %d/%d/%d', 4,'delimiter', DELIMITER);
......@@ -71,23 +117,23 @@ if ~isequal(file, 0)
year = double( C{4} );
day = double( C{3} );
month = double( C{2} );
% conversion au format du logiciel TSGQC
% -------------------------------------
dateAnalysis = datestr( datenum(year,month,day,0,0,0), 'yyyymmddHHMMSS' );
% Lecture du type de bouteilles
% -----------------------------
line = fgetl( fid );
C = textscan(line,'%*s%s', 1,'delimiter', DELIMITER)';
bottleType = char(C{1});
% Skip 4 lines
% ------------
for i = 1:4
line = fgetl( fid );
end
% Lecture des données
%
% textscan permet de lire a peu pres tout, memes les lignes
......@@ -102,7 +148,7 @@ if ~isequal(file, 0)
'headerLines', 0 ,...
'treatAsEmpty', ' ',...
'emptyValue', NaN);
% Allocation de mémoire
% ---------------------
[m,n] = size(data{1});
......@@ -114,7 +160,7 @@ if ~isequal(file, 0)
hour = NaN*ones(m,1);
min = NaN*ones(m,1);
sss_buck = NaN*ones(m,1);
% Decode la date
% Attention a la position des mois et du jour. Il y a inversion
% dans les fichiers de Denis entre 2006 2007
......@@ -127,7 +173,7 @@ if ~isequal(file, 0)
day(i) = tab(2);
end
end
% Decode l'heure
% --------------
for i = 1 : m
......@@ -137,7 +183,7 @@ if ~isequal(file, 0)
min(i) = tab(2);
end
end
% Decode les latitudes
% --------------------
for i = 1 : m
......@@ -154,7 +200,7 @@ if ~isequal(file, 0)
latitude(i) = NaN;
end
end
% Decode les longitudes
% --------------------
for i = 1 : m
......@@ -171,7 +217,7 @@ if ~isequal(file, 0)
longitude(i) = NaN;
end
end
% Decode la salinite
% ------------------
for i = 1 : m
......@@ -180,28 +226,28 @@ if ~isequal(file, 0)
sss_buck(i) = tab(1);
end
end
% Fermeture du fichier en lecture
% -------------------------------
fclose( fid );
% Ecriture resultat
% ----------------
% Test si l'un des éléments de la date est à NaN
% ----------------------------------------------
ind = find(isnan(year) | isnan(month) | isnan(day) | isnan(hour) | isnan(min) );
if ~isempty( ind )
hmb = msgbox( [{'Date incomplète pour certains enregistrements.'} {' '} ...
{'Ces enregistrements sont éliminés.'} {' '} ...
{'Liste des enregistrements dans le fenêtre Matlab.'}],...
'Décodage Date', 'warn', 'modal');
disp( 'Warning Date' );
disp( 'Date incomplète pour les enregistrements suivants' );
disp(ind);
uiwait( hmb );
year(ind) = [];
......@@ -213,21 +259,21 @@ if ~isequal(file, 0)
longitude(ind) = [];
sss_buck(ind) = [];
end
% Test si l'un des éléments de la position est à NaN
% --------------------------------------------------
ind = find( isnan(latitude) | isnan(longitude) );
if ~isempty( ind )
msgbox( [{'Position incomplète pour certains enregistrements.'} {' '} ...
{'Liste des enregistrements dans le fenêtre Matlab.'}],...
'Décodage Position', 'warn', 'modal');
disp( 'Warning Position' );
disp( 'Position incomplète pour les enregistrements suivants' );
disp(ind);
end
% Classement par date croissante
% ------------------------------
date = datenum(year, month, day, hour, min, 0 );
......@@ -240,31 +286,31 @@ if ~isequal(file, 0)
latitude = latitude(iOrder);
longitude = longitude(iOrder);
sss_buck = sss_buck(iOrder);
% Nombre d'enregistrements
% ------------------------
[nbRecords, n] = size(sss_buck);
% Type de donnees : format NetCdf de tsgqc
% Blank before and after WS, more readable after concatenation
% ------------------------------------------------------------
label = ' WS ';
sss_type = label( ones(nbRecords,1), :);
% Cree le tableau donnant la date d'analyse.
% il faut une date par echantillon
% ------------------------------------------
dateAnalysis = dateAnalysis( ones(nbRecords,1), :);
% tableau des secondes - N'existe pas pour l'instant dans les feuilles
% Excel
% --------------------------------------------------------------------
sec = zeros(nbRecords,1);
% Code d equlaite des mesures - tout a 0
% --------------------------------------
code = zeros(size(sss_buck));
% Cree un tableau de caracteres avec toutes les informations
% ----------------------------------------------------------
bucket = [year month day hour min sec latitude longitude sss_buck code];
......@@ -272,30 +318,30 @@ if ~isequal(file, 0)
for i = 1:nbRecords
bucketStr(i, :) = sprintf( theFormat, bucket(i,:) );
end
% Concatene le type et la date d'analyse
% --------------------------------------
bucketStr = [ bucketStr sss_type ];
bucketStr = [ bucketStr dateAnalysis ];
% Ouverture du fichier en sortie
% ------------------------------
fid = fopen( fileOut, 'w');
if fid ~= -1
fprintf( fid, '%%CruiseID %s\r\n', cruiseID );
fprintf( fid, '%%WS_TYPE %s\r\n', bottleType );
fprintf( fid, '%%HEADER YEAR MNTH DAYX hh mi ss LATX_EXT LONX_EXT SSPS_EXT SSPS_EXT_QC SSPS_EXT_TYPE SSPS_EXT_ANALDATE \r\n');
fout = fopen( fileOut, 'w');
if fout ~= -1
fprintf( fout, '%%CruiseID %s\n', cruiseID );
fprintf( fout, '%%WS_TYPE %s\n', bottleType );
fprintf( fout, '%%HEADER YEAR MNTH DAYX hh mi ss LATX_EXT LONX_EXT SSPS_EXT SSPS_EXT_QC SSPS_EXT_TYPE SSPS_EXT_ANALDATE\n');
if ~isempty(bucketStr)
for i = 1:nbRecords
fprintf(fid, '%s\n', bucketStr(i,:)');
fprintf(fout, '%s\n', bucketStr(i,:)');
end
end
fclose( fout );
display( 'End of program ...' );
else
fprintf('Opening error : %s\n', fileOut);
end
fclose( fid );
end
display( 'Fin du programme' );
end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment