问题
I have a technical question, which I tried to solve all week long. I created a netcdf file from observations with a measurement value of air quality on a geographical grid (lat/lon) along a certain track. Now I would like to calculate the departure (or anomaly) of these values from a larger grid (data from a computer model with mean values over a large area).
My two netcdf files are structured as follows:
Observations (Instrument measurements):
Dimensions:
lat: 1321, lon: 1321
Data variables:
Longitude (lon) float64 8.413 8.411 8.409 ... 4.904 4.905
Latitude (lat) float64 47.4 47.4 47.41 ... 52.37 52.37
obs_data (lat, lon) float64 ...
Model data:
Dimensions:
latitude: 140, level: 1, longitude: 215, time: 24
Coordinates:
longitude (longitude) float32 357.55 357.65 ... 18.85 18.95
latitude (latitude) float32 55.95 55.85 55.75 ... 42.15 42.05
level (level) float32 0.0
time (time) timedelta64[ns] 00:00:00 01:00:00 ... 23:00:00
Data variables:
model_data (time, level, latitude, longitude) float32 ...
I tried all sorts of different approaches, but every time I run into some sort of error for which there appears to be no solution and I ended up having to try a different approach. The closest I got was by following this great tutorial, but also here I hit a wall. When I try to find the nearest lat and lon for the two data sets, by
lat_idx = np.abs(model_lat - obs_lat).argmin() #subtract train lat from model lat
lon_idx = np.abs(model_lon - obs_lon).argmin() #subtract train lon from model lon
I get the follwing error
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-437-9396b00ba22f> in <module>
18
19 # Find the nearest latitude and longitude for the train data
---> 20 lat_idx = np.abs(model_lat - obs_lat).argmin()
21 lon_idx = np.abs(model_lon - obs_lon).argmin()
22
~/opt/anaconda3/lib/python3.7/site-packages/numpy/ma/core.py in __sub__(self, other)
4115 if self._delegate_binop(other):
4116 return NotImplemented
-> 4117 return subtract(self, other)
4118
4119 def __rsub__(self, other):
~/opt/anaconda3/lib/python3.7/site-packages/numpy/ma/core.py in __call__(self, a, b, *args, **kwargs)
1024 with np.errstate():
1025 np.seterr(divide='ignore', invalid='ignore')
-> 1026 result = self.f(da, db, *args, **kwargs)
1027 # Get the mask for the result
1028 (ma, mb) = (getmask(a), getmask(b))
ValueError: operands could not be broadcast together with shapes (140,) (1321,)
Isn't there a way to simply calculate:
anomaly = model_data[lat, lon] - obs_data[lat, lon]
?
My newest hope is xarray
, but I really struggle with their documentation and I've spent days figuring out a way forward.
Has anyone of you found a solution to this problem? Any tips are really appreciated.
Edit:
As requested by V. Ayrat :
In: type(model_data)
Out: xarray.core.dataset.Dataset
obs_data
is the same type.
If two obs_data
values fall into the same model_data
cell, the obs_data
should be subtracted from the same model_data
cell.
回答1:
It is not entirely clear what you are trying to do or what data structures you use. I will edit post if there is more info coming later. However, I think this solves the problem:
If you want closest lat/lon of obs_lat
to model_lat
use:
lat_idx = np.abs(model_lat - obs_lat[:,None]).argmin(axis=0)
lon_idx = np.abs(model_lon - obs_lon[:,None]).argmin(axis=0)
And if you want closest lat/lon of model_lat
to obs_lat
use:
lat_idx = np.abs(model_lat - obs_lat[:,None]).argmin(axis=1)
lon_idx = np.abs(model_lon - obs_lon[:,None]).argmin(axis=1)
来源:https://stackoverflow.com/questions/62253106/calculate-departure-or-anomaly-of-a-value-between-two-arrays-of-different-geogra