解析
 
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. C#中ExecuteReader、ExecuteNonQuery、ExecuteScalar、SqlDataReader、SqlDataAdapter的区别

    ExecuteNonQuery()执行命令对象的SQL语句,返回一个int 类型的变量,返回数据库操作之后影响的行数.适合用来验证对数据库进行增删改的情况. 2.ExecuteScalar()也可以执 ...

  2. JavaScript函数理解

    本文参考自简书javaScript之函数详解 这里从函数的构造函数开始. 在js中,函数都是对象,它们都是Function构造函数的实例.因此,类似Java中的对象,函数名可以理解为指向该Functi ...

  3. Python3之hashlib

    简介: 用于加密相关的操作,代替了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法. 在python3中已经废弃了md5和sha模块,简单说 ...

  4. Linux文件目录管理

    Linux文件目录管理 文件的路径 路径: . 表示当此层目录 .. 表示上一层目录 - 代表前一个工作目录 ~ 代表"目前用户身份"所在的文件夹 ~account 代表accou ...

  5. Vue-router的基本使用

    Vue-router的基本使用 相关Html: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  6. (win 7)使用puma以后,重启rails server报错: in `trap': unsupported signal SIGCHLD (ArgumentError)

    如图: 解决方案: 把config/puma.rb 文件中的 workers Integer(ENV['WEB_CONCURRENCY'] || 2) 改成 workers Integer(ENV[' ...

  7. CGI PL PERL脚本 提权

    windows 2003 下,安装ActivePerl-5.16.2.1602-MSWin32-x86-296513 IIS 添加CGI支持.并在对应网站配置下面,添加CGI后缀或PL后缀 与 对应的 ...

  8. Android_Activity启动模式

    在android里,有4种activity的启动模式,分别为: “standard” (默认) “singleTop” “singleTask” “singleInstance” 它们主要有如下不同: ...

  9. Eclipse *版本(图文详解)

    不多说,直接上干货! 关于Eclipse的版本介绍 Eclipse Standard 该版本是eclipse最基础的版本,适合Java se个人开发者.或希望根据自己需求配置插件的开发者使用. Ecl ...

  10. 深入JVM学习心得

    前言 相信很多人和我一样长期使用java编程,却很少关注过JVM底层实现,这很大程度上是因为JVM设计的很精巧,因此平时项目也很少遇到涉及JVM的问题.但是一方面出于对java底层技术的好奇,另一方面 ...