Skip to content
Snippets Groups Projects
Commit e37e1eef authored by jacques.grelet_ird.fr's avatar jacques.grelet_ird.fr
Browse files

add netcdf withou data

parent 88b0e9e4
No related branches found
No related tags found
No related merge requests found
......@@ -25,10 +25,11 @@ class FileExtractor:
'''
# constructor with values by defaul
def __init__(self, fname, separator=None, skip_header=0):
def __init__(self, fname, keys, separator=None, skip_header=0):
# attibutes
# public:
self.fname = fname
self.keys = keys
self.n = 0
self.m = 0
......@@ -45,12 +46,12 @@ class FileExtractor:
''' overload string representation '''
return 'Class FileExtractor, file: %s, size = %d' % (self.fname, len(self))
def disp(self, keys):
def disp(self):
# for key in keys:
# print("{}:".format(key))
# print(self.__data[key])
buf = ''
for key in keys:
for key in self.keys:
buf += "{}:\n".format(key)
buf += "{}\n".format(self.__data[key])
return buf
......@@ -87,10 +88,10 @@ class FileExtractor:
# the size of arrays
self.n = filesRead
self.m = indMax
return self.n, self.m
# return self.n, self.m
# second pass, extract data from roscop code in fname and fill array
def secondPass(self, keys, cfg, device):
def secondPass(self, cfg, device):
'''
Read the file to its internal dict
......@@ -116,7 +117,7 @@ class FileExtractor:
hash = cfg['split'][device.lower()]
# initialize arrays, move at the end of firstPass ?
for key in keys:
for key in self.keys:
# mult by __fillValue next
# the shape parameter has to be an int or sequence of ints
self.__data[key] = np.ones((self.n, self.m)) * self.__FillValue
......@@ -134,7 +135,7 @@ class FileExtractor:
str = ' '
# fill array with extracted value of line for eack key (physical parameter)
for key in keys:
for key in self.keys:
self.__data[key][n, m] = p[hash[key]]
# debug info
str += "{:>{width}}".format(
......@@ -152,9 +153,9 @@ class FileExtractor:
if __name__ == "__main__":
# usage:
# > python file_extractor.py data/cnv/dfr2900[1-3].cnv -d
# > python file_extractor.py data/cnv/dfr2900[1-3].cnv -k PRES TEMP PSAL DOX2 DENS
# > python file_extractor.py data/cnv/dfr29*.cnv -d
# > python file_extractor.py data/CTD/cnv/dfr2900[1-3].cnv -d
# > python file_extractor.py data/CTD/cnv/dfr2900[1-3].cnv -k PRES TEMP PSAL DOX2 DENS
# > python file_extractor.py data/CTD/cnv/dfr29*.cnv -d
parser = argparse.ArgumentParser(
description='This class read multiple ASCII file, extract physical parameter \
from ROSCOP codification at the given column and fill arrays ',
......@@ -163,7 +164,7 @@ if __name__ == "__main__":
action='store_true')
parser.add_argument('-c', '--config', help="toml configuration file, (default: %(default)s)",
default='tests/test.toml')
parser.add_argument('-k', '--key', nargs='+', default=['PRES', 'TEMP', 'PSAL'],
parser.add_argument('-k', '--keys', nargs='+', default=['PRES', 'TEMP', 'PSAL'],
help='display dictionary for key(s), (default: %(default)s)')
parser.add_argument('fname', nargs='*',
help='cnv file(s) to parse, (default: data/cnv/dfr29*.cnv)')
......@@ -176,12 +177,11 @@ if __name__ == "__main__":
logging.basicConfig(
format='%(levelname)s:%(message)s', level=logging.DEBUG)
fe = FileExtractor(args.fname)
fe = FileExtractor(args.fname, args.keys)
print("File(s): {}, Config: {}".format(args.fname, args.config))
cfg = toml.load(args.config)
[n, m] = fe.firstPass()
print("Indices:", n, m)
fe.secondPass(args.key, cfg, 'ctd')
# fe.secondPass(['PRES', 'TEMP', 'PSAL', 'DOX2'], cdf, 'ctd')
fe.disp(args.key)
# fe.disp(['PRES', 'TEMP', 'PSAL', 'DOX2'])
fe.firstPass()
print("Indices: {} x {}\nkeys: {}".format(fe.n, fe.m, fe.keys))
fe.secondPass(cfg, 'ctd')
# debug
# print(fe.disp())
import logging
from netCDF4 import Dataset
from numpy import arange, dtype
from physicalParameter import Roscop
def writeNetCDF(fileName, fe):
data = {}
dims = ['TIME', 'LATITUDE', 'LONGITUDE', 'DEPTH']
vars = dims.copy()
# move to main after tests
r = Roscop("code_roscop.csv")
# create netcdf file
nc = Dataset(fileName, "w", format="NETCDF3_CLASSIC")
print(nc.data_model)
logging.debug(' ' + nc.data_model)
# create dimensions
depth = nc.createDimension("DEPTH", None)
time = nc.createDimension("TIME", None)
lat = nc.createDimension("LATITUDE", 73)
lon = nc.createDimension("LONGITUDE", 144)
# n is number of profiles, m the max size of profiles
time = nc.createDimension("TIME", fe.n)
lat = nc.createDimension("LATITUDE", fe.n)
lon = nc.createDimension("LONGITUDE", fe.n)
depth = nc.createDimension('DEPTH', fe.m)
logging.debug(" depth: {}, time: {}, lat: {}, lon: {}".format(
len(depth), len(time), len(lat), len(lon)))
# create variables
# add dimensions before variables list
for k in fe.keys:
vars.append(k)
for key in vars:
# for each variables get the attributes list
hash = r.returnCode(key)
# _FillValue attribute must be set when variable is created
# (using fill_value keyword to createVariable)
if '_FillValue' in hash:
fillvalue = hash['_FillValue']
# remove from the dictionary
hash.pop('_FillValue')
else:
fillvalue = None
# create the variable
data[key] = nc.createVariable(
key, dtype(hash['types']).char, dims, fill_value=fillvalue)
# remove from the dictionary
hash.pop('types')
# create dynamically variable attributes
for k in hash.keys():
setattr(data[key], k, hash[k])
# debug
for key in vars:
print(data[key])
......@@ -38,7 +38,7 @@ def processArgs():
default='tests/test.toml')
parser.add_argument('-i', '--instrument', nargs='?', choices=ti.keys(),
help='specify the instrument that produce files, eg CTD, XBT, TSG, LADCP')
parser.add_argument('-k', '--key', nargs='+', default=['PRES', 'TEMP', 'PSAL'],
parser.add_argument('-k', '--keys', nargs='+', default=['PRES', 'TEMP', 'PSAL'],
help='display dictionary for key(s), (default: %(default)s)')
parser.add_argument('-g', '--gui', action='store_true',
help='use GUI interface')
......@@ -122,17 +122,17 @@ def process(args, cfg, ti):
# check if no file selected or cancel button pressed
logging.debug("File(s): {}, config: {}, Keys: {}".format(
args.files, args.config, args.key))
args.files, args.config, args.keys))
# fileExtractor
fe = FileExtractor(args.files)
fe = FileExtractor(args.files, args.keys)
# cfg = toml.load(args.config)
[n, m] = fe.firstPass()
fe.firstPass()
# fe.secondPass(['PRES', 'TEMP', 'PSAL', 'DOX2'], cfg, 'ctd')
fe.secondPass(args.key, cfg, ti)
fe.secondPass(cfg, ti)
# fe.disp(['PRES', 'TEMP', 'PSAL', 'DOX2'])
return fe, n, m
return fe
if __name__ == "__main__":
......@@ -224,15 +224,15 @@ if __name__ == "__main__":
for k in values.keys():
if k[0] == '_' or values[k] == False:
del new_values[k]
args.key = new_values.keys()
args.keys = new_values.keys()
# process of files start here
fe, n, m = process(args, cfg, values['_COMBO_'])
fe = process(args, cfg, values['_COMBO_'])
# display result in popup GUI
dims = "Dimensions: {} x {}".format(n, m)
sg.PopupScrolled('Oceano2python', dims,
fe.disp(args.key), size=(80, 40))
fe.disp(args.keys), size=(80, 40))
# It will output to a debug window. Bug ? debug windows xas closed before exiting program
# print = sg.Print
......@@ -264,7 +264,7 @@ if __name__ == "__main__":
keys = cfg['split'][device.lower()].keys()
# in command line mode (console)
fe, n, m = process(args, cfg, device)
print("Dimensions: {} x {}".format(m, n))
print(fe.disp(args.key))
netcdf.writeNetCDF( 'test.nc', fe)
fe = process(args, cfg, device)
print("Dimensions: {} x {}".format(fe.m, fe.n))
print(fe.disp())
netcdf.writeNetCDF( 'output/test.nc', fe)
Do not delete this file.
\ No newline at end of file
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment