Vektorisieren von drei verschachtelten Schleifen, die den Tagesmittelwert der Stundendaten berechnenPython

Python-Programme
Guest
 Vektorisieren von drei verschachtelten Schleifen, die den Tagesmittelwert der Stundendaten berechnen

Post by Guest »

Gibt es eine Möglichkeit, die folgende dreifach verschachtelte Schleife zu vektorisieren, die den Tagesmittelwert der stündlichen Daten berechnet? Die folgende Funktion führt zunächst eine Schleife über das Jahr, dann über die Monate und schließlich über die Tage durch. Außerdem wird der letzte Monat und Tag überprüft, um sicherzustellen, dass die Schleife nicht über den letzten Monat oder Tag der Daten hinausgeht.

Code: Select all

def hourly2daily(my_var,my_periods):

import pandas as pd
import numpy as np
import sys

print('######### Daily2monthly function ##################')
Frs_year   =my_periods[0].year
Frs_month  =my_periods[0].month
Frs_day    =my_periods[0].day
Frs_hour   =my_periods[0].hour

Last_year  =my_periods[-1].year
Last_month =my_periods[-1].month
Last_day   =my_periods[-1].day
Last_hour  =my_periods[-1].hour

print('First year   is '+str(Frs_year) +'\n'+\
'First months is '+str(Frs_month)+'\n'+\
'First day    is '+str(Frs_day)+'\n'+\
'First hour   is '+str(Frs_hour))
print('        ')

print('Last  year   is '+str(Last_year)+'\n'+\
'Last  months is '+str(Last_month)+'\n'+\
'Last  day    is '+str(Last_day)+'\n'+\
'Last  hour   is '+str(Last_hour))

Frs = str(Frs_year)+'/'+str(Frs_month)+'/'+str(Frs_day)+' '+str(Frs_hour)+":00"

Lst = str(Last_year)+'/'+str(Last_month)+'/'+str(Last_day)+' '+str(Last_hour)+":00"

my_daily_time=pd.date_range(Frs,Lst,freq='D')

## END of the data_range tricks ###########

nt_days=len(my_daily_time)
nd=np.ndim(my_var)

if (nd == 1): # only time series
var_mean=np.full((nt_days),np.nan)

if (nd == 2): # e.g., time, lat or lon or lev
n1=np.shape(my_var)[1]
var_mean=np.full((nt_days,n1),np.nan)

if (nd == 3): #  e.g., time, lat, lon
n1=np.shape(my_var)[1]
n2=np.shape(my_var)[2]
var_mean=np.full((nt_days,n1,n2),np.nan)

if (nd == 4): # e.g., time, lat , lon, lev
n1=np.shape(my_var)[1]
n2=np.shape(my_var)[2]
n3=np.shape(my_var)[3]
var_mean=np.full((nt_days,n1,n2,n3),np.nan)

end_mm=12
k=0
####### loop over years ################
for yy in np.arange(Frs_year,Last_year+1):
print('working on the '+str(yy))
# in case the last month is NOT 12
if (yy == Last_year):
end_mm=Last_month
print('The last month is '+str(end_mm))
## Loop over months ################
for mm in np.arange(1,end_mm+1):
end_day=pd.Period(str(yy)+'-'+str(mm)).days_in_month
# in case the last day is not at the end of the month.
if ((yy == Last_year) & (mm == Last_month)):
end_day=Last_day
#### loop over days ###############
for dd in np.arange(1,end_day+1):
print(str(yy)+'-'+str(mm)+'-'+str(dd))
#list all days of the month and year.
I=np.where((my_periods.year ==  yy) &\
(my_periods.month == mm) &\
(my_periods.day == dd  ))[0]

print(I)
# if there is a discontinuity in time.
# I will be empty and then you have to quit.
# you have first to reindex the data.
if len(I) == 0 :
print('Warning time shift here >>')
print('Check the continuity of your time sequence')
sys.exit()

var_mean[k,...]=np.nanmean(my_var[I,...],0)
k=k+1

return var_mean,my_daily_time
Hier ist vielleicht eine einfache und schnelle Möglichkeit, diese Funktion aufzurufen.
Beachten Sie, dass Sie möglicherweise aufgefordert werden, Pooch zu installieren

Code: Select all

import numpy as np
import xarray as xr

x = xr.tutorial.load_dataset("air_temperature")
time      = x['time']      # reading the time
period=time.to_index().to_period('h')
bb0,bb1=hourly2daily(x['air'],period)
Mir ist bewusst, dass es eine andere Möglichkeit gibt, dies umzusetzen; Ich kann zum Beispiel die vorherige Berechnung in einer einzigen Schleife durchführen, wie unten gezeigt, aber es hilft nicht für Daten mit zeitlichen Unterbrechungen.

Code: Select all

daily_tem2m = np.full((int(len_time/24),len_lat,len_lon),np.nan,float)

counter=0
timemm=[]
for i in np.arange(0,len_time,24):
print(period[i])
timemm.append(period[i])
daily_tem2m[counter,:,:]=np.nanmean(cleaned_tem2m_celsius.data[i:i+24,:,:],0)
counter=counter+1

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post