% 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(:,10:11)); min = str2num(tsg.DATE(:,12:13)); sec = str2num(tsg.DATE(:,14:15)); %[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