Skip to content

Commit

Permalink
pr updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bnb32 committed Apr 30, 2024
1 parent 0d862a6 commit 1a44fd8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ dependencies = [
"NREL-farms>=1.0.4",
"dask>=2022.0",
"google-auth-oauthlib==0.5.3",
"h5netcdf",
"cftime",
"h5netcdf>=1.1.0",
"cftime>=1.6.2",
"matplotlib>=3.1",
"numpy>=1.7.0",
"pandas>=2.0",
Expand Down
53 changes: 25 additions & 28 deletions sup3r/utilities/era_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,10 @@ def download_file(cls, variables, time_dict, area, out_file, level_type,
def process_surface_file(self):
"""Rename variables and convert geopotential to geopotential height."""
tmp_file = self.get_tmp_file(self.surface_file)
with xr.open_dataset(self.surface_file) as ds:
ds = self.convert_z(ds, name='orog')
ds = self.map_vars(ds)
ds.to_netcdf(tmp_file)
with xr.open_dataset(self.surface_file, mode='a') as ds:
new_ds = self.convert_z(ds, name='orog')
new_ds = self.map_vars(new_ds)
new_ds.to_netcdf(tmp_file)
os.system(f'mv {tmp_file} {self.surface_file}')
logger.info(f'Finished processing {self.surface_file}. Moved '
f'{tmp_file} to {self.surface_file}.')
Expand All @@ -348,15 +348,12 @@ def map_vars(self, ds):
Returns
-------
ds : Dataset
new_ds : Dataset
xr.Dataset() object with new variables written.
"""
for old_name in ds.data_vars:
new_name = self.NAME_MAP.get(old_name, old_name)
ds.rename({old_name: new_name})
if 'temperature' in new_name:
ds[new_name] = (ds[new_name].dims,
ds[new_name].values - 273.15)
ds = ds.rename({old_name: new_name})
return ds

def shift_temp(self, ds):
Expand All @@ -372,8 +369,9 @@ def shift_temp(self, ds):
ds : Dataset
"""
for var in ds.data_vars:
if 'temperature' in var:
if 'units' in ds[var].attrs and ds[var].attrs['units'] == 'K':
ds[var] = (ds[var].dims, ds[var].values - 273.15)
ds[var].attrs['units'] = 'C'
return ds

def add_pressure(self, ds):
Expand All @@ -390,16 +388,14 @@ def add_pressure(self, ds):
"""
if ('pressure' in self.variables
and 'pressure' not in ds.data_vars):
tmp = np.zeros(ds['zg'].shape)

if 'number' in ds.dimensions:
tmp[:] = 100 * ds['level'].values[
None, None, :, None, None]
else:
tmp[:] = 100 * ds['level'].values[
None, :, None, None]

ds['pressure'] = (ds['zg'].dims, tmp)
expand_axes = (0, 2, 3)
pres = np.zeros(ds['zg'].values.shape)
if 'number' in ds.dims:
expand_axes = (0, 1, 3, 4)
pres[:] = np.expand_dims(100 * ds['level'].values,
axis=expand_axes)
ds['pressure'] = (ds['zg'].dims, pres)
ds['pressure'].attrs['units'] = 'Pa'
return ds

def convert_z(self, ds, name):
Expand All @@ -417,19 +413,20 @@ def convert_z(self, ds, name):
ds : Dataset
xr.Dataset() object for new file with new height variable written.
"""
ds['z'] = (ds['z'].dims, ds['z'].values / 9.81)
ds.rename({'z': name})
if name not in ds.data_vars:
ds['z'] = (ds['z'].dims, ds['z'].values / 9.81)
ds = ds.rename({'z': name})
return ds

def process_level_file(self):
"""Convert geopotential to geopotential height."""
tmp_file = self.get_tmp_file(self.level_file)
with xr.open_dataset(self.level_file) as ds:
ds = self.convert_z(ds, name='zg')
ds = self.map_vars(ds)
ds = self.shift_temp(ds)
ds = self.add_pressure(ds)
ds.to_netcdf(tmp_file)
with xr.open_dataset(self.level_file, mode='a') as ds:
new_ds = self.convert_z(ds, name='zg')
new_ds = self.map_vars(new_ds)
new_ds = self.shift_temp(new_ds)
new_ds = self.add_pressure(new_ds)
new_ds.to_netcdf(tmp_file)

os.system(f'mv {tmp_file} {self.level_file}')
logger.info(f'Finished processing {self.level_file}. Moved '
Expand Down

0 comments on commit 1a44fd8

Please sign in to comment.