Python实践应用|NC文件读取

import netCDF4 as nc
import numpy as np
import matplotlib.pyplot as plt

# 打开NC文件
nc_file = 'E:/NC_file/air.sig995.2012.nc'  # 将'your_file.nc'替换为你的NC文件路径
nc_data = nc.Dataset(nc_file, 'r')

# 查看NC文件中包含的变量
print("Variables in the NC file:", nc_data.variables.keys())

# 选择要读取的变量,假设这个变量叫做'temperature'
variable_name = 'air'

# 读取经度、纬度、时间和气温数据
longitude = nc_data.variables['lon'][:]
latitude = nc_data.variables['lat'][:]
time = nc_data.variables['time'][:]
temperature = nc_data.variables[variable_name][:]

# 关闭NC文件
nc_data.close()

# 可视化气温数据
# 假设temperature的维度顺序为(time, latitude, longitude)
# 如果不是这个顺序,请相应调整索引顺序
plt.figure(figsize=(10, 5))
plt.imshow(temperature[0, :, :], cmap='jet', extent=(longitude.min(), longitude.max(), latitude.min(), latitude.max()))
plt.colorbar(label='Temperature (Celsius)')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.title('4xDaily Air temperature at sigma level 995')
plt.show()


# 提取重庆市的经纬度范围
chongqing_lon_min, chongqing_lon_max = 105, 110
chongqing_lat_min, chongqing_lat_max = 28, 32

# 找到重庆市在全局数据中的索引范围
chongqing_lon_indices = np.where((longitude >= chongqing_lon_min) & (longitude <= chongqing_lon_max))[0]
chongqing_lat_indices = np.where((latitude >= chongqing_lat_min) & (latitude <= chongqing_lat_max))[0]

# 提取重庆市的气温数据
chongqing_temperature = temperature[:, chongqing_lat_indices[0]:chongqing_lat_indices[-1]+1,
                          chongqing_lon_indices[0]:chongqing_lon_indices[-1]+1]

# 绘制重庆市的气温时间变化曲线
plt.figure(figsize=(10, 5))
plt.plot(time, chongqing_temperature[:, 0, 0], marker='o', linestyle='-')
plt.xlabel('Time')
plt.ylabel('Temperature (°C)')
plt.title('Temperature Variation in Chongqing')
plt.grid(True)
plt.show()

图1|可视化气温数据

 图2|重庆市气温时间序列变化图

相关推荐

  1. 用Qt+NetCDF 读取NC文件

    2024-04-27 22:08:02       32 阅读
  2. python如何读取文件

    2024-04-27 22:08:02       43 阅读
  3. python读取ply文件

    2024-04-27 22:08:02       39 阅读
  4. python读取文件

    2024-04-27 22:08:02       25 阅读
  5. Python实现精确读取PDF文件的全部内容(8)

    2024-04-27 22:08:02       22 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-27 22:08:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-27 22:08:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-27 22:08:02       82 阅读
  4. Python语言-面向对象

    2024-04-27 22:08:02       91 阅读

热门阅读

  1. QT案例 使用QProcess调用Aria2.exe下载网络资源文件

    2024-04-27 22:08:02       30 阅读
  2. js实现字符串转json对象的四种方法

    2024-04-27 22:08:02       37 阅读
  3. git使用技巧记录

    2024-04-27 22:08:02       30 阅读
  4. git clone 报错 记录

    2024-04-27 22:08:02       34 阅读
  5. Swift字符串

    2024-04-27 22:08:02       33 阅读
  6. Mysql常用语句

    2024-04-27 22:08:02       34 阅读
  7. 总结-要使用哪种数组方法

    2024-04-27 22:08:02       37 阅读
  8. Linux学习(函数)

    2024-04-27 22:08:02       21 阅读
  9. php 获取网页数据

    2024-04-27 22:08:02       31 阅读
  10. Kafka集群和kafka-manager安装

    2024-04-27 22:08:02       36 阅读
  11. 00.Jenkins 基本介绍与安装

    2024-04-27 22:08:02       28 阅读