微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何正确地将日期坐标添加到 xarray 数据集?

如何解决如何正确地将日期坐标添加到 xarray 数据集?

在连接给定路径中的所有文件之前,我正在尝试将日期坐标添加到带有 .assign_coords 的 xarray 数据集。日期坐标已创建但似乎为空,并且输出中坐标名称(日期)前没有“*”。


import time,glob
import xarray as xr
import datetime

def without_shutil_text(outfilename):
    with open(outfilename,'wb') as outfile:
        for filename in glob.glob('data/pm25/*.nc'):
            datefromfile = datetime.datetime.strptime(filename.replace("pm25-","").replace(".nc","").replace('data/pm25/',""),'%Y-%m-%d')
            pm25 = xr.open_dataset(filename)
            pm25 = pm25.assign_coords(date= datefromfile)
            pm25.to_netcdf(filename)
            if filename == outfilename:
                # don't want to copy the output into the output
                continue
            with open(filename,'rb') as readfile:
                outfile.write(readfile.read())

without_shutil_text("pm25_history.nc")

print('PM2.5 ... ',flush=True,end='')
filePath = ''
fileName = 'pm25_history.nc'
pm25 = xr.open_dataset(filePath + fileName)
pm25 = pm25.to_array()
pm25.coords['longitude'] = (pm25.coords['longitude'] + 180) % 360 - 180
pm25 = pm25.sortby('longitude')
pm25 = pm25.sel(longitude=slice(-10,10),latitude=slice(55,40))
#pm25norm = pm25.where(pm25>10,0).where(pm25<20,1).where(pm25<10,(pm25/10 - 1))
#pm25 = pm25norm.where(pm25norm<1,0)
print(pm25)
print('OK')

PM2.5 ... <xarray.DataArray (variable: 1,time: 24,level: 1,latitude: 100,longitude: 158)>
array([[[[[4.5350966,4.5958767,4.6632857,...,6.702124,7.0276732,8.131445 ],[4.6928015,4.758534,4.843529,6.9492154,7.185144,7.130084 ],[4.839621,4.927039,5.012674,6.2448115,6.1034846,6.13644  ],[3.2745795,3.5045824,3.7060852,6.8108654,6.8777065,6.7333384],[3.1939542,3.3333344,3.5655682,6.812379,6.892699,6.811207 ],[3.1184592,3.046879,3.202964,6.964215,6.769398,6.75312  ]]],[[[4.6594677,4.5774426,4.5025797,8.23678,7.826875,8.875352 ],[4.426857,4.3476033,4.2860346,8.031895,8.451691,9.436084 ],[4.1985455,4.1203647,4.0627465,8.417045,7.7898774,...
           7.2280064],[6.678508,6.415927,6.235769,7.821281,8.440299,7.010445 ],[6.5406556,6.2740316,6.135227,8.64421,7.198007,6.6256366]]],[[[2.2447047,2.3231416,2.4718723,2.9237278,3.0408819,3.3561144],[2.2178319,2.2976046,2.4023883,3.1011004,3.2608235,3.638967 ],[2.2159915,2.2867048,2.3532615,3.3505223,3.5799067,3.627421 ],[6.777079,8.2639675,8.593496,7.7920113,7.4600954,7.211704 ],[6.817502,7.4405837,8.580365,8.498575,7.0680676,6.8567595],[6.6477675,7.1301904,7.5420637,8.390373,7.0645647,7.684932 ]]]]],dtype=float32)
Coordinates:
  * longitude  (longitude) float32 -5.75 -5.65 -5.55 -5.45 ... 9.75 9.85 9.95
  * latitude   (latitude) float32 51.65 51.55 51.45 51.35 ... 41.95 41.85 41.75
  * level      (level) float32 0.0
  * time       (time) timedelta64[ns] 0 days 00:00:00 ... 3 days 20:00:00
    date       datetime64[ns] ...
  * variable   (variable) <U10 'pm2p5_conc'
Attributes:
    title:        PM25 Air Pollutant FORECAST at the Surface
    institution:  Data produced by Meteo France
    source:       Data from ENSEMBLE model
    history:      Model ENSEMBLE FORECAST
    FORECAST:     Europe,20200718+[0H_92H]
    summary:      ENSEMBLE model hourly FORECAST of PM25 concentration at the...
    project:      MACC-RAQ (http://macc-raq.gmes-atmosphere.eu)

所以日期坐标出现在上面的键列表中,但不在输出的 xarray 中...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。