最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python Xarray處理設(shè)置二維數(shù)組作為coordinates方式

 更新時(shí)間:2023年07月07日 11:05:15   作者:永康李  
這篇文章主要介紹了python Xarray處理設(shè)置二維數(shù)組作為coordinates方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

python Xarray處理設(shè)置二維數(shù)組作為coordinates

因?yàn)橄胱龉P記,所以直接做的很粗糙了,后面再更新!

import cv2
import numpy as np
from osgeo import gdal
import os
import xarray as xr 
import matplotlib.pyplot as plt
import matplotlib as mpl
fig, ax = plt.subplots(figsize=(6, 1))
fig.subplots_adjust(bottom=0.5)
cmap = mpl.cm.cool
norm = mpl.colors.Normalize(vmin=5, vmax=10)
fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
             cax=ax, orientation='horizontal', label='Some Units')
"""
res = cv2.resize(RasterArrray, dsize=(441,251), interpolation=cv2.INTER_CUBIC)
Here img is thus a numpy array containing the original image, whereas res is a numpy array containing the resized image. An important aspect is the interpolation parameter: there are several ways how to resize an image. Especially since you scale down the image, and the size of the original image is not a multiple of the size of the resized image. Possible interpolation schemas are:
INTER_NEAREST - a nearest-neighbor interpolation
INTER_LINEAR - a bilinear interpolation (used by default)
INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire'-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.
INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood
INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood
"""
def GetTimeSerises_nc(ncVariable):
    """
    獲取 時(shí)間序列
    :param ncVariable:
    :return:
    """
timeSerises = ncVariable.time.data
return timeSerises
inNcFile = r"./solar-1979-01.nc"
inNc = xr.open_dataset(inNcFile)
print(inNc)
print(inNc.LATIXY.data)
import pandas as pd 
# 創(chuàng)建 dataset
ds = xr.Dataset()
numLon = 1400
numLat = 800
# LATIXY LONGXY
inLat = inNc.LATIXY.data
inLon = inNc.LONGXY.data
# print("np.min(inLon):{}, np.max(inLon):{}".format(np.min(inLon), np.max(inLon)))
# print("np.min(inLat):{}, np.max(inLat):{}".format(np.min(inLat), np.max(inLat)))
lon = np.linspace(np.min(inLon), np.max(inLon), num=numLon, endpoint=True, retstep=False, dtype=None, axis=0)
lat = np.linspace(np.min(inLat), np.max(inLat), num=numLat, endpoint=True, retstep=False, dtype=None, axis=0)
lon, lat = np.meshgrid(lon, lat)
ds = ds.assign_coords({
    "lat": (["x", "y"], lat),
    "lon": (["x", "y"], lon)
})
solor = np.full(shape=(10, numLat, numLon) , fill_value= np.nan )
ncVariable = inNc.FSDS
timeSerises = GetTimeSerises_nc(ncVariable)
i = 0
for timeSerise in timeSerises[0:10]:
    print(timeSerise)
    # 獲取數(shù)據(jù)
    arr = inNc.FSDS.loc[timeSerise].data
    print(arr.shape)
    solor[i,:,:] = cv2.resize(arr, dsize=(numLon,numLat), interpolation = cv2.INTER_LINEAR)
    print(arr.shape)
    i= i+1
    print(i)
ds["solor"] = xr.DataArray(solor, dims=['time','x', 'y'], )
ds.coords['time'] = pd.date_range(start='1979-01-01',periods=10,freq='3H')
# ds["lat"]  = xr.DataArray(lat, dims=['lat'], )
# ds["lon"]  = xr.DataArray(lon, dims=['lon'], )
print(ds)
ds.to_netcdf(r"./test_1.nc")

主要解決問(wèn)題的代碼塊在這里:

lon = np.linspace(np.min(inLon), np.max(inLon), num=numLon, endpoint=True, retstep=False, dtype=None, axis=0)
lat = np.linspace(np.min(inLat), np.max(inLat), num=numLat, endpoint=True, retstep=False, dtype=None, axis=0)
lon, lat = np.meshgrid(lon, lat)
ds = ds.assign_coords({
    "lat": (["x", "y"], lat),
    "lon": (["x", "y"], lon)
})
ds["solor"] = xr.DataArray(solor, dims=['time','x', 'y'], )
ds.coords['time'] = pd.date_range(start='1979-01-01',periods=10,freq='3H')

結(jié)果:

參考鏈接https://stackoverflow.com/questions/67695672/xarray-set-new-2d-coordinate-as-dimension

Xarray(python)讀取?Sentinel-5P(S5P)哨兵數(shù)據(jù)

需求分析:NC文件的常規(guī)包netcdf4使用手感較xarray略顯笨拙,故嘗試使用xarray讀取包含Group的.nc4文件

數(shù)據(jù):S5P二級(jí)數(shù)據(jù):S5P_RPRO_L2__HCHO, 來(lái)源:歐洲哥白尼,或NASA(推薦,因?yàn)楹孟螺d)

使用panoly可視化

(1)導(dǎo)入后的界面:

(2)選擇變量后,點(diǎn)擊Create Plot按鈕可視化:

即可得到HCHO的Plot圖以及Array可視化。

使用python里的工具包讀取

import os
import xarray as xr
import netCDF4 as nc  # 對(duì)于nc4文件,其內(nèi)含groups,
Dir = ['../S5P_Pre/Wget_HCHO']   # 時(shí)間跨度180514 ~ 190805
file = os.listdir(Dir[0])
file.sort(key = lambda x:int(x.split('___')[1][:8]))  # 按年月日排序
# (1)使用nc包打開(kāi)
ns = nc.Dataset(os.path.join(Dir[0], file[0]))   #這里的數(shù)據(jù)存儲(chǔ)在groups里面的PRODUCT里面
hcho = ns['PRODUCT']['formaldehyde_tropospheric_vertical_column'][:]
# (2) 使用xarray包打開(kāi) —— 推薦方式
xs = xr.open_dataset(os.path.join(Dir[0], file[0]), group = 'PRODUCT')  # 這里需用group函數(shù)指定組名稱(chēng)

(1)netcdf4的讀取結(jié)果:

In[29]: ns
Out[29]: Subset parameters: {"PRODUCT": ["S5P_L2__HCHO__.1"], "INFILENAMES": ["S5P_RPRO_L2__HCHO___20180514T023918_20180514T042246_03018_01_010105_20190203T205044.nc"], "INFILETYPE": ["nc"], "OUTFILETYPE": ["nc4"], "TIMENAME": [["TROP2010", "/PRODUCT/time", "/PRODUCT/delta_time"]], "VARNAMES": ["/PRODUCT/formaldehyde_tropospheric_vertical_column", "/PRODUCT/qa_value", "/PRODUCT/time_utc", "/PRODUCT/scanline", "/PRODUCT/ground_pixel"], "BOXLONRANGE": [73.0, 136.0], "BOXLATRANGE": [3.0, 54.0], "TIMERANGE": [800414432.0, 800496009.0], "GRIDTYPES": ["SWATH"], "CONVERTFILETYPE": [true]}
    dimensions(sizes): 
    variables(dimensions): 
    groups: PRODUCT, METADATA
In[30]: ns['PRODUCT']
Out[30]: <class 'netCDF4._netCDF4.Group'>
group /PRODUCT:
    dimensions(sizes): time(1), scanline(725), ground_pixel(237)
    variables(dimensions): uint16 time_idx(time), uint16 scanline_idx(scanline), uint16 ground_pixel_idx(ground_pixel), float32 longitude(time,scanline,ground_pixel), float32 latitude(time,scanline,ground_pixel), int32 time(time), int32 delta_time(time,scanline,ground_pixel), float32 formaldehyde_tropospheric_vertical_column(time,scanline,ground_pixel), uint8 qa_value(time,scanline,ground_pixel), <class 'str'> time_utc(time,scanline), int32 scanline(scanline), int32 ground_pixel(ground_pixel)
    groups: SUPPORT_DATA
In[31]: ns['PRODUCT'].variables.keys()
Out[31]: dict_keys(['time_idx', 'scanline_idx', 'ground_pixel_idx', 'longitude', 'latitude', 'time', 'delta_time', 'formaldehyde_tropospheric_vertical_column', 'qa_value', 'time_utc', 'scanline', 'ground_pixel'])

(2) xarray的讀取結(jié)果:

xs
Out[34]: 
<xarray.Dataset>
Dimensions:                                    (ground_pixel: 237, scanline: 725, time: 1)
Coordinates:
  * time                                       (time) datetime64[ns] 2018-05-14
  * scanline                                   (scanline) float64 1.507e+03 ....
  * ground_pixel                               (ground_pixel) float64 1.0 ......
Data variables:
    time_idx                                   (time) float32 0.0
    scanline_idx                               (scanline) float32 1.506e+03 ....
    ground_pixel_idx                           (ground_pixel) float32 0.0 ......
    longitude                                  (time, scanline, ground_pixel) float32 ...
    latitude                                   (time, scanline, ground_pixel) float32 ...
    delta_time                                 (time, scanline, ground_pixel) timedelta64[ns] ...
    formaldehyde_tropospheric_vertical_column  (time, scanline, ground_pixel) float32 ...
    qa_value                                   (time, scanline, ground_pixel) float32 ...
    time_utc                                   (time, scanline) object nan .....

不足使用xarray讀取含Groups的嵌套文件如.nc4時(shí)

需要先知道其所在的Gropus名稱(chēng),即需要先用panoly軟件或nc4包打開(kāi)。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python pymsql模塊的使用

    Python pymsql模塊的使用

    這篇文章主要介紹了Python pymsql模塊的使用,幫助大家我們利用 python 語(yǔ)言與 mysql 進(jìn)行鏈接,感興趣的朋友可以了解下
    2020-09-09
  • django利用request id便于定位及給日志加上request_id

    django利用request id便于定位及給日志加上request_id

    這篇文章主要介紹了django利用request id便于定位及給日志加上request_id的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧
    2018-08-08
  • python的運(yùn)算符與表達(dá)式你真的了解嗎

    python的運(yùn)算符與表達(dá)式你真的了解嗎

    這篇文章主要為大家介紹了python的運(yùn)算符與表達(dá)式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • python使用PIL模塊實(shí)現(xiàn)給圖片打水印的方法

    python使用PIL模塊實(shí)現(xiàn)給圖片打水印的方法

    這篇文章主要介紹了python使用PIL模塊實(shí)現(xiàn)給圖片打水印的方法,涉及使用PIL模塊操作圖片的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • python tqdm庫(kù)的使用

    python tqdm庫(kù)的使用

    這篇文章主要介紹了python tqdm庫(kù)的使用,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-11-11
  • Python max函數(shù)中key的用法及原理解析

    Python max函數(shù)中key的用法及原理解析

    最近有童鞋向小編求助怎么樣找到字符串中出現(xiàn)字?jǐn)?shù)最多的字符呢,其實(shí)最簡(jiǎn)單的處理方法是使用max函數(shù),max()函數(shù)用于獲得給定的可迭代對(duì)象中的最大值,關(guān)于Python max函數(shù)key用法跟隨小編一起通過(guò)本文學(xué)習(xí)下吧
    2021-06-06
  • Python實(shí)現(xiàn)按照指定要求逆序輸出一個(gè)數(shù)字的方法

    Python實(shí)現(xiàn)按照指定要求逆序輸出一個(gè)數(shù)字的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)按照指定要求逆序輸出一個(gè)數(shù)字的方法,涉及Python針對(duì)字符串的遍歷、判斷、輸出等相關(guān)操作技巧,需要的朋友可以參考下
    2018-04-04
  • Python讀寫(xiě)Redis緩存數(shù)據(jù)庫(kù)的入門(mén)指南

    Python讀寫(xiě)Redis緩存數(shù)據(jù)庫(kù)的入門(mén)指南

    很多學(xué)習(xí) Python 的小伙伴,在學(xué)完文件讀寫(xiě)、SQLite、MySQL 之后,都會(huì)接觸到一個(gè)新的數(shù)據(jù)庫(kù):Redis,這篇文章會(huì)先簡(jiǎn)單介紹 Redis 是什么,然后通過(guò) Python 代碼演示如何連接 Redis,并完成常見(jiàn)的讀寫(xiě)操作,需要的朋友可以參考下
    2026-05-05
  • python selenium操作cookie的實(shí)現(xiàn)

    python selenium操作cookie的實(shí)現(xiàn)

    這篇文章主要介紹了python selenium操作cookie的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • django框架自定義模板標(biāo)簽(template tag)操作示例

    django框架自定義模板標(biāo)簽(template tag)操作示例

    這篇文章主要介紹了django框架自定義模板標(biāo)簽(template tag)操作,結(jié)合實(shí)例形式分析了Django框架自定義模板標(biāo)簽原理、操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-06-06

最新評(píng)論

孝感市| 元江| 射阳县| 洮南市| 东乡族自治县| 田林县| 盖州市| 长沙县| 囊谦县| 青龙| 昆山市| 修文县| 平乡县| 樟树市| 资兴市| 都匀市| 安阳市| 兴化市| 桦川县| 上饶县| 竹山县| 葫芦岛市| 惠来县| 浦北县| 五莲县| 宜兴市| 深圳市| 旌德县| 天气| 斗六市| 三原县| 富顺县| 遂平县| 娱乐| 永宁县| 阜新市| 鹿邑县| 甘洛县| 新疆| 天峻县| 杨浦区|