Commit 6ab9cb08 authored by ARCHER's avatar ARCHER

added writeLight to produce light standard netcdf file

parent 722758c4
#!/usr/bin/env python
import owi
import argparse
import sys
import netCDF4
import datetime
if __name__ == "__main__":
description = """Converts owi nc file to lightweight nc format"""
parser = argparse.ArgumentParser(description = description)
parser.add_argument('infile')
parser.add_argument('outfile')
args = parser.parse_args()
data = owi.readFile(args.infile)
meta = owi.readMeta(args.infile)
startdate = datetime.datetime.strptime(owi.getAttribute(args.infile,'firstMeasurementTime'),"%Y-%m-%dT%H:%M:%SZ")
stopdate = datetime.datetime.strptime(owi.getAttribute(args.infile,'lastMeasurementTime'),"%Y-%m-%dT%H:%M:%SZ")
owi.writeLight(args.outfile,data,startdate + (stopdate - startdate) / 2,meta=meta)
# check
#import xarray
#import cyclone_formatter
#dslight = xarray.open_dataset(args.outfile,drop_variables=['incidence','wind_to_direction','elevation']).transpose().squeeze().rename_dims({'X':'x','Y':'y'})
#dscycl = cyclone_formatter.readSar(args.infile)
......@@ -13,6 +13,7 @@ import numpy as np
import numpy.ma as ma
import logging
import warnings
from collections import OrderedDict
logging.basicConfig()
logger = logging.getLogger(__name__)
......@@ -140,6 +141,105 @@ def readMeta(fname):
return data
def writeLight(fname,data,date,meta=None):
"""lightweigth file format writer
specs from https://github.com/Unidata/EC-netCDF-CF/blob/master/swath/swath.adoc#image-swath
xarray reader exemple:
xarray.open_dataset(fname,drop_variables=['incidence','wind_to_direction','elevation','heterogeneity_copol_h','heterogeneity_copol_m','heterogeneity_copol_l','heterogeneity_cross_h','heterogeneity_cross_m','heterogeneity_cross_l]).transpose().squeeze().rename_dims({'X':'x','Y':'y'})
"""
names_mapping = OrderedDict([
('owiLon' , 'lon'),
('owiLat' , 'lat'),
('owiWindSpeed' , 'wind_speed'),
('owiWindDirection' , 'wind_to_direction'),
('owiElevationAngle' , 'elevation'),
('owiIncidenceAngle' , 'incidence'),
('owiPreProcessing/FilterBinary_1' , 'heterogeneity_copol_h'),
('owiPreProcessing/FilterBinary_2' , 'heterogeneity_copol_m'),
('owiPreProcessing/FilterBinary_3' , 'heterogeneity_copol_l'),
('owiPreProcessing/FilterBinary_1_cross' , 'heterogeneity_cross_h'),
('owiPreProcessing/FilterBinary_2_cross' , 'heterogeneity_cross_m'),
('owiPreProcessing/FilterBinary_3_cross' , 'heterogeneity_cross_l'),
])
# overwrite meta
meta_over = {
'wind_speed' : {
'units' : "m/s"
},
'lon' : {
'standard_name' : 'longitude',
'units' : 'degrees_east'
},
'lat' : {
'standard_name' : 'latitude',
'units' : 'degrees_north'
}
}
X,Y = data['owiLon'].shape
dims = (X,Y)
nc=netCDF4.Dataset(fname, 'w', format='NETCDF4')
nc.createDimension('time',1)
nc.createDimension('Y',Y)
nc.createDimension('X',X)
nc.Conventions = 'CF-1.6'
nc.title = 'SAR wind speed'
nc.institution = 'IFREMER LOPS/SIAM'
nc.measurementDate = date.strftime("%Y-%m-%dT%H:%M:%SZ")
time = nc.createVariable('time',np.int64,dimensions=('time'))
time.standard_name = 'time'
epoch = datetime.datetime(1970, 1, 1)
time.units = "seconds since %s" % epoch.strftime("%Y-%m-%d %H:%M:%S")
time.calendar = "gregorian"
#time[::] = int(date.timestamp())
time[::] = (date - epoch).total_seconds() # works for python < 3.3
for in_name in names_mapping.keys():
if in_name in data:
value = data[in_name]
out_name = names_mapping[in_name]
try:
fill_value=data[in_name].fill_value
except:
fill_value=None
# transpose dims for correct netcdf ordering
ncvar = nc.createVariable(out_name,value.dtype,dimensions=('time','Y','X'),fill_value=fill_value)
try:
try:
ncvar.long_name = meta_over[in_name]['description']
except:
ncvar.long_name = meta[in_name]['description']
except:
pass
else:
logger.warning('skipping missing var %s' % in_name)
if in_name not in ['owiLon','owiLat']:
ncvar.coordinates="time lat lon"
ncvar[::]=value.T
#ncvar.grid_mapping = "crs"
if out_name in meta_over:
for tag in meta_over[out_name]:
ncvar.setncattr(tag, meta_over[out_name][tag])
#unable to set crs for gdal
#crs = nc.createVariable("crs",int)
#crs.grid_mapping_name = "latitude_longitude"
#crs.semi_major_axis = 6378137
#crs.inverse_flattening = 298.2572235604902
#crs.longitude_of_prime_meridian = 0.0
nc.close()
return
#def readLight(fname):
# import xarray
# ds = xarray.open_dataset(fname)
# return ds
def getAttribute(fname, attr):
try:
nc=netCDF4.Dataset(fname)
......
......@@ -10,6 +10,7 @@ setup(name='owi',
setup_requires=['setuptools_scm'],
packages=['owi'],
zip_safe=False,
scripts=['bin/owi2nclight.py'],
install_requires=[
'numpy', 'netCDF4'
])
......
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