利用Python绘制TEC地图(详尽版)

在进行某些研究时,需要对电离层的日变化进行分析,以便直观清晰地看到电离层的变化,博主利用CDDIS的电离层格网数据,基于Python编写了电离层地图绘制函数

一天TEC效果图

以下是一天之内的电离层变化地图及源码
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

一天TEC源码

# -*- coding: utf-8 -*-
# @Time    : 2022/1/3 14:21
# @Author  : xymeng
# @FileName: 001.py 绘制TEC等值线专用程序
# @Software: PyCharm

import os
import numpy as np
import matplotlib.pyplot as plt


'''
store the lat,lon,and,TEC value
'''
lat = []
lon = []
TEC0 = []
TEC = []
TEC2D = np.zeros(shape=(71,73))
picnum = 1
timenum = 1
folder = r'F:\i-File\2021'

'''
Search for each ionex file
'''

for ifile in os.listdir(folder):
    '''
    num: Record the count of the region
    count: Record the count of the Map
    '''
    num = 1
    count = 1
    timenum = 0
    path = os.path.join(folder,ifile)
    print(path)
    with open(path) as ionex:
        icontent0 = ionex.readlines()
        for i in range(len(icontent0)):
            '''
            Turn list including other symbol into list only consisting of character
            '''
            icontent1 = icontent0[i].split(' ')
            for x in range(icontent1.count('')):
                icontent1.remove('')
            if len(icontent1) >= 5:
                if icontent1[-1] == 'LAT/LON1/LON2/DLON/H\n' and num <= 71 and count <= 13:
                    Lon0 = -180.0
                    Lat0 = 90
                    deltla = 2.5
                    deltlo = 5
                    Lat0 = Lat0 - (deltla * num)
                    lat.append(Lat0)

                    num = num + 1
                    '''
                    Begin putting TEC value into TSC list
                    '''
                    ynum = 0

                    for y in range(1,6):
                        tecvalue1 = icontent0[i+y].split(' ')
                        for z in range(tecvalue1.count('')):
                            tecvalue1.remove('')
                        for lo in range(len(tecvalue1)):
                            if num == 2:
                                lon.append(Lon0)
                            TEC2D[num-2][ynum] = int(tecvalue1[lo])
                            ynum = ynum + 1
                            Lon0 = Lon0 + deltlo
                    if num >= 72:
                        timenum = timenum + 1
                        LON,LAT = np.meshgrid(lon,lat)
                        plt.figure()
                        plt.contourf(LON, LAT, TEC2D, 8, cmap='plasma')
                        C = plt.contour(LON, LAT, TEC2D,  8, cmap='plasma')
                        # 添加标记,标记处不显示轮廓线,颜色为黑色,保留两位小数
                        plt.clabel(C, inline=True, colors='k', fmt='%1.2f')
                        path = 'F:/contour-outcome'+'/'+str(picnum)+'/'
                        if not os.path.exists(path):  # 如果不存在路径,则创建这个路径
                            os.makedirs(path)
                        print(path)
                        plt.savefig(path+'/'+str(timenum)+'.jpg')
                        plt.close()
                        num = 1
                        count = count + 1
                        lon.clear()
                        lat.clear()
                        TEC = list(TEC)
                        TEC.clear()
                        TEC2D = np.zeros(shape=(71,73))


    picnum = picnum + 1
    lon.clear()
    lat.clear()
    TEC.clear()
    TEC2D = np.zeros(shape=(71,73))


绘制指定区域电离层地图效果图

在这里插入图片描述

绘制指定区域的源码

# -*- coding: utf-8 -*-
# @Time    : 2022/1/20 10:48
# @Author  : xymeng
# @FileName: 001-specify.py 用于获取指定区域TEC图的程序
# @Software: PyCharm

import os
import numpy as np
import matplotlib.pyplot as plt
'''
绘图区域在指定经纬度5°的范围
'''
span = 5
Lat = input('请输入纬度:'.format())
Lon = input('请输入经度:'.format())
Latdown = int(Lat) - span
Londown = int(Lon) - span
Latup = int(Lat) + span
Lonup = int(Lon) + span
counLat = int(2 * (span/2.5))
counLon = int(2 * (span/5))
numlat = int((90-int(Lat))/2.5) + 2
print(counLat)
print(counLon)
print(numlat)
'''
store the lat,lon,and,TEC value
'''
lat = []
lon = []
TEC0 = []
TEC = []
TEC2D = np.zeros(shape=(counLat,counLon))
picnum = 1
timenum = 1
num1 = 0
folder = r'F:\i-File\2021'
'''
定义初始经纬度
'''
Lon0 = -180.0
Lat0 = 90
'''
Search for each ionex file
'''

for ifile in os.listdir(folder):
    '''
    num: Record the count of the region
    count: Record the count of the Map
    '''
    num = 1
    count = 1
    timenum = 0
    path = os.path.join(folder,ifile)
    print(path)
    with open(path) as ionex:
        icontent0 = ionex.readlines()
        for i in range(len(icontent0)):

            '''
            Turn list including other symbol into list only consisting of character
            '''
            icontent1 = icontent0[i].split(' ')
            for x in range(icontent1.count('')):
                icontent1.remove('')
            if len(icontent1) >= 5:
                if icontent1[-1] == 'LAT/LON1/LON2/DLON/H\n' and num <= counLat and count <= 13:
                    deltla = 2.5
                    deltlo = 5
                    num1 = num1 + 1
                    lat0 = Lat0 - (deltla * num1)
                    print(num1)
                    print(lat0)
                    if num1 <= numlat:
                        if lat0 >= Latdown and  lat0 <= Latup:
                            lat.append(lat0)
                            num = num + 1
                            '''
                            Begin putting TEC value into TEC list
                            '''
                            ynum = 0
                            for y in range(1,6):
                                print('xxx')
                                tecvalue1 = icontent0[i+y].split(' ')
                                for z in range(tecvalue1.count('')):
                                    tecvalue1.remove('')
                                for lo in range(len(tecvalue1)):
                                    if (Lon0 >= Londown) and (Lon0 <= Lonup):
                                        print(tecvalue1[lo])
                                        lon.append(Lon0)
                                        TEC2D[num-2][ynum] = int(tecvalue1[lo])
                                        ynum = ynum + 1
                                    Lon0 = Lon0 + deltlo
                            Lon0 = -180
                    elif (num1 > numlat) and num1 <= (71- numlat):
                        pass
                    if num >= (counLat+1):
                        if num1 <= numlat:
                            timenum = timenum + 1
                            lonfin = list(set(lon))
                            lonfin.sort(key=lon.index)
                            print(lonfin)
                            print(lat)
                            LON,LAT = np.meshgrid(lonfin,lat)
                            plt.figure()
                            plt.contourf(LON, LAT, TEC2D, 8, cmap='plasma')
                            C = plt.contour(LON, LAT, TEC2D,  8, cmap='plasma')
                            # 添加标记,标记处不显示轮廓线,颜色为黑色,保留两位小数
                            plt.clabel(C, inline=True, colors='k', fmt='%1.2f')
                            path = 'F:/contour-outcome-region'+'/'+str(picnum)+'/'
                            if not os.path.exists(path):  # 如果不存在路径,则创建这个路径
                                os.makedirs(path)
                            plt.savefig(path+'/'+str(timenum)+'.jpg')
                            plt.close()
                        elif (num1 > numlat) and num1 <= 71:
                            print('elif')
                            pass
                        else:
                            print('else')
                            num = 1
                            num1 = 0
                            count = count + 1
                            lon.clear()
                            lat.clear()
                            TEC = list(TEC)
                            TEC.clear()
                            TEC2D = np.zeros(shape=(counLat,counLon))
                            Lon0 = -180.0
                            Lat0 = 90

    picnum = picnum + 1
    lon.clear()
    lat.clear()
    TEC.clear()
    TEC2D = np.zeros(shape=(counLat,counLon))
    Lon0 = -180.0
    Lat0 = 90

  • 6
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
Python可以使用多个库来绘制城市地图,其中最常用的包括matplotlib和folium。下面以这两个库为例,介绍如何使用Python绘制城市地图。 1. 使用matplotlib绘制城市地图: 首先,需要准备好地图的数据,一般为地理坐标(经纬度)和对应的数值,例如城市的人口密度数据。 导入matplotlib包,使用Basemap类来创建地图对象,并设置地图的中心点和缩放级别。 然后,通过调用Basemap对象的plot()方法来绘制地图,可以传入经纬度坐标和对应的数值,用不同的颜色或大小来表示不同的数值。 最后,使用show()方法来显示地图。 以下是使用matplotlib绘制城市地图的简单示例代码: ``` python import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap # 创建地图对象 map = Basemap(llcrnrlon=115, llcrnrlat=30, urcrnrlon=120, urcrnrlat=35, resolution='i') # 绘制海岸线 map.drawcoastlines() # 绘制国界线 map.drawcountries() # 绘制省界线 map.drawstates() # 绘制城市坐标点 x, y = map(116.4, 39.9) # 北京的经纬度 map.plot(x, y, 'ro', markersize=5) # 以红色圆点表示 # 显示地图 plt.show() ``` 2. 使用folium绘制城市地图: folium是基于leaflet.js的Python库,可以生成互动式的地图,具有缩放、标记和弹出式信息等功能。 首先,导入folium包,并创建一个Map对象,设置地图的中心点和缩放级别。 然后,通过调用Map对象的方法来添加地图的标记、弹出式信息等。 最后,使用save()方法将地图保存为html文件。 以下是使用folium绘制城市地图的简单示例代码: ``` python import folium # 创建地图对象 map = folium.Map(location=[39.9, 116.4], zoom_start=12) # 添加标记和弹出式信息 folium.Marker(location=[39.9, 116.4], popup='北京').add_to(map) # 保存地图为html文件 map.save('map.html') ``` 通过这些方法,可以利用Python绘制城市地图,进一步进行数据可视化和分析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十八与她

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值