Skip to content

Commit

Permalink
Update read_data
Browse files Browse the repository at this point in the history
  • Loading branch information
mengqi-z committed Aug 31, 2023
1 parent e77dc42 commit 8055d8d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
42 changes: 42 additions & 0 deletions bed/read_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import pandas as pd
import xarray as xr
import numpy as np
import os as os
from bed.read_config import read_config

Expand Down Expand Up @@ -65,3 +66,44 @@ def __init__(self, config_file=''):
self.population = xr.open_dataset(self.config['path_population_ncdf'])

logging.info('Class Data inside module read_data completed.')

@staticmethod
def set_global_coords(ds, resolution):
offset = resolution / 2
ds['lat'] = np.linspace(90 - offset, -90 + offset, round(180 / resolution))
ds['lon'] = np.linspace(-180 + offset, 180 - offset, round(360 / resolution))

return ds

@staticmethod
def regrid(ds, target_resolution, method='extensive'):
"""Simple regridding algorithm
:param ds: xarray Dataset or DataArray, needs lat and lon and global extent
:param target_resolution: target resolution in degrees
:param method: choice of 'extensive' (preserves sums, default), 'intensive' (take average), or 'label' (for maps)
:return: ds regridded to target_resolution
"""

target_lat_size = round(180 / target_resolution)

lcm = np.lcm(ds.lat.size, target_lat_size)
r = lcm // ds.lat.size
s = lcm // target_lat_size

if method == 'label':
ds = ds.isel(lon=np.arange(ds.lon.size).repeat(r)).coarsen(lon=s, boundary='pad').max()
ds = ds.isel(lat=np.arange(ds.lat.size).repeat(r)).coarsen(lat=s, boundary='pad').max()
else:
ds = ds.isel(lon=np.arange(ds.lon.size).repeat(r)).coarsen(lon=s, boundary='pad').sum()
ds = ds.isel(lat=np.arange(ds.lat.size).repeat(r)).coarsen(lat=s, boundary='pad').sum()
if method == 'extensive':
ds = ds / (r * r) # preserve original sum
elif method == 'intensive':
ds = ds / (s * s) # take average

# set coordinates
ds = set_global_coords(ds, target_resolution)

return ds

6 changes: 5 additions & 1 deletion extras/dev_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
data = bed.Data(config_file=os.path.join(data_folder, "example_config.yml"))
data.example_dataset
data.temperature
data.population
ds=data.population
target_resolution=0.5
method='extensive'

regrid_ds = data.regrid(ds=data.population, target_resolution=0.5, method='extensive')

# Run Diagnsotics
bed.diagnostics(data=data)
Expand Down

0 comments on commit 8055d8d

Please sign in to comment.