Skip to content

Error while trying to calculate connectivity #4

@ytmben

Description

@ytmben

Hi Nicholas,

Sorry to be bothering you with these issues, which most of the time are due to my lack of experience with Icthyop and in Python.
This time, I am trying to use the compute_connectivity function from ichthyop.connectivity

I read my .nc output file using ichhtyop.read.extract_dataset and then I tried to use compute_connectivity like so:

import ichthyop.read as ichread
import ichthyop.connectivity as ichconnectivity

data = ichread.extract_dataset("mercator3d_ichthyop-run202508041513.nc")

connect = ichconnectivity.compute_connectivity(data)

When I run the last line I get this big error, which as much as I understand means that there is not release_zone variable

KeyError                                  Traceback (most recent call last)
~/miniforge3/envs/ichthyop/lib/python3.13/site-packages/xarray/core/dataset.py in ?(self, name)
   1153             variable = self._variables[name]
   1154         except KeyError:
-> 1155             _, name, variable = _get_virtual_variable(self._variables, name, self.sizes)
   1156 

KeyError: 'release_zone'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
File ~/miniforge3/envs/ichthyop/lib/python3.13/site-packages/xarray/core/dataset.py:1252, in Dataset.__getitem__(self, key)
   1251 try:
-> 1252     return self._construct_dataarray(key)
   1253 except KeyError as e:

File ~/miniforge3/envs/ichthyop/lib/python3.13/site-packages/xarray/core/dataset.py:1155, in Dataset._construct_dataarray(self, name)
   1154 except KeyError:
-> 1155     _, name, variable = _get_virtual_variable(self._variables, name, self.sizes)
   1157 needed_dims = set(variable.dims)

File ~/miniforge3/envs/ichthyop/lib/python3.13/site-packages/xarray/core/dataset_utils.py:79, in _get_virtual_variable(variables, key, dim_sizes)
     78 if len(split_key) != 2:
---> 79     raise KeyError(key)
     81 ref_name, var_name = split_key

KeyError: 'release_zone'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-6-6f91ac6cb2a0> in ?()
----> 1 connect = ichconnectivity.compute_connectivity(data)

~/miniforge3/envs/ichthyop/lib/python3.13/site-packages/ichthyop/connectivity.py in ?(data, retention)
     33 
     34     ntime = data.dims['time']
     35     ndrifter = data.dims['drifter']
     36 
---> 37     release_zones = process_release_zones(data)
     38     release_names = [zone.name for zone in release_zones]
     39     nrel_zones = len(release_zones)
     40     zone = data['release_zone'].values

~/miniforge3/envs/ichthyop/lib/python3.13/site-packages/ichthyop/connectivity.py in ?(data)
     11 def process_release_zones(data):
     12 
---> 13     release_zones = data['release_zone']
     14     attrs = [v for v in release_zones.attrs if v.startswith('release_zone_')]
     15 
     16     zone_names = []

~/miniforge3/envs/ichthyop/lib/python3.13/site-packages/xarray/core/dataset.py in ?(self, key)
   1261 
   1262                 # If someone attempts `ds['foo' , 'bar']` instead of `ds[['foo', 'bar']]`
   1263                 if isinstance(key, tuple):
   1264                     message += f"\nHint: use a list to select multiple variables, for example `ds[{list(key)}]`"
-> 1265                 raise KeyError(message) from e
   1266 
   1267         if utils.iterable_of_hashable(key):
   1268             return self._copy_listed(key)

KeyError: "No variable named 'release_zone'. Did you mean one of ('recruited_zone',)?"

And actually, if I use ncdump -h on my output file, there is no variable named release_zone.
Below is the description of my output file:

netcdf mercator3d_ichthyop-run202508041513 {
dimensions:
	time = UNLIMITED ; // (7261 currently)
	drifter = 33000 ;
	type_zone = 3 ;
	recruitment_zone = 2 ;
	edge = 1224 ;
	latlon = 2 ;
	zone0 = 1 ;
	geozone0 = 5 ;
	zone1 = 745 ;
	geozone1 = 5 ;
	zone2 = 23 ;
	geozone2 = 5 ;
variables:
	double time(time) ;
		time:calendar = "gregorian" ;
		time:units = "seconds since 1900-01-01 00:00" ;
	float lon(time, drifter) ;
		lon:long_name = "particle longitude" ;
		lon:unit = "degree east" ;
	float lat(time, drifter) ;
		lat:long_name = "particle latitude" ;
		lat:unit = "degree north" ;
	int mortality(time, drifter) ;
		mortality:long_name = "particle cause of mortality" ;
		mortality:unit = "death cause" ;
		mortality:alive = 0 ;
		mortality:dead_cold = 1 ;
		mortality:out_of_domain = 2 ;
		mortality:old = 3 ;
		mortality:beached = 4 ;
		mortality:starvation = 5 ;
		mortality:dead_hot = 6 ;
		mortality:dead_fresh = 7 ;
		mortality:dead_saline = 8 ;
	float depth(time, drifter) ;
		depth:long_name = "particle depth" ;
		depth:unit = "meter" ;
	int zone(time, drifter, type_zone) ;
		zone:long_name = "In which zones is the particle ?" ;
		zone:unit = "the index of the zone" ;
		zone:type_zone_0 = "release" ;
		zone:release_zone_0 = "Emed release 1" ;
		zone:type_zone_1 = "recruitment" ;
		zone:recruitment_zone_0 = "Emed recruitment 1" ;
		zone:recruitment_zone_1 = "Emed recruitment 2" ;
		zone:type_zone_2 = "target" ;
		zone:target_zone = "none for this run" ;
		zone:not_released_yet = -99 ;
		zone:out_of_zone = -1 ;
	int recruited_zone(time, drifter, recruitment_zone) ;
		recruited_zone:long_name = "Is particle recruited in zone ?" ;
		recruited_zone:unit = "0:false 1:true" ;
		recruited_zone:recruitment_zone_0 = "Emed recruitment 1" ;
		recruited_zone:recruitment_zone_1 = "Emed recruitment 2" ;
	float region_edge(edge, latlon) ;
		region_edge:long_name = "geoposition of region edge" ;
		region_edge:unit = "lat degree north lon degree east" ;
	int drifter(drifter) ;
		drifter:long_name = "drifter index" ;
		drifter:unit = "" ;
	float coord_zone0(zone0, latlon) ;
		coord_zone0:long_name = "Emed release 1" ;
		coord_zone0:unit = "x and y coordinates of the center of the cells in the zone" ;
		coord_zone0:type = "release" ;
		coord_zone0:color = "[r=255,g=0,b=255]" ;
	float coord_geo_zone0(geozone0, latlon) ;
	float coord_zone1(zone1, latlon) ;
		coord_zone1:long_name = "Emed recruitment 1" ;
		coord_zone1:unit = "x and y coordinates of the center of the cells in the zone" ;
		coord_zone1:type = "recruitment" ;
		coord_zone1:color = "[r=102,g=255,b=102]" ;
	float coord_geo_zone1(geozone1, latlon) ;
	float coord_zone2(zone2, latlon) ;
		coord_zone2:long_name = "Emed recruitment 2" ;
		coord_zone2:unit = "x and y coordinates of the center of the cells in the zone" ;
		coord_zone2:type = "recruitment" ;
		coord_zone2:color = "[r=255,g=153,b=0]" ;
	float coord_geo_zone2(geozone2, latlon) ;

// global attributes:
		:transport_dimension = "3d" ;
		:release.schedule.is_enabled = "true" ;
		:release.schedule.events = "\"year 2013 month 06 day 01 at 12:00\" \"year 2013 month 06 day 03 at 12:00\" \"year 2013 month 06 day 06 at 12:00\" \"year 2013 month 06 day 09 at 12:00\" \"year 2013 month 06 day 12 at 12:00\" \"year 2013 month 06 day 15 at 12:00\" \"year 2013 month 06 day 18 at 12:00\" \"year 2013 month 06 day 21 at 12:00\" \"year 2013 month 06 day 24 at 12:00\" \"year 2013 month 06 day 27 at 12:00\" \"year 2013 month 06 day 30 at 12:00\" \"year 2013 month 07 day 03 at 12:00\" \"year 2013 month 07 day 06 at 12:00\" \"year 2013 month 07 day 09 at 12:00\" \"year 2013 month 07 day 12 at 12:00\" \"year 2013 month 07 day 15 at 12:00\" \"year 2013 month 07 day 18 at 12:00\" \"year 2013 month 07 day 21 at 12:00\" \"year 2013 month 07 day 24 at 12:00\" \"year 2013 month 07 day 27 at 12:00\" \"year 2013 month 07 day 30 at 12:00\" \"year 2013 month 08 day 02 at 12:00\" \"year 2013 month 08 day 15 at 12:00\" \"year 2013 month 08 day 30 at 12:00\" \"year 2013 month 09 day 01 at 12:00\" \"year 2013 month 09 day 15 at 12:00\" \"year 2013 month 09 day 30 at 12:00\" \"year 2013 month 10 day 01 at 12:00\" \"year 2013 month 10 day 15 at 12:00\" \"year 2013 month 10 day 31 at 12:00\" \"year 2013 month 11 day 01 at 12:00\" \"year 2013 month 11 day 15 at 12:00\" \"year 2013 month 11 day 30 at 12:00\"" ;
		:app.time.initial_time = "year 2013 month 01 day 01 at 12:00" ;
		:app.time.transport_duration = "0030 day(s) 00 hour(s) 00 minute(s)" ;
		:app.time.time_step = "360" ;
		:app.output.netcdf_output_format = "netcdf3" ;
		:app.output.file_prefix = "mercator3d" ;
		:app.output.output_path = "/home/yotam/Documents/oceanographic_processes_biological_implications/output/" ;
		:app.output.record_frequency = "12" ;
		:app.particle_length.initial_length = "0.89" ;
		:app.particle_length.hatch_length = "2.09" ;
		:app.particle_length.yolk2feeding_length = "2.6" ;
		:app.transport.coastline_behavior = "Standstill" ;
		:action.recruitment.stain.enabled = "false" ;
		:action.lethal_temp.enabled = "false" ;
		:action.recruitment.zone.enabled = "true" ;
		:action.recruitment.zone.criterion = "Age criterion" ;
		:action.recruitment.zone.limit_age = "12.0" ;
		:action.recruitment.zone.limit_length = "6.0" ;
		:action.recruitment.zone.duration_min = "2.0" ;
		:action.recruitment.zone.stop_moving = "true" ;
		:action.recruitment.zone.zone_file = "/home/yotam/Downloads/cfg/east_med_zones.xml" ;
		:action.advection.enabled = "true" ;
		:action.advection.scheme = "Runge Kutta 4" ;
		:action.swimming.enabled = "false" ;
		:action.growth.enabled = "false" ;
		:action.hdisp.enabled = "true" ;
		:action.hdisp.epsilon = "1E-9" ;
		:action.wind.enabled = "false" ;
		:release.txtfile.enabled = "false" ;
		:release.ncfile.enabled = "false" ;
		:release.zone.enabled = "false" ;
		:release.patches.enabled = "true" ;
		:release.patches.number_patches = "10" ;
		:release.patches.number_agregated = "100" ;
		:release.patches.radius_patch = "2000.0" ;
		:release.patches.thickness_patch = "2.0" ;
		:release.patches.zone_file = "/home/yotam/Downloads/cfg/east_med_zones.xml" ;
		:release.stain.enabled = "false" ;
		:dataset.mercator3d.enabled = "true" ;
		:dataset.mercator3d.input_path = "/home/yotam/Documents/oceanographic_processes_biological_implications/bashscripts/bashscripts_all/copernicusdata/" ;
		:dataset.mercator3d.hgr_pattern = "MED-MFC_006_004_coordinates_cropped.nc" ;
		:dataset.mercator3d.zgr_pattern = "MED-MFC_006_004_coordinates_cropped.nc" ;
		:dataset.mercator3d.byte_mask_pattern = "MED-MFC_006_004_mask_bathy_cropped.nc" ;
		:dataset.mercator3d.gridu_pattern = "*stks_data_*.nc" ;
		:dataset.mercator3d.gridv_pattern = "*stks_data_*.nc" ;
		:dataset.mercator3d.gridt_pattern = "*stks_data_*.nc" ;
		:dataset.mercator3d.field_dim_x = "longitude" ;
		:dataset.mercator3d.field_dim_y = "latitude" ;
		:dataset.mercator3d.field_dim_z = "depth" ;
		:dataset.mercator3d.field_dim_time = "time" ;
		:dataset.mercator3d.field_var_lon = "longitude" ;
		:dataset.mercator3d.field_var_lat = "latitude" ;
		:dataset.mercator3d.field_var_mask = "mask" ;
		:dataset.mercator3d.field_var_u = "uo" ;
		:dataset.mercator3d.field_var_v = "vo" ;
		:dataset.mercator3d.field_var_time = "time" ;
		:dataset.mercator3d.field_var_e3t = "e3t" ;
		:dataset.mercator3d.field_var_e2t = "e2t" ;
		:dataset.mercator3d.field_var_e1t = "e1t" ;
		:xml_file = "/home/yotam/Downloads/cfg/cfg-mercator3d-phd_1.xml" ;
		:nb_zones = 3 ;
}

Any help would be appreciated, and again I apologize for all these questions

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions