How to use the landmask function in MATLAB?
Show older comments
I want to use the landmask function to exclude the values of the ocean and display the result. However, when I use the landmask function, it says it cannot find 'coast'. Please help.

clc; clear all;
% 파일 이름 설정
fnm = 'air.mon.ltm.1981-2010.nc';
fnm2 = 'precip.mon.ltm.1981-2010.nc';
% 데이터 읽기
lon_air = double(ncread(fnm, 'lon')); % 기온 데이터 경도
lat_air = double(ncread(fnm, 'lat')); % 기온 데이터 위도
level = ncread(fnm, 'level'); % 고도
air = ncread(fnm, 'air'); % 기온 데이터 (144x73x12)
lon_precip = double(ncread(fnm2, 'lon')); % 강수량 데이터 경도
lat_precip = double(ncread(fnm2, 'lat')); % 강수량 데이터 위도
precip = ncread(fnm2, 'precip'); % 강수량 데이터 (144x72x12)
% 1000 hPa 고도 데이터 선택
level_idx = find(level == 1000);
air = squeeze(air(:, :, level_idx, :)); % 1000 hPa 데이터 선택 (144x73x12)
% 격자 생성
[lon_precip_grid, lat_precip_grid] = meshgrid(lon_precip, lat_precip); % 강수량 격자 생성
% 보간 결과 저장을 위한 배열 초기화
air_interp = zeros(length(lon_precip), length(lat_precip), size(air, 3));
% 각 시간/월별로 보간 적용
for t = 1:size(air, 3)
% air 데이터를 precip의 위경도 격자에 맞춤
air_interp(:, :, t) = interp2(lon_air, lat_air, air(:, :, t)', lon_precip, lat_precip, 'linear')';
end
% air 데이터 보간 완료: air_interp는 precip와 동일한 크기 (144x72x12)
% 연평균 계산
MAT = mean(air_interp, 3); % 연평균 기온 (144x72)
MAP = mean(precip, 3) * 365; % 연평균 강수량 (mm/year) (144x72)
Pdry = min(precip, [], 3); % 가장 건조한 달 강수량 (144x72)
Tcold = min(air_interp, [], 3); % 가장 추운 달의 기온 (144x72)
Thot = max(air_interp, [], 3); % 가장 더운 달의 기온 (144x72)
% 여름 (4월~9월) 강수량 합산
summer_precip = sum(precip(:, :, 4:9), 3); % 여름 강수량 (144x72)
% 겨울 (10월~3월) 강수량 합산
winter_precip = sum(precip(:, :, [1:3, 10:12]), 3); % 겨울 강수량 (144x72)
% 여름/겨울 강수량 비율
summer_ratio = summer_precip ./ MAP; % 여름 강수량 비율
winter_ratio = winter_precip ./ MAP; % 겨울 강수량 비율
% Pthreshold 변수 지정
Pthreshold = zeros(size(MAP)); % 초기화
% 조건 1: 겨울 강수량 비율이 70% 이상인 경우
winter_condition = winter_ratio > 0.7; % 겨울 강수량 조건
Pthreshold(winter_condition) = 2 * MAT(winter_condition);
% 조건 2: 여름 강수량 비율이 70% 이상인 경우
summer_condition = summer_ratio > 0.7; % 여름 강수량 조건
Pthreshold(summer_condition) = 2 * MAT(summer_condition) + 28;
% 조건 3: 나머지 경우
other_condition = ~(winter_condition | summer_condition); % 여름/겨울 외 나머지
Pthreshold(other_condition) = 2 * MAT(other_condition) + 14;
%% Köppen-Geiger 1단계 분류
climate_class = zeros(size(MAP)); % 분류 배열 초기화
% A: Tropical
climate_class(MAP >= 10 & Tcold >= 18) = 1; % 열대 기후
% B: Arid
climate_class(MAP < 10 * Pthreshold) = 2; % 건조 기후
% C: Temperate
climate_class((MAP >= 10) & (Thot > 10) & (Tcold > 0) & (Tcold < 18)) = 3; % 온대 기후
% D: Cold
climate_class((MAP >= 10) & (Thot > 10) & (Tcold <= 0)) = 4; % 냉대 기후
% E: Polar
climate_class((MAP >= 10) & (Thot <= 10)) = 5; % 극지 기후
% 해양 부분 제거
climate_class(land_mask == 0) = NaN; % 해양 부분을 NaN으로 설정
% 지도 시각화
figure;
worldmap('World'); % 전 세계 지도
load coastlines; % 해안선 데이터 로드
% 격자 생성
[X, Y] = meshgrid(lon_precip, lat_precip); % 경도(lon), 위도(lat)를 격자로 변환
% pcolorm 함수 사용
pcolorm(Y, X, climate_class'); % 기후 분류 데이터 시각화
plotm(coastlat, coastlon, 'k', 'LineWidth', 1); % 해안선 그리기
% 지도 설정 추가
c = colorbar; % 컬러바 추가
colormap(parula(5)); % 분류에 맞는 색상 설정
clim([1 5]); % 1~5 범위로 컬러바 설정
c.Ticks = [1, 2, 3, 4, 5];
c.TickLabels = {'A','B','C','D', 'E'};
title('Köppen-Geiger Climate Classification (1st Level)'); % 제목 설정
Accepted Answer
More Answers (0)
Categories
Find more on Geodesy and Mapping in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
