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

63 visualizzazioni (ultimi 30 giorni)
Qian
Qian il 10 Mar 2024
Commentato: Qian il 17 Mar 2024
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.

Risposte (1)

Austin M. Weber
Austin M. Weber il 10 Mar 2024
Modificato: Austin M. Weber il 10 Mar 2024
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 Commenti
Austin M. Weber
Austin M. Weber il 17 Mar 2024
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 il 17 Mar 2024
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

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by