Python解析SWAN气象雷达数据--(解析、生成ASCII、Image、netCDF)
from datetime import *
import time
import calendar
import json
import numpy as np
from struct import *
import binascii
import netCDF4 file = open(r"D:/radarDataTest/Z_QPF_20140831203600.F030.bin", "rb")
data = file.read();
print(len(data))
file.close()
#
file = open(r"D:/radarDataTest/Z_QPF_20140831203600.F030.bin", "rb")
length = 0 zonName,dataName,flag,version, = unpack("12s38s8s8s", file.read(12+38+8+8))
zonName = zonName.decode("gbk").rstrip('\x00')
dataName = dataName.decode("gbk").rstrip('\x00')
flag = flag.decode("gbk").rstrip('\x00')
version = version.decode("gbk").rstrip('\x00')
length = length + 12+38+8+8
#
print(zonName)
print("数据说明: " + dataName)
print("文件标志: " + flag)
print("数据版本号: " + version) #
year,month,day,hour,minute,interval, = unpack("HHHHHH", file.read(2+2+2+2+2+2))
print("时间: "+str(year)+"-"+str(month)+"-"+str(day)+" "+str(hour)+":"+str(minute))
print("时段长: "+str(interval))
length = length + 2+2+2+2+2+2 #
XNumGrids,YNumGrids,ZNumGrids, = unpack("HHH", file.read(2+2+2))
print("X: " + str(XNumGrids)+" Y: "+str(YNumGrids)+" Z:"+str(ZNumGrids))
length = length + 2+2+2 #
RadarCount, = unpack("i", file.read(4))
print("拼图雷达数: " + str(RadarCount))
length = length + 4 #
StartLon,StartLat,CenterLon,CenterLat,XReso,YReso, = unpack("ffffff", file.read(4+4+4+4+4+4))
print("开始经度: "+str(StartLon)+" 开始纬度:"+str(StartLat)+" 中心经度:"+str(CenterLon)+" 中心纬度:"+str(CenterLat))
print("经度方向分辨率:"+str(XReso)+" 纬度方向分辨率:"+str(YReso))
length = length + 4+4+4+4+4+4 ZhighGrids=[]
for i in range(0, 40):
ZhighGrid, = unpack("f", file.read(4))
ZhighGrids.append(ZhighGrid)
print("垂直方向的高度:"+str(ZhighGrids))
length = length + 40*4 #
RadarStationNames=[]
for i in range(0, 20):
RadarStationName, = unpack("16s", file.read(16))
RadarStationName = RadarStationName.decode("gbk")
RadarStationNames.append(RadarStationName.rstrip('\x00'))
print("相关站点名称:"+str(RadarStationNames))
length = length + 20*16 #
RadarLongitudes=[]
for i in range(0, 20):
RadarLongitude, = unpack("f", file.read(4))
RadarLongitudes.append(RadarLongitude)
print("相关站点所在经度:"+str(RadarLongitudes))
length = length + 20*4 #
RadarLatitudes=[]
for i in range(0, 20):
RadarLatitude, = unpack("f", file.read(4))
RadarLatitudes.append(RadarLatitude)
print("相关站点所在纬度:"+str(RadarLatitudes))
length = length + 20*4 #
RadarAltitudes=[]
for i in range(0, 20):
RadarAltitude, = unpack("f", file.read(4))
RadarAltitudes.append(RadarAltitude)
print("相关站点所在海拔高度:"+str(RadarAltitudes))
length = length + 20*4 #
MosaicFlags=[]
for i in range(0, 20):
MosaicFlag, = unpack("B", file.read(1))
MosaicFlags.append(MosaicFlag)
print("该相关站点数据是否包含在本次拼图中:"+str(MosaicFlags))
length = length + 20*1 #
m_iDataType, = unpack("h", file.read(2))
print("数据类型定义:"+str(m_iDataType))
if m_iDataType==0:
print("unsigned char")
elif m_iDataType==1:
print("char")
elif m_iDataType==2:
print("unsigned short")
elif m_iDataType==3:
print("short")
elif m_iDataType==4:
print("unsigned short")
length = length + 2 #
m_iLevelDimension, = unpack("h", file.read(2))
print("每一层的向量数:"+str(m_iLevelDimension))
length = length + 2 #
Reserveds=[]
Reserveds, = unpack("168s", file.read(168))
Reserveds = Reserveds.decode("gbk").rstrip('\x00')
print("该相关站点数据是否包含在本次拼图中: "+Reserveds)
length = length + 168 #打印数据
valueZYX = []
for i in range(0, ZNumGrids):
valueYX = []
for j in range(0, YNumGrids):
valueX = []
for k in range(0, XNumGrids):
value, = unpack("h", file.read(2))
#value, = unpack("b", file.read(1))
'''
if value > 0:
print(value)
'''
valueX.append(value)
valueYX.append(valueX)
valueZYX.append(valueYX)
#
#print("数据:"+str(valueZYX))
length = length + ZNumGrids*YNumGrids*XNumGrids*2
print(length)
#
print("----------------------------数据----------------------------") file.close()
import time
from struct import * start = time.clock()
file = open(r"D:/radarDataTest/Z_QPF_20140831203600.F030.bin", "rb")
#
zonName,dataName,flag,version, = unpack("12s38s8s8s", file.read(12+38+8+8))
zonName = zonName.decode("gbk").rstrip('\x00')
dataName = dataName.decode("gbk").rstrip('\x00')
flag = flag.decode("gbk").rstrip('\x00')
version = version.decode("gbk").rstrip('\x00') #
print(zonName)
print("数据说明: " + dataName)
print("文件标志: " + flag)
print("数据版本号: " + version)
#
length = 0
length = length + 2+2+2+2+2+2 # 时间说明
file.read(length) XNumGrids,YNumGrids,ZNumGrids, = unpack("HHH", file.read(2+2+2))
print("X: " + str(XNumGrids)+" Y: "+str(YNumGrids)+" Z:"+str(ZNumGrids)) length = 0
length = length + 4 # 拼图雷达数
file.read(length)
#
StartLon,StartLat,CenterLon,CenterLat,XReso,YReso, = unpack("ffffff", file.read(4+4+4+4+4+4))
print("开始经度: "+str(StartLon)+" 开始纬度:"+str(StartLat)+" 中心经度:"+str(CenterLon)+" 中心纬度:"+str(CenterLat))
print("经度方向分辨率:"+str(XReso)+" 纬度方向分辨率:"+str(YReso)) ZhighGrids=[]
for i in range(0, 40):
ZhighGrid, = unpack("f", file.read(4))
ZhighGrids.append(ZhighGrid)
print(" 垂直方向的高度:"+str(ZhighGrids)) #
length = 0
length = length + 20*16 # 相关站点名称
length = length + 20*4 # 相关站点所在经度
length = length + 20*4 # 相关站点所在纬度
length = length + 20*4 # 相关站点所在海拔高度
length = length + 20*1 # 该相关站点数据是否包含在本次拼图中
length = length + 2 # 数据类型定义
length = length + 2 # 每一层的向量数
length = length + 168 # 保留信息
file.read(length) textZYX = []
for i in range(0, ZNumGrids):
textYX = []
for j in range(0, YNumGrids):
textX = []
for k in range(0, XNumGrids):
value, = unpack("h", file.read(2))
textX.append(str(value))
textYX.append(' '.join(textX))
textZYX.append('\n'.join(textYX))
file.close() #
#------------------------------------------------------------------------------- file_object = open('ASCIIData.txt', 'w')
file_object.write("NCOLS " + str(XNumGrids) + "\n")
file_object.write("NROWS " + str(YNumGrids) + "\n")
file_object.write("XLLCENTER " + str(StartLon) + "\n")
file_object.write("YLLCENTER " + str(StartLat - YReso * (YNumGrids - 1)) + "\n") # round(YReso, 3) *
file_object.write("CELLSIZE " + str(XReso) + "\n")
file_object.write("NODATA_VALUE " + str(-9999) + "\n")
#
#
file_object.writelines(textZYX[0])
file_object.close()
end = time.clock()
print("read: %f s" % dateSpanTransfer)
dateSpanTransfer = end - start #-------------------------------------------------------------------------------
import time
from struct import *
from osgeo import gdal, osr
from osgeo.gdalconst import *
import numpy start = time.clock()
file = open(r"D:/radarDataTest/Z_QPF_20140831203600.F030.bin", "rb")
#
zonName,dataName,flag,version, = unpack("12s38s8s8s", file.read(12+38+8+8))
zonName = zonName.decode("gbk").rstrip('\x00')
dataName = dataName.decode("gbk").rstrip('\x00')
flag = flag.decode("gbk").rstrip('\x00')
version = version.decode("gbk").rstrip('\x00') #
print(zonName)
print("数据说明: " + dataName)
print("文件标志: " + flag)
print("数据版本号: " + version)
#
length = 0
length = length + 2+2+2+2+2+2 # 时间说明
file.read(length) XNumGrids,YNumGrids,ZNumGrids, = unpack("HHH", file.read(2+2+2))
print("X: " + str(XNumGrids)+" Y: "+str(YNumGrids)+" Z:"+str(ZNumGrids)) length = 0
length = length + 4 # 拼图雷达数
file.read(length)
#
StartLon,StartLat,CenterLon,CenterLat,XReso,YReso, = unpack("ffffff", file.read(4+4+4+4+4+4))
print("开始经度: "+str(StartLon)+" 开始纬度:"+str(StartLat)+" 中心经度:"+str(CenterLon)+" 中心纬度:"+str(CenterLat))
print("经度方向分辨率:"+str(XReso)+" 纬度方向分辨率:"+str(YReso)) ZhighGrids=[]
for i in range(0, 40):
ZhighGrid, = unpack("f", file.read(4))
ZhighGrids.append(ZhighGrid)
print(" 垂直方向的高度:"+str(ZhighGrids)) #
length = 0
length = length + 20*16 # 相关站点名称
length = length + 20*4 # 相关站点所在经度
length = length + 20*4 # 相关站点所在纬度
length = length + 20*4 # 相关站点所在海拔高度
length = length + 20*1 # 该相关站点数据是否包含在本次拼图中
length = length + 2 # 数据类型定义
length = length + 2 # 每一层的向量数
length = length + 168 # 保留信息
file.read(length) valueZYX = []
for i in range(0, ZNumGrids):
valueYX = []
for j in range(0, YNumGrids):
valueX = []
for k in range(0, XNumGrids):
value, = unpack("h", file.read(2))
valueX.append(value)
valueYX.append(valueX)
valueZYX.append(valueYX)
file.close()
#
#
#------------------------------------------------------------------------------- end = time.clock()
dateSpanTransfer = end - start
print("read: %f s" % dateSpanTransfer)
#
#
driver = gdal.GetDriverByName('HFA')
driver.Register()
dataSetImg = driver.Create( "D:/radarDataTest/edarsImage.img", XNumGrids, YNumGrids, 1, gdal.GDT_Float32 )
#
dataSetImg.SetGeoTransform( [ StartLon, XReso, 0, StartLat, 0, -YReso ] )
#
srs = osr.SpatialReference()
srs.SetWellKnownGeogCS( 'WGS84' )
dataSetImg.SetProjection( srs.ExportToWkt() )
#
value2D = numpy.matrix( valueYX, dtype=numpy.float32 )
dataSetImg.GetRasterBand(1).WriteArray( value2D )
#
dataSetImg = None #datasource.Destroy()
#-------------------------------------------------------------------------------
from datetime import *
import time
import calendar
import json
import numpy as np
from struct import *
import binascii
import numpy
from numpy.random import uniform
from netCDF4 import Dataset start = time.clock()
file = open(r"D:/radarDataTest/Z_QPF_20140831203600.F030.bin", "rb")
#
zonName,dataName,flag,version, = unpack("12s38s8s8s", file.read(12+38+8+8))
zonName = zonName.decode("gbk").rstrip('\x00')
dataName = dataName.decode("gbk").rstrip('\x00')
flag = flag.decode("gbk").rstrip('\x00')
version = version.decode("gbk").rstrip('\x00') #
print(zonName)
print("数据说明: " + dataName)
print("文件标志: " + flag)
print("数据版本号: " + version)
#
length = 0
length = length + 2+2+2+2+2+2 # 时间说明
file.read(length) XNumGrids,YNumGrids,ZNumGrids, = unpack("HHH", file.read(2+2+2))
print("X: " + str(XNumGrids)+" Y: "+str(YNumGrids)+" Z:"+str(ZNumGrids)) length = 0
length = length + 4 # 拼图雷达数
file.read(length)
#
StartLon,StartLat,CenterLon,CenterLat,XReso,YReso, = unpack("ffffff", file.read(4+4+4+4+4+4))
print("开始经度: "+str(StartLon)+" 开始纬度:"+str(StartLat)+" 中心经度:"+str(CenterLon)+" 中心纬度:"+str(CenterLat))
print(" 经度方向分辨率:"+str(XReso)+" 纬度方向分辨率:"+str(YReso)) ZhighGrids=[]
for i in range(0, 40):
ZhighGrid, = unpack("f", file.read(4))
ZhighGrids.append(ZhighGrid)
print(" 垂直方向的高度:"+str(ZhighGrids)) #
length = 0
length = length + 20*16 # 相关站点名称
length = length + 20*4 # 相关站点所在经度
length = length + 20*4 # 相关站点所在纬度
length = length + 20*4 # 相关站点所在海拔高度
length = length + 20*1 # 该相关站点数据是否包含在本次拼图中
length = length + 2 # 数据类型定义
length = length + 2 # 每一层的向量数
length = length + 168 # 保留信息
file.read(length) valueZYX = []
for i in range(0, ZNumGrids):
valueYX = []
for j in range(0, YNumGrids):
valueX = []
for k in range(0, XNumGrids):
#value, = unpack("h", file.read(2))
#textX.append(str(value/10.0))
value, = unpack("b", file.read(1))
textX.append(str(value*2+66.0))
valueYX.append(valueX)
valueZYX.append(valueYX)
file.close()
#
valueXYZ = []
for k in range(0, XNumGrids):
for j in range(0, YNumGrids):
for i in range(0, ZNumGrids):
valueXYZ.append(valueZYX[i][j][k]) #
file = open(r"D:/radarDataTest/Z_QPF_20140831203600.F030.bin", "rb")
rootgrp = Dataset("test.nc", "w", format="NETCDF4")
#rootgrp = Dataset("test.nc", "a")
#fcstgrp = rootgrp.createGroup("forecasts") lon = rootgrp.createDimension("lon", XNumGrids)
lat = rootgrp.createDimension("lat", YNumGrids)
alt = rootgrp.createDimension("alt", ZNumGrids) lon = rootgrp.createVariable("lon", "f8", ("lon",))
lat = rootgrp.createVariable("lat", "f8", ("lat",))
alt = rootgrp.createVariable("alt", "f8", ("alt",)) #val = rootgrp.createVariable("val","f4",("zz","yy","xx",))
val = rootgrp.createVariable("val","f4",("lon","lat","alt",)) #
rootgrp.description = dataName
rootgrp.history = "创建时间: " + time.strftime('%Y-%m-%d %X', time.localtime())
rootgrp.Source_Software = "SmartMap"
#
lon.units = "degrees_east"
lon.long_name = "longitude coordinate"
lon.standard_name = "longitude"
#
lat.units = "degrees_north"
lat.long_name = "latitude coordinate"
lat.standard_name = "latitude"
#
alt.units = "m"
alt.long_name = "altitude"
alt.standard_name = "heigh"
#
val.long_name = "value"
val.esri_pe_string = 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'
val.coordinates = "lon lat alt"
val.units = "Degree"
val.missing_value = -9999 #interval = 0.009999999776482582
interval = 0.01
#x = numpy.arange(-90,91,2.5) x = []
for i in range(0, XNumGrids):
x.append(StartLon + i * round(XReso, 3))
#x = numpy.array(x)
lon[:] = x #
#y = numpy.arange(-180,180,2.5)
y = []
for i in range(0, YNumGrids):
y.append(StartLat - i * round(YReso, 3))
#y = numpy.array(y)
lat[:] = y
# z = []
for i in range(0, ZNumGrids):
z.append(ZhighGrids[i])
#z = numpy.array(z)
alt[:] = z
# #kk = uniform(size=(2,3,4,5))
#print(kk) #val[::]=valueZYX
val[::] = valueXYZ #
rootgrp.close()
Python解析SWAN气象雷达数据--(解析、生成ASCII、Image、netCDF)的更多相关文章
- python爬虫的页面数据解析和提取/xpath/bs4/jsonpath/正则(1)
一.数据类型及解析方式 一般来讲对我们而言,需要抓取的是某个网站或者某个应用的内容,提取有用的价值.内容一般分为两部分,非结构化的数据 和 结构化的数据. 非结构化数据:先有数据,再有结构, 结构化数 ...
- python爬虫---爬虫的数据解析的流程和解析数据的几种方式
python爬虫---爬虫的数据解析的流程和解析数据的几种方式 一丶爬虫数据解析 概念:将一整张页面中的局部数据进行提取/解析 作用:用来实现聚焦爬虫的吧 实现方式: 正则 (针对字符串) bs4 x ...
- 数据解析_bs进行数据解析
1.bs4进行数据解析 数据解析的原理 1.标签定位 2.提取标签,标签属性中存储的数据值 bs4数据解析的原理 1.实例化一个BeautifulSoup对象,并且将页面源码数据加载到该对象中 2.通 ...
- Python爬虫之三种数据解析方式
一.引入 二.回顾requests实现数据爬取的流程 指定url 基于requests模块发起请求 获取响应对象中的数据 进行持久化存储 其实,在上述流程中还需要较为重要的一步,就是在持久化存储之前需 ...
- 05 Python网络爬虫的数据解析方式
一.爬虫数据解析的流程 1.指定url 2.基于requests模块发起请求 3.获取响应中的数据 4.数据解析 5.进行持久化存储 二.解析方法 (1)正则解析 (2)bs4解析 (3)xpath解 ...
- Unity3d-XML文件数据解析&JSON数据解析
1.XML文件数据解析:(首先须要导入XMLParser解析器,The latest released download from:http://dev.grumpyferret.com/unity/ ...
- python爬虫的页面数据解析和提取/xpath/bs4/jsonpath/正则(2)
上半部分内容链接 : https://www.cnblogs.com/lowmanisbusy/p/9069330.html 四.json和jsonpath的使用 JSON(JavaScript Ob ...
- 如何使用fastJson来解析JSON格式数据和生成JSON格式数据
由于项目用到了JSON格式的数据,在网上搜索到了阿里的fastjson比较好用,特此记录fastjson用法,以备以后查询之用. decode: 首先创建一个JSON解析类: public class ...
- python+jinja2实现接口数据批量生成工具
在做接口测试的时候,我们经常会遇到一种情况就是要对接口的参数进行各种可能的校验,手动修改很麻烦,尤其是那些接口参数有几十个甚至更多的,有没有一种方法可以批量的对指定参数做生成处理呢. 答案是肯定的! ...
随机推荐
- 如何正确的加载和执行 JavaScript 代码
无论当前 JavaScript 代码是内嵌还是在外链文件中,页面的下载和渲染都必须停下来等待脚本执行完成.JavaScript 执行过程耗时越久,浏览器等待响应用户输入的时间就越长.浏览器在下载和执行 ...
- POJ 2370
//我的解题思路是先把输入的含有n个元素的数组a排序(从小到大),然后对前(n+1)/2个元素作如下的处理, //s+= (a[i]+1)/2 #include <iostream> #i ...
- POJ 1046
#include<iostream> using namespace std; #define MAXN 16 #define inf 100000000 struct node { in ...
- Oracle 数据库维护管理之--数据库基本信息表管理与优化参考1
1.查看当前系统中的会话(如果权限不足,请使用sys或者system用户登录): select * from v$session t; 2.查看此会话下正在执行的sql语句:select sql_te ...
- 剑指offer四十之数组中只出现一次的数字
一.题目 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 二.思路 建一个hashMap,统计各数字出现的次数,然后遍历hashMap,输出出现一次的数字 ...
- 48位MAC转化为唯一的128位IPV6地址
根据EUI_64规范,一个MAC地址生成唯一的一个IPV6地址. ①.反转MAC的第七位为1. ②.在24bit后加入FFFE. ③.在最前面加上FE80::. 示例:
- eclipse中explorer显示方式
不知道是不是上面的描述.做个记录 project explorer 项目资源管理器 这个要打开代码目录需要再点开java resources 还会出现deployment Descriptor项目工程 ...
- C++中虚函数的动态绑定和多态性
目录 静态类型 vs 动态类型.静态绑定 vs 动态绑定 虚函数动态绑定实现机制.虚析构函数 多态性 一.静态 vs 动态 静态类型 VS 动态类型.静态类型指的是对象声明的类型,在编译器确定的.动态 ...
- SQLAlchemy之SQL Expression
SQLAlchemy是一个强大的Python SQL工具箱, 提供了包括ORM在内的各种支持. 首先使用pip安装; pip install SQLAlchemy SQL Expression Lan ...
- node.js 读取文件
一般用法 var path = require("path"); var fs = require("fs"); //let filePath = path.j ...