Skip to content

Commit

Permalink
#389: merged with develop and resolved conflict in nansat_integration…
Browse files Browse the repository at this point in the history
…_tests/mapper_test_archive.py
  • Loading branch information
mortenwh committed Oct 24, 2018
2 parents d952300 + 50a2b8e commit fadae20
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
6 changes: 5 additions & 1 deletion nansat/mappers/mapper_opendap_arome.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ def __init__(self, filename, gdal_dataset, gdal_metadata, date=None,
self.dataset.SetMetadataItem('platform', json.dumps(ee))
self.dataset.SetMetadataItem('Data Center', 'NO/MET')
self.dataset.SetMetadataItem('Entry Title', str(ds.getncattr('title')))
self.dataset.SetMetadataItem('summary', str(ds.getncattr('summary')))
try:
# Skip the field if summary is missing in the ds
self.dataset.SetMetadataItem('summary', str(ds.getncattr('summary')))
except AttributeError:
pass

@staticmethod
def get_date(filename):
Expand Down
78 changes: 78 additions & 0 deletions nansat/mappers/mapper_opendap_mywave.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Name: mapper_opendap_mywave.py
# Purpose: Nansat mapping for MyWaveWAV data provided by MET.NO
# Author: Artem Moiseev
# Licence: This file is part of NANSAT. You can redistribute it or modify
# under the terms of GNU General Public License, v.3
# http://www.gnu.org/licenses/gpl-3.0.html

from nansat.mappers.opendap import Opendap
from nansat.exceptions import WrongMapperError
from nansat.nsr import NSR
from netCDF4 import Dataset
from datetime import datetime
import numpy as np
import json
import pythesint as pti
import os


class Mapper(Opendap):

baseURLs = ['http://thredds.met.no/thredds/dodsC/fou-hi/mywavewam4archive']
timeVarName = 'time'
xName = 'rlon'
yName = 'rlat'
timeCalendarStart = '1970-01-01'

def __init__(self, filename, gdal_dataset, gdal_metadata, date=None,
ds=None, bands=None, cachedir=None, *args, **kwargs):

self.test_mapper(filename)
timestamp = date if date else self.get_date(filename)
ds = Dataset(filename)
try:
self.srcDSProjection = NSR(ds.variables['projection_3'].proj4 +
' +to_meter=0.0174532925199 +wktext')
except KeyError:
raise WrongMapperError

self.create_vrt(filename, gdal_dataset, gdal_metadata, timestamp, ds, bands, cachedir)

self.dataset.SetMetadataItem('instrument', json.dumps(pti.get_gcmd_instrument('Computer')))
self.dataset.SetMetadataItem('platform', json.dumps(pti.get_gcmd_platform('MODELS')))
self.dataset.SetMetadataItem('Data Center', json.dumps(pti.get_gcmd_provider('NO/MET')))
self.dataset.SetMetadataItem('Entry Title', str(ds.getncattr('title')))
self.dataset.SetMetadataItem('Entry Title',
json.dumps(pti.get_iso19115_topic_category('Oceans')))
self.dataset.SetMetadataItem('gcmd_location',
json.dumps(pti.get_gcmd_location('sea surface')))

@staticmethod
def get_date(filename):
"""Extract date and time parameters from filename and return
it as a formatted (isoformat) string
Parameters
----------
filename: str
Returns
-------
str, YYYY-mm-ddThh:MM:00Z
Examples
--------
>>> Mapper.get_date('/path/to/MyWave_wam4_WAVE_20171029T18Z.nc')
'2017-10-29T18:00:00Z'
"""
_, filename = os.path.split(filename)
t = datetime.strptime(filename.split('_')[-1], '%Y%m%dT%HZ.nc')
return datetime.strftime(t, '%Y-%m-%dT%H:%M:00Z')

def convert_dstime_datetimes(self, ds_time):
"""Convert time variable to np.datetime64"""
ds_datetimes = np.array(
[(np.datetime64(self.timeCalendarStart).astype('M8[s]')
+ np.timedelta64(int(sec), 's').astype('m8[s]')) for sec in ds_time]).astype('M8[s]')
return ds_datetimes
14 changes: 14 additions & 0 deletions nansat/tests/mappers/test_mapper_opendap_mywave.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import unittest
from nansat.mappers.mapper_opendap_mywave import Mapper


class MyWaveOpenDAPTests(unittest.TestCase):

def setUp(self):
self.src = 'http://thredds.met.no/thredds/dodsC/fou-hi/mywavewam4archive' \
'/2017/10/29/MyWave_wam4_WAVE_20171029T18Z.nc'

def test_get_date(self):
res = Mapper.get_date(self.src)
self.assertIsInstance(res, str)
self.assertEqual(res, '2017-10-29T18:00:00Z')
5 changes: 5 additions & 0 deletions nansat_integration_tests/mapper_test_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,10 @@ class DataForTestingOnlineMappers(object):
'mapperName' : 'opendap_ostia',
'bands': ['analysed_sst', 'mask'],
'date': '2016-01-06'
},{
'fileName' : 'http://thredds.met.no/thredds/dodsC/fou-hi/mywavewam4archive/2017/10/29/MyWave_wam4_WAVE_20171029T18Z.nc',
'mapperName' : 'opendap_mywave4km',
'bands': ['hs'],
'date': '2017-10-29T18:00Z'
}
]

0 comments on commit fadae20

Please sign in to comment.