按天数循环月度或年度平均值(采用儒略日格式)

Loop for monthly or yearly averages by day number (in Julian day format)

我每天都有'.mat'文件,其命名法类似于'Data_grid_day_ppt_ice003.mat',最后一个'003'是儒略日格式的年份,它可以从'001'开始并以'结束365' 和 '366' 取决于是否是闰年。

我需要数据的月平均值。知道如何为日期文件制作循环吗?

有些日期也可能会丢失,所以只计算文件的数量是行不通的。

我正在用 Matlab 编写,我正在尝试类似

year = linspace(2007,2016,10); % Years of data
months = linspace(01,12,12);% Months of the year
for n = 1:length(year) 
    foldery = int2str(year(n));
    if ~exist(folderyear) 
        continue
    else
        if mod(year,4) == 0 % if the year is divisible by four 
            dpm = [31 60 91 121 152 182 213 244 274 305 335 366];      
        else 
           dpm = [31 59 90 120 151 181 212 243 273 304 334 365];     
        end
        for j = 1:length(months) %loop over months
            if ~exist(months) 
                continue
            else
                for ii = 1:dpm(j)   %loop over days
                    % ... Not sure what to do here ...
                end 
            end
        end
    end
end

但是,我想不出下一步...

首先,不要使用year作为变量名,因为它是一个内置的Matlab函数!然后小心你的变量名,比如混淆 folderyfolderyear


执行此操作的最简单方法是将一年中的某一天添加​​到该年的 1 月 1 日,然后使用 Matlab 的内置 month function. This will take care of all of the leap year issues as long as you know the year (which you seem to). Also see this documentation, Date and Time Arithmetic 获取月份,这确认将数字添加到日期时间值将添加完整的 24 小时工作日。

关键是这个从儒略日获取月份的方法:

thisyear = 2017;               % Define which year you're in
J1 = datetime(2016,1,1);       % Set January first datetime 
dayofyear = 360;               % Christmas day as an example!
m = month(J1 + dayofyear - 1)  % Add day of year to Jan 1st, get month
% J1 + dayofyear - 1 = 25-Dec-2016 00:00:00
% m = 12
% month(J1 + dayofyear - 1, 'name') = 'December'

有关更多详细信息,请参阅您的代码的此注释扩展...

y = 2007:2016; % Years of data [2007, 2008, ... 2016]
outputdata = cell(1,12);
for n = 1:length(y) 
    folderyear = int2str(y(n));
    if ~exist(folderyear, 'dir') 
        continue
    else
        % Get all .mat files within this year folder
        f = dir(fullfile(folderyear, '*.mat'));
        filenames = {f.name};
        % Set Jan 1st date
        J1 = datetime(y, 1, 1);
        % Loop over months
        for m = 1:12
            % Loop over files
            for myfile = 1:numel(filenames)
                % Get number from end of file name
                dayofyear = filenames{myfile};
                dayofyear = str2num(dayofyear(end-6:end-4));
                % Get month of day by adding to Jan 1st of that year
                % -1 used assuming Jan 1st is day 1. Not needed if Jan 1st is day 0.
                if month(J1 + dayofyear - 1) == m  
                    % hurrah, this file is in the month m, add it to an average
                    % or anything else you want, then you could assign to 
                    % an output cell or matrix
                    % outputdata{m} = ...
                end
            end
        end     
    end
end

如果我这样做现在就来了 Num = regexp(dayofyear,'\d','Match'); tt=cellstr(Num{:}); pp = strcat(tt(1),tt(2),tt(3)); dayofyear=str2double(pp); 虽然它有点幼稚,但是如果我在一行中这样做它会返回错误:P 谢谢 Wolfie :)