Saving hourly average text file

Issue

This Content is from Stack Overflow. Question asked by Samkelo Magwala

Please advise on how to make the script below fast using python. My 16GB PC has been attempting to run for the past four hours.

    import xarray as xr
    import numpy as np
    
    hour_array = np.arange(0,24,3)
    str_hour = ["".join(item) for item in hour_array.astype(str)]
    len_hour_array = len(hour_array)
    
    ds=xr.open_mfdataset('e:/Datasets_ERA5/2m_dewpoint.nc')
    
    lons = ds.variables['longitude'][:]
    lats = ds.variables['latitude'][:]
    
    print('It will take sometime.......reading dew point datan')
    d2m = ds['d2m']# grab variable 2m dew point temperature
    
    d2m_hour=(((d2m.groupby('time.hour').mean(dim='time'))))-273.15
    C = [] # creating an empty array that will store the mean
    
    for day_offset in range(0,len_hour_array):
        month_number = hour_array[day_offset]
        month_name = str_hour[day_offset]
        hour_data = d2m_hour[day_offset,60:69,136:153] 
        C.append(np.mean(np.mean(hour_data)))  
        
    np.savetxt('dew_point_means.txt',C,fmt='%10.2f')



Solution

Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?