๊ธฐ๋ณธ์ ์ธ python ๊ฐ๋
์ ์๊ณ ์๋ค๋ ์ ์ ํ,
๊ฐ๋จํ๊ฒ ์ฌ์ฉํ๋ ๊ฒ๋ค ์์ฃผ๋ก ๊ธฐ์ตํ๊ธฐ ์ํด ์ต์ํ์ ์ฝ๋๋ก ์์ฑํ๋ ๊ธ์์ ์ฐธ๊ณ ๋ถํ๋๋ฆฝ๋๋ค.
NetCDF (Network Common Data Form) ๋ฐ์ดํฐ๊ฐ ๋ญ์์ฉ?
ํด์๊ณผ ๋๊ธฐ ๊ณผํ์์ ๋๋ฆฌ ์ฐ์ธ๋ค.
- ๋ค์ฐจ์ ๋ฐ์ดํฐ
- ์๊ฐ๊ณผ ๊ณต๊ฐ์ ๋ฐ๋ฅธ ๋๊ธฐ, ํด์, ๋ฐ์ดํฐ๋ค
๋ฐฐ์ด ๋๋ ํ๋ ฌ๋ก ๊ตฌ์ฑ๋ ๊ณผํ์ ๋ณด ํ์
ํด์ ์์น ๋ชจ๋ธ ๊ฒฐ๊ณผ๋ ๊ณต๊ฐ์ ๋ณด(3์ฐจ์)์ ์๊ฐ์ ๋ฐ๋ฅธ ๊ฐ์ด ์ ๋ ฅ, ๊ฐ ๋ํ ์ฌ๋ฌ ๊ฐ์ง ๋ณ์๋ฅผ ํฌํจ. (x,y์ ๋ฐ๋ฅธ h๊ฐ. ๋ฉด๋ฐ์ดํฐ)
๋ํ ๊ณต๊ฐ๊ณผ ์๊ฐ์ ๋ฐ๋ฅธ ํ๊ณ , ํํฅ, ํ์ฃผ๊ธฐ ๋ฑ ๋ถ๊ฐ ์ ๋ณด๋ฅผ ํฌํจ.
๊ด์ธก์ ๋ณด ์ผ ๋๋ ๊ด์ธก์๋ช , ํ์ค์, ์์ธต, ์ฅ๋น๋ช ๋ฑ๊ณผ ๋ชจ๋ธ์ผ ๋๋ ์๊ฐ, ๊ณต๊ฐ ํ์ค, ๊ฐ ๋จ์, fill_value ๋ฑ๋ฑ ํฌํจ๋์ด์ผ ํ ์ ๋ณด๋ค์ด ๋ง์.
์๋ก ์๊ฐ๊ณผ ๊ณต๊ฐ (x, y, t) + ๊ทธ ๊ฐ์ ๋ฐ๋ฅธ z ๊น์ง๋ฉด 4์ฐจ์ ๋ฐ์ดํฐ๊ฐ ๋จ.
์ด๋ฐ ์๊ฐ์ ๋ฐ๋ฅธ ๋ค์์ ์ (๊ณต๊ฐ, ๋ช )์ ๊ฐ์ ๊ฐ์ง๊ธฐ์ NetCDF ํ์์ ํ์ฉํ๊ณ ์์.
(๋์ถฉ ๊ณต๊ฐ, ์๊ฐ, ๋ชจ๋ ์ ๋ณด ๋ค์ฐจ์ ๋ฐ์ดํฐ๋ผ๋ ๋ป)
NetCDF file structure
- Dimension (์ฐจ์ : ์๋, ๊ฒฝ๋, ์๊ฐ์ ํฌ๊ธฐ, ๊ฐฏ์ ๋ฑ)
- Variables (์ค์ ๋ณ์๋ค์ ๊ฐ)
- attributes (ํ์ผ์ ๋ํ ๋ฉํ์ ๋ณด) ๋ก ๋๋์ด์ ธ ์์.
ํ์ธํ๊ธฐ ์ํด์๋ → linux - ncdump, matlap - ncinfo
NetCDF Control
pip install netCDF4 seawater GSW xarray
conda install -c conda-forge basemap-data-hires
pip install netCDF4 seawater GSW xarray
conda install -c conda-forge basemap-data-hires
import numpy as np
import xarray as xr
import os
import matplotlib.pyplot as plt # Corrected import for pyplot
# Set absolute path
work_path = '/DataProcessing_TRC/'
input_file = 'data/CTD_station_P1_NLEG01-1_-_Nansen_Legacy_Cruise_-_2021_Joint_Cruise_2-1.nc'
out_file = 'data/ctd_data_set.csv'
# Use path join to create full file paths
f_name = os.path.join(work_path, input_file)
f_out_name = os.path.join(work_path, out_file)
# Load the dataset
ds = xr.open_dataset(f_name)
# Extract temperature values
temperature = ds['TEMP'].values
# Convert selected variables to a DataFrame
df = ds[['PRES', 'TEMP', 'PSAL']].to_dataframe()
# Save the DataFrame to a CSV file
df.to_csv(f_out_name)
# Plotting
fig1 = plt.figure()
# Temperature plot
ax1 = fig1.add_subplot(121)
ax1.plot(ds['TEMP'], ds['PRES'], 'bo-', markersize=2, linewidth=0.5)
ax1.set_ylim(ax1.get_ylim()[::-1]) # Reverse the y-axis
ax1.set_xlabel('Temperature (C)')
ax1.set_ylabel('Depth (m)')
plt.grid(True)
ax1.xaxis.set_label_position('top') # Move the label to the top
ax1.xaxis.set_ticks_position('top') # Move the ticks to the top
# Salinity plot
ax2 = fig1.add_subplot(122)
ax2.plot(ds['PSAL'], ds['PRES'], 'ro-', markersize=2, linewidth=0.5)
ax2.xaxis.set_label_position('top')
ax2.xaxis.set_ticks_position('top')
ax2.set_ylim(ax2.get_ylim()[::-1]) # Reverse the y-axis
plt.grid(True)
ax2.set_xlabel('Salinity (PSU)')
# Show the plots
plt.show()
NetCDF ๋ํด ํ๊ท ํด๋ฅ๋ ๋ค๋ฃจ๊ธฐ
format ํ์ธํ๊ธฐ
# Set absolute path
work_path = '/mnt/c/Users/MML/sh/DataProcessing_TRC/'
input_file = 'data/KHOA_SCU_L4_Z004_Y01_U2023_EastSea.nc'
f_name = work_path + input_file
f_data = xr.open_dataset(f_name)
print(f_data)
u_comp = f_data.data_vars['u']
print(u_comp)
Realizing The East Sea NetCDF
''' File Processing '''
f_name = work_path + input_file
ds = xr.open_dataset(f_name) # Open the NetCDF dataset
# f_data = xr.open_dataset(f_name)
# print(f_data)
# u_comp = f_data.data_vars['u']
# print(u_comp)
lat = ds["lat"].values # ์๋.values ๋ก numpy ๋ณํ
lon = ds["lon"].values # ๊ฒฝ๋.values ๋ก numpy ๋ณํ
# Transpose the u and v velocity components
u_velocity = np.transpose(ds["u"].values) # East-west velocity
v_velocity = np.transpose(ds["v"].values) # North-south velocity
- np.transpose()
๋ณดํต ํด์ ๋ฐ์ดํฐ๋ ์๊ฐ, ๊น์ด, ์๋, ๊ฒฝ๋ ๋ฑ์ ์ฐจ์์ ๊ฐ์ง๊ณ ์์ด ์๋ (u, v)์ ๋ฐฐ์ด์ด ์๊ฐ๊ณผ ๊น์ด ์ฐจ์์ ๋จผ์ ํฌํจํ๋ค๋ฉด, ์ด๋ฅผ ์ ์นํ์ฌ ์ํ๋ ์ฐจ์์ ์ด์ฉํ๊ธฐ ์ํด ์ฌ์ฉํจ!
(๋์ถฉ ์ํ๋ ๊ฑฐ ๊ณจ๋ผ์ฐ๋ ค๊ณ . ๊ฐ์ ๊ฐ์ ธ์ค๋ ค๊ณ ๋ค์ง์๋ค๋ ๋ป)
''' Convert to DataFrame for calculations '''
# Generate a grid for longitude and latitude
lon_grid, lat_grid = np.meshgrid(lon, lat)
# Create a DataFrame with longitude, latitude, and velocity components
data_frame = pd.DataFrame({
"lon": lon_grid.ravel(),
"lat": lat_grid.ravel(),
"u_velocity": u_velocity.ravel(), # Flatten to 1D
"v_velocity": v_velocity.ravel() # Flatten to 1D
})
- .ravel()
์๋ณธ์ด ์๋ view๋ฅผ ๋ฐํ.
๋ค์ฐจ์ -> 1์ฐจ์์ผ๋ก ๋ณํํ์ฌ Pandas DataFrame์ ์์ฑํ๊ณ ์์.
ํ row ๋ก ๋์ด๋์ด ์ฝ๊ฒ ์ ์ฅํ๊ณ ์ด์ฉ.
''' Map plotting '''
fig, ax = plt.subplots(figsize=(10, 8))
m = Basemap(llcrnrlon=126, # Lower-left corner longitude
llcrnrlat=33, # Lower-left corner latitude
urcrnrlon=142, # Upper-right corner longitude
urcrnrlat=48, # Upper-right corner latitude
lon_0=134, # Central longitude
lat_0=40, # Central latitude
projection="merc", # Mercator projection
resolution="h", # High resolution
ax=ax) # Specify the axes object
# Fill continents and draw coastlines
m.fillcontinents(color="#D3D3D3", lake_color="lightblue")
m.drawcoastlines()
- Figure ์ ์ฒด๊ทธ๋ฆผ, Axes ํ๋กฏ์ ์ถ = plt.subplots(figsize=(๊ฐ๋ก,์ธ๋ก))
- Basemap ( ์ผ์๋๊ฒฝ๋, ์ผ์๋์๋, ์ค๋ฅธ์๊ฒฝ๋, ์ค๋ฅธ์์๋, ์ค์ฌ๊ฒฝ๋, ์ค์ฌ์๋, ํฌ์๋ฒ, ํด์๋, ์ถ )
- .fillcontinents()
- .drawcoastlines()
''' Calculate velocity of sea '''
# np.hypot (a, b) Calculate between a and b.
# Calculate the horizontal speed using u and v components.
speeds = np.hypot(data_frame.u_velocity, data_frame.v_velocity)
# Plot arrows to represent velocity
X, Y = m(data_frame.lon, data_frame.lat)
quiver = m.quiver(X, Y, data_frame.u_velocity, data_frame.v_velocity, speeds, cmap="jet")
- np.hypot (a, b) :
a, b ๋ ์ ์ฌ์ด ๊ฑฐ๋ฆฌ ๊ณ์ฐ
ํด์์ ๋์ u ๋ฐฉํฅ, ๋จ๋ถ v ๋ฐฉํฅ ์๋๋ฅผ ์ด์ฉํด ์ํ ์๋๋ฅผ ๊ตฌํจ. - m.quiver ( ์์์ x, ์์์ y,
๋์๋ฐฉํฅ์๋(x์ถ์๋)u,
๋จ๋ถ๋ฐฉํฅ์๋(y์ถ์๋)v,
speed (๋ฒกํฐ์ ํฌ๊ธฐ๋ ๊ฐ๋),
cmap (์ปฌ๋ฌ๋งต ์ง์ ์ธ์)
''' Add remaining elements '''
# Add color bar
cbar = plt.colorbar(quiver,
ax=ax,
orientation='horizontal',
shrink=0.4,
aspect=12,
pad=0.04)
# Draw meridians and parallels
m.drawmeridians(np.arange(126, 142, 3), labels=[0, 0, 0, 1]) # Meridians with label on top
m.drawparallels(np.arange(33, 49, 3), labels=[1, 0, 0, 0]) # Parallels with label on left
# Set title and show the plot
plt.title("Ocean Current Vector Map using Quiver", fontsize=14)
plt.show()
- plt.colorbar ( ๊ฐ์ฒด, ์ถ, orientation ๋ฐฉํฅ(horizontal / vertical),
shrink(๋ง๋ ํฌ๊ธฐ), aspect(์ข ํก๋น. 10-20), pad(๋ง๋์ ์ถ ๊ฐ๊ฒฉ)) - ๊ฒฝ์ .drawmeridians(๋ฒ์, ๋ ์ด๋ธ[์๋จ, ์ฐ์ธก, ํ๋จ, ์ข์ธก])
์์ .drawparallels(๋ฒ์,๋ ์ด๋ธ[์๋จ, ์ฐ์ธก, ํ๋จ, ์ข์ธก])
์ธ๋ค์ผ ์ด๋ฏธ์ง ์ฐธ๊ณ ๋ฐ NetCDF ์ฐธ๊ณ ์๋ฃ ! (ํ ๋ฒ์ ์ฝ์ด๋ณด๋ฉด ์ข๊ฒ ๋ค)
'Data Engineering ์ฌ๋ฐ๋ฐ > Ocean Meteorological Data' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํด์๊ธฐ์์ ์ํ ํ์ด์ฌ ๊ธฐ๋ณธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ (4) | 2024.12.01 |
---|