Commit 07e909c8 authored by Alexandre Levieux's avatar Alexandre Levieux

Fix bug generate spectre render sentinel 1

parent fec0b4bb
.idea/
__pycache__/
*.pyc
......@@ -8,21 +8,22 @@ import logging
import spectrumUtil as spUtil
import os
class AbstractSpectrumReader():
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def read_spectrum(ncfile,mode="satfull",index=None):
def read_spectrum(ncfile, mode="satfull", index=None):
pass
#-----------------------------------------------
# -----------------------------------------------
# Reader for Buoys Globwave File (Ndbc and cdip)
#-----------------------------------------------
# -----------------------------------------------
class SpectrumReaderBuoyGlobwave(AbstractSpectrumReader):
@staticmethod
def read_spectrum(ncf,index,mode="buoyglobwavendbc",phi = np.arange(0,365,10),dataName='UNK'):
def read_spectrum(ncf, index, mode="buoyglobwavendbc", phi=np.arange(0, 365, 10), dataName='UNK'):
"""
read_spectra_globwave: Function that return a spectrum object from a globwave file
ncf: Net CDF File already open
......@@ -42,103 +43,105 @@ class SpectrumReaderBuoyGlobwave(AbstractSpectrumReader):
"""
if isinstance(ncf, basestring):
fileToRead = Dataset(ncf, 'r')
fileToRead = Dataset(ncf, 'r')
else:
fileToRead = ncf
mywind = SpectrumReaderBuoyGlobwave.read_wind_globwave(fileToRead,index)
mywsys = SpectrumReaderBuoyGlobwave.read_wsys_globwave(fileToRead,index)
myspec_data = SpectrumReaderBuoyGlobwave.read_spec_data_globwave(fileToRead,index,mode,phi = phi)
mymeta = SpectrumReaderBuoyGlobwave.read_meta_globwave(fileToRead,index,mode)
mywind = SpectrumReaderBuoyGlobwave.read_wind_globwave(fileToRead, index)
mywsys = SpectrumReaderBuoyGlobwave.read_wsys_globwave(fileToRead, index)
myspec_data = SpectrumReaderBuoyGlobwave.read_spec_data_globwave(fileToRead, index, mode, phi=phi)
mymeta = SpectrumReaderBuoyGlobwave.read_meta_globwave(fileToRead, index, mode)
if isinstance(ncf, basestring):
fileToRead.close()
return spc.Spectrum(myspec_data,mywsys,mywind,mymeta,dataName=dataName)
return spc.Spectrum(myspec_data, mywsys, mywind, mymeta, dataName=dataName)
@staticmethod
def read_wind_globwave(ncf,index):
wind={}
def read_wind_globwave(ncf, index):
wind = {}
return wind
@staticmethod
def read_wsys_globwave(ncf,index):
lon,lat =None,None
def read_wsys_globwave(ncf, index):
lon, lat = None, None
lat = np.squeeze(ncf.variables['lat'][:])
lon = np.squeeze(ncf.variables['lon'][:])
wsys={'lon':lon,'lat':lat}
wsys = {'lon': lon, 'lat': lat}
return wsys
@staticmethod
def read_spec_data_globwave(ncf,index,mode='buoyglobwavendbc',phi = np.arange(0,365,10),con='to'):
time = nc.num2date( ncf.variables['time'][:], ncf.variables['time'].units )
def read_spec_data_globwave(ncf, index, mode='buoyglobwavendbc', phi=np.arange(0, 365, 10), con='to'):
time = nc.num2date(ncf.variables['time'][:], ncf.variables['time'].units)
time = time[int(index)]
c1 = ncf.variables['theta1'][index,:] # theta1
c2 = ncf.variables['theta2'][index,:] # theta2
c3 = ncf.variables['stheta1'][index,:] # stheta1
c4 = ncf.variables['stheta2'][index,:] # stheta2
c1 = ncf.variables['theta1'][index, :] # theta1
c2 = ncf.variables['theta2'][index, :] # theta2
c3 = ncf.variables['stheta1'][index, :] # stheta1
c4 = ncf.variables['stheta2'][index, :] # stheta2
# convert theta & stheta(globwave data used) to a1,b1,a2,b2
a1,a2,b1,b2 = spUtil.th2ab(c1,c2,c3,c4)
a1, a2, b1, b2 = spUtil.th2ab(c1, c2, c3, c4)
if mode == "buoyglobwavendbc":
f,sf = SpectrumReaderBuoyGlobwave.read_spec_data_ndbc_globwave(ncf,index)
f, sf = SpectrumReaderBuoyGlobwave.read_spec_data_ndbc_globwave(ncf, index)
else:
f,sf = SpectrumReaderBuoyGlobwave.read_spec_data_cdip_globwave(ncf,index)
f, sf = SpectrumReaderBuoyGlobwave.read_spec_data_cdip_globwave(ncf, index)
spfphitmp,d = spUtil.buoy_spectrum2d(sf,a1,a2,b1,b2, dirs = phi)
spfphi=[spfphitmp]
#spfphi2kphi wait an array of values
spfphitmp, d = spUtil.buoy_spectrum2d(sf, a1, a2, b1, b2, dirs=phi)
spfphi = [spfphitmp]
# spfphi2kphi wait an array of values
sp2d,k = spUtil.spfphi2kphi(spfphi,f)
sp2d, k = spUtil.spfphi2kphi(spfphi, f)
if con=='to':
sp2d = spUtil.spfrom2to(sp2d,phi)
#sp2d is an array of values : so we use the first and only element
spec_data = spc.Spec_data(k=k,phi=phi,sp=sp2d[0],start_time=time,)
if con == 'to':
sp2d = spUtil.spfrom2to(sp2d, phi)
# sp2d is an array of values : so we use the first and only element
spec_data = spc.Spec_data(k=k, phi=phi, sp=sp2d[0], start_time=time, )
return spec_data
@staticmethod
def read_spec_data_ndbc_globwave(ncf,index):
def read_spec_data_ndbc_globwave(ncf, index):
f = np.squeeze(ncf.variables['wave_directional_spectrum_central_frequency'][:])
sf = ncf.variables['spectral_wave_density'][index,:]
return f,sf
sf = ncf.variables['spectral_wave_density'][index, :]
return f, sf
@staticmethod
def read_spec_data_cdip_globwave(ncf,index):
f = np.squeeze(ncf.variables['central_frequency'][index,:])
sf = ncf.variables['sea_surface_variance_spectral_density'][index,:]
return f,sf
def read_spec_data_cdip_globwave(ncf, index):
f = np.squeeze(ncf.variables['central_frequency'][index, :])
sf = ncf.variables['sea_surface_variance_spectral_density'][index, :]
return f, sf
@staticmethod
def read_meta_globwave(ncf,index,mode='buoyglobwavendbc'):
def read_meta_globwave(ncf, index, mode='buoyglobwavendbc'):
if mode == "buoyglobwavendbc":
mymeta = spc.Meta(type=mode)
else:
mymeta = SpectrumReaderBuoyGlobwave.read_meta_cdip_globwave(ncf,index,mode)
return mymeta
mymeta = SpectrumReaderBuoyGlobwave.read_meta_cdip_globwave(ncf, index, mode)
return mymeta
@staticmethod
def read_meta_cdip_globwave(ncf,index,mode):
meta={}
def read_meta_cdip_globwave(ncf, index, mode):
meta = {}
meta['type'] = mode
meta['wp'] = ncf.variables['dominant_wave_period'][index]
meta['dirp'] = np.squeeze(ncf.variables['dominant_wave_direction'][index])
meta['hst'] = np.squeeze(ncf.variables['significant_wave_height'][index])
meta['wp'] = ncf.variables['dominant_wave_period'][index]
meta['dirp'] = np.squeeze(ncf.variables['dominant_wave_direction'][index])
meta['hst'] = np.squeeze(ncf.variables['significant_wave_height'][index])
return meta
@staticmethod
def read_wsys_ndbc(ncf,index):
lon,lat =None,None
def read_wsys_ndbc(ncf, index):
lon, lat = None, None
lat = np.squeeze(ncf.variables['latitude'][:])
lon = np.squeeze(ncf.variables['longitude'][:])
wsys={'lon':lon,'lat':lat}
wsys = {'lon': lon, 'lat': lat}
return wsys
#-----------------------------------------------
# -----------------------------------------------
# End of Reader for Buoys Globwave File (Ndbc and cdip)
#-----------------------------------------------
# -----------------------------------------------
......@@ -465,7 +465,7 @@ def add_spectrum_to_plot(fig,myspectrum,vmin,vmax,colormap,cb_label,nrow,ncolumn
if hasattr(myspectrum.spec_data,'part') and myspectrum.spec_data.part is not None:
myspectrum.spec_data.part = myspectrum.spec_data.part+1
part_u = np.unique(myspectrum.spec_data.part)
part_u = part_u[part_u<200]
part_u = part_u[(part_u<200) & (part_u>=0)]
logging.debug('part_u shape before removing masked values: %s %s',part_u.shape,part_u)
if isinstance(part_u,np.ma.core.masked_array):
if isinstance(part_u.mask,np.ndarray):
......@@ -481,6 +481,8 @@ def add_spectrum_to_plot(fig,myspectrum,vmin,vmax,colormap,cb_label,nrow,ncolumn
for cpt in part_u:
ppart=myspectrum.spec_data.part*0
ppart[myspectrum.spec_data.part==cpt]=cpt
logging.error(cpt)
logging.error(ccc)
logging.debug('partition cpt = %s -> color = %s',cpt,ccc[np.int(cpt)])
if canPlotContour:
plt.contour(*init_sp(myspectrum.spec_data.k,myspectrum.spec_data.phi,ppart),levels=[0],colors=ccc[np.int(cpt)])
......
......@@ -14,6 +14,7 @@ import os, socket
from datetime import datetime
import collections
import time
import traceback
if __name__ == "__main__":
print("start generating Xspec png files")
import argparse
......@@ -59,20 +60,19 @@ if __name__ == "__main__":
output_dir, filename = os.path.split(output_file)
output_file, extension = os.path.splitext(output_file)
if not os.path.exists(output_dir):
os.makedirs(output_dir, 0775)
os.makedirs(output_dir, 0o0775)
if os.path.exists(output_file+'.png') and args.overwrite:
os.remove(output_file+'.png')
currentSpectrum = SpectrumReader.read_spectrum(input_file, mode).write(output_file+'.png')#add .png at the end agrouaze august 2018 fix missing .png at the end of the filenames
nbsuccess += 1
except Exception as e:
logging.error(traceback.format_exc())
print('exception occured:')
print(e)
filewriter = open(filesinerror, 'a')
filewriter.write(line)
filewriter.close()
logging.error("problem generate spectrum for:" + input_file)
nberror += 1
logging.info("{} generated successfully, {} not generated".format(nbsuccess, nberror))
logging.info('all product in error can be find in %s',filesinerror)
\ No newline at end of file
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