|
| 1 | +function [num_all_images, sensors, paths, info_count_text] = CheckImagesPath(path_data) |
| 2 | +%CHECKIMAGEPATH Ensure the input path is right to find a Landsats 4-8, and |
| 3 | +%Sentinel 2 image(s). |
| 4 | +% path_data - the input path |
| 5 | + |
| 6 | + % searching deeps. 0 is default. |
| 7 | + [image_types_paths] = CheckImagePath(path_data,0); |
| 8 | + |
| 9 | + % all count info foe searched images at current folder. |
| 10 | + num_all_images = size(image_types_paths,1); |
| 11 | + num_L4_tm = 0; |
| 12 | + num_L5_tm = 0; |
| 13 | + num_L6_tm = 0; |
| 14 | + num_L7_tm_plus = 0; |
| 15 | + num_L8_oli_tirs = 0; |
| 16 | + num_S2A_msi = 0; |
| 17 | + num_S2B_msi = 0; |
| 18 | + |
| 19 | + % key info: the senor and path are needed as outputs, the first one is |
| 20 | + % to determine the default parameters for Fmask, and the second one is |
| 21 | + % to determine the path loading all data. |
| 22 | + sensors = []; |
| 23 | + paths = []; |
| 24 | + info_count_text = []; |
| 25 | + |
| 26 | + for i_all = 1:num_all_images |
| 27 | + % c: current. |
| 28 | + cimage_sensor = image_types_paths{i_all,1}; |
| 29 | + cimage_num = image_types_paths{i_all,2}; |
| 30 | + cimage_type = Convert2ImageType(cimage_sensor,cimage_num); |
| 31 | + clear cimage_num; |
| 32 | + cimage_path = image_types_paths{i_all,3}; |
| 33 | + switch cimage_type |
| 34 | + case {'Landsat 4 TM'} |
| 35 | + num_L4_tm=num_L4_tm+1; |
| 36 | + case {'Landsat 5 TM'} |
| 37 | + num_L5_tm=num_L5_tm+1; |
| 38 | + case {'Landsat 6 TM'} |
| 39 | + num_L6_tm=num_L6_tm+1; |
| 40 | + case {'Landsat 7 ETM+'} |
| 41 | + num_L7_tm_plus=num_L7_tm_plus+1; |
| 42 | + case {'Landsat 8 OLI/TIRS'} |
| 43 | + num_L8_oli_tirs=num_L8_oli_tirs+1; |
| 44 | + case {'Sentinel 2A MSI'} |
| 45 | + % further check Sentinel 2 image folder, see there is |
| 46 | + % .SAFE. |
| 47 | + if contains(cimage_path,'.SAFE') |
| 48 | + num_S2A_msi=num_S2A_msi+1; |
| 49 | + else |
| 50 | + cimage_sensor = [];% no available Sentinel 2 data. |
| 51 | + end |
| 52 | + case {'Sentinel 2B MSI'} |
| 53 | + % see there is .SAFE. |
| 54 | + if contains(cimage_path,'.SAFE') |
| 55 | + num_S2B_msi=num_S2B_msi+1; |
| 56 | + else |
| 57 | + cimage_sensor = [];% no available Sentinel 2 data. |
| 58 | + end |
| 59 | + end |
| 60 | + if ~isempty(cimage_sensor) |
| 61 | + sensors = [sensors;{cimage_sensor}]; |
| 62 | + paths = [paths;{cimage_path}]; |
| 63 | + end |
| 64 | + clear cimage_sensor; |
| 65 | + end |
| 66 | + % renew num_all_images |
| 67 | + num_all_images = length(sensors); |
| 68 | + % used to notice user. |
| 69 | + text_line = 0; |
| 70 | + % multide |
| 71 | + if isequal(num_all_images,num_L4_tm)||... |
| 72 | + isequal(num_all_images,num_L5_tm)||... |
| 73 | + isequal(num_all_images,num_L6_tm)||... |
| 74 | + isequal(num_all_images,num_L7_tm_plus)||... |
| 75 | + isequal(num_all_images,num_L8_oli_tirs)||... |
| 76 | + isequal(num_all_images,num_S2A_msi)||... |
| 77 | + isequal(num_all_images,num_S2B_msi) |
| 78 | + % only for 1 type image |
| 79 | + if num_L4_tm > 0 |
| 80 | + text_line = text_line+1; |
| 81 | + info_count_text{text_line} = sprintf('%s Landsat 4 TM images are found at ''%s''\n',... |
| 82 | + num2str(num_L4_tm),path_data); |
| 83 | + end |
| 84 | + if num_L5_tm > 0 |
| 85 | + text_line = text_line+1; |
| 86 | + info_count_text{text_line} = sprintf('%s Landsat 5 TM images are found at ''%s''\n',... |
| 87 | + num2str(num_L5_tm),path_data); |
| 88 | + end |
| 89 | + if num_L6_tm > 0 |
| 90 | + text_line = text_line+1; |
| 91 | + info_count_text{text_line} = sprintf('%s Landsat 6 TM images are found at ''%s''\n',... |
| 92 | + num2str(num_L6_tm),path_data); |
| 93 | + end |
| 94 | + if num_L7_tm_plus > 0 |
| 95 | + text_line = text_line+1; |
| 96 | + info_count_text{text_line} = sprintf('%s Landsat 7 ETM+ images are found at ''%s''\n',... |
| 97 | + num2str(num_L7_tm_plus),path_data); |
| 98 | + end |
| 99 | + if num_L8_oli_tirs > 0 |
| 100 | + text_line = text_line+1; |
| 101 | + info_count_text{text_line} = sprintf('%s Landsat 8 OLI/TIRS images are found at ''%s''\n',... |
| 102 | + num2str(num_L8_oli_tirs),path_data); |
| 103 | + end |
| 104 | + if num_S2A_msi > 0 |
| 105 | + text_line = text_line+1; |
| 106 | + info_count_text{text_line} = sprintf('%s Sentinel 2A MSI images are found at ''%s''\n',... |
| 107 | + num2str(num_S2A_msi),path_data); |
| 108 | + end |
| 109 | + if num_S2B_msi > 0 |
| 110 | + text_line = text_line+1; |
| 111 | + info_count_text{text_line} = sprintf('%s Sentinel 2B MSI images are found at ''%s''\n',... |
| 112 | + num2str(num_S2B_msi),path_data); |
| 113 | + end |
| 114 | + else |
| 115 | + text_line = text_line+1; |
| 116 | + info_count_text{text_line} = sprintf(... |
| 117 | + 'A total of %s images (as follows) are found at ''%s''\n',... |
| 118 | + num2str(num_all_images), path_data); |
| 119 | + if num_L4_tm > 0 |
| 120 | + text_line = text_line+1; |
| 121 | + info_count_text{text_line} = sprintf('%s Landsat 4 TM images\n',... |
| 122 | + num2str(num_L4_tm)); |
| 123 | + end |
| 124 | + if num_L5_tm > 0 |
| 125 | + text_line = text_line+1; |
| 126 | + info_count_text{text_line} = sprintf('%s Landsat 5 TM images\n',... |
| 127 | + num2str(num_L5_tm)); |
| 128 | + end |
| 129 | + if num_L6_tm > 0 |
| 130 | + text_line = text_line+1; |
| 131 | + info_count_text{text_line} = sprintf('%s Landsat 6 TM images\n',... |
| 132 | + num2str(num_L6_tm)); |
| 133 | + end |
| 134 | + if num_L7_tm_plus > 0 |
| 135 | + text_line = text_line+1; |
| 136 | + info_count_text{text_line} = sprintf('%s Landsat 7 ETM+ images\n',... |
| 137 | + num2str(num_L7_tm_plus)); |
| 138 | + end |
| 139 | + if num_L8_oli_tirs > 0 |
| 140 | + text_line = text_line+1; |
| 141 | + info_count_text{text_line} = sprintf('%s Landsat 8 OLI/TIRS images\n',... |
| 142 | + num2str(num_L8_oli_tirs)); |
| 143 | + end |
| 144 | + if num_S2A_msi > 0 |
| 145 | + text_line = text_line+1; |
| 146 | + info_count_text{text_line} = sprintf('%s Sentinel 2A MSI images\n',... |
| 147 | + num2str(num_S2A_msi)); |
| 148 | + end |
| 149 | + if num_S2B_msi > 0 |
| 150 | + text_line = text_line+1; |
| 151 | + info_count_text{text_line} = sprintf('%s Sentinel 2B MSI images\n',... |
| 152 | + num2str(num_S2B_msi)); |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + % if no data is found, give mention info. |
| 157 | + if isempty(sensors) |
| 158 | + text_line = text_line+1; |
| 159 | + info_count_text{text_line} = sprintf('%s available images are found at ''%s''\n',... |
| 160 | + '0',path_data); |
| 161 | + text_line = text_line+1; |
| 162 | + info_count_text{text_line} = sprintf('Please ensure the path is correct\n'); |
| 163 | + end |
| 164 | +% fprintf(info_count); |
| 165 | +end |
| 166 | + |
| 167 | +function [image_types_paths] = CheckImagePath(path_data,subfolder_level) |
| 168 | +%CHECKIMAGEPATH Ensure the input path is right to find a Landsats 4-8, and |
| 169 | +%Sentinel 2 image(s). |
| 170 | +% path_data - the input path |
| 171 | +% subfolder_level - the level of subfolders that can be used to limit |
| 172 | +% searching deeps. 0 is default. |
| 173 | + |
| 174 | + % If the searching deeps are more than 5, stop and return; |
| 175 | + if subfolder_level > 5 |
| 176 | + image_types_paths = []; |
| 177 | + return; |
| 178 | + end |
| 179 | + image_types_paths = []; |
| 180 | + % first search the image(s) at current folder.image_types_paths |
| 181 | + [sensor,num_Lst,~,~] = LoadSensorType(path_data); |
| 182 | + if isempty(sensor) |
| 183 | + % if no available image at current folder, |
| 184 | + % and search its subfolders. |
| 185 | + subfolders = dir(path_data); |
| 186 | + for i_sub=1:length(subfolders) |
| 187 | + % filter out the file names starting with '.', that is not |
| 188 | + % right folder (system crashes). |
| 189 | + if strcmp(subfolders(i_sub).name(1),'.') |
| 190 | + continue; |
| 191 | + end |
| 192 | + % go to search the images at each subfolder |
| 193 | + path_subfoler = fullfile(subfolders(i_sub).folder,... |
| 194 | + subfolders(i_sub).name); |
| 195 | + [image_types_paths_sub] = CheckImagePath(path_subfoler,subfolder_level+1); |
| 196 | + if ~isempty(image_types_paths_sub) |
| 197 | + image_types_paths =[image_types_paths;image_types_paths_sub]; |
| 198 | + end |
| 199 | + end |
| 200 | + |
| 201 | + else |
| 202 | + % successfully searched a supported image. |
| 203 | + % and, return the sensor and the image path |
| 204 | + image_types_paths = [image_types_paths;{sensor,num_Lst,path_data}]; |
| 205 | + end |
| 206 | +end |
| 207 | +function image_type = Convert2ImageType(sensor,num_Lst) |
| 208 | +% CONVERT@IMAGETYPE Contruct image type from the sensor and num |
| 209 | + |
| 210 | + switch sensor |
| 211 | + case 'L_TM' |
| 212 | + image_type = ['Landsat ',num_Lst,' TM']; |
| 213 | + case 'L_ETM_PLUS' |
| 214 | + image_type = ['Landsat ',num_Lst,' ETM+']; |
| 215 | + case 'L_OLI_TIRS' |
| 216 | + image_type = ['Landsat ',num_Lst,' OLI/TIRS']; |
| 217 | + case 'S_MSI' |
| 218 | + image_type = ['Sentinel ',num_Lst,' MSI']; |
| 219 | + otherwise |
| 220 | + image_type=[]; |
| 221 | +% error(['Errors occur when searching images at ''', path_data],'''.'); |
| 222 | + end |
| 223 | +end |
| 224 | + |
0 commit comments