Changing the longitude of NetCDF from 0-360 to -180-180?

조회 수: 63 (최근 30일)
Qian
Qian 2024년 3월 10일
댓글: Qian 2024년 3월 17일
I want to use the Tmax data from https://psl.noaa.gov/data/gridded/data.cpc.globaltemp.html. Tmax is in the format of netcdf and longitude range is from 0 to 360. I want to make the longitude range from -180 to 180 and exported the changed data so that next time when I use it, I can have the proper longitude.
The latitude starts from the north so the image is flipped. How can I also change the latitude so that I can have the latitude in the data attribute starting from south.
Does anyone know how to solve the problem?
Thanks.

답변 (1개)

Austin M. Weber
Austin M. Weber 2024년 3월 10일
편집: Austin M. Weber 2024년 3월 10일
I am attaching a .mat file with some CPC Global Temperature data as an example.
EDIT: Corrections thanks to @Walter Roberson
load cpc.mat
% Convert latitude and longitude data into type 'double' (by default CPC
% lat/lon data are of type 'single' which is not valid for plotting on map axes)
lat = double(lat);
lon = double(lon);
% Convert lon from [0 360] to [-180 180]
mask = lon > 180;
lon(mask) = lon(mask) - 360;
% Flip lat from [90 -90] to [-90 90]
lat = flip(lat);
% Rotate tmax and flip so that the dimensions are 360x720 (instead of
% 720x360)
tmax = flipud(tmax');
% Plot
figure(1)
ax=axesm('braun'); % Use whatever map projection you prefer
pcolorm(lat, lon, tmax)
mlabel('on') % Longitude tick labels
plabel('on') % Latitude tick labels
colorbar
tightmap
framem
gridm
To save the data for later:
save('mydata.mat','lat','lon','tmax')
  댓글 수: 9
Austin M. Weber
Austin M. Weber 2024년 3월 17일
Yes, the "line" at 0° longitude is actually not a line but rather missing data. This is an artifact of the original CDC data, which has longitude values ranging from 0.25° to 359.75°. The Earth is a sphere, so that means the original dataset did not include the longitude values that wrap back from 359.75° to 0.25°. Therefore, 1° of longitude is missing from the original data.
When we rescaled the longitudes from -180° to 180°, this missing 1° of longitude became centered at the prime meridian. The dataset simply doesn't contain longitude coordinates for that location, but this gap is covered up if you add a grid to the map.
Qian
Qian 2024년 3월 17일
Thanks so much for your explaination. I think I fixed the problem by changeing the longitude lareger than 180 minus 359.75 and smaller than 180 minus 0.25.
load cpc.mat
% Convert latitude and longitude data into type 'double' (by default CPC
% lat/lon data are of type 'single' which is not valid for plotting on map axes)
lat = double(lat);
lon = double(lon);
% Convert lon from [0 360] to [-180 180] Revised based on the range [0.25,359.75]
mask = lon > 180;
lon(mask) = lon(mask) - 359.75;
lon(~mask) = lon(~mask)-0.25;
% Flip lat from [90 -90] to [-90 90]
lat = flip(lat);
% Rotate tmax and flip so that the dimensions are 360x720 (instead of
% 720x360)
tmax = flipud(tmax');
% Plot
figure(1)
ax=axesm('braun'); % Use whatever map projection you prefer
pcolorm(lat, lon, tmax)
mlabel('on') % Longitude tick labels
plabel('on') % Latitude tick labels
colorbar
tightmap
framem
% gridm

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 NetCDF에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by