Friday, March 11, 2016

Matlab: MODIS Products

Introduction

这里整理一些MODIS产品可以通用的代码。
月值数据从文件名称返回年月信息:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
% Created by LI Xu
% Version 1.0
% March 11, 2016

% Description:
% Recognize the sample time in YYYYMM format

% If you have any question about this code,
% please do not hesitate to contact me via E-mail: 
% jeremy456@163.com

% Blog:
% http://blog.sciencenet.cn/u/lixujeremy
% http://lixuworld.blogspot.com/

function yymm=ReYYMM(filename)

    yymm=strsplit(filename, '.');
    yymm=yymm{2};
    yymm(1)=[];
    yy=yymm(1:4);
    yy=str2num(yy);
    mm=yymm(end-2:end);
    mm=str2num(mm);
    
    for ii=1:12
        dd=eomday(yy, ii);
        mm=mm-dd;
        if mm<0
            break;
        end
        
    end
    yymm=yy*100+ii;

end
日序数据从文件名称返回年月日信息:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
% Created by LI Xu
% Version 1.0
% Feb. 05, 2025


% If you have any question about this code,
% please do not hesitate to contact me via E-mail: 
% jeremy456@163.com

% Blog:
% http://blog.sciencenet.cn/u/lixujeremy
% http://lixuworld.blogspot.com/

clear;
clc;

timebegin=tic;
cur_data=date;
cur_time=fix(clock);
str1=sprintf('%s %.2d:%.2d:%.2d', cur_data, cur_time(4), cur_time(5), cur_time(6)); 
fprintf('Time Begin: ');
fprintf(str1);
fprintf('\n');

% Source Directory
SouDir='input';
% Destination Directory
DesDir='output';

% All Files
files=dir(fullfile(SouDir, '*.tif'));

% Loop
for ii=1:numel(files)
    filepath=fullfile(SouDir, files(ii).name);
    strs=strsplit(files(ii).name, '.');

    ymlabel=strs{1};
    ymlabel(1)=[];

    ymlabel=GenYYMMDD(ymlabel);
    strs{1}=ymlabel;

    newname=strjoin(strs, '.');
    otpath=fullfile(DesDir, newname);

    % Move file
    movefile(filepath, otpath);

    disp(['[', num2str(ii), '/', num2str(numel(files)), ']~', otpath]);



end


fprintf('Time Begin: ');
fprintf(str1);
fprintf('\n');
cur_data=date;
cur_time=fix(clock);
str2=sprintf('%s %.2d:%.2d:%.2d', cur_data, cur_time(4), cur_time(5), cur_time(6)); 
fprintf('Time End: ');
disp(str2);
timespan=toc(timebegin);
fprintf('Time Span: %.4f s\n', timespan);



disp('****************************************************');

function  output=GenYYMMDD(input)
    output=[];
    yyyy=input(1:4);
    doy=input(5:7);
    [mm, dd] = ddd2mmdd(str2num(yyyy), str2num(doy));

    output=str2num(yyyy)*10000+mm*100+dd;

    output=num2str(output);

end


function [mm,dd] = ddd2mmdd(year, ddd) 
    v = datevec(datenum(year, ones(size(year)), ddd));
    mm = v(:,2); dd = v(:,3);
end

No comments:

Post a Comment