Commit c189b230 authored by GROUAZEL's avatar GROUAZEL

adapt code reader sentinel1 WV L2 to different version of processor

parent cd85b93b
import abc
import datetime
import numpy as np
from dateutil import parser
from netCDF4 import Dataset
......@@ -23,7 +24,7 @@ class AbstractSpectrumReader():
class SpectrumReaderSat(AbstractSpectrumReader):
def read_spectrum(self,ncf=None,mode='satfull',dataName='UNK'):
def read_spectrum(self,ncf=None,mode='satfull',dataName='UNK',filepath=None):
"""
read_spectrum_sat: Function that return a spectrum object from a satellite file
ncf: Net CDF File already open
......@@ -46,6 +47,7 @@ class SpectrumReaderSat(AbstractSpectrumReader):
myspectrum = read_spectrum_sat(path='path/to/ncfile.nc',mode="satimaginary")
"""
self.filepath = filepath
#index is useless for now
myspec_data = self.read_spec_data(ncf,mode)
mywsys = self.read_wsys(ncf)
......@@ -80,16 +82,17 @@ class SpectrumReaderSat(AbstractSpectrumReader):
spectrumdict={}
modes = ['satfull','satreal','satimaginary']
if isinstance(ncf, str):
fileToRead = Dataset(ncf, 'r')
self.handler = Dataset(ncf, 'r')
self.filepath = ncf
else:
fileToRead = ncf
self.handler = ncf
for mode in modes:
myspectrum = self.read_spectrum(ncf=fileToRead,mode=mode)
myspectrum = self.read_spectrum(ncf=self.handler,mode=mode)
# myspectrum = SpectrumReaderSat.read_spectrum(ncf=fileToRead,mode=mode,dataName=dataName)
spectrumdict[mode] = myspectrum
if isinstance(ncf, str):
fileToRead.close()
if isinstance(self.handler, str):
self.handler.close()
return spectrumdict
@staticmethod
......@@ -110,8 +113,7 @@ class SpectrumReaderSat(AbstractSpectrumReader):
# logging.debug('qc partition = %s',wsys['qcpartition'])
return wsys
@staticmethod
def read_wind(ncf):
def read_wind(self,ncf):
"""
read_wind_sat: Function that return a wind dictionary from a sat file
oswWindDirection:long_name = "SAR Wind direction (meteorological convention=clockwise direction from where the wind comes respect to North)"
......@@ -121,7 +123,16 @@ class SpectrumReaderSat(AbstractSpectrumReader):
# wind['U'] = ncf.variables['oswWindSpeed'][:][0][0]*np.cos(90- ncf.variables['oswWindDirection'][:][0][0]*np.pi/180.) #original version from MLB ?
# wind['V'] = ncf.variables['oswWindSpeed'][:][0][0]*np.sin(90- ncf.variables['oswWindDirection'][:][0][0]*np.pi/180.)
# oswWindDirection:long_name = "SAR Wind direction (meteorological convention=clockwise direction from where the wind comes respect to North)"
dirwind = ncf.variables['oswWindDirection'][:][0][0]
dirwind = ncf.variables['oswWindDirection'][:][0][0] # note: oswWindDirection exactly equal to oiswWindDirection
if self.filepath is None:
datedt = datetime.datetime.strptime(ncf.getattr('firstMeasurementTime'),'%Y-%m-%dT%H:%M:%S.%fZ')
else:
datedt = datetime.datetime.strptime(os.path.basename(self.filepath).split('-')[4],'%Y%m%dt%H%M%S')
if datedt < datetime.datetime(2018,3,13) : # date of convention change with IPF 2.90
value = (ncf.variables['oswWindDirection'][0][
0].squeeze() + 180.) % 360. # to have a value in the TO convention
else :
value = ncf.variables['oswWindDirection'][0][0].squeeze()
logging.debug('ESA direction given in convention FROM %s',dirwind)
dirwind = np.mod(dirwind+180,360)
logging.debug('direction given in convention TO %s',dirwind)
......@@ -129,8 +140,10 @@ class SpectrumReaderSat(AbstractSpectrumReader):
# wind['U'] = ncf.variables['oswWindSpeed'][:][0][0]*np.cos(np.radians(dirwind)) #correction A. Grouazel Sept 2019
# wind['V'] = ncf.variables['oswWindSpeed'][:][0][0]*np.sin(np.radians(dirwind))
# else:
# variable_wind_speed = "oswWindSpeed"
variable_wind_speed = 'oswEcmwfWindSpeed' #change asked by A.Mouche June 2020 to align filters in xwaves web app and png generated/displayed
if 'oswEcmwfWindSpeed' not in ncf.variables.keys():
variable_wind_speed = "oswWindSpeed" #because on old IPF version there was no oswEcmwfWindSpeed variable
else:
variable_wind_speed = 'oswEcmwfWindSpeed' #change asked by A.Mouche June 2020 to align filters in xwaves web app and png generated/displayed
wind['U'] = ncf.variables[variable_wind_speed][:][0][0]*np.cos(np.pi/2-np.radians(dirwind)) #correction A. Grouazel Sept 2019
wind['V'] = ncf.variables[variable_wind_speed][:][0][0]*np.sin(np.pi/2-np.radians(dirwind))
wind['dir'] = dirwind
......
......@@ -19,7 +19,7 @@ from SpectrumReaderSentinel import SpectrumReaderSat
def do_simple_spectra(filepath1,title,dirout=None,mode='satfull'):
nc=netCDF4.Dataset(filepath1)
inst = SpectrumReaderSat()
spectra = inst.read_spectrum(nc,dataName=title,mode=mode)
spectra = inst.read_spectrum(nc,dataName=title,mode=mode,filepath=filepath1)
# print(dir(spectra))
# print(spectra.spec_data.sp)
style0 = spc.init_spectrum_style(mode)
......@@ -84,4 +84,4 @@ if __name__ =='__main__':
do_simple_spectra(filepath1,title = 'S1 Im image spectra\n %s' % datestr,dirout = dirout,mode = 'satimaginary')
ffww3 = '/home/cercache/project/mpc-sentinel1/data/colocation/sentinel-1b/sar-model/ww3spectra/201905/netCDF_L2_daily/20190502/ww3-wv1-ocn-vv-20190502t001225-20190502t001228-016057-01e33a-001.nc'
ffww3 = '/home/datawork-cersat-public/project/mpc-sentinel1/data/colocation/sentinel-1b/sar-model/ww3spectra/202002/netCDF_L2_daily/20200229/ww3-wv2-ocn-vv-20200229t024310-20200229t024313-020477-026ccb-138.nc'
do_simple_spectra_ww3(ffww3,'test ww3',dirout = dirout,mode = 'ww3')
\ No newline at end of file
do_simple_spectra_ww3(ffww3,'test ww3',dirout = dirout,mode = 'ww3')
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