Reputation: 31
I want to draw a contour map with basemap-python. I used contour and contourf functions I have data over the land but ı want to mask these datas. Contourf is self masked with basemap but contour is not ? How can ı mask contour plot like contourf ? Here the image link : https://pasteboard.co/Iegz6ql.png
Upvotes: 2
Views: 342
Reputation: 147
I have long way to plot the contour in basemap after masking what ever data LAND or OCEAN that you wanted with global_land_mask follows (pardon for forgetting the original thread) for this as follows. I am also looking the fastest way on how to do this. Maybe our friends here can help us.
import numpy as np
import glob
from global_land_mask import globe
##### searching current folder path
path0 = os.getcwd()
print(path0)
################################################
path1=path0+'/50-Merging/'
##os.makedirs(os.path.dirname(path1), exist_ok=True)
path3=path0+'/90-Masking-OCEAN/'
os.makedirs(os.path.dirname(path3), exist_ok=True)
folder1=glob.glob(path1+'Corr-*.dat')
#folder1=glob.glob(path1+'Corr-APR.dat')
for files1 in folder1:
print (files1)
dates=files1[-7:-4]
print(dates)
data4=np.loadtxt(files1)
lat=data4[:,0]
lon=data4[:,1]
rain_ave=data4[:,2]
print(max(rain_ave))
lat_min=min(lat)
lat_max=max(lat)
lon_min=min(lon)
lon_max=max(lon)
lats=[]
lons=[]
# for y,x in zip(lat,lon):
# land=globe.is_land(y,x)
# if land == True:
# lats.append(y)
# lons.append(x)
for y,x in zip(lat,lon):
ocean=globe.is_ocean(y,x)
if ocean == True:
lats.append(y)
lons.append(x)
in_lats1=lats
in_lons1=lons
########## methods finding 3rd value using closer points #################
ind=[]
for i in range(len(in_lats1)):
dist=(lat-in_lats1[i])**2+(lon-in_lons1[i])**2
ind.append(np.where(dist==np.min(dist))[0][0])
lat2=lat[ind]
lon2=lon[ind]
param_model2=rain_ave[ind]
data3=np.array([lat2,lon2,param_model2])
data3=np.transpose(data3)
fmt1='%s'
np.savetxt(path3+'90-Masking-OCEAN-'+dates+'.dat',data3,fmt=fmt1,delimiter='\t')
Finally you can plot contour using regular methods from data3 the one you saved above. Hope it helps.
Upvotes: 0