第一次写博客,请见谅。

参加学校的预赛,做的是2020年美赛A题。解题思路可查“2020年美赛A题总结”,里面有具体的解法。我就不借花谢佛了。下面关于SST数据的读取和绘制虽然繁琐了点,但确实是可成的,之后我将优化他们。

1、SST数据(ERsst.mnmean.nc)来源于北京大学地理数据平台

2、通过matlab读取某一个月的SST数据

我当时下载时,下载下来的数据是从1854年的1月到2018年5月。

在nc文件中,经度 lon从上到下依次为0,2,4,6,8,...,358
纬度lat依次为88,86,84,...,-88
在matlab中,Data(lon,lat,month)索引分别为:
lon:1-180;lat:1-89;month:1-1973(根据SST的最终月份决定)
Data=ncread('ERsst.mnmean.nc','sst'); %Read temperature data SST
g=double(Data(:,:,1958)); 
h=g(:,89:-1:1);

3、通过python处理数据画图

将数据保存进excel中,从第二行开始粘贴!!!

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# Read temperature data (Excel)
df = pd.read_excel("data.xlsx")
height, width = df.shape
print(height, width, type(df))

# The temperature in all coordinates is set to 0 first
temperature = np.zeros((180, 89))

# Read temperature data into
for i in range(height - 1):
    for j in range(width - 1):
        """
        If the SST data in a certain latitude and longitude is very small, 
        it means that this place is land. The purpose of converting this 
        temperature to 0 degrees is to simplify the graph.
        """
        if df.iloc[i, j] < -1000:
            temperature[i, j] = 0
        else:
            temperature[i, j] = df.iloc[i, j]
            
# Draw the coordinates of XY and do it through np.meshgrid()
X, Y = np.meshgrid(np.linspace(-88, 88, 89), np.linspace(0, 180 * 2 - 2, 180))
# X/Y/temperature.shape:(180,89)
# Draw
figure = plt.figure()
axis = figure.add_subplot(1, 1, 1)
color = axis .pcolormesh(Y, X, temperature, cmap='Spectral_r')
x = fig.colorbar(color)
x.set_label('Temperature')
plt.show()

注意:

  1. df.iloc[0,0]是excel表的A2,所以excel的索引最大为df.iloc[height-1,width-1]
  2. SST数据指的是海洋温度,如果为大陆,SST数据会非常小-9.96920996838687E+36,所以必须去掉这些点。

最终成图为:
2017年2月的SST

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐