Conditions | 8 |
Total Lines | 81 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Tests | 16 |
CRAP Score | 8.512 |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | # -*- coding: utf-8 -*- |
||
63 | 1 | def bin_lat_timeavg(ds, binvar="latitude", tvar="time", |
|
64 | bins=np.r_[-90:91:5], labels=None, area_weighted=True, |
||
65 | keep_attrs=True, |
||
66 | load=True, save_progress=False): |
||
67 | """Latitudinally bin and time average xarray dataset(s) |
||
68 | |||
69 | Time-averages the variables in an :class:`xarray.Dataset` in the given |
||
70 | latitude bins. This should be applied to daily-binned datasets from |
||
71 | a groupby object (via .apply()) to yield daily zonal means. |
||
72 | |||
73 | The type of latitudes is selected by passing the appropriate |
||
74 | `binvar` and must be a variable in the data set. |
||
75 | Area weighting (cos(latitude)) is also supported. |
||
76 | |||
77 | Parameters |
||
78 | ---------- |
||
79 | ds : xarray.Dataset or xarray.DatasetGroupBy instance |
||
80 | The dataset (or GroupBy) instance to bin latitudinally. |
||
81 | binvar : str |
||
82 | The name of the variable used for binning, default: "latitude". |
||
83 | tvar : str |
||
84 | The name of the time variable of the GroupBy object, |
||
85 | default: "time". |
||
86 | bins : numpy.ndarray |
||
87 | The (latitudinal) bin edges, default: `np.r_[-90:91:5]`. |
||
88 | labels : list or None |
||
89 | The bin labels, if set to `None` (the default), the labels are |
||
90 | set to the central bin values. |
||
91 | area_weighted : bool |
||
92 | Use area weighted averages, default: `True`. |
||
93 | keep_attrs : bool |
||
94 | Keep the global and variable attributes from the data set, |
||
95 | default: `True`. |
||
96 | load : bool |
||
97 | Loads the data into memory before binning, speeds it up considerably |
||
98 | provided that the it fits into memory, default: `True` |
||
99 | save_progress : bool |
||
100 | Saves the individual binned files to netcdf, to enable recovering from |
||
101 | interrupted runs, default: `False` |
||
102 | |||
103 | Returns |
||
104 | ------- |
||
105 | ds : xarray.Dataset |
||
106 | The binned and time-averaged dataset together with the (unbiased) |
||
107 | standard deviations of the variables as `<variable>_std` and the |
||
108 | number of averaged values as `<variable>_cnt`. |
||
109 | """ |
||
110 | 1 | if load: |
|
111 | # load the chunk into memory to speed up binning |
||
112 | 1 | ds.load() |
|
113 | # adjust the time variable |
||
114 | 1 | if np.issubdtype(ds[tvar].values[0], np.floating): |
|
115 | # convert floats to datetime first (probably MLT states) |
||
116 | 1 | date = ( |
|
117 | xr.conventions.decode_cf(ds[[tvar]])[tvar] |
||
118 | .values[0] |
||
119 | .astype("datetime64[D]") |
||
120 | ) |
||
121 | else: |
||
122 | date = ds[tvar].values[0].astype('datetime64[D]') |
||
123 | 1 | if not hasattr(ds, binvar): |
|
124 | # nothing to bin |
||
125 | logging.warn("skipping %s", date) |
||
126 | return ds |
||
127 | 1 | logging.info("processing %s", date) |
|
128 | 1 | if labels is None: |
|
129 | 1 | labels = 0.5 * (bins[1:] + bins[:-1]) |
|
130 | # stack and bin and delegate to the statistics helper function |
||
131 | 1 | ds_out = (ds.stack(stacked_time_latitude=("time", "latitude")) |
|
132 | .groupby_bins(binvar, bins, labels=labels) |
||
133 | .apply(_bin_stats, |
||
134 | binvar=binvar, tvar=tvar, |
||
135 | area_weighted=area_weighted, |
||
136 | set_attrs=keep_attrs)) |
||
137 | 1 | if keep_attrs: |
|
138 | 1 | ds_out.attrs = ds.attrs |
|
139 | 1 | for var in ds.data_vars: |
|
140 | 1 | ds_out[var].attrs = ds[var].attrs |
|
141 | 1 | if save_progress: |
|
142 | ds_out.to_netcdf("tmp_binavg-{0}.nc".format(date)) |
||
143 | return ds_out |
||
144 |