Why is my loop ending after one month of data?

1 view (last 30 days)
I am trying to create a histogram of the windspeed over Seattle from 1981-1989. For some reason, it seems to only go through one month's worth of data, and the rest of the matrix remains zeros. Help!
clear all
close all
folderpath = '/d1/Data/00hour/'; % Directory
year = [1981 1982 1983 1984 1985 1986 1987 1988 1989]; % Years of data
months = [01 02 03 04 05 06 07 08 09 10 11 12]; % Months of the year
filepre = 'pgbhnl.truncated.'; %beginning of file name
for n = 1:length(year) %loop over years
folderyear = int2str(year(n)); %current year being looped over
if mod(year,4) == 0 % if the year is divisible by four (i.e. 1984 and 1988)
dpm = [31 29 31 30 31 30 31 31 30 31 30 31]; % Number of days in each month in a leap year
else % if the year is not divisible by four (not leap year)
dpm = [31 28 31 30 31 30 31 31 30 31 30 31]; % Number of days in each month
end
sttlwndsp = zeros(sum(dpm),1); %create matrix of zeros for windspeed
for j = 1:length(months) %loop over months
for ii = 1:dpm(j) %loop over days
if (j) < 10 %if month is less than ten...
netcdfpathpre = strcat(folderpath,folderyear,'/netcdf/',filepre,folderyear,'0',int2str(j));
else %if day of the month is 10 or greater...
netcdfpathpre = strcat(folderpath,folderyear,'/netcdf/',filepre,folderyear,int2str(j));
end
if (ii) < 10 %if day of the month is less than ten...
netcdfmiddle = strcat(netcdfpathpre,'0',int2str(ii));
else %if the month is 10 or greater...
netcdfmiddle = strcat(netcdfpathpre,int2str(ii));
end
netcdfpathpost = '00_Seattle.nc'; %end of the file name
netcdfpath = strcat(netcdfpathpre,int2str(dpm(j)),netcdfpathpost); %putting the entire path together
ncid = netcdf.open(netcdfpath); %opening the file
[numdims, numvars, numglobalatts, unlimdimID] = netcdf.inq(ncid); %components of the file
for i = 1:numvars;
i-1;
[varname, xtype, dimids, numatts] = netcdf.inqVar(ncid,i-1);
end
levels = [1000]; %atmospheric levels (by pressure) (1000 = surface)
varVGRDPre = 'VGRD_'; % meridional wind velocity file name
varUGRDPre = 'UGRD_';% zonal wind velocity file name
varPost = 'mb'; % end of file name
varVGRD = strcat(varVGRDPre,int2str(levels),varPost); % Horizontal wind file
varidV = netcdf.inqVarID(ncid,varVGRD); %variable id
VGRD = netcdf.getVar(ncid,varidV); %VGRD variable
varUGRD = strcat(varUGRDPre,int2str(levels),varPost); % Vertical wind file
varidU = netcdf.inqVarID(ncid,varUGRD); %variable id
UGRD = netcdf.getVar(ncid,varidU); %UGRD variable
wndsp = (UGRD.^2 + VGRD.^2).^(0.5); % windspeed calculation using U and V
sttlwndsp(ii) = wndsp(68,46); % newly calculated ws at specific coordinates
end %end loop over days
end %end loop over months
end %end loop over years
figure(1) %plot
hist(sttlwndsp,-4:0.1:4) %histogram of surface windspeed
xlabel('Windspeed','FontWeight','b','FontSize', 14)
ylabel('Frequency','FontWeight','b','FontSize', 14)
title ('Windspeed Histogram for January' ,'FontWeight','b','FontSize',16)
%save figure
% figname = strcat('images/', 'histogram_10years.png')
% print('-dpng', figname)
  1 Comment
dpb
dpb on 3 Jul 2014
That's quite an eyeful w/o knowing your data structure, and not being able to run your code to see what's going on.
I suggest working thru with the debugger and more than likely you'll uncover your logic error.
If that doesn't bring joy, at least post some information on the file data to help; better would be a small segment that folks could run and illustrates the problem.

Sign in to comment.

Answers (2)

Geoff Hayes
Geoff Hayes on 3 Jul 2014
Edited: Geoff Hayes on 3 Jul 2014
Eliza - on each iteration of the outer loop (for the year), the Seattle wind speed array is re-initialized as
sttlwndsp = zeros(sum(dpm),1); %create matrix of zeros for windspeed
It becomes a 365x1 or 366x1 vector of elements depending upon whether the year is a leap year or not.
Now note how this array is updated - in the innermost for loop as
sttlwndsp(ii) = wndsp(68,46); % newly calculated ws at specific coordinates
where ii ranges from 1 to the number of days in the month according to
for ii = 1:dpm(j)
So the code keeps replacing the first 28-31 elements of sttlwndsp with the subsequent months, and the elements for the remaining 11 months remain as zeros.
This can easily be fixed with a counter of some kind to keep track of the next index to insert the wind speed at. But this will only fix the problem for a single year as, on the next iteration of n, we will overwrite the previous years data before getting to the code to do the histogram.
To get around this, we can do the following - define a cell array where each element represents one year's worth of wind speed data before we enter the outer loop
yearlyWndSpData = cell(length(year),1);
for n = 1:length(year)
% etc.
Then, initialize the wind speed vector as before but add a line that will be used to manage the next index where we can insert data into this vector
sttlwndsp = zeros(sum(dpm),1)
sttlwndspNextIdx = 1;
And instead of doing
sttlwndsp(ii) = wndsp(68,46);
do the following assignment and increment of the next index
sttlwndsp(sttlwndspNextIdx) = wndsp(68,46);
sttlwndspNextIdx = sttlwndspNextIdx + 1;
And finally, once we have iterated over all months for that year, then we should save that wind speed data to the cell array
end %end loop over months
yearlyWndSpData{n} = sttlwndsp;
end %end loop over years
Once the iterations have been completed, all wind speed data from 1981-1989 will be stored in the yearlyWndSpData cell array.
  2 Comments
Eliza
Eliza on 3 Jul 2014
I made these adjustments and my output is definitely different, but it still isn't correct. Each month prints out now instead of just one (yes! Thank you!), but each day of each month has the same value (e.g. january says 11.7 for each day, february says 3.5 for each day, etc). I was expecting to have a different value for each individual day. Do you have any suggestions?
Geoff Hayes
Geoff Hayes on 3 Jul 2014
How is your data organized? Is there a file per month per year? Or a file per day per month per year?
I noticed that you have the following code
if (ii) < 10 %if day of the month is less than ten...
netcdfmiddle = strcat(netcdfpathpre,'0',int2str(ii));
else %if the month is 10 or greater...
netcdfmiddle = strcat(netcdfpathpre,int2str(ii));
end
where the file name, which already has the year and month, is updated with the day of the month (in this example, January 01, 1981)
/d1/Data/00hour/1981/netcdf/pgbhnl.truncated.19810101
But netcdfmiddle, while initialized, is not used beyond this point. So the file that you are loading for every day of the month (for January 1981) is fixed at
/d1/Data/00hour/1981/netcdf/pgbhnl.truncated.1981013100_Seattle.nc
which seems to be the file for January 31, 1981. So we read the same file for the last day of the month for every day of every month of every year. That explains why each day has the same value.
You may want to replace
netcdfpath = strcat(netcdfpathpre,int2str(dpm(j)),netcdfpathpost);
with
netcdfpath = strcat(netcdfmiddle,netcdfpathpost);
Also, take note of Star Strider's comment below with respect to the calculation of dpm.

Sign in to comment.


Star Strider
Star Strider on 3 Jul 2014
You don’t reference your year array other than in your folderyear variable. I don’t see it used anywhere other than to calculate dpm anyway, and there it’s treated as a vector, not a scalar.
In that regard, consider replacing your dpm ‘if’ block with:
dpm = eomday(year(n), 1:12);
You can also replace your year and month arrays with:
year = 1981:1989;
months = 1:12;
but what you have obviously works.
Also, give my regards to Prof. Cliff Mass!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!