1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| clear
clc
%% Initialize variables.
filename = 'D:\BARBEAU\BARBEAU-ECO\Centrales_Campbell\B_ProfilGaz_Profil_Gaz_Data.dat';
delimiter = ',';
startRow = 5;
%% Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s%s%*s%*s%*s%*s%*s%*s%*s%*s%s%s%s%s%s%s%s%s%[^\n\r]';
formatSpecOut = '%s,%s,%s\n';
%% Open the text file.
fileID = fopen(filename,'r');
%% Déclaration du fichier de sortie
fidout = fopen('D:BARBEAU\BARBEAU-ECO\Stockage_CO2\Affichage RTMCPro\StorageRTMCPro.dat', 'wt');
%% Création de l'entête Campbell Sci dans le fichier de sortie
fprintf(fidout, '"TOA5","Storage","CR3000","5448","CR1000.Std.32","CPU:Profil.CR1","43371","Storage"\n');
fprintf(fidout, '"TIMESTAMP","RECORD","Storage"\n');
fprintf(fidout, '"TS","RN","µmolCO2+1m-2s-1"\n');
fprintf(fidout, '"","","Avg"\n');
%% Read columns of data according to format string.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
%% Close the text file.
fclose(fileID);
%% Convert the contents of columns containing numeric strings to numbers.
% Replace non-numeric strings with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = dataArray{col};
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=[2,3,4,5,6,7,8,9,10]
% Converts strings in the input cell array to numbers. Replaced non-numeric
% strings with NaN.
rawData = dataArray{col};
for row=1:size(rawData, 1);
% Create a regular expression to detect and remove non-numeric prefixes and
% suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
try
result = regexp(rawData{row}, regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if any(numbers==',');
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(thousandsRegExp, ',', 'once'));
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric strings to numbers.
if ~invalidThousandsSeparator;
numbers = textscan(strrep(numbers, ',', ''), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end
catch me
end
end
end
%% Split data into numeric and cell columns.
rawNumericColumns = raw(:, [2,3,4,5,6,7,8,9,10]);
rawCellColumns = raw(:, 1);
%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),rawNumericColumns); % Find non-numeric cells
rawNumericColumns(R) = {NaN}; % Replace non-numeric cells
%% Allocate imported array to column variable names
TIMESTAMP = strcat(rawCellColumns(:, 1));
%TIMESTAMP = cell2mat(rawCellColumns(:, 1));
RECORD = cell2mat(rawNumericColumns(:, 1));
C1 = cell2mat(rawNumericColumns(:, 2));
C2 = cell2mat(rawNumericColumns(:, 3));
C3 = cell2mat(rawNumericColumns(:, 4));
C4 = cell2mat(rawNumericColumns(:, 5));
C5 = cell2mat(rawNumericColumns(:, 6));
C6 = cell2mat(rawNumericColumns(:, 7));
C7 = cell2mat(rawNumericColumns(:, 8));
C8 = cell2mat(rawNumericColumns(:, 9));
%% Définition des hauteurs du profil vertical
H = [0.1,0.23,0.54,1.26,2.93,6.83,15.9,37];
%% Boucle de calcul du stockage
Stock = zeros(1, length(RECORD));
for i=1:length(RECORD)
Stock(i) = (1000/22.4)* (H(1)*C1(i) + (H(2)-H(1))*(C2(i)+C1(i))/2 + (H(3)-H(2))*(C3(i)+C2(i))/2 + (H(4)-H(3))*(C4(i)+C3(i))/2 + (H(5)-H(4))*(C5(i)+C4(i))/2 + (H(6)-H(5))*(C6(i)+C5(i))/2 + (H(7)-H(6))*(C7(i)+C6(i))/2 + (H(8)-H(7))*(C8(i)+C7(i))/2);
end
Stock = Stock';
Storage = zeros(1, (length(RECORD)-1));
for j=1:(length(RECORD)-1)
Storage(j) = (Stock(j+1)-Stock(j))/1800;
end
Storage = Storage';
%% Suppression de la première ligne des colonnes TIMESTAMP et RECORD
TIMESTAMP = TIMESTAMP (2:end);
RECORD = RECORD(2:end);
%% Copie des variables dans le fichier de sortie à la suite du header
fprintf(fidout,formatSpecOut,TIMESTAMP{:},RECORD,Storage);
fclose(fidout);
%% Clear temporary variables
clearvars filename delimiter startRow formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me rawNumericColumns rawCellColumns R; |
Partager