Reputation: 27
I'm a obvious newbie to netCDF and some libraries in python. What i've been struggling to is, I want to change longitude grid in netCDF file. It's like (-180, 180) but I have to change it to (0, 360).
In this case, I would have used 'shiftgrid' which could have been used with 'from mpl_toolkits.basemap import shiftgrid'.
But this mpl_toolkits.basemap is a troublesome to me. It's impossible to use this one.. So I'd like to solve this problem with cartopy or other things, you guys can see the libraries that I normally use at the bottom.
How can I change the grid thing?
from netCDF4 import Dataset
import matplotlib.pylot as plt
from mpl_toolkits.basemap import shiftgrid
from sys import exit
import numpy as np
import pickle
file1= '/home/HadISST_sst.nc'
DATA1 = Dataset(file1, 'r')
hd_lat = DATA1.variables['latitude'][:] #89.5 -89.5
hd_lon0 = DATA1.variables['longitude'][:] #-179.5 179.5
hd_sst0 = DATA1.variables['sst'][1320:,:,:] #time, y, x
DATA1.close()
# shift grid from (-180,180) to (0,360)
hd_sst, hd_lon = shiftgrid(0, hd_sst0, hd_lon0, start=True)
hd_lon2, hd_lat2 = np.meshgrid(hd_lon, hd_lat) # to 2d data
Upvotes: 1
Views: 1338
Reputation: 275
Basemap and netCDF are libraries which you should not use any more. Xarray and Cartopy are much better to handle climate data. (some examples: https://xarray.pydata.org/en/stable/examples/visualization_gallery.html)
You can change the grid like this:
import xarray as xr
your_data = xr.open_dataset('path/to/file.nc')
your_data['lon'] = your_data.lon + 180
Upvotes: 0