使用NetCdf文件绘制特定区域的气候数据

时间:2018-06-26 21:12:31

标签: plot netcdf

我可以使用带有这些代码的全局NetCdf文件绘制温度分布图。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap


from netCDF4 import Dataset as dt
import numpy as np
import matplotlib.pyplot as plt 

filestr='E:/VIC/Forcing Data from princeton/from 48 to 2016/01.tmax/tmax_daily_2000-2000.nc'

ncfile=dt(filestr, 'r')

lats = ncfile.variables['lat'][:]
lons = ncfile.variables['lon'][:]
time = np.array(ncfile.variables['time'][:], dtype=np.float64)
data = ncfile.variables['tmax'][300,:,:]
data -=273

# Set font name
plt.rcParams["font.family"] = "cambria"

    # Add Title
plt.suptitle('sub title', fontsize=12, fontweight='bold') #<---------
plt.title('title' , fontsize=12) #<---------

    # Add basemap
map = Basemap(projection='merc',llcrnrlon=30,llcrnrlat=24,urcrnrlon=53,urcrnrlat=43,resolution='i', epsg = 4269) 
        # projection, lat/lon extents and resolution of polygons to draw
        # resolutions: c - crude, l - low, i - intermediate, h - high, f - full

    #map.drawmapscale()
map.arcgisimage(service='World_Physical_Map', xpixels = 5000, verbose= False)
map.drawcoastlines(linewidth=0.3, color='xkcd:darkblue')
    #map.drawstates(linewidth=0.8)
    #map.drawcountries(color ='r')
    #map.drawlsmask(land_color='Linen', ocean_color='#CCFFFF') # can use HTML names or codes for colors
    #map.drawcounties() # you can even add counties (and other shapefiles!)     
parallels = np.arange(24.125,42.125,25.) # make latitude lines ever 5 degrees from 30N-50N #<---------
meridians = np.arange(32.125,52.375,25.) # make longitude lines every 5 degrees from 95W to 70W #<---------
map.drawparallels(parallels,linewidth=0.3,labels=[1,0,0,0],fontsize=10, color='white')
map.drawmeridians(meridians,linewidth=0.3,labels=[0,0,0,1],fontsize=10, color='white')
map.readshapefile('C:/Users/fyunu/OneDrive/Masaüstü/ETB STUDY/Shape File Area of the ETB basin/Aqueduct_river_basins_TIGRIS & EUPHRATES', \
name='Aqueduct_river_basins_TIGRIS & EUPHRATES', drawbounds=True, linewidth=0.6) #<---------

lon,lat= np.meshgrid(lons,lats)  #(lons-360.,lats) # for this dataset, longitude is 0 through 360, so you need to subtract 180 to properly display on map
xi,yi = map(lon,lat) #<---------

levels = [i for i in range(-20, 70, 5)] 
    #levels = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.1, 1.5, 2.]
#levels = [-1., 0.6, 0.80, 0.85, 0.90, 0.95, 1, 1.05, 1.1, 1.15, 1.2, 1.4, 5]

    #cs = map.pcolor(xi,yi,var,cmap='jet',vmin=min_value, vmax=max_value) #'RdBu_r')
map.contour(xi, yi, data, levels, linewidths=0.1, colors='k', linestyles='solid')
cs = map.contourf(xi, yi, data, levels, cmap=plt.get_cmap('jet'))#cmap=plt.cm.jet) #colors=colors_range) #,vmin=min_value, vmax=max_value) #<---------

    # Add Colorbar
cbar = map.colorbar(cs, location='right', size='5%',pad="1%")
cbar.set_label('unit') #('Percent’) #<---------

plt.show()
#plt.savefig(path + 'mapPlot_' + plotInfo.variable + '_' + title + '_' + plotInfo.legend + '.png',transparent=True, dpi=300) #<---------
plt.close()

但是我只想选择特定区域的坐标。我在csv文件中有这些坐标。 CSV文件的一列为“ lons”,一列为“ lats”。我想阅读这些列,并根据这些坐标绘制数据分布。 我尝试过

    inpExcelFile = 'C:/Users/fyunu/OneDrive/Masaüstü/gridCellCoordinates6 seperately.csv'  #lat, lon
    df1 = pd.read_csv(inpExcelFile) 
    lats = float(df1.columns['lats'][:])  
    lons = float(df1.columns['lons'][:])

但是我收到OSError:从文件初始化失败。

0 个答案:

没有答案