示例#1
0
文件: suntides.py 项目: mrayson/soda
def QueryNC(dbfile, staname=None, yearrange=None, cons=None):
    """
    Query the tidal station data
    
    """
    outvar = ["NetCDF_Filename", "NetCDF_GroupID", "StationName", "StationID"]
    tablename = "observations"
    # condition = "Variable_Name = '%s' and (StationName = '%s' or StationName = '%s' or StationName = '%s')" % (varname,staname1,staname2,staname3 )

    # Create the query
    varname1 = "ssh_amp"
    varname2 = "ssh_phs"
    if staname == None and yearrange == None:
        condition = "(Variable_Name = '%s' or Variable_Name = '%s')" % (varname1, varname2)
        ydim = None
    elif not staname == None and yearrange == None:
        condition = "(Variable_Name = '%s' or Variable_Name = '%s') and StationName = '%s'" % (
            varname1,
            varname2,
            staname,
        )

        ydim = "year"
    elif not staname == None and not yearrange == None:
        t1 = "%s-01-01 00:00:00" % yearrange[0]
        t2 = "%4d-01-01 00:00:00" % yearrange[1]
        condition = (
            "(Variable_Name = '%s' or Variable_Name = '%s') and StationName = '%s' and time_start > %s and time_start < %s"
            % (varname1, varname2, staname, t1, t2)
        )

        ydim = None
    elif staname == None and not yearrange == None:
        t1 = "%s-01-01 00:00:00" % yearrange[0]
        t2 = "%4d-01-01 00:00:00" % yearrange[1]
        condition = "(Variable_Name = '%s' or Variable_Name = '%s') and time_start > '%s' and time_start < '%s'" % (
            varname1,
            varname2,
            t1,
            t2,
        )
        ydim = "station"

    data, query = netcdfio.queryNC(dbfile, outvar, tablename, condition, fastmode=True)

    # Read the constituents from the netcdf file
    ncfile = query["NetCDF_Filename"][0]
    nc = Dataset(ncfile, "r")
    # names =  nc.__dict__.keys()
    names = nc.Tidal_Constituents.split(", ")
    # print nc.['Tidal Constituents']
    nc.close()

    # Find the constituent indices
    if cons == None:
        cons = names
    ind = []
    for nn in cons:
        ind.append((i for i, j in enumerate(names) if j == nn).next())

    # Output the query data into a nicer format
    # amp = [if dd.has_key('ssh_amp'): dd['ssh_amp'][[1,3,8]].ravel() for dd in data]
    amp = []
    phs = []
    time = []
    lon = []
    lat = []
    StationName = []
    for ii, dd in enumerate(data):
        if dd.has_key("ssh_amp"):
            amp.append(dd["ssh_amp"][ind].ravel())

            if ydim == "year":
                time.append(dd["time"][0])
            elif ydim == "station":
                lon.append(dd["longitude"])
                lat.append(dd["latitude"])
                StationName.append(query["StationName"][ii])
            else:
                lon.append(dd["longitude"])
                lat.append(dd["latitude"])
                StationName.append(query["StationName"][ii])
                time.append(dd["time"][0])

        if dd.has_key("ssh_phs"):
            phs.append(dd["ssh_phs"][ind].ravel())

    amp = np.array(amp)
    phs = np.array(phs)

    # Get the time and station coordinates
    if ydim == "station":
        time = data[0]["time"][0]
        lon = np.array(lon)
        lat = np.array(lat)

    elif ydim == "year":
        lon = data[0]["longitude"]
        lat = data[0]["latitude"]
        StationName = query["StationName"][0]
        time = np.array(time)

    return amp, phs, time, lon, lat, StationName, cons
示例#2
0
def loadDBstation(dbfile, stationName, varname, timeinfo=None, \
     filttype=None,cutoff=3600.0,output_meta=False,method='linear'):
    """
    Load station data from a database file
    
    Inputs:
        dbfile - location of database file
        stationName - StationName in database
        varname - variable name e.g. 'waterlevel', 'discharge', 'salinity'
        
        timeinfo (optional) - tuple with (starttime,endtime,dt). Format 'yyyymmdd.HHMMSS'
            Use this to interpolate onto a constant time vector
        filttype (optional) - 'low' or 'high' 
            Set this to filter data
            
    Returns:
        timeseries object
        -1 on error
            
    """
    
    outvar = ['NetCDF_Filename','NetCDF_GroupID','StationName']
    tablename = 'observations'
    #condition = 'Variable_Name = "%s" and StationID = "%s"' % (varname,stationID)
    condition = 'Variable_Name = "%s" and StationName LIKE "%%%s%%"' % (varname,stationName)
    
    print 'Querying database...'
    print condition
    data, query = queryNC(dbfile,outvar,tablename,condition)  

    yout = data[0][varname].squeeze()
    # Zero nan
    yout[np.isnan(yout)] = 0.0
    
    if len(data)==0:
        print '!!! Warning - Did not find any stations matching query. Returning -1 !!!'
        return -1
    else:
        ts = timeseries(data[0]['time'],yout)
        
        
    if not timeinfo==None:
        print 'Interpolating station data between %s and %s\n'%(timeinfo[0],timeinfo[1])
        tnew,ynew =\
            ts.interp((timeinfo[0],timeinfo[1],timeinfo[2]),method=method)
        ts = timeseries(tnew,ynew)
        ts.dt = timeinfo[2] # This needs updating
        
    if not filttype==None:
        print '%s-pass filtering output data. Cutoff period = %f [s].'%(filttype,cutoff)
        yfilt = ts.filt(cutoff,btype=filttype,axis=-1)
        ts.y = yfilt.copy()
    
    if output_meta:
        if data[0].has_key('elevation'):
            ele = data[0]['elevation']
        else:
            ele = np.array([0.0])
        meta = {'longitude':data[0]['longitude'],'latitude':data[0]['latitude'],'elevation':ele,'StationName':query['StationName'][0]}
        return ts, meta        
    else:
        return ts
示例#3
0
def QueryNC(dbfile,staname=None,yearrange=None,cons=None):
    """
    Query the tidal station data

    """
    outvar = ['NetCDF_Filename','NetCDF_GroupID','StationName','StationID']
    tablename = 'observations'
    #condition = "Variable_Name = '%s' and (StationName = '%s' or StationName = '%s' or StationName = '%s')" % (varname,staname1,staname2,staname3 )

    # Create the query
    varname1 = 'ssh_amp'
    varname2 = 'ssh_phs'
    if staname == None and yearrange == None:
        condition = "(Variable_Name = '%s' or Variable_Name = '%s')"%(varname1,varname2)
        ydim = None
    elif not staname == None and yearrange == None:
        condition = "(Variable_Name = '%s' or Variable_Name = '%s') and StationName = '%s'"%(varname1,varname2, staname)

        ydim = 'year'
    elif not staname == None and not yearrange == None:
        t1 = '%s-01-01 00:00:00'%yearrange[0]
        t2 = '%4d-01-01 00:00:00'%yearrange[1]
        condition = "(Variable_Name = '%s' or Variable_Name = '%s') and StationName = '%s' and time_start > %s and time_start < %s"%(varname1,varname2,staname,t1,t2)

        ydim = None
    elif staname == None and not yearrange == None:
        t1 = '%s-01-01 00:00:00'%yearrange[0]
        t2 = '%4d-01-01 00:00:00'%yearrange[1]
        condition = "(Variable_Name = '%s' or Variable_Name = '%s') and time_start > '%s' and time_start < '%s'"%(varname1,varname2,t1,t2)
        ydim = 'station'

    data, query = netcdfio.queryNC(dbfile,outvar,tablename,condition,fastmode=True)

    # Read the constituents from the netcdf file
    ncfile = query['NetCDF_Filename'][0]
    nc = Dataset(ncfile,'r')
    #names =  nc.__dict__.keys()
    names = nc.Tidal_Constituents.split(', ')
    #print nc.['Tidal Constituents']
    nc.close()

    # Find the constituent indices
    if cons == None:
        cons=names
    ind = []
    for nn in cons:
        ind.append(next((i for i, j in enumerate(names) if j == nn)))

    # Output the query data into a nicer format
    #amp = [if dd.has_key('ssh_amp'): dd['ssh_amp'][[1,3,8]].ravel() for dd in data]
    amp = []
    phs = []
    time = []
    lon = []
    lat = []
    StationName=[]
    for ii,dd in enumerate(data):
        if 'ssh_amp' in dd:
            amp.append(dd['ssh_amp'][ind].ravel())

            if ydim == 'year':
                time.append(dd['time'][0])
            elif ydim == 'station':
                lon.append(dd['longitude'])
                lat.append(dd['latitude'])
                StationName.append(query['StationName'][ii])
            else:
                lon.append(dd['longitude'])
                lat.append(dd['latitude'])
                StationName.append(query['StationName'][ii])
                time.append(dd['time'][0])



        if 'ssh_phs' in dd:
            phs.append(dd['ssh_phs'][ind].ravel())

    amp = np.array(amp)
    phs = np.array(phs)

    # Get the time and station coordinates
    if ydim == 'station':
        time = data[0]['time'][0]
        lon = np.array(lon)
        lat = np.array(lat)

    elif ydim == 'year':
        lon = data[0]['longitude']
        lat = data[0]['latitude']
        StationName = query['StationName'][0]
        time = np.array(time)


    return amp, phs, time, lon, lat, StationName, cons