Skip to content

Commit 96531ed

Browse files
authored
Merge pull request #105 from nansencenter/issue101-non-unique-parameter
fix #101: use Parameter filter instead of get in nansat_ingestor.Data…
2 parents 97eb510 + dba6d21 commit 96531ed

File tree

5 files changed

+64
-27
lines changed

5 files changed

+64
-27
lines changed

geospaas/catalog/tests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ class DatasetParameterTests(TestCase):
159159
def test_add_sar_sigma0(self):
160160
ds = Dataset.objects.get(pk=1)
161161
p = Parameter.objects.get(
162-
standard_name='surface_backwards_scattering_coefficient_of_radar_wave')
162+
standard_name='surface_backwards_scattering_coefficient_of_radar_wave',
163+
short_name='sigma0')
163164
dp = DatasetParameter(dataset=ds, parameter=p)
164165
dp.save()
165166
self.assertEqual(dp.parameter.short_name, 'sigma0')

geospaas/nansat_ingestor/managers.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,26 @@ def get_or_create(self,
135135
all_band_meta = n.bands()
136136
for band_id in range(1, len(all_band_meta)+1):
137137
band_meta = all_band_meta[band_id]
138-
if ('standard_name' in band_meta.keys()):
139-
# There is no need to introduce latitude and longitude as a paramter for
140-
# ingested dataset. Thus, these two are excluded from this loop
141-
if ((band_meta['standard_name'] != 'latitude')
142-
& (band_meta['standard_name'] != 'longitude')):
143-
pp = Parameter.objects.get(
144-
standard_name=band_meta['standard_name'])
145-
dsp, dsp_created = DatasetParameter.objects.get_or_create(
146-
dataset=ds, parameter=pp)
147-
ds.parameters.add(pp)
138+
standard_name = band_meta.get('standard_name', None)
139+
short_name = band_meta.get('short_name', None)
140+
units = band_meta.get('units', None)
141+
if standard_name in ['latitude', 'longitude', None]:
142+
continue
143+
params = Parameter.objects.filter(standard_name=standard_name)
144+
if params.count() > 1 and short_name is not None:
145+
params = params.filter(short_name=short_name)
146+
if params.count() > 1 and units is not None:
147+
params = params.filter(units=units)
148+
if params.count() >= 1:
149+
dsp, dsp_created = DatasetParameter.objects.get_or_create(
150+
dataset=ds, parameter=params[0])
151+
ds.parameters.add(params[0])
148152

149153
# create dataset URI
150-
ds_uri, _ = DatasetURI.objects.get_or_create(name=uri_service_name, service=uri_service_type, uri=uri,
151-
dataset=ds)
154+
ds_uri, _ = DatasetURI.objects.get_or_create(
155+
name=uri_service_name,
156+
service=uri_service_type,
157+
uri=uri,
158+
dataset=ds)
152159

153160
return ds, created

geospaas/nansat_ingestor/tests/tests.py renamed to geospaas/nansat_ingestor/tests.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,20 @@ class BasetForTests(TestCase):
8787
'suffix': 'testing',
8888
'units': 'testing',
8989
'wkv': 'testing'},
90-
90+
5: {'colormap': 'gray',
91+
'dataType': '6',
92+
'long_name': 'Normalized Radar Cross Section',
93+
'minmax': '0 0.1',
94+
'name': 'gamma0_HH',
95+
'PixelFunctionType': 'Sentinel1Calibration',
96+
'polarization': 'HH',
97+
'short_name': 'gamma0',
98+
'SourceBand': '1',
99+
'SourceFilename': '/vsimem/0BSD1QSPFL.vrt',
100+
'standard_name': 'surface_backwards_scattering_coefficient_of_radar_wave',
101+
'suffix': 'HH',
102+
'units': 'm/m',
103+
'wkv': 'surface_backwards_scattering_coefficient_of_radar_wave'},
91104
}
92105

93106
def setUp(self):
@@ -167,25 +180,31 @@ def test_fail_invalid_uri(self):
167180
ds, created = Dataset.objects.get_or_create(uri)
168181

169182
@patch('os.path.isfile')
170-
def test_exception_of_parameter_handling_for_longitude_and_latitude(self, mock_isfile):
171-
'''shall return standard all specified parameter
172-
without the parameter for latitude and longitude'''
183+
def test_dont_add_longitude_latitude(self, mock_isfile):
184+
""" shall not add latitude and longitude into DatasetParameter table """
173185
mock_isfile.return_value = True
174186
uri = 'file://localhost/some/folder/filename.ext'
175187
ds0, cr0 = Dataset.objects.get_or_create(uri)
188+
ds_params_standard_names = ds0.parameters.values_list('standard_name', flat=True)
176189
# longitude should not be one of the parameters
177-
self.assertNotIn(
178-
self.predefined_band_metadata_dict[3]['standard_name'],
179-
list(ds0.parameters.all().values('standard_name'))[0].values())
190+
self.assertNotIn('longitude', ds_params_standard_names)
180191
# latitude should not be one of the parameters
181-
self.assertNotIn(
182-
self.predefined_band_metadata_dict[4]['standard_name'],
183-
list(ds0.parameters.all().values('standard_name'))[0].values())
184-
# other parameters must be in the parameters
185-
self.assertIn(
186-
self.predefined_band_metadata_dict[2]['standard_name'],
187-
list(ds0.parameters.all().values('standard_name'))[0].values())
192+
self.assertNotIn('latidtude', ds_params_standard_names)
188193

194+
@patch('os.path.isfile')
195+
def test_add_sigma0_gamma0(self, mock_isfile):
196+
""" shall add both sigma0 and gamma0 with same standard name into DatasetParameter table """
197+
mock_isfile.return_value = True
198+
uri = 'file://localhost/some/folder/filename.ext'
199+
ds0, cr0 = Dataset.objects.get_or_create(uri)
200+
ds_params_standard_names = ds0.parameters.values_list('standard_name', flat=True)
201+
ds_params_short_names = ds0.parameters.values_list('short_name', flat=True)
202+
self.assertEqual(len(ds_params_standard_names), 2)
203+
self.assertEqual(len(ds_params_short_names), 2)
204+
self.assertIn('surface_backwards_scattering_coefficient_of_radar_wave',
205+
ds_params_standard_names)
206+
self.assertIn('sigma0', ds_params_short_names)
207+
self.assertIn('gamma0', ds_params_short_names)
189208

190209
class TestDatasetURI(BasetForTests):
191210
@patch('os.path.isfile')

geospaas/nansat_ingestor/tests/__init__.py

Whitespace-only changes.

geospaas/vocabularies/fixtures/vocabularies.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129639,5 +129639,15 @@
129639129639
},
129640129640
"model": "vocabularies.parameter",
129641129641
"pk": 58
129642+
},
129643+
{
129644+
"fields": {
129645+
"units": "m/m",
129646+
"standard_name": "surface_backwards_scattering_coefficient_of_radar_wave",
129647+
"gcmd_science_keyword": null,
129648+
"short_name": "gamma0"
129649+
},
129650+
"model": "vocabularies.parameter",
129651+
"pk": 59
129642129652
}
129643129653
]

0 commit comments

Comments
 (0)