def test_geo_cell_data_vector(self): gcdv = api.GeoCellDataVector() for i in range(5): p = api.GeoPoint(100, 200, 300) ltf = api.LandTypeFractions() ltf.set_fractions(glacier=0.1, lake=0.1, reservoir=0.1, forest=0.1) gcd = api.GeoCellData(p, 1000000.0, i, 0.9, ltf) gcdv.append(gcd) self.assertEqual(len(gcdv), 5) for i in range(5): self.assertEqual(gcdv[i].catchment_id(), i)
def test_geo_cell_data_vector(self): gcdv = api.GeoCellDataVector() for i in range(5): p = api.GeoPoint(100, 200, 300) ltf = api.LandTypeFractions() ltf.set_fractions(glacier=0.1, lake=0.1, reservoir=0.1, forest=0.1) gcd = api.GeoCellData(p, 1000000.0, i, 0.9, ltf) gcdv.append(gcd) self.assertEqual(len(gcdv), 5) for i in range(5): self.assertEqual(gcdv[i].catchment_id(), i) g2 = api.GeoCellDataVector(gcdv) # copy construct a new self.assertTrue(g2 == gcdv) g2[0].set_catchment_id(10) self.assertTrue(g2 != gcdv) # serialize gcdv_s= gcdv.serialize() self.assertGreater(len(gcdv_s),2) gcdv_deserialized = api.GeoCellDataVector.deserialize(gcdv_s) self.assertIsNotNone(gcdv_deserialized) self.assertTrue(gcdv_deserialized == gcdv)
def build_model(model_t, parameter_t, model_size, num_catchments=1): cell_area = 1000 * 1000 region_parameter = parameter_t() gcds = api.GeoCellDataVector() # creating models from geo_cell-data is easier and more flexible for i in range(model_size): gp = api.GeoPoint(500+ 1000.0*i,500.0, 500.0*i/model_size) cid = 0 if num_catchments > 1: cid = random.randint(1, num_catchments + 1) geo_cell_data = api.GeoCellData(gp, cell_area, cid, 0.9, api.LandTypeFractions(0.01, 0.05, 0.19, 0.3, 0.45)) geo_cell_data.land_type_fractions_info().set_fractions(glacier=0.01, lake=0.05, reservoir=0.19, forest=0.3) gcds.append(geo_cell_data) return model_t(gcds, region_parameter)
# using a scatter plot of the cells fig, ax = plt.subplots(figsize=(15, 5)) cm = plt.cm.get_cmap('rainbow') elv_col = ax.scatter(x, y, c=z, marker='.', s=40, lw=0, cmap=cm) # cm = plt.cm.get_cmap('gist_gray') # cid_col = ax.scatter(x, y, c=cid, marker='.', s=40, lw=0, alpha=0.4, cmap=cm) plt.colorbar(elv_col).set_label('catchment elevation [m]') # plt.colorbar(cid_col).set_label('catchment indices [id]') plt.title('Nea Nidelva Catchment') plt.show() # print(set(cid)) print(cell_data.variables.keys()) # let's first create a container that will hold all of our module domain cells cell_data_vector = api.GeoCellDataVector() # get dimensions from netcdf file num_cells = cell_data.dimensions['cell'].size for i in range(num_cells): gp = api.GeoPoint(x[i], y[i], z[i]) cid = cell_data.variables['catchment_id'][i] cell_area = cell_data.variables['area'][i] glac = cell_data.variables['glacier-fraction'][i] lake = cell_data.variables['lake-fraction'][i] rsvr = cell_data.variables['reservoir-fraction'][i] frst = cell_data.variables['forest-fraction'][i] unsp = 1 - (glac + lake + rsvr + frst)