#!/usr/bin/env python # coding: utf-8 # In[1]: import numpy as np import matplotlib.pyplot as plt import datetime as dt import os import pandas as pd import arrow import xarray as xr # In[26]: basedir = '/results2/SalishSea/nowcast-green.202111/' #For Rosario # iX = 292 # iY = 267 iZ = 10 # #For VS files # iY = 240 # iX = 240 #For BP files iY = 344 iX = 276 ##For Haro # iY = 276 # iX = 344 start = dt.datetime(2017,1,1) end = dt.datetime(2022,12,31) timerange = arrow.Arrow.range('day',start,end) for i,day in enumerate(timerange): dir1 = day.format('DDMMMYY').lower() ymd = day.format('YYYYMMDD') filename = 'SalishSea_1h_'+ ymd +'_' + ymd + '_grid_U.nc' fullfile = os.path.join(basedir,dir1,filename) u_vel = xr.open_dataset(fullfile) velocity = u_vel['vozocrtx'].isel(y=iY, x=iX).sel(depthu=iZ, method='nearest') u_vel.close() if i == 0: velocity_year = velocity.copy(deep=True) velocity.close() else: velocity_year = xr.concat([velocity_year, velocity], dim='time_counter') velocity.close() if i % 10 == 0: print (i) # In[27]: velocity_year.to_netcdf('velocity_2018-22_BP.nc') # ### Low pass filter # In[29]: velocity_year.plot() # In[30]: velocity = xr.open_dataset('velocity_2018-22_BP.nc') velocity.vozocrtx.plot() # In[31]: vel_squared = velocity*velocity day_avg_tide_vel = vel_squared.resample(time_counter='1D').mean() day_avg_tide_vel.vozocrtx.plot() # In[32]: day_avg_tide_pd = day_avg_tide_vel.to_dataframe() day_avg_tide_pd = day_avg_tide_pd.drop('depthu', axis=1) day_avg_tide_pd = day_avg_tide_pd.drop('nav_lat', axis=1) day_avg_tide_pd = day_avg_tide_pd.drop('nav_lon', axis=1) day_avg_tide_pd.to_csv('day_avg_tide_BP.csv') day_avg_tide_pd.plot() # In[33]: low_pass_tide = day_avg_tide_pd.rolling(4, center=True).mean() low_pass_tide.to_csv('low_pass_tide_2018-22_BP.csv') low_pass_tide.plot()