Get the weekly average of daily .netcdf data
    4 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
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 commentaires
Réponses (1)
  Vatsal
      
 le 6 Oct 2023
        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. 
2 commentaires
  Andebo Waza
 le 17 Déc 2024
				Dear all,
I need your help. I have wind data in 2D (Height, Time). I want to plot a 60-minute average data, and I want to do that using pcolor plot. I managed to generate the plot with the original data (30 min), but failed to do it on a 60-min average. I am relatively new to netcdf data. Any help is greatly appreciated!
Here is my code for original data:
clear all
close all
clc
ncid = netcdf.open('wl_geomar_CSM_l2_v04_20240916.nc','NC_NOWRITE');
filename = ncinfo('wl_geomar_CSM_l2_v04_20240916.nc');
disp(filename);
ncdisp('wl_geomar_CSM_l2_v04_20240916.nc');
longitude = ncread('wl_geomar_CSM_l2_v04_20240916.nc','lon');
latitude = ncread('wl_geomar_CSM_l2_v04_20240916.nc','lat');
time = ncread('wl_geomar_CSM_l2_v04_20240916.nc','time');
epoch = datetime(1970, 01, 01)
T = epoch + seconds(time)
u = ncread('wl_geomar_CSM_l2_v04_20240916.nc','u');
v = ncread('wl_geomar_CSM_l2_v04_20240916.nc','v');
wspeed = ncread('wl_geomar_CSM_l2_v04_20240916.nc','wspeed');
height = ncread('wl_geomar_CSM_l2_v04_20240916.nc','height');
u_mean=sqrt(u.^2+v.^2+wspeed.^2);
mymap=pcolor(T,height,u_mean);
shading interp
h= colorbar
caxis([0 20])
drawnow
ylabel(h,'Mean wind speed (m/s)');
grid on
xlabel('Time (UTC)')
ylabel('Height (m)')
xtk=xticks;
I would be happy to provide you the data if needed.
  Walter Roberson
      
      
 le 18 Déc 2024
				Voir également
Catégories
				En savoir plus sur Standard File Formats dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



