Как получить доступ к атрибуту указанных переменных NetCDF - PullRequest
0 голосов
/ 06 марта 2020

Итак, у меня есть файл 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

Итак, в этом файле я хочу go для каждого изображения, для каждой указанной переменной, выполнять итерации по изображениям (по высоте и времени) и подготовить их. Я сделал это, но я не знаю, как получить доступ к атрибуту Speci c выбранного изображения, чтобы выбрать заголовок, например. Я знаю, как сделать это вручную (скажем, я знаю, что означает указанный шаг времени c, и я могу рассчитать его, но я не хочу делать это таким образом). Мой код пока:

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')
...