From 1ceeffdcaa320b6ec6d2f845f357bf3e4cf9faa1 Mon Sep 17 00:00:00 2001 From: jvegasbsc Date: Tue, 12 Mar 2019 11:18:31 +0100 Subject: [PATCH 1/5] First draft of new basins script --- .gitignore | 1 + new_masks/basins.yml | 324 ++++++++++++++++++++++++++++++++ new_masks/create_basins_mask.py | 146 ++++++++++++++ 3 files changed, 471 insertions(+) create mode 100644 new_masks/basins.yml create mode 100644 new_masks/create_basins_mask.py diff --git a/.gitignore b/.gitignore index fff9635..5658818 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .idea *.o *.mod +.vscode diff --git a/new_masks/basins.yml b/new_masks/basins.yml new file mode 100644 index 0000000..5193e3c --- /dev/null +++ b/new_masks/basins.yml @@ -0,0 +1,324 @@ +basins: + Global_Ocean: + add: + - {lat: [-90, 90], lon: [-180, 180]} + + Northern_Hemisphere: + add: + - {lat: [0, 90], lon: [-180, 180]} + + Southern_Hemisphere: + add: + - {lat: [-90, 0], lon: [-180, 180]} + + Antarctic_Ocean: + add: + - {lat: [-90, -40], lon: [-180, 180]} + + Mediterranean_Sea: + add: &Mediterranean_Sea + - {lat: [30, 40], lon: [-5, 0]} + - {lat: [30, 46], lon: [0, 28]} + - {lat: [30, 40], lon: [28, 40]} + + North_Atlantic_Ocean: + add: + - {lat: [32, 73], lon: [-95, 26]} + - {lat: [73, 75], lon: [-30, 24]} + - {lat: [75, 77], lon: [-30, 22]} + - {lat: [77, 79], lon: [-30, 18]} + - {lat: [79, 81], lon: [-30, 17]} + - {lat: [73, 82], lon: [-83, -35]} + remove: *Mediterranean_Sea + + Greenland_Sea: + add: &Greenland_Sea + - {lat: [76, 81], lon: [-30, 17]} + - {lat: [75, 76], lon: [-30, 12]} + - {lat: [74, 75], lon: [-30, 9]} + - {lat: [73, 74], lon: [-30, 4]} + - {lat: [72, 73], lon: [-30, 0]} + - {lat: [71, 72], lon: [-30, -3]} + - {lat: [70, 71], lon: [-30, -8]} + + Icelandic_Sea: + add: &Icelandic_Sea + - {lat: [69, 70], lon: [-29, -9]} + - {lat: [68, 69], lon: [-28, -10]} + - {lat: [67, 68], lon: [-25, -11]} + - {lat: [66, 67], lon: [-23, -12]} + - {lat: [65, 66], lon: [-20, -13]} + + Norwegian_Sea: + add: &Norwegian_Sea + - {lat: [75, 76], lon: [12, 17]} + - {lat: [74, 75], lon: [9, 18]} + - {lat: [73, 74], lon: [4, 19]} + - {lat: [72, 73], lon: [0, 21]} + - {lat: [71, 72], lon: [-3, 23]} + - {lat: [70, 71], lon: [-8, 25]} + - {lat: [66, 70], lon: [-13, 25]} + - {lat: [62, 66], lon: [-6, 15]} + - {lat: [61, 62], lon: [0, 15]} + - {lat: [63, 64], lon: [-8, -6]} + - {lat: [64, 66], lon: [-10, -6]} + - {lat: [65, 66], lon: [-13, -6]} + remove: *Icelandic_Sea + + Barents_Sea: + add: &Barents_Sea + - {lat: [76, 80], lon: [17, 65]} + - {lat: [75, 76], lon: [17, 62]} + - {lat: [74, 75], lon: [18, 59]} + - {lat: [73, 74], lon: [19, 58]} + - {lat: [72, 73], lon: [21, 56]} + - {lat: [71, 72], lon: [23, 56]} + - {lat: [70, 71], lon: [25, 57]} + - {lat: [66, 70], lon: [25, 60]} + - {lat: [63, 66], lon: [30, 45]} + + Kara_Sea: + add: &Kara_Sea + - {lat: [79, 80], lon: [65, 96]} + - {lat: [76, 79], lon: [65, 105]} + - {lat: [75, 76], lon: [62, 100]} + - {lat: [74, 75], lon: [59, 100]} + - {lat: [73, 74], lon: [58, 100]} + - {lat: [71, 73], lon: [56, 100]} + - {lat: [70, 71], lon: [57, 100]} + - {lat: [66, 70], lon: [60, 90]} + + Laptev_Sea: + add: &Laptev_Sea + - {lat: [80, 81], lon: [96, 102]} + - {lat: [79, 80], lon: [100, 111]} + - {lat: [78, 79], lon: [105, 120]} + - {lat: [77, 78], lon: [105, 129]} + - {lat: [76, 77], lon: [100, 138]} + - {lat: [70, 76], lon: [105, 140]} + + remove: *Kara_Sea + + East_Siberian_Sea: + add: &East_Siberian_Sea + - {lat: [74, 75], lon: [140, 146]} + - {lat: [73, 74], lon: [140, 154]} + - {lat: [72, 73], lon: [140, 162]} + - {lat: [71, 72], lon: [140, 170]} + - {lat: [70, 71], lon: [140, 179]} + - {lat: [69, 70], lon: [140, 178]} + - {lat: [68, 69], lon: [140, 178]} + + Chukchi_Sea: + add: &Chukchi_Sea + - {lat: [66, 71], lon: [-180, -156]} + - {lat: [66, 71], lon: [179, 180]} + - {lat: [69, 70], lon: [178, 180]} + + Okhotsk: + add: &Okhotsk + - {lat: [43, 44], lon: [141, 146]} + - {lat: [44, 45], lon: [141, 147]} + - {lat: [45, 46], lon: [141, 150]} + - {lat: [46, 47], lon: [141, 150]} + - {lat: [47, 48], lon: [141, 151]} + - {lat: [48, 49], lon: [141, 152.5]} + - {lat: [49, 50], lon: [141, 154]} + - {lat: [50, 51], lon: [141, 155.5]} + - {lat: [51, 52], lon: [139, 157]} + - {lat: [52, 63], lon: [135, 157]} + - {lat: [58, 63], lon: [157, 163]} + - {lat: [60, 63], lon: [162, 164]} + - {lat: [48, 51], lon: [151, 157]} + + Bering: + add: &Bering + - {lat: [58, 66], lon: [-180, -154]} + - {lat: [55, 66], lon: [161, 180]} + - {lat: [53, 60], lon: [160, 180]} + - {lat: [56, 60], lon: [-180, -155]} + - {lat: [55, 58], lon: [-180, -157]} + - {lat: [55, 58], lon: [163, 180]} + - {lat: [54, 57], lon: [-180, -160]} + - {lat: [54, 57], lon: [167, 180]} + - {lat: [54, 56], lon: [-180, -165]} + - {lat: [54, 56], lon: [171, 180]} + - {lat: [53, 55], lon: [-180, -170]} + - {lat: [53, 55], lon: [174, 180]} + - {lat: [53, 54], lon: [-180, -174]} + - {lat: [53, 54], lon: [177, 180]} + - {lat: [52, 53], lon: [-180, -178]} + remove: *Okhotsk + + Beaufort_Sea: + add: &Beaufort_Sea + - {lat: [75, 76], lon: [-130, -124]} + - {lat: [74, 75], lon: [-136, -124]} + - {lat: [73, 74], lon: [-143, -125]} + - {lat: [72, 73], lon: [-149, -125]} + - {lat: [71, 72], lon: [-156, -126]} + - {lat: [70, 71], lon: [-156, -126]} + - {lat: [69, 70], lon: [-156, -127]} + - {lat: [73, 77], lon: [-124, -120]} + - {lat: [73, 75], lon: [-125, -124]} + - {lat: [69, 73], lon: [-127, -124]} + + Baffin_Bay: + add: &Baffin_Bay + - {lat: [70, 81], lon: [-84, -45]} + - {lat: [66, 70], lon: [-70, -45]} + - {lat: [62, 66], lon: [-66, -45]} + - {lat: [61, 62], lon: [-65, -45]} + + Hudson: + add: &Hudson + - {lat: [50, 67], lon: [-95, -66]} + - {lat: [67, 70], lon: [-85, -70]} + - {lat: [59, 62], lon: [-66, -65]} + + Labrador_Sea: + add: &Labrador_Sea + - {lat: [60, 61], lon: [-65, -45]} + - {lat: [59, 60], lon: [-65, -46]} + - {lat: [58, 59], lon: [-64, -46]} + - {lat: [57, 58], lon: [-63, -47]} + - {lat: [56, 57], lon: [-62, -47]} + - {lat: [55, 56], lon: [-61, -48]} + - {lat: [54, 55], lon: [-60, -48]} + - {lat: [53, 54], lon: [-59, -49]} + - {lat: [51, 53], lon: [-58, -49]} + - {lat: [48.5, 52], lon: [-57, -50]} + - {lat: [50, 51], lon: [-56, -50]} + - {lat: [47.5, 50], lon: [-56, -51]} + + Irminger_Sea: + add: + - {lat: [58.3, 67.5], lon: [-46, -40]} + - {lat: [60, 67.5], lon: [-40, -35]} + - {lat: [61, 68], lon: [-35, -23]} + remove: + - *Icelandic_Sea + - *Labrador_Sea + + NorthWest_Passage: + add: &NorthWest_Passage + - {lat: [73, 77], lon: [-120, -84]} + - {lat: [67, 73], lon: [-124, -85]} + - {lat: [76, 77], lon: [-120, -84]} + - {lat: [77, 78], lon: [-116, -84]} + - {lat: [78, 79], lon: [-108, -84]} + - {lat: [79, 80], lon: [-100, -84]} + - {lat: [80, 81], lon: [-93, -84]} + - {lat: [81, 82], lon: [-85, -84]} + - {lat: [82, 83], lon: [-77, -75]} + + CanArch: + add: *NorthWest_Passage + + Lincoln_Sea: + add: &Lincoln_Sea + - {lat: [81, 84], lon: [-60, -32]} + - {lat: [81, 83], lon: [-70, -60]} + + Central_Arctic_1: + add: + - {lat: [70, 90], lon: [-180, 180]} + + remove: + - *Lincoln_Sea + + Central_Arctic: + add: + - {lat: [70, 90], lon: [-180, 180]} + + Western_Central_Arctic: + add: + - {lat: [70, 90], lon: [-180, -90]} + - {lat: [70, 90], lon: [90, 180]} + + Western_Central_Arctic: + add: + - {lat: [70, 90], lon: [-90, 90]} + + Arctic_Ocean: + add: + - {lat: [70, 90], lon: [-180, 180]} + +combined: + Nordic_Seas: &Nordic_Seas + - Icelandic_Sea + - Norwegian_Sea + - Greenland_Sea + + Grnland: + - Icelandic_Sea + - Norwegian_Sea + - Greenland_Sea + + Arctic_Marginal_seas: + - Icelandic_Sea + - Norwegian_Sea + - Greenland_Sea + - Barents_Sea + - Kara_Sea + - Baffin_Bay + - Laptev_Sea + - Hudson + - East_Siberian_Sea + - Beaufort_Sea + - NorthWest_Passage + - Chukchi_Sea + - Bering + - Okhotsk + + Barkara: + - Barents_Sea + - Kara_Sea + + Laptev-East_Siberian_Seas: + - East_Siberian_Sea + - Laptev_Sea + + Canadian_Waters: + - NorthWest_Passage + - Baffin_Bay + - Hudson + - Labrador_Sea + + Beaufort-Chukchi_Sea: + - Beaufort_Sea + - Chukchi_Sea + + Laptev-East_Siberian-Chukchi_Seas: + - East_Siberian_Sea + - Laptev_Sea + - Chukchi_Sea + + Nordic-Barents_Seas: + - *Nordic_Seas + - Barents_Sea + + # # Mask Serreze + # boolarcticxx = dom_dict['Central_Arctic'] + dom_dict['Barents_Sea'] + \ + # dom_dict['Kara_Sea'] + dom_dict['Laptev_Sea'] + \ + # dom_dict['East_Siberian_Sea'] + dom_dict['Chukchi_Sea'] + \ + # dom_dict['Beaufort_Sea'] + + # boolamundsen = np.logical_and(nwestpas, longitude < -113.) + # dom_dict['Serreze_Arctic'] = boolarcticxx + boolamundsen + # # + # # ======================= + # # COMBINATION FOR COMPARING AGAINST THE NSIDC + + # nsbaffin = baffinxx + labrador + # dom_dict['Baffin'] = nsbaffin + # nsarcton = centrarc + lincolnx + chukchis + eastsibe + laptevxx + beaufort + # dom_dict['ArctOcn'] = nsarcton + # openocen = nnhemisp - arcticoc - labrador - slawrglf + baltseax + # dom_dict['OpenOcean'] = openocen + # # + # total_arc = baffinxx + barentsx + beaufort + beringx + nwestpas + \ + # centrarc + chukchis + eastsibe + greenlan + hudsonxx + \ + # icelands + karaxxxx + labrador + laptevxx + norskhav + okhotskx + lincolnx + irminger + + # dom_dict['TotalArc'] = total_arc \ No newline at end of file diff --git a/new_masks/create_basins_mask.py b/new_masks/create_basins_mask.py new file mode 100644 index 0000000..396faeb --- /dev/null +++ b/new_masks/create_basins_mask.py @@ -0,0 +1,146 @@ +import os +from six import iteritems, string_types +import iris +from iris.cube import CubeList +from iris.coords import AuxCoord +from iris.util import squeeze +import yaml +import numba + +def create_mask(mesh_file, config_file=None): + if not config_file: + cwd = os.path.dirname(os.path.realpath(__file__)) + config_file = os.path.join(cwd, 'basins.yml') + + with open(config_file, 'r') as yfile: + config = yaml.safe_load(yfile) + + sea_mask = squeeze(iris.load_cube(mesh_file, 'tmask'))[0, ...] + lat = iris.load_cube(mesh_file, 'nav_lat').data + lon = iris.load_cube(mesh_file, 'nav_lon').data + basins = CubeList() + + basin_model = sea_mask.copy() + + + print('\nCreating basins from boxes') + for basin, basin_config in iteritems(config['basins']): + print('Creating basin', basin) + add_boxes = flatten_list(basin_config.get('add', [])) + add_mask = merge_boxes(add_boxes, lat, lon) + + remove_boxes = flatten_list(basin_config.get('remove', [])) + remove_mask = merge_boxes(remove_boxes, lat, lon) + + mask = add_mask + if remove_mask is not None: + mask = merge_mask(add_mask, remove_mask) + + basin_mask = basin_model.copy() * mask + basins.append(prepare_cube(basin_mask, basin)) + + basic_basins = basins.merge_cube() + + print('\nCreating basins by merging others') + for basin, basin_config in iteritems(config['combined']): + print('Creating basin', basin, 'from', basin_config) + seas = [] + for sea in flatten_list(basin_config): + seas.append(basic_basins.extract(iris.Constraint(region=sea))) + + mask = None + for sea in seas: + if mask is None: + mask = sea.data + else: + mask += sea.data + basin_mask = basin_model.copy() + basin_mask.data = normalize(mask) + basins.append(prepare_cube(basin_mask, basin)) + + print('Merging and saving') + basins = basins.merge_cube() + basins.add_aux_coord( + AuxCoord(lat, 'latitude', 'nav_lat'), + (1, 2) + ) + basins.add_aux_coord( + AuxCoord(lat, 'longitude', 'nav_lon'), + (1, 2) + ) + iris.save(basins, 'basins.nc', zlib=True) + + import matplotlib + matplotlib.use('Qt5Agg') + import matplotlib.pyplot as plt + import iris.quickplot as qplt + print(basins) + basins.remove_coord('latitude') + basins.remove_coord('longitude') + for basin_slice in basins.slices_over('region'): + basin_slice.long_name = basin_slice.coord('region').points[0] + basin_slice.standard_name = None + qplt.pcolormesh(basin_slice) + plt.show() + +def flatten_list(config, final_type=dict): + final_list = [] + for conf in config: + if isinstance(conf, dict) or isinstance(conf, string_types): + final_list.append(conf) + else: + final_list.extend(conf) + return final_list + +def merge_boxes(box_list, lat, lon): + if not box_list: + return None + + mask = None + for box in box_list: + current_box = apply_config(box['lat'][0], box['lat'][1], lat) * apply_config(box['lon'][0], box['lon'][1], lon) + if mask is None: + mask = current_box + else: + mask += current_box + return normalize(mask) + +def prepare_cube(cube, basin): + cube.add_aux_coord(AuxCoord([basin], var_name='region')) + cube.var_name = 'basin' + cube.standard_name = 'region' + cube.long_name = 'Regional masks' + cube.attributes = {} + return cube + +@numba.vectorize() +def apply_config(min, max, data): + if data < min: + return 0 + if data > max: + return 0 + return 1 + +@numba.vectorize() +def normalize(data): + if data < 1: + return 0 + return 1 + +@numba.vectorize() +def merge_mask(add, remove): + if add - remove < 1: + return 0 + return 1 + +@numba.vectorize() +def intersection(mask1, mask2): + if mask1 and mask2: + return 1 + return 0 + + +if __name__ == "__main__": + create_mask( + '/Users/nube/cpg_tools/new_masks/mesh_mask_nemo.Ec3.2_O1L75.nc' + ) \ No newline at end of file -- GitLab From fae44d23fdda88b1c51f8fdc758fbd2ccccccb7c Mon Sep 17 00:00:00 2001 From: jvegasbsc Date: Tue, 12 Mar 2019 13:15:01 +0100 Subject: [PATCH 2/5] Added more basins --- new_masks/basins.yml | 290 +++++++++++++++++++++++++------- new_masks/create_basins_mask.py | 41 +++-- 2 files changed, 257 insertions(+), 74 deletions(-) diff --git a/new_masks/basins.yml b/new_masks/basins.yml index 5193e3c..08f6f32 100644 --- a/new_masks/basins.yml +++ b/new_masks/basins.yml @@ -244,81 +244,249 @@ basins: add: - {lat: [70, 90], lon: [-180, 180]} + Baltic_Sea: + add: + - {lat: [50, 66], lon: [15, 30]} + - {lat: [50, 56], lon: [10, 15]} + + StLawr: + add: + - {lat: [48, 52], lon: [-70, -55]} + - {lat: [47, 48], lon: [-70, -58]} + - {lat: [46, 47], lon: [-70, -59]} + - {lat: [45, 46], lon: [-70, -60]} + + Caspian_Sea: + add: + - {lat: [35, 50], lon: [45, 60]} + + Fram_Strait: + add: + - {lat: [80, 81], lon: [-30, 17]} + + Bering_Strait: + add: + - {lat: [65, 66], lon: [-175, -165]} + + Nares_Strait: + add: + - {lat: [80, 81], lon: [-84, -45]} + + Kara_Gate_Strait: + add: + - {lat: [70, 71], lon: [57, 59]} + - {lat: [69, 70], lon: [60, 61]} + + Vilkitsky_Strait: + add: + - {lat: [76, 79], lon: [100, 105]} + - {lat: [70, 76], lon: [99, 100]} + + Weddell_Sea: + add: + - {lat: [-90, -40], lon: [-65, -15]} + + Ross_Sea: + add: + - {lat: [-90, -40], lon: [-180, -140]} + - {lat: [-90, -40], lon: [160, 180]} + + Amundsen_Sea: + add: + - {lat: [-90, -40], lon: [-140, -90]} + + Bellingshausen_Sea: + add: + - {lat: [-90, -40], lon: [-90, -65]} + + Antarctic_Atlantic_Sector: + add: + - {lat: [-90, -40], lon: [-15, 22]} + + Antarctic_Indian_Sector: + add: + - {lat: [-90, -40], lon: [22, 160]} + + LAT80N: + add: + - {lat: [80, 90], lon: [-180, 180]} + + CntArcticRing1: + add: + - {lat: [78, 86], lon: [0, 45]} + + CntArcticRing2: + add: + - {lat: [78, 86], lon: [45, 90]} + + CntArcticRing3: + add: + - {lat: [78, 86], lon: [90, 135]} + + CntArcticRing4: + add: + - {lat: [78, 86], lon: [135, 180]} + + CntArcticRing5: + add: + - {lat: [78, 86], lon: [-180, -135]} + + CntArcticRing6: + add: + - {lat: [78, 86], lon: [-135, -90]} + + CntArcticRing7: + add: + - {lat: [78, 86], lon: [-90, -45]} + + CntArcticRing8: + add: + - {lat: [78, 86], lon: [-45, 0]} + + CntArctic: + add: + - {lat: [86, 90], lon: [-180, 180]} + combined: - Nordic_Seas: &Nordic_Seas - - Icelandic_Sea - - Norwegian_Sea - - Greenland_Sea + Nordic_Seas: + add: &Nordic_Seas + - Icelandic_Sea + - Norwegian_Sea + - Greenland_Sea Grnland: - - Icelandic_Sea - - Norwegian_Sea - - Greenland_Sea + add: + - Icelandic_Sea + - Norwegian_Sea + - Greenland_Sea Arctic_Marginal_seas: - - Icelandic_Sea - - Norwegian_Sea - - Greenland_Sea - - Barents_Sea - - Kara_Sea - - Baffin_Bay - - Laptev_Sea - - Hudson - - East_Siberian_Sea - - Beaufort_Sea - - NorthWest_Passage - - Chukchi_Sea - - Bering - - Okhotsk + add: + - Icelandic_Sea + - Norwegian_Sea + - Greenland_Sea + - Barents_Sea + - Kara_Sea + - Baffin_Bay + - Laptev_Sea + - Hudson + - East_Siberian_Sea + - Beaufort_Sea + - NorthWest_Passage + - Chukchi_Sea + - Bering + - Okhotsk Barkara: - - Barents_Sea - - Kara_Sea + add: + - Barents_Sea + - Kara_Sea Laptev-East_Siberian_Seas: - - East_Siberian_Sea - - Laptev_Sea + add: + - East_Siberian_Sea + - Laptev_Sea Canadian_Waters: - - NorthWest_Passage - - Baffin_Bay - - Hudson - - Labrador_Sea + add: + - NorthWest_Passage + - Baffin_Bay + - Hudson + - Labrador_Sea Beaufort-Chukchi_Sea: - - Beaufort_Sea - - Chukchi_Sea + add: + - Beaufort_Sea + - Chukchi_Sea Laptev-East_Siberian-Chukchi_Seas: - - East_Siberian_Sea - - Laptev_Sea - - Chukchi_Sea + add: + - East_Siberian_Sea + - Laptev_Sea + - Chukchi_Sea Nordic-Barents_Seas: - - *Nordic_Seas - - Barents_Sea - - # # Mask Serreze - # boolarcticxx = dom_dict['Central_Arctic'] + dom_dict['Barents_Sea'] + \ - # dom_dict['Kara_Sea'] + dom_dict['Laptev_Sea'] + \ - # dom_dict['East_Siberian_Sea'] + dom_dict['Chukchi_Sea'] + \ - # dom_dict['Beaufort_Sea'] - - # boolamundsen = np.logical_and(nwestpas, longitude < -113.) - # dom_dict['Serreze_Arctic'] = boolarcticxx + boolamundsen - # # - # # ======================= - # # COMBINATION FOR COMPARING AGAINST THE NSIDC - - # nsbaffin = baffinxx + labrador - # dom_dict['Baffin'] = nsbaffin - # nsarcton = centrarc + lincolnx + chukchis + eastsibe + laptevxx + beaufort - # dom_dict['ArctOcn'] = nsarcton - # openocen = nnhemisp - arcticoc - labrador - slawrglf + baltseax - # dom_dict['OpenOcean'] = openocen - # # - # total_arc = baffinxx + barentsx + beaufort + beringx + nwestpas + \ - # centrarc + chukchis + eastsibe + greenlan + hudsonxx + \ - # icelands + karaxxxx + labrador + laptevxx + norskhav + okhotskx + lincolnx + irminger - - # dom_dict['TotalArc'] = total_arc \ No newline at end of file + add: + - *Nordic_Seas + - Barents_Sea + + Serreze_Arctic: + add: + - Central_Arctic + - Barents_Sea + - Kara_Sea + - Laptev_Sea + - East_Siberian_Sea + - Chukchi_Sea + - Beaufort_Sea + + North_Hemisphere_Ocean: + add: + - Northern_Hemisphere + remove: + - Caspian_Sea + - Baltic_Sea + + Baffin: + add: + - Baffin_Bay + - Labrador_Sea + + ArctOcn: + add: + - Central_Arctic + - Lincoln_Sea + - Chukchi_Sea + - East_Siberian_Sea + - Laptev_Sea + - Beaufort_Sea + + OpenOcean: + add: + - Northern_Hemisphere + remove: + - Arctic_Ocean + - Labrador_Sea + - StLawr + + TotalArc: + add: + - Baffin_Bay + - Barents_Sea + - Beaufort_Sea + - Bering + - NorthWest_Passage + - Central_Arctic + - Chukchi_Sea + - East_Siberian_Sea + - Greenland_Sea + - Hudson + - Icelandic_Sea + - Labrador_Sea + - Laptev_Sea + - Norwegian_Sea + - Okhotsk + - Lincoln_Sea + - Irminger_Sea + + North_Atlantic-Arctic: + add: + - North_Atlantic_Ocean + - Barents_Sea + remove: + - Hudson + - CanArch + - Baltic_Sea + + Subpolar_Gyre: + add: + - North_Atlantic-Arctic + remove: + - Baffin_Bay + - Nordic-Barents_Seas + + Arctic_Ocean-North_Atlantic: + add: + - North_Atlantic-Arctic + - Kara_Sea + - ArctOcn diff --git a/new_masks/create_basins_mask.py b/new_masks/create_basins_mask.py index 396faeb..5d6b158 100644 --- a/new_masks/create_basins_mask.py +++ b/new_masks/create_basins_mask.py @@ -39,23 +39,23 @@ def create_mask(mesh_file, config_file=None): basin_mask = basin_model.copy() * mask basins.append(prepare_cube(basin_mask, basin)) - basic_basins = basins.merge_cube() print('\nCreating basins by merging others') for basin, basin_config in iteritems(config['combined']): print('Creating basin', basin, 'from', basin_config) - seas = [] - for sea in flatten_list(basin_config): - seas.append(basic_basins.extract(iris.Constraint(region=sea))) - - mask = None - for sea in seas: - if mask is None: - mask = sea.data - else: - mask += sea.data - basin_mask = basin_model.copy() - basin_mask.data = normalize(mask) + + mask = merge_basic_basins( + basin_config.get('add', []), + basins + ) + remove_mask = merge_basic_basins( + basin_config.get('remove', []), + basins + ) + if remove_mask is not None: + mask = merge_mask(mask, normalize(remove_mask)) + + basin_mask = basin_model.copy(mask) basins.append(prepare_cube(basin_mask, basin)) print('Merging and saving') @@ -105,6 +105,21 @@ def merge_boxes(box_list, lat, lon): mask += current_box return normalize(mask) +def merge_basic_basins(config, basins): + seas = [] + for sea in flatten_list(config): + seas.append(basins.extract(iris.Constraint(region=sea))[0]) + if not seas: + return None + + mask = None + for sea in seas: + if mask is None: + mask = sea.data.copy() + else: + mask += sea.data + return normalize(mask) + def prepare_cube(cube, basin): cube.add_aux_coord(AuxCoord([basin], var_name='region')) cube.var_name = 'basin' -- GitLab From 87e9a8bf85785b88619f81a0e2afd11a36c008cf Mon Sep 17 00:00:00 2001 From: jvegasbsc Date: Tue, 12 Mar 2019 16:58:48 +0100 Subject: [PATCH 3/5] Finished new version of masks script --- new_masks/basins.yml | 640 ++++++++++++++------------------ new_masks/create_basins_mask.py | 123 +++--- 2 files changed, 338 insertions(+), 425 deletions(-) diff --git a/new_masks/basins.yml b/new_masks/basins.yml index 08f6f32..82c8847 100644 --- a/new_masks/basins.yml +++ b/new_masks/basins.yml @@ -1,492 +1,418 @@ basins: Global_Ocean: - add: - - {lat: [-90, 90], lon: [-180, 180]} + - {lat: [-90, 90], lon: [-180, 180]} Northern_Hemisphere: - add: - - {lat: [0, 90], lon: [-180, 180]} + - {lat: [0, 90], lon: [-180, 180]} Southern_Hemisphere: - add: - - {lat: [-90, 0], lon: [-180, 180]} + - {lat: [-90, 0], lon: [-180, 180]} Antarctic_Ocean: - add: - - {lat: [-90, -40], lon: [-180, 180]} + - {lat: [-90, -40], lon: [-180, 180]} Mediterranean_Sea: - add: &Mediterranean_Sea - - {lat: [30, 40], lon: [-5, 0]} - - {lat: [30, 46], lon: [0, 28]} - - {lat: [30, 40], lon: [28, 40]} + - {lat: [30, 40], lon: [-5, 0]} + - {lat: [30, 46], lon: [0, 28]} + - {lat: [30, 40], lon: [28, 40]} North_Atlantic_Ocean: - add: - - {lat: [32, 73], lon: [-95, 26]} - - {lat: [73, 75], lon: [-30, 24]} - - {lat: [75, 77], lon: [-30, 22]} - - {lat: [77, 79], lon: [-30, 18]} - - {lat: [79, 81], lon: [-30, 17]} - - {lat: [73, 82], lon: [-83, -35]} - remove: *Mediterranean_Sea + - {basin: Atlantic_Ocean} + - {lat: [32, 82], lon: [-180, 180], action: intersection} + - {basin: Mediterranean_Sea, action: remove} Greenland_Sea: - add: &Greenland_Sea - - {lat: [76, 81], lon: [-30, 17]} - - {lat: [75, 76], lon: [-30, 12]} - - {lat: [74, 75], lon: [-30, 9]} - - {lat: [73, 74], lon: [-30, 4]} - - {lat: [72, 73], lon: [-30, 0]} - - {lat: [71, 72], lon: [-30, -3]} - - {lat: [70, 71], lon: [-30, -8]} + - {lat: [76, 81], lon: [-30, 17]} + - {lat: [75, 76], lon: [-30, 12]} + - {lat: [74, 75], lon: [-30, 9]} + - {lat: [73, 74], lon: [-30, 4]} + - {lat: [72, 73], lon: [-30, 0]} + - {lat: [71, 72], lon: [-30, -3]} + - {lat: [70, 71], lon: [-30, -8]} Icelandic_Sea: - add: &Icelandic_Sea - - {lat: [69, 70], lon: [-29, -9]} - - {lat: [68, 69], lon: [-28, -10]} - - {lat: [67, 68], lon: [-25, -11]} - - {lat: [66, 67], lon: [-23, -12]} - - {lat: [65, 66], lon: [-20, -13]} + - {lat: [69, 70], lon: [-29, -9]} + - {lat: [68, 69], lon: [-28, -10]} + - {lat: [67, 68], lon: [-25, -11]} + - {lat: [66, 67], lon: [-23, -12]} + - {lat: [65, 66], lon: [-20, -13]} Norwegian_Sea: - add: &Norwegian_Sea - - {lat: [75, 76], lon: [12, 17]} - - {lat: [74, 75], lon: [9, 18]} - - {lat: [73, 74], lon: [4, 19]} - - {lat: [72, 73], lon: [0, 21]} - - {lat: [71, 72], lon: [-3, 23]} - - {lat: [70, 71], lon: [-8, 25]} - - {lat: [66, 70], lon: [-13, 25]} - - {lat: [62, 66], lon: [-6, 15]} - - {lat: [61, 62], lon: [0, 15]} - - {lat: [63, 64], lon: [-8, -6]} - - {lat: [64, 66], lon: [-10, -6]} - - {lat: [65, 66], lon: [-13, -6]} - remove: *Icelandic_Sea + - {lat: [75, 76], lon: [12, 17]} + - {lat: [74, 75], lon: [9, 18]} + - {lat: [73, 74], lon: [4, 19]} + - {lat: [72, 73], lon: [0, 21]} + - {lat: [71, 72], lon: [-3, 23]} + - {lat: [70, 71], lon: [-8, 25]} + - {lat: [66, 70], lon: [-13, 25]} + - {lat: [62, 66], lon: [-6, 15]} + - {lat: [61, 62], lon: [0, 15]} + - {lat: [63, 64], lon: [-8, -6]} + - {lat: [64, 66], lon: [-10, -6]} + - {lat: [65, 66], lon: [-13, -6]} + - {basin: Icelandic_Sea, action: remove} Barents_Sea: - add: &Barents_Sea - - {lat: [76, 80], lon: [17, 65]} - - {lat: [75, 76], lon: [17, 62]} - - {lat: [74, 75], lon: [18, 59]} - - {lat: [73, 74], lon: [19, 58]} - - {lat: [72, 73], lon: [21, 56]} - - {lat: [71, 72], lon: [23, 56]} - - {lat: [70, 71], lon: [25, 57]} - - {lat: [66, 70], lon: [25, 60]} - - {lat: [63, 66], lon: [30, 45]} + - {lat: [76, 80], lon: [17, 65]} + - {lat: [75, 76], lon: [17, 62]} + - {lat: [74, 75], lon: [18, 59]} + - {lat: [73, 74], lon: [19, 58]} + - {lat: [72, 73], lon: [21, 56]} + - {lat: [71, 72], lon: [23, 56]} + - {lat: [70, 71], lon: [25, 57]} + - {lat: [66, 70], lon: [25, 60]} + - {lat: [63, 66], lon: [30, 45]} Kara_Sea: - add: &Kara_Sea - - {lat: [79, 80], lon: [65, 96]} - - {lat: [76, 79], lon: [65, 105]} - - {lat: [75, 76], lon: [62, 100]} - - {lat: [74, 75], lon: [59, 100]} - - {lat: [73, 74], lon: [58, 100]} - - {lat: [71, 73], lon: [56, 100]} - - {lat: [70, 71], lon: [57, 100]} - - {lat: [66, 70], lon: [60, 90]} + - {lat: [79, 80], lon: [65, 96]} + - {lat: [76, 79], lon: [65, 105]} + - {lat: [75, 76], lon: [62, 100]} + - {lat: [74, 75], lon: [59, 100]} + - {lat: [73, 74], lon: [58, 100]} + - {lat: [71, 73], lon: [56, 100]} + - {lat: [70, 71], lon: [57, 100]} + - {lat: [66, 70], lon: [60, 90]} Laptev_Sea: - add: &Laptev_Sea - - {lat: [80, 81], lon: [96, 102]} - - {lat: [79, 80], lon: [100, 111]} - - {lat: [78, 79], lon: [105, 120]} - - {lat: [77, 78], lon: [105, 129]} - - {lat: [76, 77], lon: [100, 138]} - - {lat: [70, 76], lon: [105, 140]} - - remove: *Kara_Sea + - {lat: [80, 81], lon: [96, 102]} + - {lat: [79, 80], lon: [100, 111]} + - {lat: [78, 79], lon: [105, 120]} + - {lat: [77, 78], lon: [105, 129]} + - {lat: [76, 77], lon: [100, 138]} + - {lat: [70, 76], lon: [105, 140]} + - {basin: Kara_Sea, action: remove} East_Siberian_Sea: - add: &East_Siberian_Sea - - {lat: [74, 75], lon: [140, 146]} - - {lat: [73, 74], lon: [140, 154]} - - {lat: [72, 73], lon: [140, 162]} - - {lat: [71, 72], lon: [140, 170]} - - {lat: [70, 71], lon: [140, 179]} - - {lat: [69, 70], lon: [140, 178]} - - {lat: [68, 69], lon: [140, 178]} + - {lat: [74, 75], lon: [140, 146]} + - {lat: [73, 74], lon: [140, 154]} + - {lat: [72, 73], lon: [140, 162]} + - {lat: [71, 72], lon: [140, 170]} + - {lat: [70, 71], lon: [140, 179]} + - {lat: [69, 70], lon: [140, 178]} + - {lat: [68, 69], lon: [140, 178]} Chukchi_Sea: - add: &Chukchi_Sea - - {lat: [66, 71], lon: [-180, -156]} - - {lat: [66, 71], lon: [179, 180]} - - {lat: [69, 70], lon: [178, 180]} + - {lat: [66, 71], lon: [-180, -156]} + - {lat: [66, 71], lon: [179, 180]} + - {lat: [69, 70], lon: [178, 180]} Okhotsk: - add: &Okhotsk - - {lat: [43, 44], lon: [141, 146]} - - {lat: [44, 45], lon: [141, 147]} - - {lat: [45, 46], lon: [141, 150]} - - {lat: [46, 47], lon: [141, 150]} - - {lat: [47, 48], lon: [141, 151]} - - {lat: [48, 49], lon: [141, 152.5]} - - {lat: [49, 50], lon: [141, 154]} - - {lat: [50, 51], lon: [141, 155.5]} - - {lat: [51, 52], lon: [139, 157]} - - {lat: [52, 63], lon: [135, 157]} - - {lat: [58, 63], lon: [157, 163]} - - {lat: [60, 63], lon: [162, 164]} - - {lat: [48, 51], lon: [151, 157]} + - {lat: [43, 44], lon: [141, 146]} + - {lat: [44, 45], lon: [141, 147]} + - {lat: [45, 46], lon: [141, 150]} + - {lat: [46, 47], lon: [141, 150]} + - {lat: [47, 48], lon: [141, 151]} + - {lat: [48, 49], lon: [141, 152.5]} + - {lat: [49, 50], lon: [141, 154]} + - {lat: [50, 51], lon: [141, 155.5]} + - {lat: [51, 52], lon: [139, 157]} + - {lat: [52, 63], lon: [135, 157]} + - {lat: [58, 63], lon: [157, 163]} + - {lat: [60, 63], lon: [162, 164]} + - {lat: [48, 51], lon: [151, 157]} Bering: - add: &Bering - - {lat: [58, 66], lon: [-180, -154]} - - {lat: [55, 66], lon: [161, 180]} - - {lat: [53, 60], lon: [160, 180]} - - {lat: [56, 60], lon: [-180, -155]} - - {lat: [55, 58], lon: [-180, -157]} - - {lat: [55, 58], lon: [163, 180]} - - {lat: [54, 57], lon: [-180, -160]} - - {lat: [54, 57], lon: [167, 180]} - - {lat: [54, 56], lon: [-180, -165]} - - {lat: [54, 56], lon: [171, 180]} - - {lat: [53, 55], lon: [-180, -170]} - - {lat: [53, 55], lon: [174, 180]} - - {lat: [53, 54], lon: [-180, -174]} - - {lat: [53, 54], lon: [177, 180]} - - {lat: [52, 53], lon: [-180, -178]} - remove: *Okhotsk + - {lat: [58, 66], lon: [-180, -154]} + - {lat: [55, 66], lon: [161, 180]} + - {lat: [53, 60], lon: [160, 180]} + - {lat: [56, 60], lon: [-180, -155]} + - {lat: [55, 58], lon: [-180, -157]} + - {lat: [55, 58], lon: [163, 180]} + - {lat: [54, 57], lon: [-180, -160]} + - {lat: [54, 57], lon: [167, 180]} + - {lat: [54, 56], lon: [-180, -165]} + - {lat: [54, 56], lon: [171, 180]} + - {lat: [53, 55], lon: [-180, -170]} + - {lat: [53, 55], lon: [174, 180]} + - {lat: [53, 54], lon: [-180, -174]} + - {lat: [53, 54], lon: [177, 180]} + - {lat: [52, 53], lon: [-180, -178]} + - {basin: Okhotsk, action: remove} Beaufort_Sea: - add: &Beaufort_Sea - - {lat: [75, 76], lon: [-130, -124]} - - {lat: [74, 75], lon: [-136, -124]} - - {lat: [73, 74], lon: [-143, -125]} - - {lat: [72, 73], lon: [-149, -125]} - - {lat: [71, 72], lon: [-156, -126]} - - {lat: [70, 71], lon: [-156, -126]} - - {lat: [69, 70], lon: [-156, -127]} - - {lat: [73, 77], lon: [-124, -120]} - - {lat: [73, 75], lon: [-125, -124]} - - {lat: [69, 73], lon: [-127, -124]} + - {lat: [75, 76], lon: [-130, -124]} + - {lat: [74, 75], lon: [-136, -124]} + - {lat: [73, 74], lon: [-143, -125]} + - {lat: [72, 73], lon: [-149, -125]} + - {lat: [71, 72], lon: [-156, -126]} + - {lat: [70, 71], lon: [-156, -126]} + - {lat: [69, 70], lon: [-156, -127]} + - {lat: [73, 77], lon: [-124, -120]} + - {lat: [73, 75], lon: [-125, -124]} + - {lat: [69, 73], lon: [-127, -124]} Baffin_Bay: - add: &Baffin_Bay - - {lat: [70, 81], lon: [-84, -45]} - - {lat: [66, 70], lon: [-70, -45]} - - {lat: [62, 66], lon: [-66, -45]} - - {lat: [61, 62], lon: [-65, -45]} + - {lat: [70, 81], lon: [-84, -45]} + - {lat: [66, 70], lon: [-70, -45]} + - {lat: [62, 66], lon: [-66, -45]} + - {lat: [61, 62], lon: [-65, -45]} Hudson: - add: &Hudson - - {lat: [50, 67], lon: [-95, -66]} - - {lat: [67, 70], lon: [-85, -70]} - - {lat: [59, 62], lon: [-66, -65]} + - {lat: [50, 67], lon: [-95, -66]} + - {lat: [67, 70], lon: [-85, -70]} + - {lat: [59, 62], lon: [-66, -65]} Labrador_Sea: - add: &Labrador_Sea - - {lat: [60, 61], lon: [-65, -45]} - - {lat: [59, 60], lon: [-65, -46]} - - {lat: [58, 59], lon: [-64, -46]} - - {lat: [57, 58], lon: [-63, -47]} - - {lat: [56, 57], lon: [-62, -47]} - - {lat: [55, 56], lon: [-61, -48]} - - {lat: [54, 55], lon: [-60, -48]} - - {lat: [53, 54], lon: [-59, -49]} - - {lat: [51, 53], lon: [-58, -49]} - - {lat: [48.5, 52], lon: [-57, -50]} - - {lat: [50, 51], lon: [-56, -50]} - - {lat: [47.5, 50], lon: [-56, -51]} + - {lat: [60, 61], lon: [-65, -45]} + - {lat: [59, 60], lon: [-65, -46]} + - {lat: [58, 59], lon: [-64, -46]} + - {lat: [57, 58], lon: [-63, -47]} + - {lat: [56, 57], lon: [-62, -47]} + - {lat: [55, 56], lon: [-61, -48]} + - {lat: [54, 55], lon: [-60, -48]} + - {lat: [53, 54], lon: [-59, -49]} + - {lat: [51, 53], lon: [-58, -49]} + - {lat: [48.5, 52], lon: [-57, -50]} + - {lat: [50, 51], lon: [-56, -50]} + - {lat: [47.5, 50], lon: [-56, -51]} Irminger_Sea: - add: - - {lat: [58.3, 67.5], lon: [-46, -40]} - - {lat: [60, 67.5], lon: [-40, -35]} - - {lat: [61, 68], lon: [-35, -23]} - remove: - - *Icelandic_Sea - - *Labrador_Sea + - {lat: [58.3, 67.5], lon: [-46, -40]} + - {lat: [60, 67.5], lon: [-40, -35]} + - {lat: [61, 68], lon: [-35, -23]} + - {basin: Icelandic_Sea, action: remove} + - {basin: Labrador_Sea, action: remove} NorthWest_Passage: - add: &NorthWest_Passage - - {lat: [73, 77], lon: [-120, -84]} - - {lat: [67, 73], lon: [-124, -85]} - - {lat: [76, 77], lon: [-120, -84]} - - {lat: [77, 78], lon: [-116, -84]} - - {lat: [78, 79], lon: [-108, -84]} - - {lat: [79, 80], lon: [-100, -84]} - - {lat: [80, 81], lon: [-93, -84]} - - {lat: [81, 82], lon: [-85, -84]} - - {lat: [82, 83], lon: [-77, -75]} + - {lat: [73, 77], lon: [-120, -84]} + - {lat: [67, 73], lon: [-124, -85]} + - {lat: [76, 77], lon: [-120, -84]} + - {lat: [77, 78], lon: [-116, -84]} + - {lat: [78, 79], lon: [-108, -84]} + - {lat: [79, 80], lon: [-100, -84]} + - {lat: [80, 81], lon: [-93, -84]} + - {lat: [81, 82], lon: [-85, -84]} + - {lat: [82, 83], lon: [-77, -75]} CanArch: - add: *NorthWest_Passage + - {basin: NorthWest_Passage} Lincoln_Sea: - add: &Lincoln_Sea - - {lat: [81, 84], lon: [-60, -32]} - - {lat: [81, 83], lon: [-70, -60]} + - {lat: [81, 84], lon: [-60, -32]} + - {lat: [81, 83], lon: [-70, -60]} - Central_Arctic_1: - add: - - {lat: [70, 90], lon: [-180, 180]} + Nordic_Seas: + - {basin: Icelandic_Sea} + - {basin: Norwegian_Sea} + - {basin: Greenland_Sea} - remove: - - *Lincoln_Sea + Grnland: + - {basin: Icelandic_Sea} + - {basin: Norwegian_Sea} + - {basin: Greenland_Sea} + + Arctic_Marginal_seas: + - {basin: Icelandic_Sea} + - {basin: Norwegian_Sea} + - {basin: Greenland_Sea} + - {basin: Barents_Sea} + - {basin: Kara_Sea} + - {basin: Baffin_Bay} + - {basin: Laptev_Sea} + - {basin: Hudson} + - {basin: East_Siberian_Sea} + - {basin: Beaufort_Sea} + - {basin: NorthWest_Passage} + - {basin: Chukchi_Sea} + - {basin: Bering} + - {basin: Okhotsk} + + Central_Arctic_1: + - {basin: Arctic_Marginal_seas} + - {lat: [70, 90], lon: [-180, 180], action: intersection} + - {basin: Lincoln_Sea, action: remove} Central_Arctic: - add: - - {lat: [70, 90], lon: [-180, 180]} + - {basin: Central_Arctic_1} + - {basin: Lincoln_Sea} Western_Central_Arctic: - add: - - {lat: [70, 90], lon: [-180, -90]} - - {lat: [70, 90], lon: [90, 180]} + - {lat: [70, 90], lon: [-180, -90]} + - {lat: [70, 90], lon: [90, 180]} + - {basin: Central_Arctic, action: intersection} - Western_Central_Arctic: - add: - - {lat: [70, 90], lon: [-90, 90]} + Eastern_Central_Arctic: + - {basin: Central_Arctic} + - {lat: [70, 90], lon: [-90, 90], action: intersection} Arctic_Ocean: - add: - - {lat: [70, 90], lon: [-180, 180]} + - {basin: Arctic_Marginal_seas} + - {lat: [0, 90], lon: [-180, 180], action: intersection} + - {basin: Central_Arctic} + + Arctic_Ocean: + - {lat: [70, 90], lon: [-180, 180]} Baltic_Sea: - add: - - {lat: [50, 66], lon: [15, 30]} - - {lat: [50, 56], lon: [10, 15]} + - {lat: [50, 66], lon: [15, 30]} + - {lat: [50, 56], lon: [10, 15]} StLawr: - add: - - {lat: [48, 52], lon: [-70, -55]} - - {lat: [47, 48], lon: [-70, -58]} - - {lat: [46, 47], lon: [-70, -59]} - - {lat: [45, 46], lon: [-70, -60]} + - {lat: [48, 52], lon: [-70, -55]} + - {lat: [47, 48], lon: [-70, -58]} + - {lat: [46, 47], lon: [-70, -59]} + - {lat: [45, 46], lon: [-70, -60]} Caspian_Sea: - add: - - {lat: [35, 50], lon: [45, 60]} + - {lat: [35, 50], lon: [45, 60]} Fram_Strait: - add: - - {lat: [80, 81], lon: [-30, 17]} + - {lat: [80, 81], lon: [-30, 17]} Bering_Strait: - add: - - {lat: [65, 66], lon: [-175, -165]} + - {lat: [65, 66], lon: [-175, -165]} Nares_Strait: - add: - - {lat: [80, 81], lon: [-84, -45]} + - {lat: [80, 81], lon: [-84, -45]} Kara_Gate_Strait: - add: - - {lat: [70, 71], lon: [57, 59]} - - {lat: [69, 70], lon: [60, 61]} + - {lat: [70, 71], lon: [57, 59]} + - {lat: [69, 70], lon: [60, 61]} Vilkitsky_Strait: - add: - - {lat: [76, 79], lon: [100, 105]} - - {lat: [70, 76], lon: [99, 100]} + - {lat: [76, 79], lon: [100, 105]} + - {lat: [70, 76], lon: [99, 100]} Weddell_Sea: - add: - - {lat: [-90, -40], lon: [-65, -15]} + - {lat: [-90, -40], lon: [-65, -15]} Ross_Sea: - add: - - {lat: [-90, -40], lon: [-180, -140]} - - {lat: [-90, -40], lon: [160, 180]} + - {lat: [-90, -40], lon: [-180, -140]} + - {lat: [-90, -40], lon: [160, 180]} Amundsen_Sea: - add: - - {lat: [-90, -40], lon: [-140, -90]} + - {lat: [-90, -40], lon: [-140, -90]} Bellingshausen_Sea: - add: - - {lat: [-90, -40], lon: [-90, -65]} + - {lat: [-90, -40], lon: [-90, -65]} Antarctic_Atlantic_Sector: - add: - - {lat: [-90, -40], lon: [-15, 22]} + - {lat: [-90, -40], lon: [-15, 22]} Antarctic_Indian_Sector: - add: - - {lat: [-90, -40], lon: [22, 160]} + - {lat: [-90, -40], lon: [22, 160]} LAT80N: - add: - - {lat: [80, 90], lon: [-180, 180]} + - {lat: [80, 90], lon: [-180, 180]} CntArcticRing1: - add: - - {lat: [78, 86], lon: [0, 45]} + - {lat: [78, 86], lon: [0, 45]} CntArcticRing2: - add: - - {lat: [78, 86], lon: [45, 90]} + - {lat: [78, 86], lon: [45, 90]} CntArcticRing3: - add: - - {lat: [78, 86], lon: [90, 135]} + - {lat: [78, 86], lon: [90, 135]} CntArcticRing4: - add: - - {lat: [78, 86], lon: [135, 180]} + - {lat: [78, 86], lon: [135, 180]} CntArcticRing5: - add: - - {lat: [78, 86], lon: [-180, -135]} + - {lat: [78, 86], lon: [-180, -135]} CntArcticRing6: - add: - - {lat: [78, 86], lon: [-135, -90]} + - {lat: [78, 86], lon: [-135, -90]} CntArcticRing7: - add: - - {lat: [78, 86], lon: [-90, -45]} + - {lat: [78, 86], lon: [-90, -45]} CntArcticRing8: - add: - - {lat: [78, 86], lon: [-45, 0]} + - {lat: [78, 86], lon: [-45, 0]} CntArctic: - add: - - {lat: [86, 90], lon: [-180, 180]} - -combined: - Nordic_Seas: - add: &Nordic_Seas - - Icelandic_Sea - - Norwegian_Sea - - Greenland_Sea - - Grnland: - add: - - Icelandic_Sea - - Norwegian_Sea - - Greenland_Sea - - Arctic_Marginal_seas: - add: - - Icelandic_Sea - - Norwegian_Sea - - Greenland_Sea - - Barents_Sea - - Kara_Sea - - Baffin_Bay - - Laptev_Sea - - Hudson - - East_Siberian_Sea - - Beaufort_Sea - - NorthWest_Passage - - Chukchi_Sea - - Bering - - Okhotsk + - {lat: [86, 90], lon: [-180, 180]} Barkara: - add: - - Barents_Sea - - Kara_Sea + - {basin: Barents_Sea} + - {basin: Kara_Sea} Laptev-East_Siberian_Seas: - add: - - East_Siberian_Sea - - Laptev_Sea + - {basin: East_Siberian_Sea} + - {basin: Laptev_Sea} Canadian_Waters: - add: - - NorthWest_Passage - - Baffin_Bay - - Hudson - - Labrador_Sea + - {basin: NorthWest_Passage} + - {basin: Baffin_Bay} + - {basin: Hudson} + - {basin: Labrador_Sea} Beaufort-Chukchi_Sea: - add: - - Beaufort_Sea - - Chukchi_Sea + - {basin: Beaufort_Sea} + - {basin: Chukchi_Sea} Laptev-East_Siberian-Chukchi_Seas: - add: - - East_Siberian_Sea - - Laptev_Sea - - Chukchi_Sea + - {basin: East_Siberian_Sea} + - {basin: Laptev_Sea} + - {basin: Chukchi_Sea} Nordic-Barents_Seas: - add: - - *Nordic_Seas - - Barents_Sea + - {basin: Nordic_Seas} + - {basin: Barents_Sea} Serreze_Arctic: - add: - - Central_Arctic - - Barents_Sea - - Kara_Sea - - Laptev_Sea - - East_Siberian_Sea - - Chukchi_Sea - - Beaufort_Sea + - {basin: Central_Arctic} + - {basin: Barents_Sea} + - {basin: Kara_Sea} + - {basin: Laptev_Sea} + - {basin: East_Siberian_Sea} + - {basin: Chukchi_Sea} + - {basin: Beaufort_Sea} North_Hemisphere_Ocean: - add: - - Northern_Hemisphere - remove: - - Caspian_Sea - - Baltic_Sea + - {basin: Northern_Hemisphere} + - {basin: Caspian_Sea, action: remove} + - {basin: Baltic_Sea, action: remove} Baffin: - add: - - Baffin_Bay - - Labrador_Sea + - {basin: Baffin_Bay} + - {basin: Labrador_Sea} ArctOcn: - add: - - Central_Arctic - - Lincoln_Sea - - Chukchi_Sea - - East_Siberian_Sea - - Laptev_Sea - - Beaufort_Sea + - {basin: Central_Arctic} + - {basin: Lincoln_Sea} + - {basin: Chukchi_Sea} + - {basin: East_Siberian_Sea} + - {basin: Laptev_Sea} + - {basin: Beaufort_Sea} OpenOcean: - add: - - Northern_Hemisphere - remove: - - Arctic_Ocean - - Labrador_Sea - - StLawr + - {basin: Northern_Hemisphere} + - {basin: Arctic_Ocean, action: remove} + - {basin: Labrador_Sea, action: remove} + - {basin: StLawr, action: remove} TotalArc: - add: - - Baffin_Bay - - Barents_Sea - - Beaufort_Sea - - Bering - - NorthWest_Passage - - Central_Arctic - - Chukchi_Sea - - East_Siberian_Sea - - Greenland_Sea - - Hudson - - Icelandic_Sea - - Labrador_Sea - - Laptev_Sea - - Norwegian_Sea - - Okhotsk - - Lincoln_Sea - - Irminger_Sea + - {basin: Baffin_Bay} + - {basin: Barents_Sea} + - {basin: Beaufort_Sea} + - {basin: Bering} + - {basin: NorthWest_Passage} + - {basin: Central_Arctic} + - {basin: Chukchi_Sea} + - {basin: East_Siberian_Sea} + - {basin: Greenland_Sea} + - {basin: Hudson} + - {basin: Icelandic_Sea} + - {basin: Labrador_Sea} + - {basin: Laptev_Sea} + - {basin: Norwegian_Sea} + - {basin: Okhotsk} + - {basin: Lincoln_Sea} + - {basin: Irminger_Sea} North_Atlantic-Arctic: - add: - - North_Atlantic_Ocean - - Barents_Sea - remove: - - Hudson - - CanArch - - Baltic_Sea + - {basin: North_Atlantic_Ocean} + - {basin: Barents_Sea} + - {basin: Hudson, action: remove} + - {basin: CanArch, action: remove} + - {basin: Baltic_Sea, action: remove} Subpolar_Gyre: - add: - - North_Atlantic-Arctic - remove: - - Baffin_Bay - - Nordic-Barents_Seas + - {basin: North_Atlantic-Arctic} + - {basin: Baffin_Bay, action: remove} + - {basin: Nordic-Barents_Seas, action: remove} Arctic_Ocean-North_Atlantic: - add: - - North_Atlantic-Arctic - - Kara_Sea - - ArctOcn + - {basin: North_Atlantic-Arctic} + - {basin: Kara_Sea} + - {basin: ArctOcn} diff --git a/new_masks/create_basins_mask.py b/new_masks/create_basins_mask.py index 5d6b158..3774b7b 100644 --- a/new_masks/create_basins_mask.py +++ b/new_masks/create_basins_mask.py @@ -1,13 +1,14 @@ import os from six import iteritems, string_types +import numpy as np import iris from iris.cube import CubeList from iris.coords import AuxCoord from iris.util import squeeze import yaml -import numba +from numba import vectorize, int8, float32 -def create_mask(mesh_file, config_file=None): +def create_mask(mesh_file, maskglo_file, config_file=None): if not config_file: cwd = os.path.dirname(os.path.realpath(__file__)) config_file = os.path.join(cwd, 'basins.yml') @@ -19,43 +20,23 @@ def create_mask(mesh_file, config_file=None): lat = iris.load_cube(mesh_file, 'nav_lat').data lon = iris.load_cube(mesh_file, 'nav_lon').data basins = CubeList() - basin_model = sea_mask.copy() - - print('\nCreating basins from boxes') - for basin, basin_config in iteritems(config['basins']): - print('Creating basin', basin) - add_boxes = flatten_list(basin_config.get('add', [])) - add_mask = merge_boxes(add_boxes, lat, lon) - - remove_boxes = flatten_list(basin_config.get('remove', [])) - remove_mask = merge_boxes(remove_boxes, lat, lon) - - mask = add_mask - if remove_mask is not None: - mask = merge_mask(add_mask, remove_mask) - + basins_from_file = { + 'Atlantic_Ocean': 'tmaskatl', + 'Pacific_Ocean': 'tmaskpac', + 'Indian_Ocean': 'tmaskind', + } + for basin, variable in iteritems(basins_from_file): + mask = squeeze(iris.load_cube(maskglo_file, variable)).data.astype(np.int8) basin_mask = basin_model.copy() * mask basins.append(prepare_cube(basin_mask, basin)) - - print('\nCreating basins by merging others') - for basin, basin_config in iteritems(config['combined']): + print('\nCreating basins from boxes') + for basin, basin_config in iteritems(config['basins']): print('Creating basin', basin, 'from', basin_config) - - mask = merge_basic_basins( - basin_config.get('add', []), - basins - ) - remove_mask = merge_basic_basins( - basin_config.get('remove', []), - basins - ) - if remove_mask is not None: - mask = merge_mask(mask, normalize(remove_mask)) - - basin_mask = basin_model.copy(mask) + mask = create_basin(flatten_list(basin_config), lat, lon, basins) + basin_mask = basin_model.copy() * mask basins.append(prepare_cube(basin_mask, basin)) print('Merging and saving') @@ -68,7 +49,12 @@ def create_mask(mesh_file, config_file=None): AuxCoord(lat, 'longitude', 'nav_lon'), (1, 2) ) - iris.save(basins, 'basins.nc', zlib=True) + iris.save( + basins, + 'basins.nc', + zlib=True, + chunksizes=(1, basins.shape[1], basins.shape[2]) + ) import matplotlib matplotlib.use('Qt5Agg') @@ -83,7 +69,7 @@ def create_mask(mesh_file, config_file=None): qplt.pcolormesh(basin_slice) plt.show() -def flatten_list(config, final_type=dict): +def flatten_list(config): final_list = [] for conf in config: if isinstance(conf, dict) or isinstance(conf, string_types): @@ -92,33 +78,33 @@ def flatten_list(config, final_type=dict): final_list.extend(conf) return final_list -def merge_boxes(box_list, lat, lon): +def create_basin(box_list, lat, lon, basins): if not box_list: return None mask = None for box in box_list: - current_box = apply_config(box['lat'][0], box['lat'][1], lat) * apply_config(box['lon'][0], box['lon'][1], lon) - if mask is None: - mask = current_box - else: - mask += current_box - return normalize(mask) - -def merge_basic_basins(config, basins): - seas = [] - for sea in flatten_list(config): - seas.append(basins.extract(iris.Constraint(region=sea))[0]) - if not seas: - return None - - mask = None - for sea in seas: - if mask is None: - mask = sea.data.copy() - else: - mask += sea.data - return normalize(mask) + if 'lat' in box: + current_box = apply_config(box['lat'][0], box['lat'][1], lat) * apply_config(box['lon'][0], box['lon'][1], lon) + if mask is None: + mask = current_box + continue + next_mask = current_box + elif 'basin' in box: + sea = basins.extract(iris.Constraint(region=box['basin']))[0] + if mask is None: + mask = sea.data.copy() + continue + next_mask = sea.data + action = box.get('action', 'add') + if action == 'add': + mask = add(mask, next_mask) + elif action == 'remove': + mask = remove(mask, next_mask) + elif action == 'intersection': + mask = intersection(mask, next_mask) + + return mask def prepare_cube(cube, basin): cube.add_aux_coord(AuxCoord([basin], var_name='region')) @@ -128,7 +114,7 @@ def prepare_cube(cube, basin): cube.attributes = {} return cube -@numba.vectorize() +@vectorize([int8(float32, float32, float32)], nopython=True) def apply_config(min, max, data): if data < min: return 0 @@ -136,19 +122,19 @@ def apply_config(min, max, data): return 0 return 1 -@numba.vectorize() -def normalize(data): - if data < 1: - return 0 - return 1 +@vectorize([int8(int8, int8)], nopython=True) +def add(mask1, mask2): + if mask1 + mask2 > 0: + return 1 + return 0 -@numba.vectorize() -def merge_mask(add, remove): - if add - remove < 1: +@vectorize([int8(int8, int8)], nopython=True) +def remove(mask1, mask2): + if mask1 - mask2 < 1: return 0 return 1 -@numba.vectorize() +@vectorize([int8(int8, int8)], nopython=True) def intersection(mask1, mask2): if mask1 and mask2: return 1 @@ -157,5 +143,6 @@ def intersection(mask1, mask2): if __name__ == "__main__": create_mask( - '/Users/nube/cpg_tools/new_masks/mesh_mask_nemo.Ec3.2_O1L75.nc' + '/Users/nube/cpg_tools/new_masks/mesh_mask_nemo.Ec3.2_O1L75.nc', + '/Users/nube/cpg_tools/new_masks/new_maskglo.Ec3.2_O1L75.nc', ) \ No newline at end of file -- GitLab From 24b877336663fd9f8f83501c65c76278ff4721b5 Mon Sep 17 00:00:00 2001 From: jvegasbsc Date: Tue, 12 Mar 2019 18:09:52 +0100 Subject: [PATCH 4/5] =?UTF-8?q?Added=20Ni=C3=B1o=20regions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- new_masks/basins.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/new_masks/basins.yml b/new_masks/basins.yml index 82c8847..e91bc1d 100644 --- a/new_masks/basins.yml +++ b/new_masks/basins.yml @@ -416,3 +416,16 @@ basins: - {basin: North_Atlantic-Arctic} - {basin: Kara_Sea} - {basin: ArctOcn} + + Nino1+2: + - {lat: [-10, 0], lon: [-90, -80]} + + Nino3: + - {lat: [-5, 5], lon: [-150, -90]} + + Nino3.4: + - {lat: [-5, 5], lon: [-170, -120]} + + Nino4: + - {lat: [-5, 5], lon: [-180, -150]} + - {lat: [-5, 5], lon: [160, 180]} -- GitLab From c78e4dc3614cb39e2aca86115647eae727d86a8c Mon Sep 17 00:00:00 2001 From: Javier Vegas-Regidor Date: Fri, 31 May 2019 10:37:01 +0200 Subject: [PATCH 5/5] Add AMV regions --- new_masks/basins.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/new_masks/basins.yml b/new_masks/basins.yml index e91bc1d..201d6fe 100644 --- a/new_masks/basins.yml +++ b/new_masks/basins.yml @@ -429,3 +429,10 @@ basins: Nino4: - {lat: [-5, 5], lon: [-180, -150]} - {lat: [-5, 5], lon: [160, 180]} + + AMV_North_Atlantic: + - {lat: [0, 60], lon:[-80, 0]} + + AMV_trend: + - {lat: [-60, 60], lon: [-180, 180]} + \ No newline at end of file -- GitLab