简体   繁体   中英

How can I get country data from a NC (NetCDF) file containing global data in the form of longitudes and latitude?

I have a temperature data from a NetCDF file from NASA's website for global level. The data is in this link in my google drive.

I am able to open the data using:

from netCDF4 import Dataset
data = Dataset("../data/amaps_robinson_1000km.nc")

The data looks as shown below when I print it: 在此处输入图像描述

data.variables yields me following:

{'lon': <class 'netCDF4._netCDF4.Variable'>
 float32 lon(lon)
     long_name: Longitude
     standard_name: longitude
     units: degrees_east
 unlimited dimensions: 
 current shape = (180,)
 filling on, default _FillValue of 9.969209968386869e+36 used,
 'lat': <class 'netCDF4._netCDF4.Variable'>
 float32 lat(lat)
     long_name: Latitude
     standard_name: latitude
     units: degrees_north
 unlimited dimensions: 
 current shape = (90,)
 filling on, default _FillValue of 9.969209968386869e+36 used,
 'TEMPANOMALY': <class 'netCDF4._netCDF4.Variable'>
 float32 TEMPANOMALY(lat, lon)
     long_name: Temperature anomaly
     standard_name: surface_temperature_anomaly
     missing_value: 9999.0
     units: K
 unlimited dimensions: 
 current shape = (90, 180)
 filling on, default _FillValue of 9.969209968386869e+36 used}

I am able to access the longitude, latitude and temperature data using

lons = data.variables["lon"][:]
lats = data.variables["lat"][:]
temp_anomaly = data.variables["TEMPANOMALY"][:]

I am also able to plot it using the following code:

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import matplotlib as mpl

fig = plt.figure(figsize = (12, 6))

ax = plt.axes(projection = ccrs.PlateCarree())

ax.set_global()
ax.coastlines(resolution = "10m", lw = 1)
#ax.gridlines(linestyle = "--", color = "black")


#Set contour levels, then draw the plot and a colorbar
clevs = np.arange(-4, 5)
cmap = "coolwarm"

#Plot filled contours
plt.contourf(lons, lats, 
             temp_anomaly,
             clevs,
             transform = ccrs.PlateCarree(),
             cmap = cmap
             #cmap options for temperature anomaly: coolwarm, RdBu, bwr
            )


plt.title("(°C) Anomaly in 2021 vs 1951-1980")


cb = plt.colorbar(ax = ax,
                  orientation = "horizontal",
                  pad = 0.02,
                  aspect = 10,  #ratio of long to short dimension
                  shrink = 0.5 #Fraction by which to multiply the size of the colorbar
                 )


cb.set_label("°C", size = 12, rotation = 0, labelpad = 15)

cb.ax.tick_params(labelsize = 10)

plt.show()

It looks as follows: 在此处输入图像描述

However, I would like to get the data for individual countries like Germany, France, Nepal, India, etc. and plot them by country individually instead of clipping them on the map of the world. How can I get the data for the countries individually?

I'm actually doing something really similar, in fact, using the same data source just instead with windSpeed data, and I found this neet library called "reverse_geocoder" which when given an input of lat/long coords, will return location data for that location, and then you can just export the data points located in a certain country into another file. For example, as shown on the git:

import reverse_geocoder as rg

coordinates = (51.5214588,-0.1729636),(9.936033, 76.259952),(37.38605,-122.08385)

results = rg.search(coordinates) # default mode = 2

print results

returns :

  [{'name': 'Bayswater', 
  'cc': 'GB', 
  'lat': '51.51116',
  'lon': '-0.18426', 
  'admin1': 'England', 
  'admin2': 'Greater London'}, 
 {'name': 'Cochin', 
  'cc': 'IN', 
  'lat': '9.93988',
  'lon': '76.26022', 
  'admin1': 'Kerala', 
  'admin2': 'Ernakulam'},
 {'name': 'Mountain View', 
  'cc': 'US', 
  'lat': '37.38605',
  'lon': '-122.08385', 
  'admin1': 'California', 
  'admin2': 'Santa Clara County'}]

https://github.com/thampiman/reverse-geocoder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM