Python Forum
Plotting climate data with NetCdf files for a specific region with coordinates - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Plotting climate data with NetCdf files for a specific region with coordinates (/thread-11179.html)



Plotting climate data with NetCdf files for a specific region with coordinates - fyec - Jun-26-2018

I can plot temperature distribution figures with global NetCdf files with these codes.

    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()
    
But I want to select only coordinates of the specific region. I have those coordinates in a csv file. Csv file has one column as 'lons' and one column as 'lats'. I want to read these columns and plot the data distribution according to these coordinates.
I tried
 
        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'][:])
        
But I got OSError: Initializing from file failed.


RE: Plotting climate data with NetCdf files for a specific region with coordinates - Maverick494 - Jun-27-2018

I suspect it is due to the space in the file name; try removing the space or adding an _ or changing to \\ in the path instead of /


RE: Plotting climate data with NetCdf files for a specific region with coordinates - fyec - Jun-27-2018

(Jun-27-2018, 02:03 AM)Maverick494 Wrote: I suspect it is due to the space in the file name; try removing the space or adding an _ or changing to \\ in the path instead of /
I removed that space but still doesn't work.


RE: Plotting climate data with NetCdf files for a specific region with coordinates - buran - Jun-27-2018

maybe it is related and the problem is ü in Masaüstü
https://github.com/pandas-dev/pandas/issues/15086