Get the weekly average of daily .netcdf data
조회 수: 4 (최근 30일)
이전 댓글 표시
I am using OSCAR 2.0 ocean current velocity data: https://podaac.jpl.nasa.gov/dataset/OSCAR_L4_OC_FINAL_V2.0#
Each daily file contains the following variables:
Lat (719 x 1 double), Long (1440 x 1 double), Time, Zonal Velocity "U" (1440 x 719 double), & Meridional Velocity "V" (1440 x 719 double).
I need to find the weekly average of the zonal and meridional velocity over 3 years: 2008-05-01 to 2001-04-31. Rather than having a bunch of daily files, I need weekly files. The lat, long variables will keep the same values. For the first 7 daily files, time = 1. The next 7 files, time = 2, etc. "Avg U" should be the average of the first seven "u".
Ex) File #1
Lat (719x1 double), Long (1440 x 1 double), Time = 1, AvgU (1440 x 719 double), Avg V (1440 x 719 double)
Here is what I've been working with, but clearly it is unfinished / wrong.
% have a bunch of daily files % extract 7 files at a time % take the average of those 7 files
days = 20080501 - 20110431
n = length(days)
for i = 1:n
name = int2str(days(i))
fn = ['Daily Files', cyr, '.nc']
Z = read_NetCDF(fn)
t=Z.time
x=Z.lat
y=Z.long
u=Z.u
v=Z.v
ii = find(x>=120 & x<=145)
jj = find(y>=15 & y<=30)
x=x(ii)
y=y(jj)
u = u(:, jj, ii)
v = v(:, jj, ii)
ALSO
ncfiles = dir('*.nc');
Numfiles = length(ncfiles);
all_U = cell(Numfiles, 2);
all_V = cell(Numfiles, 1);
%create array of all the u
for i = 1:Numfiles
all_U{i} = read_NetCDF(ncfiles(i).name, '-var', 'u');
end
댓글 수: 0
답변 (1개)
Vatsal
2023년 10월 6일
I understand that you have daily files for three years, each containing five variables: "Lat", "Long", "Time", "U", and "V". The goal is to convert these daily files into weekly files. The "Lat" and "Long" variables will remain the same, while the "Time" variable will represent the week number (1 for the first week, 2 for the second week, and so on). Additionally, the "U" and "V" values will be updated with the average value for each week.
I am also attaching the code below to convert the daily files into weekly files: -
startDate = datetime('2008-05-01');
endDate = datetime('2011-04-31');
avgU = zeros(1440, 719);
avgV = zeros(1440, 719);
numFiles = 7; % Number of files to average per week
numWeeks = weeks(endDate - startDate);
for i = 1:numWeeks
weekStartDate = startDate + (i-1)*7;
weekEndDate = weekStartDate + days(numFiles-1);
sumU = zeros(1440, 719);
sumV = zeros(1440, 719);
filename = ['path_to_your_data/daily_file_', datestr(weekStartDate + (j-1), 'yyyymmdd'), '.nc'];
data = load(filename);
lat = ncread(filename, 'Lat');
long = ncread(filename, 'Long');
for j = 1:numFiles
% Load the daily file for the given date
filename = ['path_to_your_data/daily_file_', datestr(weekStartDate + (j-1), 'yyyymmdd'), '.nc'];
data = load(filename);
sumU = sumU + ncread(filename, 'U');
sumV = sumV + ncread(filename, 'V');
end
% Calculate the average U and V velocities for the week
avgU = sumU / numFiles;
avgV = sumV / numFiles;
% Save the weekly average data to a NetCDF file
outputFilename = ['path_to_save_weekly_files/weekly_file_', num2str(i), '.nc'];
nccreate(outputFilename, 'Lat', 'Dimensions', {'Lat', 719});
nccreate(outputFilename, 'Lon', 'Dimensions', {'Long', 1440});
nccreate(outputFilename, 'AvgU', 'Dimensions', {'Long', 1440, 'Lat', 719});
nccreate(outputFilename, 'AvgV', 'Dimensions', {'Long', 1440, 'Lat', 719});
nccreate(outputFilename, 'Time', 'Dimensions', {'Time', 1});
ncwrite(outputFilename, 'Lat', lat);
ncwrite(outputFilename, 'Long', long);
ncwrite(outputFilename, 'AvgU', avgU);
ncwrite(outputFilename, 'AvgV', avgV);
ncwrite(outputFilename, 'Time', i);
End
You can also refer to the MATLAB documentation for "ncread" to obtain more information on its usage and syntax. The link is provided below: -
I hope this helps.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 NetCDF에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!