Home > Software engineering >  How to average a 3-D Array Into 2-D Array Python
How to average a 3-D Array Into 2-D Array Python

Time:01-26

I would like to take a temperature variable from a netcdf file in python and average over all of the satellite's scans.

The temperature variable is given as:

tdisk  = file.variables['tdisk'][:,:,:]               # Disk Temp(nscans,nlons,nlats)

The shape of the tdisk array is 68,52,46. The satellite makes 68 scans per day. The longitude and latitude variables are given as:

lats   = file.variables['latitude'][:,:]              # Latitude(nlons,nlats)
lons   = file.variables['longitude'][:,:]             # Longitude(nlons,nlats)

Which have sizes of 52,46. I would like to average the each nscan of temperature together to get a daily mean so the temperature size becomes 52,46. I've seen ways to stack the arrays and concatenate them, but I would like a mean value. Eventually I am looking to make a contour plot with (x=longitude, y=latitude , and z=temp)

Is this possible to do? Thanks for any help

CodePudding user response:

If you are using Xarray, you can do this using DataArray.mean:

import xarray as xr


# open netcdf file
ds = xr.open_dataset('file.nc')

# take the mean of the tdisk variable
da = ds['tdisk'].mean(dim='nscans')

# make a contour plot
da.plot.contour('longitude', 'latitude')

CodePudding user response:

Based on the question you seem to want to calculate a temporal mean, not a daily mean as you seem to only have one day. So the following will probably work:

ds.mean(“time”)
  • Related