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

format with autopip8

parent eede25ae
No related branches found
No related tags found
No related merge requests found
Sous Windows, VSCode et Python 3.6 Sous Windows, VSCode et Python 3.7.2
> pip3 > pip3
Fatal error in launcher: Unable to create process using '"' Fatal error in launcher: Unable to create process using '"'
[ntird-us191-jg4:jgrelet]/c/git/python/pirata [ntird-us191-jg4:jgrelet]/c/git/python/pirata
> python3 -m pip install --upgrade pip > python3 -m pip install --upgrade pip
Collecting pip ...
Downloading https://files.pythonhosted.org/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl (1.4MB)
100% |████████████████████████████████| 1.4MB 72kB/s
Installing collected packages: pip
Found existing installation: pip 9.0.1
Uninstalling pip-9.0.1:
Successfully uninstalled pip-9.0.1
Successfully installed pip-19.0.3 Successfully installed pip-19.0.3
[ntird-us191-jg4:jgrelet]/c/git/python/pirata [ntird-us191-jg4:jgrelet]/c/git/python/pirata
> pip install toml > pip install toml
...@@ -56,4 +50,10 @@ Configuration du fichier setting.json de jgrelet: ...@@ -56,4 +50,10 @@ Configuration du fichier setting.json de jgrelet:
Pour utiliser le bash de git, modifier la ligne: Pour utiliser le bash de git, modifier la ligne:
"terminal.integrated.shell.windows": "C:\\MinGW\\msys\\1.0\\bin\\bash.exe", "terminal.integrated.shell.windows": "C:\\MinGW\\msys\\1.0\\bin\\bash.exe",
par par
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe", "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
\ No newline at end of file
Autoformatting
----------------------
> pip install autopep8
Installer l'extension Python-autopep8
Cmd+P choisir autopip
\ No newline at end of file
...@@ -39,48 +39,53 @@ example: ...@@ -39,48 +39,53 @@ example:
$ python3 ctd.py --cycle_mesure=PIRATA-FR29 --institut=IRD --plateforme="THALASSA" --sn=09P01263 --type=SBE911+ --pi=BOURLES --date_debut=01/03/2019 --date_fin=04/04/2019 data/asc/fr29???.hdr --echo --local --ascii $ python3 ctd.py --cycle_mesure=PIRATA-FR29 --institut=IRD --plateforme="THALASSA" --sn=09P01263 --type=SBE911+ --pi=BOURLES --date_debut=01/03/2019 --date_fin=04/04/2019 data/asc/fr29???.hdr --echo --local --ascii
""" """
import sys, re, getopt, string, fileinput, csv import sys
import re
import getopt
import string
import fileinput
import csv
from datetime import datetime from datetime import datetime
# a lire dans la premier fichier # a lire dans la premier fichier
seasave_version = "7.21b" seasave_version = "7.21b"
# initialize constants # initialize constants
VERSION = "V1.1 J Grelet - IRD - US191 IMAGO, Plouzane - March 2019" VERSION = "V1.1 J Grelet - IRD - US191 IMAGO, Plouzane - March 2019"
DEGREE = 176 DEGREE = 176
CODE = -1 CODE = -1
CONTEXTE = "PIRATA" CONTEXTE = "PIRATA"
TIMEZONE = "GMT" TIMEZONE = "GMT"
FORMAT_DATE = "DMY" FORMAT_DATE = "DMY"
# initialize variables and options # initialize variables and options
# -------------------------------- # --------------------------------
echo = False echo = False
quiet = True quiet = True
xml = True xml = True
ascii = False ascii = False
odv = False odv = False
netcdf = False netcdf = False
dtd = True dtd = True
debug = 0 debug = 0
code_oopc = '0A' code_oopc = '0A'
cycle_mesure = 'PIRATA-FR29' cycle_mesure = 'PIRATA-FR29'
plateforme = 'THALASSA' plateforme = 'THALASSA'
institut = 'IRD' institut = 'IRD'
sn = '09P01263' sn = '09P01263'
type = 'SBE911+' type = 'SBE911+'
pi = 'BOURLES' pi = 'BOURLES'
date_debut = '01/03/2019' date_debut = '01/03/2019'
date_fin = '04/04/2019' date_fin = '04/04/2019'
output_file = 'pirata-fr29' output_file = 'pirata-fr29'
latitude = 0. latitude = 0.
latitude_str = '' latitude_str = ''
longitude = 0. longitude = 0.
longitude_str = '' longitude_str = ''
julian = 0 julian = 0
epic_date = '' epic_date = ''
Pres = 0 Pres = 0
bottom_depth = 0 bottom_depth = 0
# initialize datetime object # initialize datetime object
# -------------------------- # --------------------------
...@@ -94,10 +99,11 @@ dt = datetime ...@@ -94,10 +99,11 @@ dt = datetime
# ------------------------------------------------- # -------------------------------------------------
re_station_number = re.compile(r"fr29(\d{3})") re_station_number = re.compile(r"fr29(\d{3})")
# extract CTD station start date # extract CTD station start date
# ------------------------------ # ------------------------------
re_ctd_upload_time = \ re_ctd_upload_time = \
re.compile(r"System UpLoad Time\s*=\s*(\w+)\s+(\d+)\s+(\d+)\s+(\d+):(\d+):(\d+)") re.compile(
r"System UpLoad Time\s*=\s*(\w+)\s+(\d+)\s+(\d+)\s+(\d+):(\d+):(\d+)")
# extract CTD station latitude # extract CTD station latitude
# ---------------------------- # ----------------------------
...@@ -105,7 +111,8 @@ re_latitude = re.compile(r"NMEA\s+Latitude\s*[:=]\s*(\d+)\s+(\d+.\d+)\s+(\w)") ...@@ -105,7 +111,8 @@ re_latitude = re.compile(r"NMEA\s+Latitude\s*[:=]\s*(\d+)\s+(\d+.\d+)\s+(\w)")
# extract CTD station longitude # extract CTD station longitude
# ----------------------------- # -----------------------------
re_longitude = re.compile(r"NMEA\s+Longitude\s*[:=]\s*(\d+)\s+(\d+.\d+)\s+(\w)") re_longitude = re.compile(
r"NMEA\s+Longitude\s*[:=]\s*(\d+)\s+(\d+.\d+)\s+(\w)")
# extract bottom depth at station position from echo sounder # extract bottom depth at station position from echo sounder
# ------------------------------------------------------------ # ------------------------------------------------------------
...@@ -115,363 +122,384 @@ re_bottom_depth = re.compile(r"Bottom\sDepth\s*[:=]\s*(\d+)") ...@@ -115,363 +122,384 @@ re_bottom_depth = re.compile(r"Bottom\sDepth\s*[:=]\s*(\d+)")
# display help using text inside """ """" at the beginning of the # display help using text inside """ """" at the beginning of the
# script with special attribute __doc__ and quit # script with special attribute __doc__ and quit
# --------------------------------------------------------------- # ---------------------------------------------------------------
def usage(): def usage():
print(__doc__) print(__doc__)
sys.exit() sys.exit()
# ------------------------------------------------ # ------------------------------------------------
# display version and quit # display version and quit
# ------------------------------------------------ # ------------------------------------------------
def version(): def version():
print("%s: %s" % (sys.argv[0], VERSION)) print("%s: %s" % (sys.argv[0], VERSION))
sys.exit() sys.exit()
#------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# entete XML # entete XML
#------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
def entete_xml(fd):
#my $today = &dateFormat(undef,"%d/%m/%Y");
today = '06/03/2019' def entete_xml(fd):
# my $today = &dateFormat(undef,"%d/%m/%Y");
fd.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") today = '06/03/2019'
# les commentaires ne sont pas acceptes par XML Toolbox Matlab de Geodise
if (dtd): fd.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
#fd.write("<!DOCTYPE OCEANO SYSTEM \"M:\ACQUISIT\oceano.dtd\">\n') # les commentaires ne sont pas acceptes par XML Toolbox Matlab de Geodise
fd.write("<!DOCTYPE OCEANO SYSTEM \"/m/oceano.dtd\">\n") if (dtd):
#fd.write("<!DOCTYPE OCEANO PUBLIC \"-//US191//DTD OCEANO//FR\" \ # fd.write("<!DOCTYPE OCEANO SYSTEM \"M:\ACQUISIT\oceano.dtd\">\n')
# \"http://antea/acquisit/oceano.dtd\">\n') fd.write("<!DOCTYPE OCEANO SYSTEM \"/m/oceano.dtd\">\n")
else: # fd.write("<!DOCTYPE OCEANO PUBLIC \"-//US191//DTD OCEANO//FR\" \
fd.write("<!DOCTYPE OCEANO PUBLIC \"-//US191//DTD OCEANO//FR\" \ # \"http://antea/acquisit/oceano.dtd\">\n')
\"http://www.brest.ird.fr/us191/database/oceano.dtd\">\n") else:
fd.write("<!DOCTYPE OCEANO PUBLIC \"-//US191//DTD OCEANO//FR\" \
fd.write("<OCEANO TYPE=\"PROFIL\">\n") \"http://www.brest.ird.fr/us191/database/oceano.dtd\">\n")
fd.write(" <ENTETE>\n")
fd.write(" <PLATEFORME>\n") fd.write("<OCEANO TYPE=\"PROFIL\">\n")
fd.write(" <LIBELLE>%s</LIBELLE>\n" % plateforme) fd.write(" <ENTETE>\n")
fd.write(" </PLATEFORME>\n") fd.write(" <PLATEFORME>\n")
fd.write(" <CYCLE_MESURE CONTEXTE=\"%s\" TIMEZONE=\"%s\" FORMAT=\"%s\">\n" % fd.write(" <LIBELLE>%s</LIBELLE>\n" % plateforme)
(CONTEXTE, TIMEZONE, FORMAT_DATE)) fd.write(" </PLATEFORME>\n")
fd.write(" <LIBELLE>%s</LIBELLE>\n" % cycle_mesure) fd.write(" <CYCLE_MESURE CONTEXTE=\"%s\" TIMEZONE=\"%s\" FORMAT=\"%s\">\n" %
fd.write(" <DATE_DEBUT>%s</DATE_DEBUT>\n" % date_debut) (CONTEXTE, TIMEZONE, FORMAT_DATE))
fd.write(" <DATE_FIN>%s</DATE_FIN>\n" % date_fin) fd.write(" <LIBELLE>%s</LIBELLE>\n" % cycle_mesure)
fd.write(" <INSTITUT>%s</INSTITUT>\n" % institut) fd.write(" <DATE_DEBUT>%s</DATE_DEBUT>\n" % date_debut)
fd.write(" <RESPONSABLE>%s</RESPONSABLE>\n" % pi) fd.write(" <DATE_FIN>%s</DATE_FIN>\n" % date_fin)
fd.write(" <ACQUISITION LOGICIEL=\"SEASAVE\" VERSION=\"%s\"></ACQUISITION>\n" % fd.write(" <INSTITUT>%s</INSTITUT>\n" % institut)
seasave_version) fd.write(" <RESPONSABLE>%s</RESPONSABLE>\n" % pi)
fd.write(" <TRAITEMENT LOGICIEL=\"SBEDATAPROCESSING\" VERSION=\"%s\"></TRAITEMENT>\n" % seasave_version) fd.write(" <ACQUISITION LOGICIEL=\"SEASAVE\" VERSION=\"%s\"></ACQUISITION>\n" %
fd.write(" <VALIDATION LOGICIEL=\"%s\" VERSION=\"%s\" DATE=\"%s\" OPERATEUR=\"%s\" CODIFICATION=\"OOPC\">\n" % (sys.argv[0], VERSION, today, pi)) seasave_version)
fd.write(" <CODE>%s</CODE>\n" % code_oopc) fd.write(" <TRAITEMENT LOGICIEL=\"SBEDATAPROCESSING\" VERSION=\"%s\"></TRAITEMENT>\n" %
fd.write(" <COMMENTAIRE>Extraction realisee avant la post-calibration</COMMENTAIRE>\n") seasave_version)
fd.write(" </VALIDATION>\n") fd.write(" <VALIDATION LOGICIEL=\"%s\" VERSION=\"%s\" DATE=\"%s\" OPERATEUR=\"%s\" CODIFICATION=\"OOPC\">\n" % (
fd.write(" </CYCLE_MESURE>\n") sys.argv[0], VERSION, today, pi))
fd.write(" <INSTRUMENT TYPE=\"%s\" NUMERO_SERIE=\"%s\">\n" % (type, sn)) fd.write(" <CODE>%s</CODE>\n" % code_oopc)
fd.write(" </INSTRUMENT>\n") fd.write(
" <COMMENTAIRE>Extraction realisee avant la post-calibration</COMMENTAIRE>\n")
#decode_con_file(fileName) fd.write(" </VALIDATION>\n")
fd.write(" </CYCLE_MESURE>\n")
fd.write(" </ENTETE>\n") fd.write(" <INSTRUMENT TYPE=\"%s\" NUMERO_SERIE=\"%s\">\n" % (type, sn))
fd.write(" <DATA>\n") fd.write(" </INSTRUMENT>\n")
#------------------------------------------------------------------------------ # decode_con_file(fileName)
fd.write(" </ENTETE>\n")
fd.write(" <DATA>\n")
# ------------------------------------------------------------------------------
# read code roscop file # read code roscop file
#------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
def codeRoscop(file): def codeRoscop(file):
print("Code roscop file: %s" % code_roscop) print("Code roscop file: %s" % code_roscop)
with open(code_roscop, 'rt') as f: with open(code_roscop, 'rt') as f:
reader = csv.DictReader(f, delimiter=';') reader = csv.DictReader(f, delimiter=';')
for row in reader: for row in reader:
print("%s : %s : %s : %s : %s" % (row['key'], row['long_name'], print("%s : %s : %s : %s : %s" % (row['key'], row['long_name'],
row['standard_name'], row['units'], row['format'])) row['standard_name'], row['units'], row['format']))
sys.exit() sys.exit()
# display help with no arg on command-line # display help with no arg on command-line
# ---------------------------------------- # ----------------------------------------
if len(sys.argv) == 1: if len(sys.argv) == 1:
usage() usage()
# setting and get otpions # setting and get otpions
# ----------------------- # -----------------------
try: try:
# The return args consists of two elements: the first is a list of # The return args consists of two elements: the first is a list of
# (option, arg) pairs; the second is the list of program arguments # (option, arg) pairs; the second is the list of program arguments
# left after the option list was stripped # left after the option list was stripped
# see optparse, a powerful library for parsing command-line options # see optparse, a powerful library for parsing command-line options
# The gnu version of getopt means that option and non-option # The gnu version of getopt means that option and non-option
# arguments may be intermixed # arguments may be intermixed
# The -e is a simple flag, while -d, -o and -r require an argument. # The -e is a simple flag, while -d, -o and -r require an argument.
# The option definition string is "ed:r:". # The option definition string is "ed:r:".
# To mix option and non-option arguments on the command line in any order, # To mix option and non-option arguments on the command line in any order,
# use gnu_getopt() instead getopt # use gnu_getopt() instead getopt
# ------------------------------------------------------------------ # ------------------------------------------------------------------
options, args = getopt.gnu_getopt(sys.argv[1:], 'ed:qo:hvr:', options, args = getopt.gnu_getopt(sys.argv[1:], 'ed:qo:hvr:',
['echo', 'debug=', 'quiet', 'output=', 'help', 'version', 'roscop=', ['echo', 'debug=', 'quiet', 'output=', 'help', 'version', 'roscop=',
'cycle_mesure=', 'plateforme=', 'date_debut=', 'date_fin=', 'cycle_mesure=', 'plateforme=', 'date_debut=', 'date_fin=',
'institut=', 'code_oopc=', 'pi=', 'ascii', 'xml', 'odv', 'institut=', 'code_oopc=', 'pi=', 'ascii', 'xml', 'odv',
'netcdf', 'all', 'local', 'secondary', 'sn=', 'type=']) 'netcdf', 'all', 'local', 'secondary', 'sn=', 'type='])
# if bad option, display an error message and usage # if bad option, display an error message and usage
# ------------------------------------------------- # -------------------------------------------------
except getopt.GetoptError as err: except getopt.GetoptError as err:
print(str(err)) print(str(err))
usage() usage()
# iterate over options list # iterate over options list
# ------------------------- # -------------------------
for option, arg in options: for option, arg in options:
if option in ('-e', '--echo'): if option in ('-e', '--echo'):
echo = True echo = True
elif option in ('-q', '--quiet'): elif option in ('-q', '--quiet'):
quiet = False quiet = False
elif option in ('-d', '--debug'): elif option in ('-d', '--debug'):
debug = int(arg) debug = int(arg)
elif option in ('-o', '--output'): elif option in ('-o', '--output'):
outputfile = arg outputfile = arg
elif option in ('-h', '--help'): elif option in ('-h', '--help'):
usage() usage()
elif option in ('-v', '--version'): elif option in ('-v', '--version'):
version() version()
elif option in ('-r', '--roscop'): elif option in ('-r', '--roscop'):
code_roscop = arg code_roscop = arg
codeRoscop(code_roscop) codeRoscop(code_roscop)
elif option == '--cycle_mesure': elif option == '--cycle_mesure':
cycle_mesure = arg cycle_mesure = arg
elif option == '--plateforme': elif option == '--plateforme':
plateforme = arg plateforme = arg
elif option == '--date_debut': elif option == '--date_debut':
date_debut = arg date_debut = arg
elif option == '--date_fin': elif option == '--date_fin':
date_fin = arg date_fin = arg
elif option == '--institut': elif option == '--institut':
institut = arg institut = arg
elif option == '--code_oopc': elif option == '--code_oopc':
code_oopc = arg code_oopc = arg
elif option == '--pi': elif option == '--pi':
pi = arg pi = arg
elif option == '--ascii': elif option == '--ascii':
ascii = True ascii = True
elif option == '--xml': elif option == '--xml':
xml = True xml = True
elif option == '--odv': elif option == '--odv':
odv = True odv = True
elif option == '--netcdf': elif option == '--netcdf':
netcdf = True netcdf = True
elif option == '--all': elif option == '--all':
ascii = xml = odv = netcdf = True ascii = xml = odv = netcdf = True
# for debug only, display arg list # for debug only, display arg list
# -------------------------------- # --------------------------------
if debug == 1: if debug == 1:
print("Args: %s\t%s\t%s\t%s\t%s\t%s\t%s\n" % \ print("Args: %s\t%s\t%s\t%s\t%s\t%s\t%s\n" %
(cycle_mesure, plateforme, date_debut, date_fin, institut, (cycle_mesure, plateforme, date_debut, date_fin, institut,
code_oopc, pi)) code_oopc, pi))
# open ascii files # open ascii files
# ---------------- # ----------------
if ascii: if ascii:
hdr_file = open( output_file + '.ctd', "w") hdr_file = open(output_file + '.ctd', "w")
ascii_file = open( output_file + '_ctd', "w") ascii_file = open(output_file + '_ctd', "w")
# write header to ascii files # write header to ascii files
# --------------------------------- # ---------------------------------
hdr_file.write("//%s %s %s %s %s %s\n" % \ hdr_file.write("//%s %s %s %s %s %s\n" %
(cycle_mesure, plateforme, institut, type, sn, pi)) (cycle_mesure, plateforme, institut, type, sn, pi))
hdr_file.write("St Date Time Latitude Longitude Depth\n") hdr_file.write("St Date Time Latitude Longitude Depth\n")
ascii_file.write("//%s %s %s %s %s %s\n" % \ ascii_file.write("//%s %s %s %s %s %s\n" %
(cycle_mesure, plateforme, institut, type, sn, pi)) (cycle_mesure, plateforme, institut, type, sn, pi))
ascii_file.write("PRFL PRES TEMP PSAL DENS SVEL DOX2 FLU2 TUR3 NAVG\n") ascii_file.write(
"PRFL PRES TEMP PSAL DENS SVEL DOX2 FLU2 TUR3 NAVG\n")
# open xml file # open xml file
# ------------- # -------------
if xml: if xml:
xml_file = open( output_file + '_ctd.xml', "w") xml_file = open(output_file + '_ctd.xml', "w")
# write header to xml files # write header to xml files
# --------------------------------- # ---------------------------------
entete_xml(xml_file) entete_xml(xml_file)
xml_file.write("PRFL PRES TEMP PSAL DENS SVEL DOX2 FLU2 TUR3 NAVG\n") xml_file.write(
"PRFL PRES TEMP PSAL DENS SVEL DOX2 FLU2 TUR3 NAVG\n")
if echo:
# display selected output format if echo:
# ------------------------------ # display selected output format
print("Output: ") # ------------------------------
if (ascii): print("ASCII ") print("Output: ")
if (xml) : print("XML ") if (ascii):
if (odv) : print("ODV ") print("ASCII ")
if (xml):
# display header print("XML ")
# -------------- if (odv):
print("\n//%s %s %s %s %s %s" % \ print("ODV ")
(cycle_mesure, plateforme, institut, type, sn, pi))
print(" File St Date Time Latitude Longitude Depth Bottom", end='') # display header
# --------------
# iterate over file list getting with getopt.gnu_getopt print("\n//%s %s %s %s %s %s" %
(cycle_mesure, plateforme, institut, type, sn, pi))
print(" File St Date Time Latitude Longitude Depth Bottom", end='')
# iterate over file list getting with getopt.gnu_getopt
# ---------------------------------------------------- # ----------------------------------------------------
for fileName in args: for fileName in args:
# display processed filename # display processed filename
# suppress '\n' character written at the end with a comma # suppress '\n' character written at the end with a comma
# ------------------------------------------------------- # -------------------------------------------------------
if echo: print("\n%s" % (fileName), end='') if echo:
print("\n%s" % (fileName), end='')
# extract station number from file name using compiled regexp
# -----------------------------------------------------------
if re_station_number.search(fileName):
station = int(re_station_number.search(fileName).group(1))
# open current file # extract station number from file name using compiled regexp
# TODOS: add some tests on file existence # -----------------------------------------------------------
# --------------------------------------- if re_station_number.search(fileName):
file = open( fileName, "r", encoding = "ISO-8859-1") station = int(re_station_number.search(fileName).group(1))
# read each line of current file # open current file
# ------------------------------ # TODOS: add some tests on file existence
for line in file.readlines(): # ---------------------------------------
file = open(fileName, "r", encoding="ISO-8859-1")
# extract CTD station start date # read each line of current file
# ------------------------------ # ------------------------------
if re_ctd_upload_time.search(line): for line in file.readlines():
(month, day, year, hour, minute, second) = \
re_ctd_upload_time.search(line).groups() # extract CTD station start date
# ------------------------------
# format date and time to "May 09 2011 16:33:53" if re_ctd_upload_time.search(line):
# ----------------------------------------------- (month, day, year, hour, minute, second) = \
dateTime = "%s/%s/%s %s:%s:%s" % (day, month, year, hour, minute, second) re_ctd_upload_time.search(line).groups()
# dateTime conversion to "09/05/2011 16:33:53" # format date and time to "May 09 2011 16:33:53"
# -------------------------------------------- # -----------------------------------------------
dateTime = "%s" % \ dateTime = "%s/%s/%s %s:%s:%s" % (day,
(dt.strptime(dateTime, "%d/%b/%Y %H:%M:%S").strftime("%d/%m/%Y %H:%M:%S")) month, year, hour, minute, second)
# conversion to "20110509163353"
# -------------------------------------------- # dateTime conversion to "09/05/2011 16:33:53"
epic_date = "%s" % \ # --------------------------------------------
(dt.strptime(dateTime, "%d/%m/%Y %H:%M:%S").strftime("%Y%m%d%H%M%S")) dateTime = "%s" % \
(dt.strptime(dateTime, "%d/%b/%Y %H:%M:%S").strftime("%d/%m/%Y %H:%M:%S"))
# conversion to julian day # conversion to "20110509163353"
# ------------------------ # --------------------------------------------
julian = float((dt.strptime(dateTime, "%d/%m/%Y %H:%M:%S").strftime("%j"))) \ epic_date = "%s" % \
+ ((float(hour) * 3600.) + (float(minute) * 60.) + float(second) ) / 86400. (dt.strptime(dateTime, "%d/%m/%Y %H:%M:%S").strftime("%Y%m%d%H%M%S"))
# we use julian day with origine 0 # conversion to julian day
# -------------------------------- # ------------------------
julian -= 1 julian = float((dt.strptime(dateTime, "%d/%m/%Y %H:%M:%S").strftime("%j"))) \
+ ((float(hour) * 3600.) + (float(minute) * 60.) + float(second)) / \
# extract latitude conversion 86400.
# --------------------------------------------
if re_latitude.search(line): # we use julian day with origine 0
(lat_deg, lat_min, lat_hemi) = re_latitude.search(line).groups() # --------------------------------
julian -= 1
# format latitude to string
# ------------------------- # extract latitude conversion
latitude_str = "%s%c%s %s" % (lat_deg, DEGREE, lat_min, lat_hemi) # --------------------------------------------
if re_latitude.search(line):
# transform to decimal using ternary operator (lat_deg, lat_min, lat_hemi) = re_latitude.search(line).groups()
# -------------------------------------------
latitude = float(lat_deg) + (float(lat_min) / 60.) if lat_hemi == 'N' else \ # format latitude to string
(float(lat_deg) + (float(lat_min) / 60.)) * -1 # -------------------------
latitude_str = "%s%c%s %s" % (lat_deg, DEGREE, lat_min, lat_hemi)
# extract longitude conversion
# -------------------------------------------- # transform to decimal using ternary operator
if re_longitude.search(line): # -------------------------------------------
(lon_deg, lon_min, lon_hemi) = re_longitude.search(line).groups() latitude = float(lat_deg) + (float(lat_min) / 60.) if lat_hemi == 'N' else \
(float(lat_deg) + (float(lat_min) / 60.)) * -1
# format longitude to string
# ------------------------- # extract longitude conversion
longitude_str = "%s%c%s %s" % (lon_deg, DEGREE, lon_min, lon_hemi) # --------------------------------------------
if re_longitude.search(line):
# transform to decimal using ternary operator (lon_deg, lon_min, lon_hemi) = re_longitude.search(line).groups()
# -------------------------------------------
longitude = float(lon_deg) + (float(lon_min) / 60.) if lon_hemi == 'E' \ # format longitude to string
else (float(lon_deg) + (float(lon_min) / 60.)) * -1 # -------------------------
longitude_str = "%s%c%s %s" % (lon_deg, DEGREE, lon_min, lon_hemi)
# extract bottom depth
# -------------------------------------------- # transform to decimal using ternary operator
if re_bottom_depth.search(line): # -------------------------------------------
bottom_depth = re_bottom_depth.search(line).group(1) longitude = float(lon_deg) + (float(lon_min) / 60.) if lon_hemi == 'E' \
else (float(lon_deg) + (float(lon_min) / 60.)) * -1
# close header file
# ----------------- # extract bottom depth
file.close() # --------------------------------------------
if re_bottom_depth.search(line):
# display header information in console bottom_depth = re_bottom_depth.search(line).group(1)
# -------------------------------------
if echo: print(" %03d %s %s %s %5.0f %5.0f" % \ # close header file
(station, dateTime, latitude_str, longitude_str, float(Pres), # -----------------
float(bottom_depth)), end = '') file.close()
# write station header to ascii files # display header information in console
# ----------------------------------- # -------------------------------------
if ascii: if echo:
hdr_file.write("%03d %s %s %s %5.0f\n" % \ print(" %03d %s %s %s %5.0f %5.0f" %
(station, dateTime, latitude_str, longitude_str, float(Pres))) (station, dateTime, latitude_str, longitude_str, float(Pres),
float(bottom_depth)), end='')
# write station header in decimal with PRES = -1, and 5 decimals
# -------------------------------------------------------------- # write station header to ascii files
ascii_file.write("%3d %4d %9.5f %8.5f %9.5f %s 1e36 1e36 1e36 1e36\n" % \ # -----------------------------------
(station, CODE, julian, latitude, longitude, epic_date)) if ascii:
hdr_file.write("%03d %s %s %s %5.0f\n" %
# write station header in decimal with PRES = -1, and 5 decimals (station, dateTime, latitude_str, longitude_str, float(Pres)))
# --------------------------------------------------------------
xml_file.write("%3d %4d %9.5f %8.5f %9.5f %s 1e36 1e36 1e36 1e36\n" % \ # write station header in decimal with PRES = -1, and 5 decimals
(station, CODE, julian, latitude, longitude, epic_date)) # --------------------------------------------------------------
ascii_file.write("%3d %4d %9.5f %8.5f %9.5f %s 1e36 1e36 1e36 1e36\n" %
# substitute .hdr or .HDR in fileName with .asc (station, CODE, julian, latitude, longitude, epic_date))
# ---------------------------------------------
fileName = re.sub(r'\.(?i)hdr$', '.asc', fileName) # write station header in decimal with PRES = -1, and 5 decimals
# --------------------------------------------------------------
# we don't use __builtin__ readline method of file object that haven't xml_file.write("%3d %4d %9.5f %8.5f %9.5f %s 1e36 1e36 1e36 1e36\n" %
# method to get read line number (station, CODE, julian, latitude, longitude, epic_date))
# --------------------------------------------------------------------
file = fileinput.input(fileName, openhook=fileinput.hook_encoded("ISO-8859-1")) # substitute .hdr or .HDR in fileName with .asc
# ---------------------------------------------
# iterate over the lines of opened file "fileName" fileName = re.sub(r'\.(?i)hdr$', '.asc', fileName)
# ------------------------------------------------
for line in file: # we don't use __builtin__ readline method of file object that haven't
# method to get read line number
# skip header line # --------------------------------------------------------------------
# ---------------- file = fileinput.input(
if file.isfirstline(): fileName, openhook=fileinput.hook_encoded("ISO-8859-1"))
continue
else: # iterate over the lines of opened file "fileName"
# ------------------------------------------------
# extract data for line in file:
# ------------
(scan,TimeJ,Pres,Depth,T0,T1,C0,C1,v1,v2,v1dt,v2dt,Xmiss,FlC,Aqua,Ox0,Ox1,S0,S1,sigmateta0, \ # skip header line
sigmateta1,sndvel0,sndvel1,nbin,flag) = line.split() # ----------------
if file.isfirstline():
# write data to ascii file continue
# ------------------------ else:
if ascii:
ascii_file.write( \ # extract data
"%3d %4d %6.5g %6.5g %6.3f %7.2f %7.6g %6.4g %7.6g %3d\n" % \ # ------------
(station, float(Pres), float(T0), float(S0), float(sigmateta0), \ (scan, TimeJ, Pres, Depth, T0, T1, C0, C1, v1, v2, v1dt, v2dt, Xmiss, FlC, Aqua, Ox0, Ox1, S0, S1, sigmateta0,
float(sndvel0), float(Ox0), float(FlC), float(Xmiss), int(nbin))) sigmateta1, sndvel0, sndvel1, nbin, flag) = line.split()
# write data to xml file # write data to ascii file
# ------------------------ # ------------------------
if xml: if ascii:
xml_file.write( \ ascii_file.write(
"%3d %4d %6.5g %6.5g %6.3f %7.2f %7.6g %6.4g %7.6g %3d\n" % \ "%3d %4d %6.5g %6.5g %6.3f %7.2f %7.6g %6.4g %7.6g %3d\n" %
(station, float(Pres), float(T0), float(S0), float(sigmateta0), \ (station, float(Pres), float(T0), float(S0), float(sigmateta0),
float(sndvel0), float(Ox0), float(FlC), float(Xmiss), int(nbin))) float(sndvel0), float(Ox0), float(FlC), float(Xmiss), int(nbin)))
# write data to xml file
# close current file # ------------------------
# ------------------ if xml:
file.close() xml_file.write(
"%3d %4d %6.5g %6.5g %6.3f %7.2f %7.6g %6.4g %7.6g %3d\n" %
(station, float(Pres), float(T0), float(S0), float(sigmateta0),
float(sndvel0), float(Ox0), float(FlC), float(Xmiss), int(nbin)))
# close current file
# ------------------
file.close()
# end of loop over files # end of loop over files
# ---------------------- # ----------------------
if echo: if echo:
print("\n") print("\n")
# close files # close files
# ----------- # -----------
if ascii: if ascii:
hdr_file.close() hdr_file.close()
ascii_file.close() ascii_file.close()
if xml: if xml:
xml_file.write(" </DATA>\n") xml_file.write(" </DATA>\n")
xml_file.write("</OCEANO>\n") xml_file.write("</OCEANO>\n")
xml_file.close() xml_file.close()
...@@ -2,35 +2,46 @@ ...@@ -2,35 +2,46 @@
code roscop code roscop
""" """
import csv, shelve,sys import csv
import shelve
import sys
# class roscop
class db:
def __setattr__(self, name, value):
self.__dict__[name] = value
# class roscop
# ------------ # ------------
class Roscop: class Roscop:
# constructor with values by default # constructor with values by default
def __init__(self, file): def __init__(self, file):
self.file = file self.file = file
# call by print() # call by print()
def __repr__(self): def __repr__(self):
return "class Roscop, file: {}".format(self.file) #print("%s:" % row[key], end='')
# print()
# read code roscop file return "class Roscop, file: {}".format(self.file)
def read(self):
print("Code roscop file: %s" % self.file) # read code roscop file
with open(self.file, 'rt') as f: def read(self):
reader = csv.DictReader(f, delimiter=';') print("Code roscop file: %s" % self.file)
print(reader.fieldnames) with open(self.file, 'rt') as f:
for row in reader: reader = csv.DictReader(f, delimiter=';')
for key in reader.fieldnames: for row in reader:
print("%s:" % row[key], end='') d = db()
print() for key in reader.fieldnames:
return d.key = row[key]
return
# for testing in standalone context # for testing in standalone context
# --------------------------------- # ---------------------------------
if __name__ == "__main__": if __name__ == "__main__":
from roscop import Roscop from roscop import Roscop
r = Roscop("code_roscop.csv").read() r = Roscop("code_roscop.csv").read()
print(r) print(r)
\ No newline at end of file
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