|
1 | 1 | import cartopy.crs as ccrs |
2 | | -import genutil |
3 | 2 | import matplotlib.pyplot as plt |
4 | | -import MV2 |
| 3 | +import numpy as np |
5 | 4 |
|
6 | 5 |
|
7 | 6 | def model_land_only(model, model_timeseries, lf, debug=False): |
8 | 7 | # ------------------------------------------------- |
9 | 8 | # Mask out over ocean grid |
10 | 9 | # - - - - - - - - - - - - - - - - - - - - - - - - - |
| 10 | + |
11 | 11 | if debug: |
12 | | - plot_map(model_timeseries[0], "_".join(["test", model, "beforeMask.png"])) |
| 12 | + # plot_map(model_timeseries[0], "_".join(["test", model, "beforeMask.png"])) |
13 | 13 | print("debug: plot for beforeMask done") |
14 | 14 |
|
15 | 15 | # Check land fraction variable to see if it meet criteria |
16 | 16 | # (0 for ocean, 100 for land, no missing value) |
17 | | - lat_c = lf.getAxis(0) |
18 | | - lon_c = lf.getAxis(1) |
19 | | - lf_id = lf.id |
20 | | - |
21 | | - lf = MV2.array(lf.filled(0.0)) |
22 | | - |
23 | | - lf.setAxis(0, lat_c) |
24 | | - lf.setAxis(1, lon_c) |
25 | | - lf.id = lf_id |
26 | 17 |
|
27 | | - if float(MV2.max(lf)) == 1.0: |
28 | | - lf = MV2.multiply(lf, 100.0) |
29 | | - |
30 | | - # Matching dimension |
31 | | - if debug: |
32 | | - print("debug: match dimension in model_land_only") |
33 | | - model_timeseries, lf_timeConst = genutil.grower(model_timeseries, lf) |
34 | | - |
35 | | - # Conserve axes |
36 | | - time_c = model_timeseries.getAxis(0) |
37 | | - lat_c2 = model_timeseries.getAxis(1) |
38 | | - lon_c2 = model_timeseries.getAxis(2) |
| 18 | + if np.max(lf) == 1.0: |
| 19 | + lf = lf * 100.0 |
39 | 20 |
|
40 | 21 | opt1 = False |
41 | 22 |
|
42 | 23 | if opt1: # Masking out partial ocean grids as well |
43 | 24 | # Mask out ocean even fractional (leave only pure ocean grid) |
44 | | - model_timeseries_masked = MV2.masked_where(lf_timeConst < 100, model_timeseries) |
| 25 | + model_timeseries_masked = model_timeseries.where(lf > 0 & lf < 100) |
| 26 | + |
45 | 27 | else: # Mask out only full ocean grid & use weighting for partial ocean grid |
46 | | - model_timeseries_masked = MV2.masked_where( |
47 | | - lf_timeConst == 0, model_timeseries |
48 | | - ) # mask out pure ocean grids |
| 28 | + model_timeseries_masked = model_timeseries.where(lf > 0) |
| 29 | + |
49 | 30 | if model == "EC-EARTH": |
50 | 31 | # Mask out over 90% land grids for models those consider river as |
51 | 32 | # part of land-sea fraction. So far only 'EC-EARTH' does.. |
52 | | - model_timeseries_masked = MV2.masked_where( |
53 | | - lf_timeConst < 90, model_timeseries |
54 | | - ) |
55 | | - lf2 = MV2.divide(lf, 100.0) |
56 | | - model_timeseries, lf2_timeConst = genutil.grower( |
57 | | - model_timeseries, lf2 |
58 | | - ) # Matching dimension |
59 | | - model_timeseries_masked = MV2.multiply( |
60 | | - model_timeseries_masked, lf2_timeConst |
61 | | - ) # consider land fraction like as weighting |
62 | | - |
63 | | - # Make sure to have consistent axes |
64 | | - model_timeseries_masked.setAxis(0, time_c) |
65 | | - model_timeseries_masked.setAxis(1, lat_c2) |
66 | | - model_timeseries_masked.setAxis(2, lon_c2) |
| 33 | + model_timeseries_masked = model_timeseries.where(lf > 90) |
67 | 34 |
|
68 | 35 | if debug: |
69 | | - plot_map(model_timeseries_masked[0], "_".join(["test", model, "afterMask.png"])) |
| 36 | + # plot_map(model_timeseries_masked[0], "_".join(["test", model, "afterMask.png"])) |
70 | 37 | print("debug: plot for afterMask done") |
71 | 38 |
|
72 | 39 | return model_timeseries_masked |
73 | 40 |
|
74 | 41 |
|
75 | 42 | def plot_map(data, filename): |
76 | | - lons = data.getLongitude() |
77 | | - lats = data.getLatitude() |
| 43 | + lons = data["lon"] |
| 44 | + lats = data["lat"] |
78 | 45 | ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180)) |
79 | 46 | ax.contourf(lons, lats, data, transform=ccrs.PlateCarree(), cmap="viridis") |
80 | 47 | ax.coastlines() |
|
0 commit comments