Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
close db connection
  • Loading branch information
mortenwh committed Aug 20, 2023
commit eb5d5fbaecab418ef3850b94c61032c905c6a55b
10 changes: 10 additions & 0 deletions geospaas/nansat_ingestor/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pythesint as pti
from django.contrib.gis.geos import WKTReader
from django.db import connection
from django.db import models

from geospaas.catalog.managers import FILE_SERVICE_NAME, LOCAL_FILE_SERVICE
Expand Down Expand Up @@ -56,6 +57,7 @@ def get_or_create(self,
uris = DatasetURI.objects.filter(uri=uri, **uri_filter_args)
if len(uris) > 0:
return uris[0].dataset, False
connection.close()

# Open file with Nansat
n = Nansat(nansat_filename(uri), **kwargs)
Expand All @@ -72,17 +74,20 @@ def get_or_create(self,
pp_entry = [elem.strip() for elem in pp.split('>')]
pp_dict = pti.get_gcmd_platform(pp_entry[-1].split('(')[1][0:-1])
platform, _ = Platform.objects.get_or_create(pp_dict)
connection.close()
ii = n_metadata['instrument']
try:
ii_dict = json.loads(ii)
except json.JSONDecodeError:
ii_entry = [elem.strip() for elem in ii.split('>')]
ii_dict = pti.get_gcmd_instrument(ii_entry[-1].split('(')[1][0:-1])
instrument, _ = Instrument.objects.get_or_create(ii_dict)
connection.close()
specs = n_metadata.get('specs', '')
source, _ = Source.objects.get_or_create(platform=platform,
instrument=instrument,
specs=specs)
connection.close()

default_char_fields = {
# Adding NERSC_ in front of the id violates the string representation of the uuid
Expand All @@ -98,6 +103,7 @@ def get_or_create(self,
existing_ds = Dataset.objects.get(entry_id=entry_id)
except Dataset.DoesNotExist:
existing_ds = None
connection.close()
for name in default_char_fields:
if name not in n_metadata:
logging.debug('%s is not provided in Nansat metadata!' % name)
Expand Down Expand Up @@ -140,6 +146,7 @@ def get_or_create(self,
n.reproject_gcps()
geolocation = GeographicLocation.objects.get_or_create(
geometry=WKTReader().read(n.get_border_wkt(nPoints=n_points)))[0]
connection.close()

# create dataset
# - the get_or_create method should use get_or_create here as well,
Expand All @@ -155,6 +162,7 @@ def get_or_create(self,
'entry_title': options["entry_title"],
'summary': options["summary"]}
)
connection.close()

# create parameter
all_band_meta = n.bands()
Expand All @@ -172,12 +180,14 @@ def get_or_create(self,
params = params.filter(units=units)
if params.count() >= 1:
ds.parameters.add(params[0])
connection.close()

# create dataset URI
DatasetURI.objects.get_or_create(
name=uri_service_name,
service=uri_service_type,
uri=uri,
dataset=ds)
connection.close()

return ds, created