In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to achieve the global temperature map by Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve the global temperature map by Python".
Global average air temperature chart for 2012:
Note: this article only takes basemap as an example. Cartopy asks readers to modify it by referring to previous tweets. Import datetime as dt # Python standard library datetime moduleimport numpy as npfrom netCDF4 import Dataset # http://code.google.com/p/netcdf4-python/import matplotlib.pyplot as pltfrom mpl_toolkits.basemap import Basemap, addcyclic, shiftgridnc_f = 'Fjouxan Rpython Your filenamenc_fid lp28 Your filenamenc_fid databank air.sig995.2012.nc' # RpythonUnion = Dataset (nc_f) 'r') # Dataset is the class behavior to open the file # and create an instance of the ncCDF4 class# Extract data from NetCDF filelats = nc_fid.variables [' lat'] [:] # extract/copy the datalons = nc_fid.variables ['lon'] [:] time = nc_fid.variables [' time'] [:] air = nc_fid.variables ['air'] [:] # shape is time, lat Lon as shown abovetime_idx = 237 # some random day in 2018 Python and the renalaysis are slightly off in time so this fixes that problemoffset = dt.timedelta (hours=48) # List of all times in the file as datetime objectsdt_time = [dt.date (1,1,1) + dt.timedelta (hours=t)-offset\ for t in time] cur_time = dt_ time [time _ idx] # Plot of global temperature on our random day#fig = plt.figure () # fig.subplots_adjust (left=0., right=1., bottom=0. Top=0.9) fig=plt.figure (figsize= (16pr 9)) ax=fig.add_subplot (111C) # Setup the map. See http://matplotlib.org/basemap/users/mapsetup.html# for other projections.m=Basemap (llcrnrlat=-90,urcrnrlat=90,llcrnrlon=-180,urcrnrlon=180,resolution='c') m.drawparallels (np.arange (- 90,90 + 1,30), labels = [1,0,0,0], fontsize=14,linewidth='0.2',color='black') m.drawmeridians (np.arange (- 180,180 + 1,60), labels = [0,0,0,1], fontsize=14,linewidth='0.2' Color='black') m.drawcoastlines () m.drawmapboundary () air2= [time _ idx,:,:] # convert 0mm 360 to-180mm 1800Make the plot continuousair_cyclic, lons_cyclic = addcyclic (air2, lons) # Shift the grid so lons go from-180to 180 instead of 0 to 360.air_cyclic, lons_cyclic = shiftgrid (1800.air_cyclic, lons_cyclic, start=False) # False True# Create 2D lat/lon arrays for Basemaplon2d, lat2d = np.meshgrid (lons_cyclic, lats) # Transforms lat/lon into plotting coordinates for projectionx Y = m (lon2d, lat2d) # View array latitude dimen = np.array (x) .shapeprint (dimen) dimen1 = np.array (y) .shapeprint (dimen1) dimen2 = np.array (air_cyclic) .shapeprint (dimen2) # Plot of air temperature with 11 contour intervalsclevs = np.arange cs = m.contourf (cs,shrink=0.75,orientation='vertical',extend='both',pad=0.025) cbar=plt.colorbar (cs,shrink=0.75,orientation='vertical',extend='both',pad=0.025) Aspect=20) # orientation='horizontal'#cbar = plt.colorbar (cs, orientation='horizontal', shrink=0.5) cbar.set_label ("% s (% s)"% (nc_fid.variables ['air'] .var _ desc,nc_fid.variables [' air'] .units)) plt.title ("% s on% s"% (nc_fid.variables ['air'] .var _ desc, cur_time) plt.savefig (' Rpythonapace lp28plot33.pngmmdpian800) plt.show ()
Temperature curve of Darwin, Australia in 2012:
Darwin = {'name':' Darwin, Australia', 'lat':-12.45,' lon': 130.83} # Find the nearest latitude and longitude for Darwinlat_idx = np.abs (lats-darwin ['lat']). Argmin () lon_idx = np.abs (lons-darwin [' lon']). Argmin () # Simple example: temperature profile for the entire year at Darwin.# Open a new NetCDF file to write the data to. For format, you can choose from# 'NETCDF3_CLASSIC',' NETCDF3_64BIT', 'NETCDF4_CLASSIC', and' NETCDF4'w_nc_fid = Dataset ('FGV peg RpythonCharger lp28 RpythonUniverse) w_nc_fid.description = "NCEP/NCAR Reanalysis% s from its value at% s.% s" (nc_fid.variables [' air']. Var_desc.lower ()) Darwin ['name'], nc_fid.description) # Using our previous dimension info, we can create the new time dimension# Even though we know the size, we are going to set the size to unknownw_nc_fid.createDimension (' time', None) w_nc_dim = w_nc_fid.createVariable ('time', nc_fid.variables [' time'] .dtype, ('time') ) # You can do this step yourself but someone else did the work for us.for ncattr in nc_fid.variables ['time'] .ncattrs (): w_nc_dim.setncattr (ncattr, nc_fid.variables [' time'] .getncattr (ncattr)) # Assign the dimension data to the new NetCDF file.w_nc_fid.variables ['time'] [:] = timew_nc_var = w_nc_fid.createVariable (' air', 'f8' ('time') w_nc_var.setncatts ({' long_name': u "mean Daily Air temperature",\ 'units': u "degK",' level_desc': upright surface,\ 'var_desc': u "Air temperature",\' statistic': u'Mean\ nM'}) w_nc_fid.variables ['air'] [:] = air [time _ idx Lat_idx, lon_idx] w_nc_fid.close () # close the new file# A plot of the temperature profile for Darwin in 2012fig = plt.figure () plt.plot (dt_time, air [:, lat_idx, lon_idx], caterpillar r') plt.plot (dt_ time [time _ idx], air [time _ idx, lat_idx, lon_idx], cymbals, marker='o') plt.text (dt_ time [time _ idx], Airport [time _ idx, lat_idx, lon_idx] Cur_time,ha='right') fig.autofmt_xdate () plt.ylabel ("% s (% s)"% (nc_fid.variables ['air'] .var _ desc,nc_fid.variables [' air'] .units) plt.xlabel ("Time") plt.title ("% s from\ n% s for% s"% (nc_fid.variables ['air']. Var_desc,darwin [' name']) Cur_time.year)) plt.savefig ('FRPG Rpython plt.savefig lp28 RPY plot33. 2. Png recording and writing dpirates 800) plt.show ()
Thank you for your reading, the above is the content of "how to achieve the global temperature map of Python". After the study of this article, I believe you have a deeper understanding of how to achieve the global temperature map of Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.