# imports
from sqlalchemy import create_engine, Column, String, Integer, Numeric, MetaData, Table, type_coerce, case
from sqlalchemy.orm import mapper, create_session
from sqlalchemy.sql import and_, or_, not_, select
import sqlalchemy.types as types
from sqlalchemy.ext.automap import automap_base
import csv
import numpy as np
import matplotlib.pyplot as plt
import numbers
import re
test=re.split('\s*,\s*','VARIABLES , ,, abc,,def ,g\n')
print(test)
['VARIABLES', '', '', 'abc', '', 'def', 'g\n']
if re.search('^VARIABLES', 'VARIABLES , ,, abc,,def ,g\n'):
print('match')
if re.search('^VARIABLES', ' VARIABLES , ,, abc,,def ,g\n'):
print('spacematch')
match
if re.search('^END\sOF\sVARIABLES','END OF VARIABLES SECTION,'):
print('match')
match
splitline=re.split('\s*,\s*','VARIABLES ,Depth ,F,O,Temperatur,F,O,Salinity ,F,O,Oxygen ,F,O,Phosphate ,F,O,Silicate ,F,O,Nitrate ,F,O,pH ,F,O,,')
print (splitline)
['VARIABLES', 'Depth', 'F', 'O', 'Temperatur', 'F', 'O', 'Salinity', 'F', 'O', 'Oxygen', 'F', 'O', 'Phosphate', 'F', 'O', 'Silicate', 'F', 'O', 'Nitrate', 'F', 'O', 'pH', 'F', 'O', '', '']
sear=re.compile('^.+$').search
test=[m.group(0) for m in map(sear, splitline) if m]
print(test)
n=int((len(test)-1)/3)
for i in range(0,n):
print(test[i*3+1])
print(test[i*3+2])
print(test[i*3+3])
['VARIABLES', 'Depth', 'F', 'O', 'Temperatur', 'F', 'O', 'Salinity', 'F', 'O', 'Oxygen', 'F', 'O', 'Phosphate', 'F', 'O', 'Silicate', 'F', 'O', 'Nitrate', 'F', 'O', 'pH', 'F', 'O'] Depth F O Temperatur F O Salinity F O Oxygen F O Phosphate F O Silicate F O Nitrate F O pH F O
# definitions
file1='/ocean/eolson/MEOPAR/obs/NODCJDFSearch/ocldb1449111998.1790.OSD.csv'
f=open(file1,'r')
linno=0
for line in f:
linno+=1
if linno>25:
break
test=re.split('\s*,\s*',line)
print(test)
['#--------------------------------------------------------------------------------', ''] ['CAST', '', '6510729', 'WOD Unique Cast Number', 'WOD code', ''] ['NODC Cruise ID', '', 'CA-10936', '', '', ''] ['Originators Station ID', '', '3', '', '', 'integer', ''] ['Originators Cruise ID', '', '30600', '', '', ''] ['Latitude', '', '49.2583', 'decimal degrees', '', ''] ['Longitude', '', '-123.7833', 'decimal degrees', '', ''] ['Year', '', '1931', '', '', ''] ['Month', '', '1', '', '', ''] ['Day', '', '28', '', '', ''] ['Time', '', '22.70', 'decimal hours (UT)', '', ''] ['METADATA', ''] ['Country', '', 'CA', 'NODC code', 'CANADA\n'] ['Accession Number', '', '9600170.', 'NODC code', '', ''] ['Platform', '', '789.', 'OCL code', 'A. P. KNIGHT (F/V;comm-d 1927;decomm-d 1974)', ''] ['Institute', '', '141.', 'NODC code', 'FISH RES BOARD OF CANADA PACIFIC OCEANOGRAPHIC GROUP (NANAIMO)', ''] ['Wind Force', '', '0.', 'Beaufort Scale', 'CALM MEAN VELOCITY IN KNOTS <1 IN METERS/SEC 0-0.2 IN KM/H <1 IN M.P.H. <1 /WAV', ''] ['Wind Direction', '', '0.', 'WMO code 0877', 'CALM (NO WAVES-NO MOTION)', ''] ['Wind speed', '', '0.0', 'knots', '', ''] ['Barometric pressure', '', '1020.90', 'millibars', '', ''] ['Air temperature (dry bulb)', '', '9.90', 'degrees Celsius', '', ''] ['probe_type', '', '7.', 'OCL_code', 'bottle/rossette/net', ''] ['Original units', 'Oxygen', '7.', 'NODC code', 'ug-at/l mmol/m3 umol/l uM umol/dm3', ''] ['VARIABLES', 'Depth', 'F', 'O', 'Temperatur', 'F', 'O', 'Salinity', 'F', 'O', 'Oxygen', 'F', 'O', 'Phosphate', 'F', 'O', 'Nitrate', 'F', 'O', 'pH', 'F', 'O', '', ''] ['UNITS', 'm', '', '', 'degrees C', '', '', 'PSS', '', '', 'ml/l', '', '', 'umol/l', '', '', 'umol/l', '', '', '(n/a)', '', '', '', '']
varlist={x for x in ['var1', 'F','O','var2','F','O'] if x not in {'F','O'}}
varlist
{'var1', 'var2'}
varlist=varlist | {x for x in ['var3', 'F','O','var2','','F','O'] if x not in {'F','O',''}}
varlist
{'var1', 'var2', 'var3'}