如何访问指定的NetCDF变量的属性

时间:2020-03-06 10:33:19

标签: python python-3.x netcdf netcdf4

所以我有具有以下特征的NetCDF文件:

netCDF dimensions:
odict_keys(['lon', 'lat', 'height', 'time'])

netCDF groups:
OrderedDict()


netCDF variables:
odict_keys(['lon', 'lat', 'height', 'dz', 'time', 'a1', 'a2', 'a3'])

Shape: 
current shape = (248, 5, 1780, 3600) #time, height, lat, lon

所以在这个文件中,我想遍历每张图像,对于每个指定的变量,遍历图像(按高度和时间)并绘制它们。我这样做了,但是例如,我不知道如何访问所选图像的特定属性来选择标题。我知道如何手动执行操作(例如,我知道特定的时间步长意味着什么,我可以计算出来,但是我不想那样做)。到目前为止,我的代码:

variables = ['a1', 'a2', 'a3']

 for variable in variables:
     # extract the time
     time = nc_ds.variables['time']
     time_array = time[:]
     time_steps = len(time_array)

     for i in range(0, time_steps):
         general_path = 'C:/user/'

         # extract the height
         height = nc_ds.variables['height']
         height_array = height[:]
         height_steps = len(height_array)

         for h in range(0, height_steps):

             no2 = nc_ds.variables[variable][i, h, :, :]  


             no2_units = nc_ds.variables[variable].units ***#this is ok, because unit as well as long name below are the static attributes that are the same for every image***
             no2_title = nc_ds.variables[variable].long_name

             ***#no2_time = no2.variables['time'].units*** ##here I need help, how to access the specific attribute of no2 variable? I can access to an attribute if I type  nc_ds.variables['time'].units but I don't iterate through this variable and in this case, I would get only the value of the first variable. Also I want to access the height variable.




              lat = nc_ds.variables['lat'][:]
              lon = nc_ds.variables['lon'][:]
              lon, lat = np.meshgrid(lon, lat)

              plt.rcParams.update({'font.size': 30})
              plt.figure(figsize=(40, 30))

              m = Basemap(
                  resolution='h', epsg=4326, \
                  lat_ts=44, lat_0=0.00018038761410922772, lon_0=16.511650090898588
    )

              xi, yi = m(lon, lat)
              # Plot Data
              viridisBig = pylab.cm.get_cmap('jet', 512)
              newcmp = ListedColormap(viridisBig(np.linspace(0.25, 1, 386)))
         cs = m.pcolor(xi, yi, np.squeeze(no2), vmin=0, vmax=0.000000000000002109375, cmap=newcmp)

              # Add Grid Lines
              m.drawparallels(np.arange(-80., 81., 10.), labels=[1, 0, 0, 0], fontsize=10)
              m.drawmeridians(np.arange(-180., 181., 10.), labels=[0, 0, 0, 1], fontsize=10)

              # Add Colorbar
              cbar = m.colorbar(cs, location='bottom', pad="10%")
              cbar.set_label(no2_units)




              # Add Title
              plt.title(no2_title )
              # plt.show()


              plt.savefig(general_path + 'plot/' + variable +'.png')

0 个答案:

没有答案
相关问题