numpy 在低分辨率上聚合高分辨率数据集,以便坐标与低分辨率数据集完全匹配

f0ofjuux  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(224)

我有以下数据集:

import xarray as xr
    import numpy as np
    
    lon=np.array([[-116.02769 , -116.01372 , -115.99975 , -115.98577 , -115.9718  ,
            -115.957825, -115.943855, -115.92988 , -115.9159  , -115.901924,
            -115.88794 , -115.87396 , -115.859985, -115.846   , -115.832016,
            -115.81803 , -115.804054, -115.79006 , -115.77608 , -115.76209 ,
            -115.74811 , -115.734116, -115.72012 , -115.70614 , -115.69215 ],
           [-116.02497 , -116.011   , -115.99703 , -115.98306 , -115.96909 ,
            -115.955124, -115.94115 , -115.92718 , -115.9132  , -115.89922 ,
            -115.88525 , -115.87127 , -115.85729 , -115.843315, -115.82933 ,
            -115.81535 , -115.80137 , -115.787384, -115.7734  , -115.759415,
            -115.74543 , -115.731445, -115.71746 , -115.70347 , -115.68948 ],
           [-116.022255, -116.00829 , -115.99432 , -115.980354, -115.966385,
            -115.952415, -115.938446, -115.92448 , -115.9105  , -115.89653 ,
            -115.88255 , -115.868576, -115.8546  , -115.84062 , -115.826645,
            -115.81267 , -115.79869 , -115.784706, -115.77073 , -115.756744,
            -115.74276 , -115.728775, -115.71479 , -115.700806, -115.68682 ],
           [-116.01954 , -116.00558 , -115.99161 , -115.977646, -115.96368 ,
            -115.949715, -115.935745, -115.921776, -115.90781 , -115.89383 ,
            -115.87986 , -115.86589 , -115.85191 , -115.83794 , -115.82397 ,
            -115.80999 , -115.79601 , -115.78203 , -115.76805 , -115.754074,
            -115.74009 , -115.72611 , -115.71213 , -115.69814 , -115.68416 ],
           [-116.01682 , -116.00286 , -115.9889  , -115.97494 , -115.960976,
            -115.94701 , -115.933044, -115.919075, -115.905106, -115.891136,
            -115.87717 , -115.8632  , -115.84923 , -115.83525 , -115.82128 ,
            -115.807304, -115.79333 , -115.77936 , -115.76538 , -115.7514  ,
            -115.73742 , -115.72344 , -115.70946 , -115.69548 , -115.681496],
           [-116.014114, -116.00015 , -115.98619 , -115.97223 , -115.95827 ,
            -115.944305, -115.93034 , -115.916374, -115.90241 , -115.88844 ,
            -115.87447 , -115.86051 , -115.84654 , -115.83257 , -115.818596,
            -115.80463 , -115.79065 , -115.77668 , -115.7627  , -115.748726,
            -115.73475 , -115.72077 , -115.706795, -115.69282 , -115.67884 ],
           [-116.0114  , -115.997444, -115.98348 , -115.96953 , -115.95557 ,
            -115.941605, -115.92764 , -115.91368 , -115.89972 , -115.88575 ,
            -115.87179 , -115.85782 , -115.84386 , -115.82989 , -115.81592 ,
            -115.80195 , -115.78798 , -115.774   , -115.76003 , -115.746056,
            -115.732086, -115.71811 , -115.70413 , -115.690155, -115.67618 ],
           [-116.00869 , -115.99473 , -115.980774, -115.96682 , -115.952866,
            -115.9389  , -115.92494 , -115.91098 , -115.897026, -115.88306 ,
            -115.869095, -115.85513 , -115.84117 , -115.8272  , -115.81324 ,
            -115.79927 , -115.7853  , -115.77133 , -115.75736 , -115.74339 ,
            -115.729416, -115.71545 , -115.70147 , -115.6875  , -115.67352 ],
           [-116.005974, -115.99202 , -115.97807 , -115.96411 , -115.95016 ,
            -115.9362  , -115.92225 , -115.90829 , -115.89433 , -115.88037 ,
            -115.86641 , -115.85245 , -115.838486, -115.824524, -115.810555,
            -115.79659 , -115.78262 , -115.76866 , -115.75469 , -115.74072 ,
            -115.72675 , -115.71278 , -115.69881 , -115.68484 , -115.67087 ],
           [-116.003265, -115.98932 , -115.975365, -115.96141 , -115.94746 ,
            -115.9335  , -115.91955 , -115.905594, -115.89164 , -115.87768 ,
            -115.86372 , -115.84976 , -115.8358  , -115.82184 , -115.80788 ,
            -115.793915, -115.77995 , -115.76598 , -115.75202 , -115.73805 ,
            -115.72409 , -115.71012 , -115.69615 , -115.68218 , -115.668205],
           [-116.00056 , -115.98661 , -115.97266 , -115.95871 , -115.944756,
            -115.93081 , -115.916855, -115.9029  , -115.88895 , -115.87499 ,
            -115.86104 , -115.84708 , -115.83312 , -115.81916 , -115.8052  ,
            -115.79124 , -115.777275, -115.76331 , -115.74935 , -115.73539 ,
            -115.72142 , -115.70746 , -115.69349 , -115.67952 , -115.66555 ],
           [-115.99785 , -115.9839  , -115.969955, -115.95601 , -115.94206 ,
            -115.92811 , -115.91416 , -115.90021 , -115.88625 , -115.8723  ,
            -115.858345, -115.84439 , -115.83044 , -115.81648 , -115.80252 ,
            -115.78857 , -115.774605, -115.76064 , -115.74668 , -115.73272 ,
            -115.71876 , -115.704796, -115.690834, -115.676865, -115.662895],
           [-115.99514 , -115.98119 , -115.967255, -115.95331 , -115.93936 ,
            -115.925415, -115.91147 , -115.897514, -115.88357 , -115.86961 ,
            -115.85567 , -115.84171 , -115.82776 , -115.813805, -115.79985 ,
            -115.78589 , -115.771935, -115.75797 , -115.74402 , -115.73006 ,
            -115.716095, -115.70213 , -115.68817 , -115.67421 , -115.66025 ],
           [-115.99243 , -115.97849 , -115.964554, -115.95061 , -115.93666 ,
            -115.92272 , -115.908775, -115.89483 , -115.880875, -115.86693 ,
            -115.85298 , -115.83903 , -115.82508 , -115.81113 , -115.79717 ,
            -115.78322 , -115.769264, -115.75531 , -115.74135 , -115.727394,
            -115.71343 , -115.69948 , -115.68552 , -115.671555, -115.65759 ],
           [-115.98973 , -115.97579 , -115.96185 , -115.94791 , -115.93397 ,
            -115.92002 , -115.90608 , -115.892136, -115.87819 , -115.86424 ,
            -115.850296, -115.83635 , -115.8224  , -115.80845 , -115.794495,
            -115.78055 , -115.766594, -115.75264 , -115.738686, -115.72473 ,
            -115.71078 , -115.696815, -115.68286 , -115.6689  , -115.65494 ],
           [-115.98702 , -115.97308 , -115.95915 , -115.94521 , -115.931274,
            -115.91733 , -115.90339 , -115.88945 , -115.8755  , -115.86156 ,
            -115.84761 , -115.83367 , -115.819725, -115.80577 , -115.791824,
            -115.77788 , -115.76392 , -115.74998 , -115.73602 , -115.72207 ,
            -115.708115, -115.69416 , -115.68021 , -115.666245, -115.65229 ],
           [-115.98432 , -115.97038 , -115.95645 , -115.94251 , -115.92857 ,
            -115.914635, -115.900696, -115.88676 , -115.87282 , -115.85888 ,
            -115.84493 , -115.830986, -115.81705 , -115.8031  , -115.789154,
            -115.77521 , -115.76126 , -115.74731 , -115.73336 , -115.719406,
            -115.70546 , -115.691505, -115.67755 , -115.6636  , -115.64964 ],
           [-115.98162 , -115.96768 , -115.95375 , -115.93982 , -115.92588 ,
            -115.91195 , -115.89801 , -115.88407 , -115.87013 , -115.85619 ,
            -115.842255, -115.82831 , -115.81437 , -115.80042 , -115.78648 ,
            -115.77254 , -115.75859 , -115.744644, -115.7307  , -115.71675 ,
            -115.7028  , -115.68885 , -115.6749  , -115.66094 , -115.64699 ]],
          dtype='float32')
    
    lat = np.array([[48.881176, 48.882965, 48.884758, 48.886547, 48.888332, 48.890118,
            48.891903, 48.893684, 48.895466, 48.897243, 48.89902 , 48.9008  ,
            48.902573, 48.904346, 48.906116, 48.907887, 48.909657, 48.911423,
            48.91319 , 48.91495 , 48.916714, 48.918476, 48.920235, 48.921993,
            48.923748],
           [48.87196 , 48.873753, 48.87554 , 48.87733 , 48.879116, 48.8809  ,
            48.882687, 48.884468, 48.88625 , 48.888027, 48.889805, 48.891582,
            48.893356, 48.89513 , 48.8969  , 48.89867 , 48.900436, 48.902206,
            48.90397 , 48.905735, 48.907497, 48.909256, 48.911015, 48.912773,
            48.914528],
           [48.862747, 48.864536, 48.866325, 48.868114, 48.869904, 48.87169 ,
            48.87347 , 48.87525 , 48.877033, 48.87881 , 48.88059 , 48.882366,
            48.88414 , 48.88591 , 48.887684, 48.88945 , 48.89122 , 48.892986,
            48.894753, 48.896515, 48.898277, 48.900036, 48.901794, 48.903553,
            48.905308],
           [48.85353 , 48.855324, 48.857113, 48.858902, 48.860687, 48.862473,
            48.864254, 48.866035, 48.867817, 48.869595, 48.871372, 48.873146,
            48.87492 , 48.876694, 48.878464, 48.880234, 48.882   , 48.883766,
            48.885532, 48.887295, 48.889057, 48.890816, 48.892574, 48.894333,
            48.896088],
           [48.84432 , 48.846107, 48.847897, 48.849686, 48.85147 , 48.853256,
            48.855038, 48.85682 , 48.8586  , 48.86038 , 48.862156, 48.86393 ,
            48.865704, 48.867474, 48.869247, 48.871014, 48.872784, 48.87455 ,
            48.876312, 48.878075, 48.879837, 48.881596, 48.883354, 48.88511 ,
            48.886868],
           [48.835102, 48.83689 , 48.83868 , 48.84047 , 48.842255, 48.84404 ,
            48.84582 , 48.847603, 48.84938 , 48.851162, 48.852936, 48.854713,
            48.856483, 48.858257, 48.860027, 48.861797, 48.863564, 48.86533 ,
            48.867092, 48.868855, 48.870617, 48.872375, 48.874134, 48.87589 ,
            48.877644],
           [48.825886, 48.82768 , 48.829468, 48.831253, 48.83304 , 48.834824,
            48.836605, 48.838387, 48.840164, 48.84194 , 48.84372 , 48.845493,
            48.847267, 48.849037, 48.850807, 48.852577, 48.854343, 48.85611 ,
            48.857872, 48.859634, 48.861397, 48.863155, 48.864914, 48.86667 ,
            48.868423],
           [48.816673, 48.818462, 48.82025 , 48.822037, 48.823822, 48.825607,
            48.82739 , 48.82917 , 48.830948, 48.832726, 48.8345  , 48.836277,
            48.838047, 48.83982 , 48.84159 , 48.843357, 48.845123, 48.84689 ,
            48.84865 , 48.850414, 48.852177, 48.853935, 48.85569 , 48.85745 ,
            48.859203],
           [48.807457, 48.809246, 48.811035, 48.81282 , 48.814606, 48.816387,
            48.818172, 48.81995 , 48.82173 , 48.823505, 48.825283, 48.827057,
            48.82883 , 48.8306  , 48.83237 , 48.834137, 48.835903, 48.83767 ,
            48.83943 , 48.841194, 48.842953, 48.84471 , 48.84647 , 48.848225,
            48.84998 ],
           [48.79824 , 48.80003 , 48.80182 , 48.803604, 48.80539 , 48.80717 ,
            48.808952, 48.810734, 48.81251 , 48.81429 , 48.816063, 48.817837,
            48.81961 , 48.82138 , 48.82315 , 48.824917, 48.826683, 48.82845 ,
            48.83021 , 48.831974, 48.833733, 48.83549 , 48.83725 , 48.839005,
            48.84076 ],
           [48.789024, 48.790813, 48.792603, 48.794388, 48.796173, 48.797955,
            48.799736, 48.801517, 48.803295, 48.80507 , 48.806847, 48.80862 ,
            48.81039 , 48.81216 , 48.81393 , 48.815697, 48.817463, 48.81923 ,
            48.82099 , 48.822754, 48.824512, 48.82627 , 48.828026, 48.82978 ,
            48.831535],
           [48.779808, 48.781597, 48.783386, 48.78517 , 48.786953, 48.78874 ,
            48.79052 , 48.792297, 48.794075, 48.795853, 48.797626, 48.7994  ,
            48.80117 , 48.802944, 48.80471 , 48.806477, 48.808243, 48.81001 ,
            48.81177 , 48.81353 , 48.815292, 48.817047, 48.818806, 48.82056 ,
            48.82231 ],
           [48.77059 , 48.77238 , 48.774166, 48.77595 , 48.777737, 48.77952 ,
            48.7813  , 48.78308 , 48.78486 , 48.786633, 48.788406, 48.79018 ,
            48.791954, 48.793724, 48.79549 , 48.797256, 48.799023, 48.800785,
            48.802547, 48.80431 , 48.80607 , 48.807827, 48.80958 , 48.811337,
            48.81309 ],
           [48.761375, 48.763165, 48.76495 , 48.766735, 48.76852 , 48.7703  ,
            48.772083, 48.77386 , 48.77564 , 48.777412, 48.77919 , 48.78096 ,
            48.782734, 48.784504, 48.78627 , 48.788036, 48.789803, 48.791565,
            48.793327, 48.79509 , 48.79685 , 48.798603, 48.80036 , 48.802113,
            48.803867],
           [48.75216 , 48.75395 , 48.755733, 48.75752 , 48.7593  , 48.76108 ,
            48.762863, 48.76464 , 48.76642 , 48.768196, 48.76997 , 48.77174 ,
            48.773514, 48.775284, 48.77705 , 48.778816, 48.780582, 48.782345,
            48.784107, 48.785866, 48.787624, 48.789383, 48.791138, 48.792892,
            48.794643],
           [48.742943, 48.744728, 48.746517, 48.7483  , 48.750084, 48.751865,
            48.753643, 48.755424, 48.7572  , 48.758976, 48.76075 , 48.76252 ,
            48.764294, 48.76606 , 48.76783 , 48.769596, 48.77136 , 48.77312 ,
            48.774883, 48.776646, 48.7784  , 48.78016 , 48.781914, 48.78367 ,
            48.78542 ],
           [48.733723, 48.73551 , 48.737297, 48.739082, 48.740864, 48.742645,
            48.744427, 48.746204, 48.747982, 48.749756, 48.75153 , 48.7533  ,
            48.755074, 48.75684 , 48.75861 , 48.760372, 48.76214 , 48.7639  ,
            48.765663, 48.76742 , 48.76918 , 48.770935, 48.77269 , 48.774445,
            48.776196],
           [48.724506, 48.726295, 48.72808 , 48.729862, 48.731647, 48.73343 ,
            48.735207, 48.736984, 48.73876 , 48.740536, 48.74231 , 48.74408 ,
            48.74585 , 48.74762 , 48.749386, 48.751152, 48.75292 , 48.75468 ,
            48.75644 , 48.758198, 48.759956, 48.761715, 48.76347 , 48.76522 ,
            48.76697 ]], dtype='float32')
    
    
    temperature = 15 + 8 * np.random.randn(18, 25)
    
    
    daymet = xr.DataArray(
        data=temperature,
        dims=["y", "x"],
        coords=dict(
            lon=(["y", "x"], lon),
            lat=(["y", "x"], lat),
    
        ),
        attrs=dict(
            description="Ambient temperature.",
            units="degC",
        ),
    )
    
    
    lon = np.array([-116.02501 , -115.975006, -115.925   , -115.87501 , -115.825005,
           -115.775   , -115.725006, -115.675   ], dtype='float32')
    
    lat = np.array([48.725   , 48.774994, 48.824997, 48.874992], dtype='float32')
    
    temperature = 15 + 8 * np.random.randn(4, 8)

    chirps = xr.DataArray(
        data=temperature,
        dims=["lat", "lon"],
        coords=dict(
            lon=(['lon'], lon),
            lat=(['lat'], lat),
    
        ),
        attrs=dict(
            description="Ambient temperature.",
            units="degC",
        ),
    )

字符串
我写了下面的代码来聚合chirps上的daymet,条件是得到的daymet必须与chirps数据集的坐标完全匹配。但是由于最后的for循环,它非常慢。有没有一种方法可以不使用for循环来完成它?还要注意,chirps数据的坐标是1D,而daymet的坐标是2D(这造成了整个问题)。

def FTranspose(lon, lat):
       
        if lat.ndim == 1:
    
            x, y = np.meshgrid(lon, lat)
    
        if lat.ndim == 2:
            x = lon
            y = lat
            
        xf, yf = x.flatten(), y.flatten()
    
        points = np.column_stack((xf,yf))    
        
        return points
    
    
    lat_daymet = np.array(daymet.lat)
    lon_daymet = np.array(daymet.lon)
    
    
    points_daymet = FTranspose(lon_daymet, lat_daymet)
    
    points_chirps = FTranspose(chirps.lon, chirps.lat)
    
    from scipy.spatial import KDTree
    kdtree = KDTree(points_chirps)
    d, arg_dd = kdtree.query(points_daymet)
    
    daymet_f = np.array(daymet).flatten()
    chirps_f = np.array(chirps).flatten()
    
    daymet_coarse = np.empty(len(chirps_f))
    
    for i in range(len(chirps_f)):
        print(i, len(chirps_f))
        daymet_coarse[i] = np.nanmean(daymet_f[np.where(arg_dd==i)])
    daymet_coarse = daymet_coarse.reshape(chirps.shape)

ars1skjm

ars1skjm1#

理论上,你应该留在xarray生态系统中并调用interp_like;在实践中,我发现它的行为毫无意义。

import xarray as xr
import numpy as np
from scipy.interpolate import griddata

rand = np.random.default_rng(seed=0)
temperature = rand.normal(loc=15, scale=8, size=lat.shape)

daymet = xr.DataArray(
    data=temperature,
    dims=('y', 'x'),
    coords=dict(
        lon=(('y', 'x'), lon),
        lat=(('y', 'x'), lat),
    ),
    attrs=dict(
        description='Ambient temperature.',
        units='degC',
    ),
)

lon_coarse = np.array(
    object=(-116.02501, -115.975006, -115.925, -115.87501, -115.825005, -115.775, -115.725006, -115.675),
    dtype=np.float32,
)
lat_coarse = np.array(
    object=(48.725, 48.774994, 48.824997, 48.874992),
    dtype=np.float32,
)
temperature = rand.normal(loc=150, scale=8, size=(lat_coarse.size, lon_coarse.size))

chirps = xr.DataArray(
    data=temperature,
    dims=daymet.dims,
    coords=dict(
        lon=(daymet.dims, np.broadcast_to(lon_coarse, temperature.shape)),
        lat=(daymet.dims, np.broadcast_to(lat_coarse[:, np.newaxis], temperature.shape)),
    ),
    attrs=daymet.attrs,
)

daymet_coarse = griddata(
    values=daymet.values.ravel(),
    points=np.stack((daymet['lon'], daymet['lat']), axis=-1).reshape((-1, 2)),
    xi=np.stack(np.meshgrid(lon_coarse, lat_coarse), axis=-1).reshape((-1, 2)),
).reshape(chirps.shape)

字符串

相关问题