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: ...@@ -25,10 +25,11 @@ class FileExtractor:
''' '''
# constructor with values by defaul # 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 # attibutes
# public: # public:
self.fname = fname self.fname = fname
self.keys = keys
self.n = 0 self.n = 0
self.m = 0 self.m = 0
...@@ -45,12 +46,12 @@ class FileExtractor: ...@@ -45,12 +46,12 @@ class FileExtractor:
''' overload string representation ''' ''' overload string representation '''
return 'Class FileExtractor, file: %s, size = %d' % (self.fname, len(self)) return 'Class FileExtractor, file: %s, size = %d' % (self.fname, len(self))
def disp(self, keys): def disp(self):
# for key in keys: # for key in keys:
# print("{}:".format(key)) # print("{}:".format(key))
# print(self.__data[key]) # print(self.__data[key])
buf = '' buf = ''
for key in keys: for key in self.keys:
buf += "{}:\n".format(key) buf += "{}:\n".format(key)
buf += "{}\n".format(self.__data[key]) buf += "{}\n".format(self.__data[key])
return buf return buf
...@@ -87,10 +88,10 @@ class FileExtractor: ...@@ -87,10 +88,10 @@ class FileExtractor:
# the size of arrays # the size of arrays
self.n = filesRead self.n = filesRead
self.m = indMax 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 # 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 Read the file to its internal dict
...@@ -116,7 +117,7 @@ class FileExtractor: ...@@ -116,7 +117,7 @@ class FileExtractor:
hash = cfg['split'][device.lower()] hash = cfg['split'][device.lower()]
# initialize arrays, move at the end of firstPass ? # initialize arrays, move at the end of firstPass ?
for key in keys: for key in self.keys:
# mult by __fillValue next # mult by __fillValue next
# the shape parameter has to be an int or sequence of ints # the shape parameter has to be an int or sequence of ints
self.__data[key] = np.ones((self.n, self.m)) * self.__FillValue self.__data[key] = np.ones((self.n, self.m)) * self.__FillValue
...@@ -134,7 +135,7 @@ class FileExtractor: ...@@ -134,7 +135,7 @@ class FileExtractor:
str = ' ' str = ' '
# fill array with extracted value of line for eack key (physical parameter) # 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]] self.__data[key][n, m] = p[hash[key]]
# debug info # debug info
str += "{:>{width}}".format( str += "{:>{width}}".format(
...@@ -152,9 +153,9 @@ class FileExtractor: ...@@ -152,9 +153,9 @@ class FileExtractor:
if __name__ == "__main__": if __name__ == "__main__":
# usage: # usage:
# > python file_extractor.py data/cnv/dfr2900[1-3].cnv -d # > python file_extractor.py data/CTD/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/CTD/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/dfr29*.cnv -d
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='This class read multiple ASCII file, extract physical parameter \ description='This class read multiple ASCII file, extract physical parameter \
from ROSCOP codification at the given column and fill arrays ', from ROSCOP codification at the given column and fill arrays ',
...@@ -163,7 +164,7 @@ if __name__ == "__main__": ...@@ -163,7 +164,7 @@ if __name__ == "__main__":
action='store_true') action='store_true')
parser.add_argument('-c', '--config', help="toml configuration file, (default: %(default)s)", parser.add_argument('-c', '--config', help="toml configuration file, (default: %(default)s)",
default='tests/test.toml') 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)') help='display dictionary for key(s), (default: %(default)s)')
parser.add_argument('fname', nargs='*', parser.add_argument('fname', nargs='*',
help='cnv file(s) to parse, (default: data/cnv/dfr29*.cnv)') help='cnv file(s) to parse, (default: data/cnv/dfr29*.cnv)')
...@@ -176,12 +177,11 @@ if __name__ == "__main__": ...@@ -176,12 +177,11 @@ if __name__ == "__main__":
logging.basicConfig( logging.basicConfig(
format='%(levelname)s:%(message)s', level=logging.DEBUG) 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)) print("File(s): {}, Config: {}".format(args.fname, args.config))
cfg = toml.load(args.config) cfg = toml.load(args.config)
[n, m] = fe.firstPass() fe.firstPass()
print("Indices:", n, m) print("Indices: {} x {}\nkeys: {}".format(fe.n, fe.m, fe.keys))
fe.secondPass(args.key, cfg, 'ctd') fe.secondPass(cfg, 'ctd')
# fe.secondPass(['PRES', 'TEMP', 'PSAL', 'DOX2'], cdf, 'ctd') # debug
fe.disp(args.key) # print(fe.disp())
# fe.disp(['PRES', 'TEMP', 'PSAL', 'DOX2'])
import logging
from netCDF4 import Dataset from netCDF4 import Dataset
from numpy import arange, dtype
from physicalParameter import Roscop
def writeNetCDF(fileName, fe): 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 # create netcdf file
nc = Dataset(fileName, "w", format="NETCDF3_CLASSIC") nc = Dataset(fileName, "w", format="NETCDF3_CLASSIC")
print(nc.data_model) logging.debug(' ' + nc.data_model)
# create dimensions # create dimensions
depth = nc.createDimension("DEPTH", None) # n is number of profiles, m the max size of profiles
time = nc.createDimension("TIME", None) time = nc.createDimension("TIME", fe.n)
lat = nc.createDimension("LATITUDE", 73) lat = nc.createDimension("LATITUDE", fe.n)
lon = nc.createDimension("LONGITUDE", 144) 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(): ...@@ -38,7 +38,7 @@ def processArgs():
default='tests/test.toml') default='tests/test.toml')
parser.add_argument('-i', '--instrument', nargs='?', choices=ti.keys(), parser.add_argument('-i', '--instrument', nargs='?', choices=ti.keys(),
help='specify the instrument that produce files, eg CTD, XBT, TSG, LADCP') 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)') help='display dictionary for key(s), (default: %(default)s)')
parser.add_argument('-g', '--gui', action='store_true', parser.add_argument('-g', '--gui', action='store_true',
help='use GUI interface') help='use GUI interface')
...@@ -122,17 +122,17 @@ def process(args, cfg, ti): ...@@ -122,17 +122,17 @@ def process(args, cfg, ti):
# check if no file selected or cancel button pressed # check if no file selected or cancel button pressed
logging.debug("File(s): {}, config: {}, Keys: {}".format( logging.debug("File(s): {}, config: {}, Keys: {}".format(
args.files, args.config, args.key)) args.files, args.config, args.keys))
# fileExtractor # fileExtractor
fe = FileExtractor(args.files) fe = FileExtractor(args.files, args.keys)
# cfg = toml.load(args.config) # cfg = toml.load(args.config)
[n, m] = fe.firstPass() fe.firstPass()
# fe.secondPass(['PRES', 'TEMP', 'PSAL', 'DOX2'], cfg, 'ctd') # fe.secondPass(['PRES', 'TEMP', 'PSAL', 'DOX2'], cfg, 'ctd')
fe.secondPass(args.key, cfg, ti) fe.secondPass(cfg, ti)
# fe.disp(['PRES', 'TEMP', 'PSAL', 'DOX2']) # fe.disp(['PRES', 'TEMP', 'PSAL', 'DOX2'])
return fe, n, m return fe
if __name__ == "__main__": if __name__ == "__main__":
...@@ -224,15 +224,15 @@ if __name__ == "__main__": ...@@ -224,15 +224,15 @@ if __name__ == "__main__":
for k in values.keys(): for k in values.keys():
if k[0] == '_' or values[k] == False: if k[0] == '_' or values[k] == False:
del new_values[k] del new_values[k]
args.key = new_values.keys() args.keys = new_values.keys()
# process of files start here # process of files start here
fe, n, m = process(args, cfg, values['_COMBO_']) fe = process(args, cfg, values['_COMBO_'])
# display result in popup GUI # display result in popup GUI
dims = "Dimensions: {} x {}".format(n, m) dims = "Dimensions: {} x {}".format(n, m)
sg.PopupScrolled('Oceano2python', dims, 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 # It will output to a debug window. Bug ? debug windows xas closed before exiting program
# print = sg.Print # print = sg.Print
...@@ -264,7 +264,7 @@ if __name__ == "__main__": ...@@ -264,7 +264,7 @@ if __name__ == "__main__":
keys = cfg['split'][device.lower()].keys() keys = cfg['split'][device.lower()].keys()
# in command line mode (console) # in command line mode (console)
fe, n, m = process(args, cfg, device) fe = process(args, cfg, device)
print("Dimensions: {} x {}".format(m, n)) print("Dimensions: {} x {}".format(fe.m, fe.n))
print(fe.disp(args.key)) print(fe.disp())
netcdf.writeNetCDF( 'test.nc', fe) 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