How can I convert my matlab code into python?
close all;
clear all;
clc;
Datafiles = fileDatastore("temp_summary.12*.txt","ReadFcn",@readMonth,"UniformRead",true);
dataAll = readall(Datafiles);
dataAll.Year = year(dataAll.Day);
dataAll.Month = month(dataAll.Day);
dataAll.DD = day(dataAll.Day);
LY = (dataAll.Month(:)==2 & dataAll.DD(:)==29);
dataAll(LY,:) = [];
% Unstack variables
minT_tbl = unstack(dataAll,"MinT","Year","GroupingVariables", ["Month","DD"],"VariableNamingRule","preserve")
maxT_tbl = unstack(dataAll,"MaxT","Year","GroupingVariables", ["Month","DD"],"VariableNamingRule","preserve")
yrs =str2double(minT_tbl.Properties.VariableNames(3:end))’;
% find min
[Tmin,idxMn] = min(minT_tbl{:,3:end},[],2,’omitnan’);
Tmin_yr = yrs(idxMn);
% find max
[Tmax,idxMx] = max(maxT_tbl{:,3:end},[],2,’omitnan’);
Tmax_yr = yrs(idxMx);
% find low high
[lowTMax,idxMx] = min(maxT_tbl{:,3:end},[],2,’omitnan’);
LowTMax_yr = yrs(idxMx);
% find high low
[highlowTMn,idxMn] = max(minT_tbl{:,3:end},[],2,’omitnan’);
HighLowT_yr = yrs(idxMn);
% find avg high
AvgTMx = round(mean(table2array(maxT_tbl(:,3:end)),2,’omitnan’));
% find avg low
AvgTMn = round(mean(table2array(minT_tbl(:,3:end)),2,’omitnan’));
% Results
tempTbl = [maxT_tbl(:,["Month","DD"]), table(Tmax,Tmax_yr,AvgTMx,lowTMax,LowTMax_yr,Tmin,Tmin_yr,AvgTMn,highlowTMn,HighLowT_yr)]
tempTbl2 = splitvars(tempTbl)
FID = fopen(‘Meda 12 Temperature Climatology.txt’,’w’);
report_date = datetime(‘now’,’format’,’yyyy-MM-dd HH:MM’);
fprintf(FID,’Meda 12 Temperature Climatology at %s n’, report_date);
fprintf(FID,"Month DD Temp Max (°F) Tmax_yr AvgTMax (°F) lowTMax (°F) LowTMax_yr TempMin (°F) TMin_yr AvgTMin (°F) HighlowTMin (°F) HighlowT_yr n");
fprintf(FID,’%3d %6d %7d %14d %11d %11d %15d %11d %13d %10d %13d %17d n’, tempTbl2{:,1:end}’);
fclose(FID);
winopen(‘Meda 12 Temperature Climatology.txt’)
function Tbl = readMonth(filename)
opts = detectImportOptions(filename);
opts.ConsecutiveDelimitersRule = ‘join’;
opts.MissingRule = ‘omitvar’;
opts = setvartype(opts,’double’);
opts.VariableNames = ["Day","MaxT","MinT","AvgT"];
Tbl = readtable(filename,opts);
Tbl = standardizeMissing(Tbl,{999,’N/A’},"DataVariables",{‘MaxT’,’MinT’,’AvgT’});
Tbl = standardizeMissing(Tbl,{-99,’N/A’},"DataVariables",{‘MaxT’,’MinT’,’AvgT’});
[~,basename] = fileparts(filename);
% use the base file name, not the full file name:
d = str2double(extract(basename,digitsPattern));
if ~leapyear(d(3)) && d(2) == 2 % February of a non-leap-year
Tbl(Tbl.Day == 29,:) = []; % remove the 29th day data, if any
end
Tbl.Day = datetime(d(3),d(2),Tbl.Day);
end
function tf = leapyear(y)
if mod(y,4) % year is not divisible by 4
tf = false; % it is a common year
elseif mod(y,100) % year is not divisible by 100
tf = true; % it is a leap year
elseif mod(y,400) % year is not divisible by 400
tf = false; % it is a common year
else
tf = true; % it is a leap year
end
endclose all;
clear all;
clc;
Datafiles = fileDatastore("temp_summary.12*.txt","ReadFcn",@readMonth,"UniformRead",true);
dataAll = readall(Datafiles);
dataAll.Year = year(dataAll.Day);
dataAll.Month = month(dataAll.Day);
dataAll.DD = day(dataAll.Day);
LY = (dataAll.Month(:)==2 & dataAll.DD(:)==29);
dataAll(LY,:) = [];
% Unstack variables
minT_tbl = unstack(dataAll,"MinT","Year","GroupingVariables", ["Month","DD"],"VariableNamingRule","preserve")
maxT_tbl = unstack(dataAll,"MaxT","Year","GroupingVariables", ["Month","DD"],"VariableNamingRule","preserve")
yrs =str2double(minT_tbl.Properties.VariableNames(3:end))’;
% find min
[Tmin,idxMn] = min(minT_tbl{:,3:end},[],2,’omitnan’);
Tmin_yr = yrs(idxMn);
% find max
[Tmax,idxMx] = max(maxT_tbl{:,3:end},[],2,’omitnan’);
Tmax_yr = yrs(idxMx);
% find low high
[lowTMax,idxMx] = min(maxT_tbl{:,3:end},[],2,’omitnan’);
LowTMax_yr = yrs(idxMx);
% find high low
[highlowTMn,idxMn] = max(minT_tbl{:,3:end},[],2,’omitnan’);
HighLowT_yr = yrs(idxMn);
% find avg high
AvgTMx = round(mean(table2array(maxT_tbl(:,3:end)),2,’omitnan’));
% find avg low
AvgTMn = round(mean(table2array(minT_tbl(:,3:end)),2,’omitnan’));
% Results
tempTbl = [maxT_tbl(:,["Month","DD"]), table(Tmax,Tmax_yr,AvgTMx,lowTMax,LowTMax_yr,Tmin,Tmin_yr,AvgTMn,highlowTMn,HighLowT_yr)]
tempTbl2 = splitvars(tempTbl)
FID = fopen(‘Meda 12 Temperature Climatology.txt’,’w’);
report_date = datetime(‘now’,’format’,’yyyy-MM-dd HH:MM’);
fprintf(FID,’Meda 12 Temperature Climatology at %s n’, report_date);
fprintf(FID,"Month DD Temp Max (°F) Tmax_yr AvgTMax (°F) lowTMax (°F) LowTMax_yr TempMin (°F) TMin_yr AvgTMin (°F) HighlowTMin (°F) HighlowT_yr n");
fprintf(FID,’%3d %6d %7d %14d %11d %11d %15d %11d %13d %10d %13d %17d n’, tempTbl2{:,1:end}’);
fclose(FID);
winopen(‘Meda 12 Temperature Climatology.txt’)
function Tbl = readMonth(filename)
opts = detectImportOptions(filename);
opts.ConsecutiveDelimitersRule = ‘join’;
opts.MissingRule = ‘omitvar’;
opts = setvartype(opts,’double’);
opts.VariableNames = ["Day","MaxT","MinT","AvgT"];
Tbl = readtable(filename,opts);
Tbl = standardizeMissing(Tbl,{999,’N/A’},"DataVariables",{‘MaxT’,’MinT’,’AvgT’});
Tbl = standardizeMissing(Tbl,{-99,’N/A’},"DataVariables",{‘MaxT’,’MinT’,’AvgT’});
[~,basename] = fileparts(filename);
% use the base file name, not the full file name:
d = str2double(extract(basename,digitsPattern));
if ~leapyear(d(3)) && d(2) == 2 % February of a non-leap-year
Tbl(Tbl.Day == 29,:) = []; % remove the 29th day data, if any
end
Tbl.Day = datetime(d(3),d(2),Tbl.Day);
end
function tf = leapyear(y)
if mod(y,4) % year is not divisible by 4
tf = false; % it is a common year
elseif mod(y,100) % year is not divisible by 100
tf = true; % it is a leap year
elseif mod(y,400) % year is not divisible by 400
tf = false; % it is a common year
else
tf = true; % it is a leap year
end
end close all;
clear all;
clc;
Datafiles = fileDatastore("temp_summary.12*.txt","ReadFcn",@readMonth,"UniformRead",true);
dataAll = readall(Datafiles);
dataAll.Year = year(dataAll.Day);
dataAll.Month = month(dataAll.Day);
dataAll.DD = day(dataAll.Day);
LY = (dataAll.Month(:)==2 & dataAll.DD(:)==29);
dataAll(LY,:) = [];
% Unstack variables
minT_tbl = unstack(dataAll,"MinT","Year","GroupingVariables", ["Month","DD"],"VariableNamingRule","preserve")
maxT_tbl = unstack(dataAll,"MaxT","Year","GroupingVariables", ["Month","DD"],"VariableNamingRule","preserve")
yrs =str2double(minT_tbl.Properties.VariableNames(3:end))’;
% find min
[Tmin,idxMn] = min(minT_tbl{:,3:end},[],2,’omitnan’);
Tmin_yr = yrs(idxMn);
% find max
[Tmax,idxMx] = max(maxT_tbl{:,3:end},[],2,’omitnan’);
Tmax_yr = yrs(idxMx);
% find low high
[lowTMax,idxMx] = min(maxT_tbl{:,3:end},[],2,’omitnan’);
LowTMax_yr = yrs(idxMx);
% find high low
[highlowTMn,idxMn] = max(minT_tbl{:,3:end},[],2,’omitnan’);
HighLowT_yr = yrs(idxMn);
% find avg high
AvgTMx = round(mean(table2array(maxT_tbl(:,3:end)),2,’omitnan’));
% find avg low
AvgTMn = round(mean(table2array(minT_tbl(:,3:end)),2,’omitnan’));
% Results
tempTbl = [maxT_tbl(:,["Month","DD"]), table(Tmax,Tmax_yr,AvgTMx,lowTMax,LowTMax_yr,Tmin,Tmin_yr,AvgTMn,highlowTMn,HighLowT_yr)]
tempTbl2 = splitvars(tempTbl)
FID = fopen(‘Meda 12 Temperature Climatology.txt’,’w’);
report_date = datetime(‘now’,’format’,’yyyy-MM-dd HH:MM’);
fprintf(FID,’Meda 12 Temperature Climatology at %s n’, report_date);
fprintf(FID,"Month DD Temp Max (°F) Tmax_yr AvgTMax (°F) lowTMax (°F) LowTMax_yr TempMin (°F) TMin_yr AvgTMin (°F) HighlowTMin (°F) HighlowT_yr n");
fprintf(FID,’%3d %6d %7d %14d %11d %11d %15d %11d %13d %10d %13d %17d n’, tempTbl2{:,1:end}’);
fclose(FID);
winopen(‘Meda 12 Temperature Climatology.txt’)
function Tbl = readMonth(filename)
opts = detectImportOptions(filename);
opts.ConsecutiveDelimitersRule = ‘join’;
opts.MissingRule = ‘omitvar’;
opts = setvartype(opts,’double’);
opts.VariableNames = ["Day","MaxT","MinT","AvgT"];
Tbl = readtable(filename,opts);
Tbl = standardizeMissing(Tbl,{999,’N/A’},"DataVariables",{‘MaxT’,’MinT’,’AvgT’});
Tbl = standardizeMissing(Tbl,{-99,’N/A’},"DataVariables",{‘MaxT’,’MinT’,’AvgT’});
[~,basename] = fileparts(filename);
% use the base file name, not the full file name:
d = str2double(extract(basename,digitsPattern));
if ~leapyear(d(3)) && d(2) == 2 % February of a non-leap-year
Tbl(Tbl.Day == 29,:) = []; % remove the 29th day data, if any
end
Tbl.Day = datetime(d(3),d(2),Tbl.Day);
end
function tf = leapyear(y)
if mod(y,4) % year is not divisible by 4
tf = false; % it is a common year
elseif mod(y,100) % year is not divisible by 100
tf = true; % it is a leap year
elseif mod(y,400) % year is not divisible by 400
tf = false; % it is a common year
else
tf = true; % it is a leap year
end
end python, matlab code MATLAB Answers — New Questions