From 2437a9349a6706027c33516b5e9c00449ee956f2 Mon Sep 17 00:00:00 2001 From: Yves Gouriou <yves.gouriou@ird.fr> Date: Mon, 21 Feb 2011 13:10:55 +0000 Subject: [PATCH] Ajout dans TSG_UTIL d'un script permettant la conversion au format ASCII TSGQC de fichiers au format netCDF --- tsg_util/netcdfTOtsgqc.m | 211 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 tsg_util/netcdfTOtsgqc.m diff --git a/tsg_util/netcdfTOtsgqc.m b/tsg_util/netcdfTOtsgqc.m new file mode 100644 index 0000000..da8b96f --- /dev/null +++ b/tsg_util/netcdfTOtsgqc.m @@ -0,0 +1,211 @@ +% Conversion d'une liste de fichier NetCDF TSG en fichier ASCCI +% +% La liste des fichiers netCDF doit être placé dans un dichier ASCII. +% un nom de fichier par ligne +% +clear all; +clc; + +% Repertoire des fichiers NetCdf en lecture +% ----------------------------------------- +dirIn = 'D:\temp\'; + +% Repertoire des fichiers ASCII en écriture +% ----------------------------------------- +dirOut = dirIn; + +% Nom du fichier qui liste les fichiers netCdf +% -------------------------------------------- +fichierListe = 'list_nc.txt'; + +% Liste des paramètres à écrire - Create empty structure array +% ------------------------------------------------------------ +PARA = { 'CNDC'; 'CNDC_CAL'; ... + 'SSPS'; 'SSPS_QC'; 'SSPS_CAL';... + 'SSPS_ADJUSTED'; 'SSPS_ADJUSTED_QC'; 'SSPS_ADJUSTED_ERROR'; ... + 'SSJT'; 'SSJT_QC'; 'SSJT_CAL';... + 'SSJT_ADJUSTED'; 'SSJT_ADJUSTED_QC'; 'SSJT_ADJUSTED_ERROR'; ... + 'SSTP'; 'SSTP_QC'; 'SSTP_CAL';... + 'SSTP_ADJUSTED'; 'SSTP_ADJUSTED_QC'; 'SSTP_ADJUSTED_ERROR'}; +nPara = size( PARA, 1 ); + +% Lecture du nom des fichiers à convertir. Ceux-ci se trouvent dans +% le fichier liste_nc.txt +% ----------------------------------------------------------------- +list_nc = textread([dirIn fichierListe],'%s'); + +% Conversion du nom des fichiers en caracteres +% -------------------------------------------- +list_nc = char(list_nc); + +% Boucle sur les fichiers à convertir +% ---------------------------------- +nfiles = size(list_nc,1); +for i = 1 : nfiles + + % Create structure array + % ---------------------- + tsg = struct; + + fileName = deblank(list_nc(i,:)); + display( ['Traitement du fichier : ' fileName]); + + % Ouverture d'un fichier NetCdf en lecture + % ---------------------------------------- + nc = netcdf([dirIn fileName],'read'); + + if ~isempty(nc) + + % Creation et ouverture du fichier en écriture .tsgqc + % --------------------------------------------------- + ind = find(fileName == '.'); + fileNameOut = fileName(1 : ind-1); + fid = fopen([dirOut fileNameOut '.tsgqc'],'wt'); + display( [' Ecriture du fichier : ' fileNameOut]); + + % -------------------------------- + % Get global attributes: meta data + % -------------------------------- + global_att = att(nc); + for iatt = 1:length(global_att) + + % extract name and value from netcdf globals attributes + % ----------------------------------------------------- + attribute = name(global_att{iatt}); + value = global_att{iatt}(:); + + % assign globals attributes in base workspace + % ------------------------------------------- + assignin('base', attribute, value); + + % populate tsg structure with globals attributes + % ---------------------------------------------- + tsg.(attribute) = value; + + end % end of attributes loop + + % ---------------------------- + % Get variables describing TSG + % ---------------------------- + variables = var(nc); + for ivar = 1:length(variables) + + % extract name and value from netcdf variables + % -------------------------------------------- + variable = name(variables{ivar}); + nv = nc{ivar}; + + % use autonan mode, remplace fillValue with NaN + % --------------------------------------------- + nv = autonan(nv, true); + + % populate tsg structure with netcdf variables + % -------------------------------------------- + tsg.(variable) = nv(:); + + % assign netcdf variables in base workspace + % ----------------------------------------- + assignin('base', variable, nv(:)); + + end % end of variables loop + + % Initialise le tableau des variables à écrire + % -------------------------------------------- + nbRecords = length( tsg.DAYD ); + tsg_data = NaN * ones(nPara+8, nbRecords); + + % Selectionne les paramètres et crée le tableau en écriture + % -------------------------------------------------------- + if nbRecords ~= 0 + + % Met en forme les dates et positions + % ------------------------------------ + year = str2num(tsg.DATE(:,1:4)); + month = str2num(tsg.DATE(:,5:6)); + day = str2num(tsg.DATE(:,7:8)); + hour = str2num(tsg.DATE(:,9:10)); + min = str2num(tsg.DATE(:,11:12)); + sec = str2num(tsg.DATE(:,13:14)); + + %[year, month, day, hour, min, sec] = datevec( julianToDatenum(tsg.DAYD) ); + tsg_data = [year month day hour min fix(sec) tsg.LATX mod(tsg.LONX+180,360)-180 ]; + header = 'YEAR MNTH DAYX hh mi ss LATX LONX'; + format = '%04d %02d %02d %02d %02d %02d %12.7f %12.7f'; + + % Boucle sur les paramètres à sauvegarder + % --------------------------------------- + for iPara = 1 : nPara + + % Sélectionne le nom du paramètre + % ------------------------------- + para = PARA{iPara}; + + if isempty(tsg.(para)) + tsg.(para) = NaN * ones( size( tsg.DAYD ) ); + end + + if findstr( '_QC', para) + + % Test pour contourner un bug de TSGQC. Les QC devaient être castés + % en byte sinon la valeur 0 devenait égale à -127 + ind = 0; + ind = find( tsg.(para) == -127 ); + if ~isempty(ind) + tsg.(para)(ind) = 0; + end + tsg_data = [tsg_data double(tsg.(para))]; + format = [format ' %1d']; + else + tsg_data = [tsg_data tsg.(para)]; + format = [format ' %6.3f']; + end + header = [header ' ' para]; + + end % Fin de boucle sur les paramètres à sauvegarder + + % Write some information + % ---------------------- + if ~isempty( tsg.DATE_CREATION ) + fprintf( fid, '%%DATE_CREATION %s\n', tsg.DATE_CREATION ); + end + if ~isempty( tsg.CYCLE_MESURE ) + fprintf( fid, '%%CYCLE_MESURE %s\n', tsg.CYCLE_MESURE); + end + if ~isempty( tsg.PLATFORM_NAME ) + fprintf( fid, '%%PLATFORM_NAME %s\n', tsg.PLATFORM_NAME); + end + if ~isempty( tsg.SHIP_CALL_SIGN ) + fprintf( fid, '%%SHIP_CALL_SIGN %s\n', tsg.SHIP_CALL_SIGN); + end + if ~isempty( tsg.PI_NAME ) + fprintf( fid, '%%PI_NAME %s\n', tsg.PI_NAME); + end + if ~isempty( tsg.DATA_ACQUISITION ) + fprintf( fid, '%%DATA_ACQUISITION %s\n', tsg.DATA_ACQUISITION); + end + + % Write the file + % -------------- + fprintf( fid, '%%HEADER %s\n', header); + fprintf( fid, [format '\n'], tsg_data'); + + % Everything OK + % ------------- + error = 1; + end % Selectionne les paramètres et crée le tableau en écriture + + fclose( fid ); + + % Clear the Workspace + % ------------------- + clear tsg tsg_data; + + else + + msg_error = 'ok'; + if isempty(nc) + msg_error = ['Problem opening file : ' fileName]; + end + + end +end % Find Boucle sur les fichiers à convertir -- GitLab