How can I average data from June,July, and August into one matrix
Show older comments
I have monthly data files that are 768x1152 (latitudexlongitude). I am reading in 3 monthly files from 1980 through 2005. I want to average the 3 monthly files for each year and put the average into a matrix that is latitude by longitude by time. With time being the 26 years.
Here is my code currently:
lon = ncread('/glade/p/cesm/amwg/runs/cam5_1_amip_run2/cam5_1_amip_run2.cam2.h0.1980-01.nc','lon');
lat = ncread('/glade/p/cesm/amwg/runs/cam5_1_amip_run2/cam5_1_amip_run2.cam2.h0.1980-01.nc','lat');
% Read in lat lon data
klat = find((lat>=0)&(lat<=40));
klong = find((lon>=280));
klat = klat(1:3:end);
klong = klong(1:4:end);
% Find lat/lon for only the North Atlantic and skip points
pressure = NaN(length(klat),length(klong),26);
% Create an array of NaN to fill in loop
dir = '/glade/p/cesm/amwg/runs/cam5_1_amip_run2/cam5_1_amip_run2.cam2.h0';
for y =1980:2005
for m =6:8
year=num2str(y);
month = num2str(m);
infile=[dir '.' year '-0' month '.nc'];
infile1 = [dir '.' year '-0' month '.nc'];
infile2 = [dir '.' year '-0' month '.nc'];
June = ncread(infile,'PSL');
June = June'; % needs to be transposed so it is lat by long
July = ncread(infile1, 'PSL');
July = July';
August = ncread(infile2, 'PSL');
August = August';
JJA = [June July August];
mJJA = mean(JJA);
index=y-1979;
pressure(:,:,index)=mJJA(klat,klong);
end
end
% Create loop to read in 26 years of average data from June, July, and August
press= reshape(press,3648,26);
% Reshape data so it is 2 dimensional
So the problem I have is when I go to save the averages into the array pressure I get an error because the averages are (1 by 3456) whereas klat by klong is 3648 as shown in the reshape.
1 Comment
Ced
on 2 Nov 2015
Hi
Your average does not really make sense to me. What you want is to have a pressure array for each latitude and longitude point, averaged over the three data sets of each month, correct?
Because what you are doing is taking the files of each month, and then averaging over the longitude. (That's why you have a vector, and not a matrix as you probably want)
Then, for the dimension error: It seems as if the data you are reading in actually has 1152 longitude points per month, and not 1216 as you are expecting. At least that's what I can gather from your comment.
In short, a few comments:
1) for the dimension problem: check again for which latitude/longitude points your data is, and form klat, klong accordingly
2) Maybe I'm missing something, but you seem to be reading the same file three times (infile == infile1 == infile2) ??
3) If you want to average each month, I would do something like
nlat = length(klat);
nlong = length(klong);
pressure = zeros(klat,klong,26); % nicer than NaN imho
June = zeros(klat,klong,3);
July = zeros(klat,klong,3);
August = zeros(klat,klong,3);
for i = 1:26 % for each year
for j = 1:3 % for each data set
infile = name_for_data_set_j_of_June;
June(:,:,i) = ncread(infile);
% ... repeat for july and august
end
% now average over the three data sets and save
pressure(:,:,i) = mean([June July August],3);
end
So yeah... There are quite a few points which don't really make sense to me in how your read and save your data, but maybe that will help you move forward.
Answers (1)
Chad Greene
on 6 Nov 2015
0 votes
Categories
Find more on Time Series Events in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!