Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 464 logger instead of print #499

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions nansat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
from __future__ import absolute_import
import logging.config
import os
import sys
import os.path
import warnings
import importlib
import yaml

pixfun_module_name = 'nansat._pixfun_py{0}'.format(sys.version_info[0])

# check if pixel functions were compiled using setup_tools
Expand Down
7 changes: 5 additions & 2 deletions nansat/mappers/mapper_aapp_l1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
import sys
import struct
import datetime
import logging

from nansat.exceptions import WrongMapperError
from nansat.geolocation import Geolocation
from nansat.vrt import VRT

LOGGER = logging.getLogger("nansat.mappers."+__name__)
LOGGER.addHandler(logging.NullHandler())

satIDs = {4: 'NOAA-15', 2: 'NOAA-16', 6: 'NOAA-17', 7: 'NOAA-18', 8: 'NOAA-19',
11: 'Metop-B (Metop-1)', 12: 'Metop-A (Metop-2)',
13: 'Metop-C (Metop-3)'}
Expand Down Expand Up @@ -66,8 +70,7 @@ def __init__(self, filename, gdalDataset, gdalMetadata, **kwargs):
numCalibratedScanLines = int(struct.unpack('<H', fp.read(2))[0])
missingScanLines = int(struct.unpack('<H', fp.read(2))[0])
if missingScanLines != 0:
print('WARNING: Missing scanlines: ' + str(missingScanLines))

LOGGER.warning(f"Missing scanlines: {missingScanLines}")
##################
# Read time
##################
Expand Down
13 changes: 8 additions & 5 deletions nansat/mappers/mapper_aapp_l1c.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import struct
import datetime
import warnings
import logging

from nansat.exceptions import WrongMapperError
from nansat.geolocation import Geolocation
Expand All @@ -22,6 +23,8 @@
headerLength = recordLength
imageOffset = headerLength + 1092

LOGGER = logging.getLogger("nansat.mappers."+__name__)
LOGGER.addHandler(logging.NullHandler())

class Mapper(VRT):
''' VRT with mapping of WKV for AVHRR L1C output from AAPP '''
Expand Down Expand Up @@ -60,7 +63,7 @@ def __init__(self, filename, gdalDataset, gdalMetadata, **kwargs):
missingScanLines = int(struct.unpack('<l', fp.read(4))[0])
numCalibratedScanLines = int(struct.unpack('<l', fp.read(4))[0])
if missingScanLines != 0:
print('WARNING: Missing scanlines: ' + str(missingScanLines))
LOGGER.warning(f"Missing scanlines: {missingScanLines}")

fp.seek(88)
dataFormatNum = int(struct.unpack('<l', fp.read(4))[0])
Expand All @@ -84,10 +87,10 @@ def int2bitstring(s):
endsWith3A = False

if startsWith3A != endsWith3A:
print('############################################')
print('WARNING: channel 3 switches ')
print('between daytime and nighttime (3A <-> 3B)')
print('###########################################')
LOGGER.warning('############################################')
LOGGER.warning('WARNING: channel 3 switches ')
LOGGER.warning('between daytime and nighttime (3A <-> 3B)')
LOGGER.warning('###########################################')

###########################
# Make Geolocation Arrays
Expand Down
14 changes: 8 additions & 6 deletions nansat/mappers/mapper_geostationary.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
from datetime import datetime
from numpy import array, arange
import warnings
import logging

from nansat.exceptions import WrongMapperError
from nansat.vrt import VRT
from nansat.node import Node

LOGGER = logging.getLogger("nansat.mappers."+__name__)
LOGGER.addHandler(logging.NullHandler())

def arrays2LUTString(a, b):
LUTString = ''
Expand Down Expand Up @@ -176,24 +179,24 @@ def __init__(self, filename, gdalDataset, gdalMetadata, **kwargs):

for sat in satDict:
if sat['name'] == satellite:
print('This is ' + satellite)
LOGGER.info(f'This is {satellite}')
wavelengths = sat['wavelengths']
try:
scale = sat['scale']
offset = sat['offset']
except:
print("No scale and offset found")
LOGGER.info("No scale and offset found")
scale = None
offset = None
try:
LUT = sat['LUT']
except:
print("No LUT found")
LOGGER.info("No LUT found")
LUT = [""]*len(wavelengths)
try:
NODATA = sat['NODATA']
except:
print("No NODATA values found")
LOGGER.info("No NODATA values found")
NODATA = [""]*len(wavelengths)

if wavelengths is None:
Expand All @@ -220,8 +223,7 @@ def __init__(self, filename, gdalDataset, gdalMetadata, **kwargs):
try:
gdal.Open(bandSource)
except:
print('Warning: band missing for wavelength ' +
str(wavelength) + 'nm')
LOGGER.warning(f"band missing for wavelength {wavelength} nm")
continue
src = {'SourceFilename': bandSource, 'SourceBand': 1,
'LUT': LUT[i], 'NODATA': NODATA[i]}
Expand Down
5 changes: 4 additions & 1 deletion nansat/mappers/mapper_globcolour_l3b.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import json
import tempfile
import shutil
import logging

import numpy as np

Expand All @@ -22,6 +23,8 @@
from nansat.vrt import VRT
from nansat.mappers.globcolour import Globcolour

LOGGER = logging.getLogger("nansat.mappers."+__name__)
LOGGER.addHandler(logging.NullHandler())

class Mapper(VRT, Globcolour):
''' Create VRT with mapping of WKV for MERIS Level 2 (FR or RR)'''
Expand Down Expand Up @@ -73,7 +76,7 @@ def __init__(self, filename, gdalDataset, gdalMetadata, latlonGrid=None,
self.band_vrts = {'mask': [], 'lonlat': []}
mask = None
for simFile in simFiles:
print('sim: ', simFile)
LOGGER.info(f'sim: {simFile}')
# copy simFile to a temporary file
tmpf = tempfile.mkstemp()
shutil.copyfile(simFile, tmpf[1])
Expand Down
23 changes: 13 additions & 10 deletions nansat/mappers/mapper_globcolour_l3m.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import datetime
import os.path
import glob
import logging

import numpy as np

Expand All @@ -17,6 +18,8 @@
from nansat.utils import gdal, ogr
from nansat.exceptions import WrongMapperError

LOGGER = logging.getLogger("nansat.mappers."+__name__)
LOGGER.addHandler(logging.NullHandler())

class Mapper(VRT, Globcolour):
"""Mapper for GLOBCOLOR L3M products"""
Expand All @@ -25,7 +28,7 @@ def __init__(self, filename, gdalDataset, gdalMetadata, **kwargs):
''' GLOBCOLOR L3M VRT '''

try:
print("=>%s<=" % gdalMetadata['NC_GLOBAL#title'])
LOGGER.info(f"=>{gdalMetadata['NC_GLOBAL#title']}<=")
except (TypeError, KeyError):
raise WrongMapperError

Expand All @@ -35,15 +38,15 @@ def __init__(self, filename, gdalDataset, gdalMetadata, **kwargs):
# get list of similar (same date) files in the directory
iDir, iFile = os.path.split(filename)
iFileName, iFileExt = os.path.splitext(iFile)
print('idir:', iDir, iFile, iFileName[0:30], iFileExt[0:8])
LOGGER.info(f'idir: {iDir} {iFile} {iFileName[0:30]} {iFileExt[0:8]}')

simFilesMask = os.path.join(iDir, iFileName[0:30] + '*.nc')
simFiles = glob.glob(simFilesMask)
print('simFilesMask, simFiles', simFilesMask, simFiles)
LOGGER.info(f'simFilesMask:{simFilesMask}, simFiles:{simFiles}')

metaDict = []
for simFile in simFiles:
print('simFile', simFile)
LOGGER.info(f'simFile:{simFile}')
# open file, get metadata and get parameter name
simSupDataset = gdal.Open(simFile)
simSubDatasets = simSupDataset.GetSubDatasets()
Expand All @@ -56,7 +59,7 @@ def __init__(self, filename, gdalDataset, gdalMetadata, **kwargs):
simBandMetadata = simBand.GetMetadata()
simVarname = simBandMetadata['NETCDF_VARNAME']
# get WKV
print(' simVarname', simVarname)
LOGGER.info(f' simVarname:{simVarname}')
if simVarname in self.varname2wkv:
simWKV = self.varname2wkv[simVarname]
break
Expand Down Expand Up @@ -93,17 +96,17 @@ def __init__(self, filename, gdalDataset, gdalMetadata, **kwargs):
(simWavelength, solarIrradiance)
}

print(' metaEntry', metaEntry)
LOGGER.info(f' metaEntry:{metaEntry}')
metaDict.append(metaEntry)
if metaEntry2 is not None:
print(' metaEntry2', metaEntry2)
LOGGER.info(f' metaEntry2:{metaEntry2}')
metaDict.append(metaEntry2)

print('simSubDatasets', simValidSupDataset.GetSubDatasets())
LOGGER.info(f'simSubDatasets:{simValidSupDataset.GetSubDatasets()}')
for simSubDataset in simValidSupDataset.GetSubDatasets():
print('simSubDataset', simSubDataset)
LOGGER.info(f'simSubDataset:{simSubDataset}')
if '_flags ' in simSubDataset[1]:
print(' mask simSubDataset', simSubDataset[1])
LOGGER.info(f' mask simSubDataset:{simSubDataset[1]}')
flags = gdal.Open(simSubDataset[0]).ReadAsArray()
mask = np.ones(flags.shape) * 64
mask[np.bitwise_and(flags, np.power(2, 0)) > 0] = 1
Expand Down
6 changes: 4 additions & 2 deletions nansat/mappers/mapper_metno_local_hires_seaice.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
import sys
import os
from datetime import datetime, timedelta
import logging

from nansat.utils import gdal, ogr
from nansat.exceptions import WrongMapperError
from nansat.vrt import VRT
import nansat.mappers.mapper_generic as mg

LOGGER = logging.getLogger("nansat.mappers."+__name__)
LOGGER.addHandler(logging.NullHandler())

class Mapper(mg.Mapper):
"""Create VRT with mapping of WKV for Met.no seaice"""
Expand Down Expand Up @@ -52,8 +55,7 @@ def __init__(self, filename, gdalDataset, gdalMetadata, **kwargs):
filename = (ice_folder_name + 'ice_conc_svalbard_' +
valid_time.strftime('%Y%m%d1500.nc'))
if os.path.exists(filename):
print('Found file:')
print(filename)
LOGGER.info(f'Found file:{filename}')
gdal_dataset = gdal.Open(filename)
gdal_metadata = gdalDataset.GetMetadata()
mg.Mapper.__init__(self, filename, gdal_dataset, gdal_metadata)
Expand Down
18 changes: 11 additions & 7 deletions nansat/mappers/mapper_ncep_wind_online.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
import os
import sys
from datetime import datetime, timedelta
import logging

from nansat.vrt import VRT
from nansat.exceptions import WrongMapperError
from nansat.nansat import Nansat

LOGGER = logging.getLogger("nansat.mappers."+__name__)
LOGGER.addHandler(logging.NullHandler())

# Place to store downloads - this can be changed via the "outFolder" argument
# to Mapper.__init__
downloads = os.path.join(os.path.expanduser('~'), 'ncep_gfs_downloads')
Expand All @@ -49,7 +53,7 @@ def __init__(self, filename, gdalDataset, gdalMetadata,

time_str = filename[len(keyword_base)+1::]
time = datetime.strptime(time_str, '%Y%m%d%H%M')
print(time)
LOGGER.info(f'{time}')

########################################
# Find and download online grib file
Expand Down Expand Up @@ -86,13 +90,13 @@ def __init__(self, filename, gdalDataset, gdalMetadata,
'%.2d' % forecast_hour +
'.10m.uv.grib2'))
if os.path.exists(out_filename):
print('NCEP wind is already downloaded: ' + out_filename)
LOGGER.info(f'NCEP wind is already downloaded: {out_filename}')
else:
os.system('curl -so ' + out_filename + ' ' + url)
if os.path.exists(out_filename):
print('Downloaded ' + out_filename)
LOGGER.info(f'Downloaded {out_filename}')
else:
print('NRT GRIB file not available: ' + url)
LOGGER.info(f'NRT GRIB file not available: {url}')
#########################################################
# If NRT file not available, search in long term archive
#########################################################
Expand All @@ -103,7 +107,7 @@ def __init__(self, filename, gdalDataset, gdalMetadata,
'%.3d' % forecast_hour)
filename = basename + '.grb2'
out_filename = os.path.join(outFolder, filename)
print('Downloading ' + url + filename)
LOGGER.info(f'Downloading {url}{filename}')

# Download subset of grib file
mapper_dir = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -121,9 +125,9 @@ def __init__(self, filename, gdalDataset, gdalMetadata,
get_grib + ' ' + url + filename + ' ' + out_filename)
os.system(command)
if os.path.isfile(out_filename):
print('Downloaded ' + filename + ' to ' + outFolder)
LOGGER.info(f'Downloaded {filename} to {outFolder}')
else:
print('Already downloaded %s' % out_filename)
LOGGER.info(f'Already downloaded {out_filename}')

if not os.path.isfile(out_filename):
sys.exit('No NCEP wind files found for requested time')
Expand Down
6 changes: 5 additions & 1 deletion nansat/mappers/mapper_obpg_l3.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import datetime
import os.path
import glob
import logging

import numpy as np

Expand All @@ -18,6 +19,9 @@

from nansat.exceptions import WrongMapperError

LOGGER = logging.getLogger("nansat.mappers."+__name__)
LOGGER.addHandler(logging.NullHandler())

class Mapper(VRT):
''' Mapper for Level-3 Standard Mapped Image from
http://oceancolor.gsfc.nasa.gov'''
Expand Down Expand Up @@ -85,7 +89,7 @@ def __init__(self, filename, gdalDataset, gdalMetadata, **kwargs):
tmpGdalMetadata = tmpGdalDataset.GetMetadata()
simParameter = tmpGdalMetadata['Parameter']
except:
print('No parameter: %s not a supported SMI file')
LOGGER.info(f"No parameter: {tmpGdalMetadata['Parameter']} not a supported SMI file")
continue
else:
# set params of the similar file
Expand Down
6 changes: 5 additions & 1 deletion nansat/mappers/mapper_ocean_productivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import datetime
import os.path
import glob
import logging

import numpy as np

Expand All @@ -17,6 +18,9 @@

from nansat.exceptions import WrongMapperError

LOGGER = logging.getLogger("nansat.mappers."+__name__)
LOGGER.addHandler(logging.NullHandler())

class Mapper(VRT):
''' Mapper for Ocean Productivity website
http://www.science.oregonstate.edu/ocean.productivity/'''
Expand All @@ -43,7 +47,7 @@ def __init__(self, filename, gdalDataset, gdalMetadata, **kwargs):
except:
raise WrongMapperError

print('Ocean Productivity website data')
LOGGER.info(f"Ocean Productivity website data")
# get list of similar (same date) files in the directory
iDir, iFile = os.path.split(filename)
iFileName, iFileExt = os.path.splitext(iFile)
Expand Down
Loading