如何在每个网格IE处按时间轴对气候数据集的特定纬度和经度上的时间轴进行排序

发布于 2025-01-29 03:04:09 字数 1894 浏览 4 评论 0 原文

我有一个气候数据集的NetCDF文件,其形状为20 * 445 * 445 AS(时间,纬度,经度),

我已将其读为 numpy.ma.core.maskedarray 使用 > netcdf4

array 在时间轴上的数据

我想对此 numpy 3D - (纬度,经度)该值应为上升或降序,

我尝试了

import netCDF4 as nc
from netCDF4 import Dataset,num2date,date2num
import sys,os,numpy as np,logging
from datetime import date, datetime, timedelta
import matplotlib.pyplot as plt

fileName = 'some_file'
data = Dataset(fileName, 'r')

temp_data = data.variables['Band1'][:,:,:]

数据集的原始数据集图

for i in range(0,20):
    data_to_plot = temp_data[i]
    
    fig, ax = plt.subplots(figsize = (4,4))
    ax.set_title('Layer '+ str(i))
    cax = ax.imshow(data_to_plot, cmap = plt.cm.Accent)
    
    cbar = plt.colorbar(cax,
                        orientation='vertical',
                        fraction=0.045, 
                        pad=0.05)
    ax.set_xlabel('X-axis')
    ax.set_ylabel('Y-axis')
sorted_temp_data = np.argsort(temp_data, axis=0)

排序

for i in range(0,20):
    data_to_plot = sorted_temp_data[i]
    
    fig, ax = plt.subplots(figsize = (4,4))
    ax.set_title('Layer '+ str(i))
    cax = ax.imshow(data_to_plot, cmap = plt.cm.Accent)
    
    cbar = plt.colorbar(cax,
                        orientation='vertical',
                        fraction=0.045, 
                        pad=0.05)
    
    ax.set_xlabel('X-axis')
    ax.set_ylabel('Y-axis')

如何做?

I have a NetCDF file of Climate dataset having 3D structure with a shape of 20 * 445 * 445 as (time, latitude, longitude)

I have read it as numpy.ma.core.MaskedArray using netCDF4 library

I want to sort this NumPy 3D-array data on the time axis at each grid i.e.

at any specific grid (latitude,longitude) the value should be in either ascending or descending order

I have tried the numpy.argsort function but the range of values at any grid/pixel exceeds the original value of dataset, so its not working i.e. the original dataset has values between 7 to 16, but after sorting, the values in dataset ranges from 1 to 19.

the code is available at the Google Collab, and the NetCDF is available here

import netCDF4 as nc
from netCDF4 import Dataset,num2date,date2num
import sys,os,numpy as np,logging
from datetime import date, datetime, timedelta
import matplotlib.pyplot as plt

fileName = 'some_file'
data = Dataset(fileName, 'r')

temp_data = data.variables['Band1'][:,:,:]

Plots of original Dataset

for i in range(0,20):
    data_to_plot = temp_data[i]
    
    fig, ax = plt.subplots(figsize = (4,4))
    ax.set_title('Layer '+ str(i))
    cax = ax.imshow(data_to_plot, cmap = plt.cm.Accent)
    
    cbar = plt.colorbar(cax,
                        orientation='vertical',
                        fraction=0.045, 
                        pad=0.05)
    ax.set_xlabel('X-axis')
    ax.set_ylabel('Y-axis')
sorted_temp_data = np.argsort(temp_data, axis=0)

Plots of Sorted Datasets

for i in range(0,20):
    data_to_plot = sorted_temp_data[i]
    
    fig, ax = plt.subplots(figsize = (4,4))
    ax.set_title('Layer '+ str(i))
    cax = ax.imshow(data_to_plot, cmap = plt.cm.Accent)
    
    cbar = plt.colorbar(cax,
                        orientation='vertical',
                        fraction=0.045, 
                        pad=0.05)
    
    ax.set_xlabel('X-axis')
    ax.set_ylabel('Y-axis')

How to do so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间海 2025-02-05 03:04:09

numpy的 argsort 函数不返回排序的数组。它返回排序的数组的索引。您可以直接沿时间轴(假设时间轴为0轴)直接对数组进行排序,如下所示:

np.sort(temp_data, axis=0)

如果要使用ArgSort,则代码应该是这样:

idx = np.argsort(temp_data, axis=0)
sort_data = np.take_along_axis(temp_data, idx,axis=0)

Numpy's argsort function doesnt return sorted array. It returns the index of sorted array. You can directly sort the array along the time axis (assuming time axis is the 0th axis) as follows:

np.sort(temp_data, axis=0)

If you want to use argsort the code should be like this:

idx = np.argsort(temp_data, axis=0)
sort_data = np.take_along_axis(temp_data, idx,axis=0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文