简体   繁体   English

从每日数据中查找每日和每月平均值

[英]Find the daily and monthly mean from daily data

I am very new to python so please bare with me.我对 python 很陌生,所以请与我裸露。

So far I have a loop that identifies my netcdf files within a date range.到目前为止,我有一个循环可以识别日期范围内的 netcdf 文件。 I now need to calculate the daily averages and then the monthly averages for each month and add it to my dataframe so I can plot a time series.我现在需要计算每日平均值,然后计算每个月的月平均值,并将其添加到我的 dataframe 中,这样我就可以 plot 一个时间序列。

Heres my code so far到目前为止,这是我的代码

# !/usr/bin/python
# MODIS LONDON TIMESERIES 

print ('Initiating AQUA MODIS Time Series')

import pandas as pd              
import xarray as xr
from glob import glob
import netCDF4 
import numpy as np
import matplotlib.pyplot as plt
import os

print ('All packages imported')

#list of files 
days = pd.date_range (start='4/7/2002', end='31/12/2002')

#create dataframe
df = final_data = pd.DataFrame(index = days, columns = ['day', 'night'])

print ('Data frame created')

#for loop iterating through %daterange stated in 'days' to find path using string 

for day in days:
    path = "%i/%02d/%02d/ESACCI-LST-L3C-LST-MODISA-LONDON_0.01deg_1DAILY_DAY-%i%02d%02d000000-fv2.00.nc" % (day.year, day.month, day.day, day.year, day.month, day.day)
    print(path)

Welcome to SO, As suggested.欢迎来到 SO,正如建议的那样。 please try to make a minimal reproducible example.请尝试制作一个最小的可重现示例。

If you are able to create an Xarray dataset, here is how to take monthly avearges如果您能够创建 Xarray 数据集,这里是如何获取每月平均值

import xarray as xr

# tutorial dataset with air temperature every 6 hours
ds = xr.tutorial.open_dataset('air_temperature')

# reasamples along time dimension
ds_monthly = ds.resample(time='1MS').mean()

resample() is used for upscaling and downscaling the temporal resolution. resample()用于放大和缩小时间分辨率。 If you are familiar with Pandas, it effectively works the same way.如果您熟悉 Pandas,它的有效工作方式相同。

What resample(time='1MS') means is group along the time and 1MS is the frequency. resample(time='1MS')意思是沿time分组, 1MS是频率。 1MS means sample by 1 month (this is the 1M part) and have the new time vector begin at the start of the month (this is the S part). 1MS表示按 1 个月采样(这是1M部分),新的时间向量从月初开始(这是S部分)。 This is very powerful, you can supply different frequencies, see the Pandas offset documentation这个很强大,可以提供不同的频率,见Pandas偏移文档

.mean() takes the average of the data over our desired frequency. .mean()在我们想要的频率上取数据的平均值。 In this case, each month.在这种情况下,每个月。

You could replace mean() with min() , max() , median() , std() , var() , sum() , and maybe a few others.您可以将mean()替换为min()max()median()std()var()sum() ,也许还有其他一些。

Xarray has wonderful documentation, the resample() doc is here Xarray 有很棒的文档, resample()文档在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM