Beispiel #1
0
def tide_pred_correc(modfile,
                     lon,
                     lat,
                     time,
                     dbfile,
                     ID,
                     z=None,
                     conlist=None):
    """
    Performs a tidal prediction at all points in [lon,lat] at times in vector [time]

    Applies an amplitude and phase correction based on a time series
    """
    raise Exception("RH: Not translated!")

    from timeseries import timeseries, loadDBstation

    print('Calculating tidal correction factors from time series...')
    # Load using the timeseries module
    t0 = datetime.strftime(time[0], '%Y%m%d.%H%M%S')
    t1 = datetime.strftime(time[-1], '%Y%m%d.%H%M%S')
    dt = time[1] - time[0]

    print(t0, t1, dt.total_seconds())
    timeinfo = (t0, t1, dt.total_seconds())
    TS, meta = loadDBstation(dbfile,
                             ID,
                             'waterlevel',
                             timeinfo=timeinfo,
                             filttype='low',
                             cutoff=2 * 3600,
                             output_meta=True)
    lonpt = meta['longitude']
    latpt = meta['latitude']
    print(lonpt, latpt)

    # Extract the OTIS tide prediction
    u_re, u_im, v_re, v_im, h_re, h_im, omega, conlist = extract_HC(
        modfile, lonpt, latpt)
    h_amp = np.abs(h_re + 1j * h_im)[:, 0]
    h_phs = np.angle(h_re + 1j * h_im)[:, 0]

    # Harmonic analysis of observation time series
    amp, phs, frq, frqnames, htide = TS.tidefit(frqnames=conlist)
    TS_harm = timeseries(time, htide)
    residual = TS.y - htide

    # Calculate the amp and phase corrections
    dphs = phs - h_phs + np.pi
    damp = amp / h_amp

    # Extract the data along the specified points
    u_re, u_im, v_re, v_im, h_re, h_im, omega, conlist = extract_HC(
        modfile, lon, lat, z=z, conlist=conlist)

    h_amp = np.abs(h_re + 1j * h_im)
    h_phs = np.angle(h_re + 1j * h_im)
    u_amp = np.abs(u_re + 1j * u_im)
    u_phs = np.angle(u_re + 1j * u_im)
    v_amp = np.abs(v_re + 1j * v_im)
    v_phs = np.angle(v_re + 1j * v_im)

    # Initialise the output arrays
    sz = lon.shape
    nx = np.prod(sz)
    nt = time.shape[0]

    h = np.zeros((nt, nx))
    u = np.zeros((nt, nx))
    v = np.zeros((nt, nx))

    # Rebuild the time series
    #tsec=TS_harm.tsec - TS_harm.tsec[0]
    tsec = othertime.SecondsSince(time, basetime=time[0])
    print(tsec[0])
    for nn, om in enumerate(omega):
        for ii in range(0, nx):
            h[:,
              ii] += damp[nn] * h_amp[nn,
                                      ii] * np.cos(om * tsec -
                                                   (h_phs[nn, ii] + dphs[nn]))
            u[:,
              ii] += damp[nn] * u_amp[nn,
                                      ii] * np.cos(om * tsec -
                                                   (u_phs[nn, ii] + dphs[nn]))
            v[:,
              ii] += damp[nn] * v_amp[nn,
                                      ii] * np.cos(om * tsec -
                                                   (v_phs[nn, ii] + dphs[nn]))

    szo = (nt, ) + sz
    return h.reshape(szo), u.reshape(szo), v.reshape(szo), residual
Beispiel #2
0
    def _makebnd(self):
        """
        Generate boundary condition files
        """
        
        if self.modifyedges:        
            modifyBCmarker(self.suntanspath,self.bcpolygonfile)
    
        #Load the boundary object from the grid
        bnd = Boundary(self.suntanspath,(self.starttime,self.endtime,self.dt))
        
        ###
        # Segment (flux) boundaries
        ###
        if self.opt_bcseg == 'constant':
            print 'Setting %d boundary segments to discharge of %6.3f m3/s'%(bnd.Nseg,self.Q0)
            bnd.boundary_Q[:]=self.Q0
            
        elif self.opt_bcseg == 'file':
            print 'Loading river segment data from file...\n'
            for ii, ID in enumerate(bnd.segp):
                print 'Loading discahrge data for boundary segment (%d) StationID: %d...'%(ii,ID)
                
                ts = timeseries.loadDBstation(self.dbasefile,ID,'discharge',timeinfo=(self.starttime,self.endtime,self.dt),\
                    filttype=self.filttype,cutoff=self.cutoff)
                    
                bnd.boundary_Q[:,ii]=ts.y.copy()
            
        else:
            print 'Unknown option: opt_bcseg = %s. Not setting boundary segment data.'%self.opt_bcseg
            
        ###
        # Type-3 boundaries
        ### 
        self.useROMS = False
        self.useOTIS = False
        self.useFILE = False
        self.useOTISFILE = False

        if self.opt_bctype3=='constant':
            print 'Setting constant type-3 boundary conditions...'  
            print 'Setting salinity = %f, temperature = %f'%(self.S0,self.T0)
            bnd.S[:]=self.S0
            bnd.T[:]=self.T0
            
        elif self.opt_bctype3=='depth_profile':
            print 'Setting type-3 boundary T/S from profile...'  
            
            self.loadTSprofile()
            for ii in range(0,bnd.N3):
                bnd.T[0,:,ii] = self.Tz
                bnd.S[0,:,ii] = self.Sz
        
        elif self.opt_bctype3 in ('ROMS'):
            self.useROMS = True

        elif self.opt_bctype3 in ('OTIS'):
            self.useOTIS = True
            
        elif self.opt_bctype3 in ('file'):
            self.useFILE = True
            
        elif self.opt_bctype3 in ('ROMSOTIS'):
            self.useROMS = True
            self.useOTIS = True
        
        elif self.opt_bctype3 in ('ROMSFILE'):
            self.useROMS = True
            self.useFILE = True
        
        elif self.opt_bctype3 in ('OTISFILE'):
            self.useOTISFILE = True

        elif self.opt_bctype3 in ('ROMSOTISFILE'):
            self.useOTISFILE = True
            self.useROMS = True

        else:
            print 'Unknown option: opt_bctype3 = %s. Not setting type-3 boundaries.'%self.opt_bctype3

            
        if self.useROMS:
            bnd.roms2boundary(self.romsfile,setUV=self.useROMSuv,seth=self.useROMSeta)
            
        if self.useOTIS:
            bnd.otis2boundary(self.otisfile,setUV=self.useOTISuv)

        if self.useOTISFILE:
            bnd.otisfile2boundary(self.otisfile,self.dbasefile,self.waterlevelstationID,setUV=self.useOTISuv)
            
        if self.useFILE:
            ID = self.waterlevelstationID
            print 'Loading waterlevel onto all type-3 points from stationID: %d...'%(ID)
            ts = timeseries.loadDBstation(self.dbasefile,ID,'waterlevel',timeinfo=(self.starttime,self.endtime,self.dt),\
                    filttype=self.filttype,cutoff=self.cutoff)
                    
            for ii in range(bnd.N3):
                bnd.h[:,ii] += ts.y.copy()
            
        ###
        # Type-2 boundaries
        ###
        self.useFILE2 = False

        if self.opt_bctype2 == 'constant':
            print 'Setting constant type-2 boundary conditions...'  
            print 'Setting salinity = %f, temperature = %f'%(self.S0,self.T0)
            bnd.boundary_S[:]=self.S0
            bnd.boundary_T[:]=self.T0
        elif self.opt_bctype2 == 'file':
            print 'Using file for type-2 boundary condition (temperature only)'
            print 'Setting salinity = %f'%(self.S0)
            bnd.boundary_S[:]=self.S0
            self.useFILE2 = True
        else:
            print 'Unknown option: opt_bctype2 = %s. Not setting type-2 boundaries.'%self.opt_bctype3
            
            
        if self.useFILE2:
            ID = self.TairstationID
            print 'Loading air temperature onto all type-2 points from stationID: %s...'%(ID)
            ts = timeseries.loadDBstation(self.dbasefile,ID,'Tair',timeinfo=(self.starttime,self.endtime,self.dt),\
            filttype=self.tairfilttype,cutoff=self.taircutoff)
                    
            for ii in range(bnd.N2):
                for kk in range(bnd.Nk):
                    bnd.boundary_T[:,kk,ii] += ts.y.copy()
            
        # Write to netcdf
        bnd.write2NC(self.suntanspath+'/'+self.bcfile)
Beispiel #3
0
def tide_pred_correc(modfile,lon,lat,time,dbfile,ID,z=None,conlist=None):
    """
    Performs a tidal prediction at all points in [lon,lat] at times in vector [time]
    
    Applies an amplitude and phase correction based on a time series
    """
    from timeseries import timeseries, loadDBstation
    
    print 'Calculating tidal correction factors from time series...'
    # Load using the timeseries module
    t0 = datetime.strftime(time[0],'%Y%m%d.%H%M%S')
    t1 = datetime.strftime(time[-1],'%Y%m%d.%H%M%S')
    dt = time[1]-time[0]
    
    print t0, t1, dt.total_seconds()
    timeinfo = (t0,t1,dt.total_seconds())
    TS,meta = loadDBstation(dbfile,ID,'waterlevel',timeinfo=timeinfo,filttype='low',cutoff=2*3600,output_meta=True)
    lonpt=meta['longitude']
    latpt=meta['latitude']
    print lonpt,latpt
    
    # Extract the OTIS tide prediction
    u_re, u_im, v_re, v_im, h_re, h_im, omega, conlist = extract_HC(modfile,lonpt,latpt)
    h_amp = np.abs(h_re+1j*h_im)[:,0]
    h_phs = np.angle(h_re+1j*h_im)[:,0]
    
    # Harmonic analysis of observation time series
    amp, phs, frq, frqnames, htide = TS.tidefit(frqnames=conlist)
    TS_harm = timeseries(time,htide)
    residual = TS.y - htide
    
    # Calculate the amp and phase corrections
    dphs = phs - h_phs + np.pi
    damp = amp/h_amp

    # Extract the data along the specified points
    u_re, u_im, v_re, v_im, h_re, h_im, omega, conlist = extract_HC(modfile,lon,lat,z=z,conlist=conlist)
    
    h_amp = np.abs(h_re+1j*h_im)
    h_phs = np.angle(h_re+1j*h_im)
    u_amp = np.abs(u_re+1j*u_im)
    u_phs = np.angle(u_re+1j*u_im)
    v_amp = np.abs(v_re+1j*v_im)
    v_phs = np.angle(v_re+1j*v_im)
        
    # Initialise the output arrays
    sz = lon.shape
    nx = np.prod(sz)
    nt = time.shape[0]

    h=np.zeros((nt,nx))
    u=np.zeros((nt,nx))
    v=np.zeros((nt,nx))
    
    # Rebuild the time series
    #tsec=TS_harm.tsec - TS_harm.tsec[0]
    tsec = othertime.SecondsSince(time,basetime=time[0])
    print tsec[0]
    for nn,om in enumerate(omega):
        for ii in range(0,nx):
            h[:,ii] += damp[nn]*h_amp[nn,ii] * np.cos(om*tsec - (h_phs[nn,ii] + dphs[nn]))
            u[:,ii] += damp[nn]*u_amp[nn,ii] * np.cos(om*tsec - (u_phs[nn,ii] + dphs[nn]))
            v[:,ii] += damp[nn]*v_amp[nn,ii] * np.cos(om*tsec - (v_phs[nn,ii] + dphs[nn]))
            
    szo = (nt,)+sz
    return h.reshape(szo), u.reshape(szo), v.reshape(szo), residual
Beispiel #4
0
    def _makebnd(self):
        """
        Generate boundary condition files
        """

        if self.modifyedges:
            modifyBCmarker(self.suntanspath, self.bcpolygonfile)

        #Load the boundary object from the grid
        bnd = Boundary(self.suntanspath,
                       (self.starttime, self.endtime, self.dt))

        ###
        # Segment (flux) boundaries
        ###
        if self.opt_bcseg == 'constant':
            print 'Setting %d boundary segments to discharge of %6.3f m3/s' % (
                bnd.Nseg, self.Q0)
            bnd.boundary_Q[:] = self.Q0

        elif self.opt_bcseg == 'file':
            print 'Loading river segment data from file...\n'
            for ii, ID in enumerate(bnd.segp):
                print 'Loading discahrge data for boundary segment (%d) StationID: %d...' % (
                    ii, ID)

                ts = timeseries.loadDBstation(self.dbasefile,ID,'discharge',timeinfo=(self.starttime,self.endtime,self.dt),\
                    filttype=self.filttype,cutoff=self.cutoff)

                bnd.boundary_Q[:, ii] = ts.y.copy()

        else:
            print 'Unknown option: opt_bcseg = %s. Not setting boundary segment data.' % self.opt_bcseg

        ###
        # Type-3 boundaries
        ###
        self.useROMS = False
        self.useOTIS = False
        self.useFILE = False
        self.useOTISFILE = False

        if self.opt_bctype3 == 'constant':
            print 'Setting constant type-3 boundary conditions...'
            print 'Setting salinity = %f, temperature = %f' % (self.S0,
                                                               self.T0)
            bnd.S[:] = self.S0
            bnd.T[:] = self.T0

        elif self.opt_bctype3 == 'depth_profile':
            print 'Setting type-3 boundary T/S from profile...'

            self.loadTSprofile()
            for ii in range(0, bnd.N3):
                bnd.T[0, :, ii] = self.Tz
                bnd.S[0, :, ii] = self.Sz

        elif self.opt_bctype3 in ('ROMS'):
            self.useROMS = True

        elif self.opt_bctype3 in ('OTIS'):
            self.useOTIS = True

        elif self.opt_bctype3 in ('file'):
            self.useFILE = True

        elif self.opt_bctype3 in ('ROMSOTIS'):
            self.useROMS = True
            self.useOTIS = True

        elif self.opt_bctype3 in ('ROMSFILE'):
            self.useROMS = True
            self.useFILE = True

        elif self.opt_bctype3 in ('OTISFILE'):
            self.useOTISFILE = True

        elif self.opt_bctype3 in ('ROMSOTISFILE'):
            self.useOTISFILE = True
            self.useROMS = True

        else:
            print 'Unknown option: opt_bctype3 = %s. Not setting type-3 boundaries.' % self.opt_bctype3

        if self.useROMS:
            bnd.roms2boundary(self.romsfile,
                              setUV=self.useROMSuv,
                              seth=self.useROMSeta)

        if self.useOTIS:
            bnd.otis2boundary(self.otisfile, setUV=self.useOTISuv)

        if self.useOTISFILE:
            bnd.otisfile2boundary(self.otisfile,
                                  self.dbasefile,
                                  self.waterlevelstationID,
                                  setUV=self.useOTISuv)

        if self.useFILE:
            ID = self.waterlevelstationID
            print 'Loading waterlevel onto all type-3 points from stationID: %d...' % (
                ID)
            ts = timeseries.loadDBstation(self.dbasefile,ID,'waterlevel',timeinfo=(self.starttime,self.endtime,self.dt),\
                    filttype=self.filttype,cutoff=self.cutoff)

            for ii in range(bnd.N3):
                bnd.h[:, ii] += ts.y.copy()

        ###
        # Type-2 boundaries
        ###
        self.useFILE2 = False

        if self.opt_bctype2 == 'constant':
            print 'Setting constant type-2 boundary conditions...'
            print 'Setting salinity = %f, temperature = %f' % (self.S0,
                                                               self.T0)
            bnd.boundary_S[:] = self.S0
            bnd.boundary_T[:] = self.T0
        elif self.opt_bctype2 == 'file':
            print 'Using file for type-2 boundary condition (temperature only)'
            print 'Setting salinity = %f' % (self.S0)
            bnd.boundary_S[:] = self.S0
            self.useFILE2 = True
        else:
            print 'Unknown option: opt_bctype2 = %s. Not setting type-2 boundaries.' % self.opt_bctype3

        if self.useFILE2:
            ID = self.TairstationID
            print 'Loading air temperature onto all type-2 points from stationID: %s...' % (
                ID)
            ts = timeseries.loadDBstation(self.dbasefile,ID,'Tair',timeinfo=(self.starttime,self.endtime,self.dt),\
            filttype=self.tairfilttype,cutoff=self.taircutoff)

            for ii in range(bnd.N2):
                for kk in range(bnd.Nk):
                    bnd.boundary_T[:, kk, ii] += ts.y.copy()

        # Write to netcdf
        bnd.write2NC(self.suntanspath + '/' + self.bcfile)