Skip to content
Permalink
Browse files
2b Final Memory Lab
  • Loading branch information
Youssef EL_Dieb committed Jun 24, 2021
1 parent 03322bd commit 1554f6a5b28df7a89dd620de21a3ddfd9c56cad3
@@ -0,0 +1,15 @@
%% Section 2: Load all the model data together

function [AllDataMem] = LoadAllData(FileName)
MemorySaving;
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
for idx = 1: 8
AllData(idx,:,:,:) = ncread(FileName, Contents.Variables(idx).Name);
fprintf('Loading %s\n', Contents.Variables(idx).Name); % display loading information
end

AllDataMem = whos('AllData').bytes/1000000;
fprintf('Memory used for all data: %.3f MB\n', AllDataMem)
end

@@ -0,0 +1,30 @@
%% Section 4: Cycle through the hours and load all the models for each hour and record memory use
% We use an index named 'StartHour' in our loop
function [AllHoursMem] = LoadAllHours(FileName)
MemorySaving;
HourMem = 0; % storage variable for the maximum memory in use by our data variable
StartLat = 1; % starting latitude
NumLat = 400; % number of latitude positions
StartLon = 1; % starying longitude
NumLon = 700; % number of lingitude positions
% StartHour = 1; % starting time for analyises
NumHour = 1; % Number of hours of data to load

% loop through the hours loading one at a time
for StartHour = 1:25
Models2Load = [1, 2, 4, 5, 6, 7, 8]; % list of models to load
idxModel = 0; % current model
for idx = 1:7
idxModel = idxModel + 1; % move to next model index
LoadModel = Models2Load(idx);% which model to load
HourlyData(idxModel,:,:,:) = ncread(FileName, Contents.Variables(LoadModel).Name,...
[StartLon, StartLat, StartHour], [NumLon, NumLat, NumHour]);
fprintf('Loading %s\n', Contents.Variables(LoadModel).Name); % display loading information
end

% Record the maximum memory used by the data variable so far
AllHoursMem = max( [ HourMem, whos('HourlyData').bytes/1000000 ] );
fprintf('Loaded Hour %i, memory used: %.3f MB\n', StartHour, AllHoursMem); % display loading information
end
end

@@ -0,0 +1,27 @@
%% Section 3: Loading all the data for a single hour from all the models
function [HourMem] = LoadHours(FileName)
% We combine the aboce code to cycle through the names and load each model.
% We load the data into successive 'layers' using 'idx', and let the other
% two dimensions take care of themselves by using ':'
MemorySaving;
StartLat = 1; % starting latitude
NumLat = 400; % number of latitude positions
StartLon = 1; % starying longitude
NumLon = 700; % number of lingitude positions
StartHour = 1; % starting time for analyises
NumHour = 1; % Number of hours of data to load

% loop through the models loading *ALL* the data into an array
Models2Load = [1, 2, 4, 5, 6, 7, 8]; % list of models to load
idxModel = 0; % current model
for idx = 1:7
idxModel = idxModel + 1; % move to next model index
LoadModel = Models2Load(idx); % which model to load
ModelData(idxModel,:,:,:) = ncread(FileName, Contents.Variables(LoadModel).Name,...
[StartLon, StartLat, StartHour], [NumLon, NumLat, NumHour]);
fprintf('Loading %s\n', Contents.Variables(LoadModel).Name); % display loading information
end

HourMem = whos('ModelData').bytes/1000000;
fprintf('Memory used for 1 hour of data: %.3f MB\n', HourMem)
end

0 comments on commit 1554f6a

Please sign in to comment.