Ultimately, I want to calculate the difference between modelled and measured air quality. I have two netcdf files. The first one (A) contains air particle data from a model with latitude (y1) index-length 100 and longitude (x1) index-length 200. From this A, I want to subtract observation data (B) with latitude (y2) index-length 1300 and longitude (x2) index-length 1300. The actual latitude values of B (in degrees North and East) are present in A, although not exactly, i.e. values in A are evenly spaced (e.g. 55.95°, 55.85°, 55.75°, etc.) but the values in B have 3 decimals and are spaced by changing increments of roughly 0.001 to 0.003.
It feels like this should be straight forward: take obs data in a lat/lon range (e.g. 50.5 to 51°N and 8.1 to 8.2°E) and subtract it from model data in the same lat/lon range.
At first I tried with numpy
adapting from this example of calculating 'departure from global temperature'. But I keep running into dead ends.
Then, I tried a gazillion variations of something along the lines of this (which is obviously wrong, but I am no coding wizzard):
anomaly=[]
for j in range(len(100)):
for k in range(len(200)):
for i in range(len(1300)):
if latitude_model[j] == latitude_observation[i] and longitude_model[k] == longitude_observation[i]:
departure = model_data[0,0,j,k] - observation_data[i,i] #the first two dimensions of the model data are 'time' and 'level'
anomaly = np.append(departure)
My third approach was with xarray
adapting from this example. Xarray would allow to use method='nearest'
and tolerance = 0.1
functions which would help with the not-matching lat/lon data (as far as I understand).
But after loading the two netcdf files I can't even find an entrance point to how to continue the code. Plus I would probably have to reshape (but how?) the model data to match the observations. Or subtract observation data from the same model grid, if several observation points fall within the same grid.
PS: This question is eventually related to my other question, which is about the same data and problem.