Commit bb93bc34 authored by CEVAER's avatar CEVAER
Browse files

Initial commit

parents
Provides help functions to open and read owi files (netCDF files)
Package installation :
pip install git+https://git.cersat.fr/oarcher/pathurl.git
In setup.py of other project add :
dependency_links=['https://git.cersat.fr/oarcher/pathurl/tree/master/']
install_requires=['pathurl']
\ No newline at end of file
#!/usr/bin/env python
import sys
import os
import datetime
import glob
import netCDF4
import numpy as np
import numpy.ma as ma
import logging
import pdb
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
def readFile(fname):
data={}
#list_var=('owiLon','owiLat','owiHeading','owiEcmwfWindSpeed','owiWindSpeed_IPF', 'owiPreProcessing/estimWindDir200', 'owiPreProcessing/MixWindDir200', 'owiPreProcessing/CoefMixWindDir200', 'owiEcmwfWindDirection','owiWindDirection_mod1','owiWindSpeed_mod1','owiWindDirection_mod2','owiWindSpeed_mod2','owiWindDirection_mod3','owiWindSpeed_mod3','owiPreProcessing/FilterBinary200','owiLandFlag','owiIncidenceAngle')
try:
nc=netCDF4.Dataset(fname)
except Exception, e:
logger.warn('skipping unreadable %s : %s' % (fname, str(e)))
return None
# read owiLon to get valid shape
data['owiLon']=nc.variables['owiLon'][::]
refDims=nc.variables['owiLon'].dimensions
#refShape=np.shape(data['owiLon'])
#pdb.set_trace()
# read regular var
#for varname in nc.variables:
# var=nc.variables[varname]
# if var.dimensions == refDims:
# pdb.set_trace()
# data[varname]=var[::]
# read groups
for groupName in [ None ] + nc.groups.keys():
if groupName:
group=nc.groups[groupName]
groupNamePref="%s/" % groupName
else:
group=nc
groupNamePref=""
groupAttrs=group.ncattrs()
for varName in group.variables:
var=group[varName]
dims=var.dimensions
if dims == refDims:
data["%s%s" % (groupNamePref,varName)] = var[::]
else:
# some vars are 3D:
# pdb.set_trace()
try:
if dims[1:] == refDims:
values3D=var[::]
for islice in range(np.shape(values3D)[0]):
value=values3D[islice,:,:]
try:
# try to find sliceName from varName and ncattrs
sliceName=group.getncattr([ groupAttr for groupAttr in groupAttrs if groupAttr in varName ][0]).split(', ')[islice]
except:
sliceName="%d" % islice
#pdb.set_trace()
data["%s%s/%s" % (groupNamePref,varName, sliceName)]=value
#pdb.set_trace()
except:
pass
#for var in nc.groups[group].variables:
# values=nc.groups[group].variables[var][::]
# if np.shape(values) == refShape:
# data["%s/%s" % (group,var)]=values
nc.close
return data
def readMeta(fname):
data={}
#list_var=('owiLon','owiLat','owiHeading','owiEcmwfWindSpeed','owiWindSpeed_IPF', 'owiPreProcessing/estimWindDir200', 'owiPreProcessing/MixWindDir200', 'owiPreProcessing/CoefMixWindDir200', 'owiEcmwfWindDirection','owiWindDirection_mod1','owiWindSpeed_mod1','owiWindDirection_mod2','owiWindSpeed_mod2','owiWindDirection_mod3','owiWindSpeed_mod3','owiPreProcessing/FilterBinary200','owiLandFlag','owiIncidenceAngle')
try:
nc=netCDF4.Dataset(fname)
except Exception, e:
logger.warn('skipping unreadable %s : %s' % (fname, str(e)))
return None
# read globals attr
for attr in nc.ncattrs():
data[attr]=nc.getncattr(attr)
#logger.warn("got attr : %s = %s" % (attr,nc.getncattr(attr)))
refDims=nc.variables['owiLon'].dimensions
# read groups
for groupName in [ None ] + nc.groups.keys():
if groupName:
group=nc.groups[groupName]
groupNamePref="%s/" % groupName
else:
group=nc
groupNamePref=""
groupAttrs=group.ncattrs()
for varName in group.variables:
var=group[varName]
data["%s%s" % (groupNamePref,varName)]={}
try:
data["%s%s" % (groupNamePref,varName)]['description']=var.long_name
except:
data["%s%s" % (groupNamePref,varName)]['description']=None
#print "XXXX %s" % (varName)
try:
data["%s%s" % (groupNamePref,varName)]['units']=var.units
except:
data["%s%s" % (groupNamePref,varName)]['units']=None
dims=var.dimensions
if dims != refDims:
# some vars are 3D:
# pdb.set_trace()
try:
if dims[1:] == refDims:
#values3D=var[::]
for islice in range(var.shape[0]):
#value=values3D[islice,:,:]
try:
# try to find sliceName from varName and ncattrs
sliceName=group.getncattr([ groupAttr for groupAttr in groupAttrs if groupAttr in varName ][0]).split(', ')[islice]
#print "got meta slice : %s" %sliceName
except:
sliceName="%d" % islice
#pdb.set_trace()
data["%s%s/%s" % (groupNamePref,varName, sliceName)]={}
try:
data["%s%s/%s" % (groupNamePref,varName, sliceName)]['description']=var.long_name
except:
data["%s%s/%s" % (groupNamePref,varName, sliceName)]['description']=None
try:
data["%s%s/%s" % (groupNamePref,varName, sliceName)]['units']=var.units
except:
data["%s%s/%s" % (groupNamePref,varName, sliceName)]['units']=None
#pdb.set_trace()
except:
pass
nc.close
return data
def getAttribute(fname, attr):
try:
nc=netCDF4.Dataset(fname)
except Exception, e:
logger.warn('skipping unreadable %s : %s' % (fname, str(e)))
return None
if attr in nc.ncattrs():
return getattr(nc, attr)
return None
def getFootprint(fname):
try:
nc=netCDF4.Dataset(fname)
except Exception, e:
logger.warn('skipping unreadable %s : %s' % (fname, str(e)))
return None
if "footprint" in nc.ncattrs():
return nc.footprint
else:
return None
def getDateFromFname(fname):
tmp=os.path.basename(fname).split('-')
startDate=datetime.datetime.strptime(tmp[4],"%Y%m%dt%H%M%S")
stopDate=datetime.datetime.strptime(tmp[5],"%Y%m%dt%H%M%S")
date=startDate+(stopDate-startDate)/2
return date
def getDataAtCoord(data,lon,lat):
import geo
dataAtCoord={}
#logger.info("lon : %s" % str(lon))
landFlag=data['owiLandFlag']
landFlag[landFlag>0]=1
Mask = landFlag
# Mask[Mask>0]=1
# Mask1=data['owiLandFlag']
# Mask2=np.logical_not(data['FilterBinary200'])
lons = ma.array(data['owiLon'],mask=Mask)
lats = ma.array(data['owiLat'],mask=Mask)
# lons = ma.array(data['owiLon'],mask=Mask2)
# lats = ma.array(data['owiLat'],mask=Mask2)
# lons = data['owiLon']
# lats = data['owiLat']
dist,bearing=geo.haversine(lon, lat,lons, lats)
mindist=np.min(dist)
imindist=np.argmin(dist)
if mindist > 20: #
logger.warn("Buoy to far from sar image (%s km)" % mindist)
return None
data['dist_km']=mindist
for key in data:
try:
dataAtCoord[key]=np.ravel(data[key])[imindist]
except:
dataAtCoord[key]=data[key]
# convert dir in image conv to oceano convention
# if ("Dir" in key) and (not key.startswith("DirectionModifie")) and (not key.startswith("Coef")) :
if ("Dir200" in key) and (not key.startswith("DirectionModifie")) and (not key.startswith("Coef")) :
if dataAtCoord[key] >180:
dataAtCoord[key]=dataAtCoord[key]-180
else :
dataAtCoord[key]=dataAtCoord[key]+180
return dataAtCoord
\ No newline at end of file
from setuptools import setup
setup(name='owi',
description='Owi netCDF reader',
url='https://git.cersat.fr/oarcher/owi.git',
version = "0.0.1",
author = "Theo Cevaer",
author_email = "Theo.Cevaer@ifremer.fr",
license='GPL',
packages=['owi'],
zip_safe=False,
install_requires=[
'numpy', 'netCDF4'
])
#scripts=['bin/pathUrl.py'])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment