Skip to content

Commit

Permalink
fix assignments and add attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ninarobbins committed Sep 25, 2024
1 parent 9408579 commit 272106c
Showing 1 changed file with 51 additions and 21 deletions.
72 changes: 51 additions & 21 deletions pydropsonde/circles.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,18 @@ def get_div_and_vor(self):
D = self.circle_ds.dudx + self.circle_ds.dvdy
vor = self.circle_ds.dvdx - self.circle_ds.dudy

vor_attrs = {
"standard_name": "atmosphere_relative_vorticity",
"long_name": "Area-averaged horizontal relative vorticity",
"units": str(1 / units.second),
}
D_attrs = {
"standard_name": "divergence_of_wind",
"long_name": "Area-averaged horizontal mass divergence",
"units": str(1 / units.second),
}
self.circle_ds = self.circle_ds.assign(
dict(div=(["alt"], D.values), vor=(["alt"], vor.values))
dict(D=(["alt"], D.values, D_attrs), vor=(["alt"], vor.values, vor_attrs))
)
return self

Expand All @@ -43,20 +53,26 @@ def get_density(self, sonde_dim="sonde_id"):
self.circle_ds.ta.values * units.kelvin,
mr,
)
self.circle_ds = self.circle_ds.assign(
dict(density=(self.circle_ds.ta.dims, density.magnitude))
)
self.circle_ds["density"].attrs = {
"standard_name": "density",
density_attrs = {
"standard_name": "air_density",
"long_name": "Air density",
"units": str(density.units),
}
self.circle_ds = self.circle_ds.assign(
dict(mean_density=self.circle_ds["density"].mean(sonde_dim))
)
self.circle_ds["mean_density"].attrs = {
"standard_name": "mean density",
mean_density_attrs = {
"standard_name": "mean_air_density",
"long_name": "Mean air density in circle",
"units": str(density.units),
}
self.circle_ds = self.circle_ds.assign(
dict(
density=(self.circle_ds.ta.dims, density.magnitude, density_attrs),
mean_density=(
["alt"],
self.circle_ds.density.mean(sonde_dim),
mean_density_attrs,
),
)
)

return self

Expand All @@ -72,24 +88,38 @@ def get_vertical_velocity(self):
del_w = -div * height_diff.values

w_vel = del_w.cumsum(dim="alt")
self.circle_ds = self.circle_ds.assign(dict(w_vel=w_vel))
self.circle_ds["w_vel"].attrs = {
"standard name": "vertical velocity",
w_vel_attrs = {
"standard_name": "upward_air_velocity",
"long_name": "Area-averaged vertical air velocity",
"units": str(units.meter / units.second),
}
self.circle_ds = self.circle_ds.assign(
w_vel=(["alt"], w_vel.values, w_vel_attrs)
)

return self

def get_omega(self):
p_vel = (
-self.circle_ds.mean_density.values
* units(self.circle_ds.mean_density.attrs["units"])
* self.circle_ds.w_vel.values
* units(self.circle_ds.w_vel.attrs["units"])
* mpconst.earth_gravity
# Ensure density and vertical velocity are properly aligned and use correct units
rho = self.circle_ds.mean_density * units(
self.circle_ds.mean_density.attrs["units"]
)
w = self.circle_ds.w_vel * units(self.circle_ds.w_vel.attrs["units"])

# Calculate omega using the correct formula: omega = - rho * g * w
g = mpconst.earth_gravity # gravitational constant (metpy constant)

# Compute omega (vertical pressure velocity)
p_vel = -(rho * g * w)

# Assign omega to the dataset with correct attributes
omega_attrs = {
"standard_name": "atmosphere_vertical_velocity",
"long_name": "Area-averaged atmospheric pressure velocity",
"units": str(p_vel.units),
}
self.circle_ds = self.circle_ds.assign(
dict(omega=(self.circle_ds.w_vel.dims, p_vel.magnitude))
omega=(self.circle_ds.w_vel.dims, p_vel.magnitude, omega_attrs)
)

return self

0 comments on commit 272106c

Please sign in to comment.