解析
 
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()
生成ASCII
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 #-------------------------------------------------------------------------------
生成Image(.img)
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()
#-------------------------------------------------------------------------------
生成netCDF
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)的更多相关文章

  1. python爬虫的页面数据解析和提取/xpath/bs4/jsonpath/正则(1)

    一.数据类型及解析方式 一般来讲对我们而言,需要抓取的是某个网站或者某个应用的内容,提取有用的价值.内容一般分为两部分,非结构化的数据 和 结构化的数据. 非结构化数据:先有数据,再有结构, 结构化数 ...

  2. python爬虫---爬虫的数据解析的流程和解析数据的几种方式

    python爬虫---爬虫的数据解析的流程和解析数据的几种方式 一丶爬虫数据解析 概念:将一整张页面中的局部数据进行提取/解析 作用:用来实现聚焦爬虫的吧 实现方式: 正则 (针对字符串) bs4 x ...

  3. 数据解析_bs进行数据解析

    1.bs4进行数据解析 数据解析的原理 1.标签定位 2.提取标签,标签属性中存储的数据值 bs4数据解析的原理 1.实例化一个BeautifulSoup对象,并且将页面源码数据加载到该对象中 2.通 ...

  4. Python爬虫之三种数据解析方式

    一.引入 二.回顾requests实现数据爬取的流程 指定url 基于requests模块发起请求 获取响应对象中的数据 进行持久化存储 其实,在上述流程中还需要较为重要的一步,就是在持久化存储之前需 ...

  5. 05 Python网络爬虫的数据解析方式

    一.爬虫数据解析的流程 1.指定url 2.基于requests模块发起请求 3.获取响应中的数据 4.数据解析 5.进行持久化存储 二.解析方法 (1)正则解析 (2)bs4解析 (3)xpath解 ...

  6. Unity3d-XML文件数据解析&JSON数据解析

    1.XML文件数据解析:(首先须要导入XMLParser解析器,The latest released download from:http://dev.grumpyferret.com/unity/ ...

  7. python爬虫的页面数据解析和提取/xpath/bs4/jsonpath/正则(2)

    上半部分内容链接 : https://www.cnblogs.com/lowmanisbusy/p/9069330.html 四.json和jsonpath的使用 JSON(JavaScript Ob ...

  8. 如何使用fastJson来解析JSON格式数据和生成JSON格式数据

    由于项目用到了JSON格式的数据,在网上搜索到了阿里的fastjson比较好用,特此记录fastjson用法,以备以后查询之用. decode: 首先创建一个JSON解析类: public class ...

  9. python+jinja2实现接口数据批量生成工具

    在做接口测试的时候,我们经常会遇到一种情况就是要对接口的参数进行各种可能的校验,手动修改很麻烦,尤其是那些接口参数有几十个甚至更多的,有没有一种方法可以批量的对指定参数做生成处理呢. 答案是肯定的! ...

随机推荐

  1. 转载,自己留着看eclipse 快捷键

    Eclipse中10个最有用的快捷键组合  一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升.    ...

  2. 【LeetCode】502. IPO

    题目 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最多 ...

  3. linux 从0开始

    网络配置: http://blog.51yip.com/linux/1120.html 网络配置为自动获取 vi命令参考: http://c.biancheng.net/cpp/html/2735.h ...

  4. php如何判断字符串是否是字母和数字的组合

    转载自百度 /其实判断是否是字母和数字或字母数字的组合还可以用PHP ctype_alnum函数 if(!ctype_alnum($vipurl)){ echo '只能是字母或数字的组合';exit; ...

  5. 补间动画Tweened Animations

    本例子简单讲一下怎么用补间动画 1.在xml中定义好动画的资源文件,如下(注意把不同的效果放在一起可以一起用,同时起效果) <?xml version="1.0" encod ...

  6. MediaWIKI部署流程

    1.下载mediawiki,地址:https://www.mediawiki.org/wiki/MediaWiki 2.下载xxamp集成软件,地址:https://www.apachefriends ...

  7. vscode 常用配置

    { "workbench.iconTheme": "vscode-icons", "editor.tabSize": 2, "ed ...

  8. Spark Shell简单使用

    基础 Spark的shell作为一个强大的交互式数据分析工具,提供了一个简单的方式学习API.它可以使用Scala(在Java虚拟机上运行现有的Java库的一个很好方式)或Python.在Spark目 ...

  9. Ruby on Rails中的Rake教程(Rake如何把我灌醉!)

    下面是我们使用Rake任务的例子: 1.给列表中的用户发送邮件 2.每晚数据的计算和报告 3.过期或重新生成缓存 4.备份数据和svn版本(how's this : subversion reposi ...

  10. [PY3]——Queue

    Queue class Queue(builtins.object) __init__(self, maxsize=0) empty(self) full(self) get(self, block= ...