|
2 | 2 | import pandas as pd
|
3 | 3 |
|
4 | 4 |
|
5 |
| -def time_since_storm(precipitation, perc_snow, time_step=1/24, mass=1.0, |
6 |
| - time=4, stormDays=None, stormPrecip=None, ps_thresh=0.5): |
| 5 | +def time_since_storm( |
| 6 | + precipitation, |
| 7 | + percent_snow_precipitation, |
| 8 | + storm_days, |
| 9 | + storm_precip, |
| 10 | + time_step, |
| 11 | + mass_threshold=1.0, |
| 12 | + percent_snow_threshold=0.5 |
| 13 | +): |
7 | 14 | """
|
8 |
| - Calculate the decimal days since the last storm given a precip time series, |
9 |
| - percent snow, mass threshold, and time threshold |
| 15 | + Increase or reset the given storm days and storm precipitation based of |
| 16 | + given percent snow and mass threshold. |
10 | 17 |
|
11 |
| - - Will look for pixels where perc_snow > 50% as storm locations |
12 |
| - - A new storm will start if the mass at the pixel has exceeded the mass |
13 |
| - limit, this ensures that the enough has accumulated |
| 18 | + Steps: |
| 19 | + - Look for pixels where percent snow precipitation is above the threshold. |
| 20 | + - Reset storm days if the precipitation at the pixel is above the mass |
| 21 | + threshold or increase the counter if it is below. |
| 22 | + - Add the precipitation amount to the storm precipitation. |
| 23 | +
|
| 24 | + *NOTE*: Storm day precipitation is initialized and set to 0 with each |
| 25 | + processed day. Hence, no cross-day storm tracking is possible if the |
| 26 | + period between the days falls below the threshold. |
14 | 27 |
|
15 | 28 | Args:
|
16 | 29 | precipitation: Precipitation values
|
17 |
| - perc_snow: Percent of precipitation that was snow |
| 30 | + percent_snow_precipitation: Percent of precipitation that was snow |
| 31 | + storm_days: Storm days to keep track of |
| 32 | + storm_precip: Keeps track of the total storm precip |
18 | 33 | time_step: Step in days of the model run
|
19 |
| - mass: Threshold for the mass to start a new storm |
20 |
| - time: Threshold for the time to start a new storm |
21 |
| - stormDays: If specified, this is the output from a previous run of |
22 |
| - storms else it will be set to the date_time value |
23 |
| - stormPrecip: Keeps track of the total storm precip |
| 34 | + (Optional) |
| 35 | + mass_threshold: Minimum amount of precipitation required to be a storm |
| 36 | + (snow mass). Default: 0.5 |
| 37 | + percent_snow_threshold: Minimum fraction for values in |
| 38 | + `percent_snow_precipitation` to be considered |
| 39 | + a snow event. Default: 0.5 (50%) |
24 | 40 |
|
25 | 41 | Returns:
|
26 | 42 | tuple:
|
27 |
| - - **stormDays** - Array representing the days since the last storm at |
28 |
| - a pixel |
29 |
| - - **stormPrecip** - Array representing the precip accumulated during |
30 |
| - the most recent storm |
| 43 | + - **stormDays** - Updated storm days |
| 44 | + - **stormPrecip** - Updated storm precipitation |
31 | 45 |
|
32 |
| - Created Janurary 5, 2016 |
| 46 | + Created January 5, 2016 |
33 | 47 | @author: Scott Havens
|
34 |
| - """ |
35 |
| - # either preallocate or use the input |
36 |
| - if stormDays is None: |
37 |
| - stormDays = np.zeros(precipitation.shape) |
38 |
| - |
39 |
| - if stormPrecip is None: |
40 |
| - stormPrecip = np.zeros(precipitation.shape) |
41 |
| - |
42 |
| - # if there is no snow, don't reset the counter |
43 |
| - # This ensures that the albedo won't be reset |
44 |
| - stormDays += 1 |
45 |
| - if np.sum(perc_snow) == 0: |
46 |
| - # stormDays = np.add(stormDays, 1) |
47 |
| - stormPrecip = np.zeros(precipitation.shape) |
48 |
| - return stormDays, stormPrecip |
49 | 48 |
|
50 |
| - # determine locations where it has snowed |
51 |
| - idx = perc_snow >= ps_thresh |
52 |
| - |
53 |
| - # determine locations where the time threshold has passed |
54 |
| - # these areas, the stormPrecip will be set back to zero |
55 |
| - idx_time = stormDays >= time |
56 |
| - stormPrecip[idx_time] = 0 |
| 49 | + Updated: February 07, 2022 |
| 50 | + @author: Joachim Meyer, Dillon Ragar |
| 51 | + """ |
57 | 52 |
|
58 |
| - # add the values to the stormPrecip |
59 |
| - stormPrecip[idx] = + precipitation[idx] |
| 53 | + # Step 1: Pixels above snow percent threshold |
| 54 | + location_index = (percent_snow_precipitation >= percent_snow_threshold) |
| 55 | + storm_precip[location_index] += precipitation[location_index] |
60 | 56 |
|
61 |
| - # see if the mass threshold has been passed |
62 |
| - idx_mass = stormPrecip >= mass |
| 57 | + # Step 2: Reset locations above mass threshold or increase counter when |
| 58 | + # below |
| 59 | + location_index = (storm_precip >= mass_threshold) |
| 60 | + storm_days[location_index] = 0 |
| 61 | + storm_days[~location_index] += time_step |
63 | 62 |
|
64 |
| - # reset the stormDays to zero where the storm is present |
65 |
| - stormDays[idx_mass] = 0 |
| 63 | + # Step 3: Increase the storm precipitation total for the day |
| 64 | + storm_precip[~location_index] = 0 |
66 | 65 |
|
67 |
| - return stormDays, stormPrecip |
| 66 | + return storm_days, storm_precip |
68 | 67 |
|
69 | 68 |
|
70 | 69 | def time_since_storm_pixel(precipitation, dpt, perc_snow, storming,
|
|
0 commit comments