qq音速道具删除道具网页缺少对象jquery-ui-new.js导致无法删除道具

jquery ui(2)draggable,droppable_jquery总结3_Netcdf (2)__脚本百事通
稍等,加载中……
^_^请注意,有可能下面的2篇文章才是您想要的内容:
jquery ui(2)draggable,droppable
jquery总结3
Netcdf (2)
jquery ui(2)draggable,droppable
jquery ui(二)draggable,droppable
刚接触的时候,分不清draggable和droppable的区别,瞎弄了一会,其实很简单,draggable就是“拖”的功能,droppable就是“放”的功能。
一、先上一个简单例子
&script src="/jquery-1.9.1.js"&&/script&&script src="/ui/1.10.2/jquery-ui.js"&&/script&&div id="draggable" class="ui-widget-content"&
&p&Drag me to my target--&/p&&/div&&div id="droppable" class="ui-widget-header"&
&p&Drop here
-- &/p&&/div&
$(function() {
// 初始化#draggable 可以被拖动
$( "#draggable" ).draggable();
// 初始化,有东西拖到#droppable时,弹出alert窗口
$( "#droppable" ).droppable({
drop: function( event, ui ) {
alert("has drop!--");
拖动 #draggable之前的截图
把#draggable 丢到 #droppable 之后的截图
二、具体用法
1、需要加载的JS ,jquery 需要在jquery-ui之前
&script src="/jquery-1.9.1.js"&&/script&&script src="/ui/1.10.2/jquery-ui.js"&&/script&
2、页面上的HTML
(1)拖动的element
//可以是任何html元素,一个图片,一个div,或者一个 A 标签
&div id="draggable" class="ui-widget-content"&
&p& 这是一个可以拖动的元素--&/p&
(2) 如果需要指定放下到哪里,则需要写一个接受的元素
//下面是一个DIV
&div id="droppable" class="ui-widget-header"&
&p&可以拖动到这里来
3、主要的JS代码
(1)初始化draggable(可拖动)
draggable()函数有许多参数和用法,详见二、4
// 初始化#draggable 可以被拖动
$( "#draggable" ).draggable({
//这里是一些参数
(2)初始化#droppable,当有东西丢下时,执行
droppable()函数有许多参数和用法(详见二、5)
$( "#droppable" ).droppable({
drop: function( event, ui ) {
alert("has drop!--");
4、draggable()函数的其他参数
(1)回调函数
有start, stop, drag等事件,这些函数都接受两个参数:event和ui。start: 拖动开始, 指鼠标按下, 开始移动.drag: 拖动过程中鼠标移动.stop: 拖动结束.
//初始化时设置事件.
$('.selector').draggable({
start: function(event, ui){ alert(this); },
drag: function(event, ui) { alert(this); },
stop: function(event, ui) { alert(this); }
(2)常见参数
addClasses: [类型]Boolean(布尔值) [默认值]true是否给draggable元素增加 ui-draggable这个css的classaxis: [类型]String [支持] ‘x’, ‘y’, false控制元素 只能沿 X轴|Y轴 移动containment:[类型]选择器, 元素, 字符串, 数组只能在选择器约束的元素内拖动delay:[类型]整数, 单位是毫秒可拖动控件从鼠标左键按下开始, 到拖动效果产生的延时还有:distance,distance,handle,helper,opacity (详见 四、)
$('.selector').draggable({
addClasses: true,
axis: 'x',
containment: 'parent',
//parent: 只能在父容器内拖动
delay: 500,
opacity: 0.35,
//被拖到时的不透明度
5、 droppable()函数的参数
activate:在允许的draggable对象开始拖动时触发.deactivate:在允许的draggable对象停止拖动时触发.over:在允许的draggable对象”经过”这个droppable对象时触发out:在允许的draggable对象离开 这个droppable对象时触发drop:在允许的draggable对象填充进这个droppable对象时触发.
$('.selector').droppable({
activate: function(event, ui) { ... },
deactivate: function(event, ui) { ... },
over: function(event, ui) { ... },
out: function(event, ui) { ... },
drop: function(event, ui) { ... }
(2)常见参数
accept :[类型]Selector, Function [默认值]‘*’允许被放下来的元素.hoverClass :[类型]String [默认值]false一个被允许的draggable对象悬停在droppable对象上时添加的class
还有:activeClass,greedy,scope,tolerance (详见 五、)
$('.selector').droppable({
accept: '#someid',
hoverClass: 'drophover'
三、具体用法文章
1、jquery ui(一)简介2、jquery ui(二)拖拽 draggable和droppable3、jquery ui(三)弹出窗口 dialog4、jquery ui(四)进度条 progressbar5、jquery ui(五)日期选择器 datepicker
四、drappable的超级详细参数
1、回调函数
有start, stop, drag等事件,这些函数都接受两个参数:event和ui。event: 浏览器原生的事件 ; ui: 一个JQuery的ui对象。其中ui 有以下属性:a) ui.helper: 正在拖动的元素的JQuery包装对象, ui.helper.context可以获取到原生的DOM元素.b) ui.position: ui.helper(也就是我们要拖动的元素)相对于父元素(包含自己的元素, 如果是顶层, 对应body)的偏移, 值是一个对象{top, left}—-也就是可以用ui.position.top获取到该元素与父元素的top当前偏移c) ui.offset: 与ui.position同意, 这里表示的是和浏览器内容区域左上边界的偏移(注意, 是内容区域, 而不是html的body区域. html的body在默认情况下, 各种浏览器中都会相对offset有偏移的.)(1) start: 拖动开始, 指鼠标按下, 开始移动.(2) drag: 拖动过程中鼠标移动.(3) stop: 拖动结束.[代码示例]初始化时设置事件.$(‘.selector’).draggable({start: function(event, ui){ alert(this); },drag: function(event, ui) { alert(this); },stop: function(event, ui) { alert(this); }});
2、常用参数(选项)
(1)addClasses
[类型]Boolean(布尔值)[默认值]true[产生影响]用来设置是否给draggable元素通过ui-draggable样式才装饰它. 主要为了在通过.draggable()初始化很多(成百个)元素的时候优化性能考虑, 但是, 这个选项的设置, 不会影响ui-draggable-dragging样式改变拖动过程样式.true表示ui-draggable样式被添加到该元素.false表示ui-draggable样式不被添加到该元素.[代码示例]draggable其他选项的初始化,$(‘.selector’).draggable({ addClasses: false });将.selector选择器选中的元素渲染成为一个可拖动控件, 不为其添加ui-draggable样式
(2)appendTo:
[类型]Element, Selector(HTML元素对象或选择器)[默认值]‘parent’ 父元素[产生影响]用来指定控件在拖动过程中ui.helper的容器, 默认情况下, ui.helper使用和初始定义的draggable相同的容器, 也就是其父元素.[代码示例]$(‘.selector’).draggable({ appendTo: ‘body’ });
[类型]String, Boolean(可取的值有’x', ‘y’, false)‘x’: 只能水平拖动‘y’: 只能垂直拖动false: 既可以水平, 也可以垂直拖动.[默认值]false, 不限制拖动的方向[产生影响]约束拖动过程中的取向.[代码示例]$(‘.selector’).draggable({ axis: ‘x’ });
(4)containment:
[类型]选择器, 元素, 字符串, 数组.选择器: 只能在选择器约束的元素内拖动元素: 只能在给定的元素内拖动字符串:parent: 只能在父容器内拖动document: 在当前html文档的document下可拖动, 超出浏览器窗口范围时, 自动出现滚动条widow: 只能在当前浏览器窗口的内容区域拖动, 拖动超出当前窗口范围, 不会导致出现滚动条…数组: [x1, y1, x2, y2]以[开始水平坐标, 开始垂直坐标, 结束水平坐标, 结束垂直坐标]的方式划定一个区域, 只能在此区域内拖动. 这种方式指定时, 值是相对当前浏览器窗口内容区域左上角的偏移值.false: 不限制拖动的活动范围[默认值]false[产生影响]影响指定可拖动控件的活动区域.[代码示例]$(‘.selector’).draggable({ containment: ‘parent’ });$(‘.selector’).draggable({ containment: ‘#demo-frame’ });
(5)cursor:
[类型]字符串.[默认值]‘auto’[产生影响]影响指定可拖动控件在拖动过程中的鼠标样式, 该样式设定之后, 需要控件的原始元素支持指定的cursor样式, 如果指定的值原始元素不支持, 则使用原始元素默认的cursor样式.[代码示例]$(‘.selector’).draggable({ cursor: ‘crosshair’ });
(6)cursorAt:
[类型]对象通过设置对象的top, left, right, bottom的属性值中的一个或两个来确定位置.[默认值]false[产生影响]在拖动控件的过程中, 鼠标在控件上显示的位置, 值为false(默认)时, 从哪里点击开始拖动, 鼠标位置就在哪里, 如果设置了, 就会在一个相对控件自身左上角偏移位置处, 比如: .[代码示例]$(‘.selector’).draggable(‘option’, ‘cursorAt’, {left: 8, top: 8});那么拖动过程中, 鼠标就会在自身的左上角向下向右各偏移8像素处
[类型]整数, 单位是毫秒[默认值]0[产生影响]可拖动控件从鼠标左键按下开始, 到拖动效果产生的延时. 该选项可以被用来阻止一些不期望的点击带来的无效拖动. 具体效果是: 一次拖动, 从鼠标左键按下, 到delay指定的时间, 如果鼠标左键还没有松开, 那么就认为这次拖动有效, 否则, 认为这次拖动无效.[代码示例]$(‘.selector’).draggable({ delay: 500 });
(8)distance:
[类型]整数, 单位是像素[默认值]1[产生影响]可拖动控件从鼠标左键按下开始, 到拖动效果产生的时鼠标必须产生的位移. 该选项可以被用来阻止一些不期望的点击带来的无效拖动. 具体效果是: 一次拖动, 从鼠标左键按下, 只有当鼠标产生的位移达到distance指定的值时, 才认为是有效的拖动.[代码示例]$(‘.selector’).draggable({ distance: 30 });
[类型]数组[x, y], x代表水平大小, y代表垂直大小, 单位是像素[默认值]false[产生影响]可拖动控件拖动时采用grid的方式拖动, 也就是说拖动过程中的单位是guid选项指定的数组描述的格子那么大.[代码示例]$(‘.selector’).draggable({ grid: [50, 20] });
(10)handle:
[类型]选择器或元素[默认值]false[产生影响]指定触发拖动的元素. 用法: 将一个id=window的div设置为可拖动控件, 设置它的handle是该div中的一个id=title的span, 那么, 就只有在id=title的span上点击拖动才是有效的. 注意: 该元素一定要是可拖动控件的子元素.[代码示例]$(‘.selector’).draggable({ handle: ‘h2′ });
(11)helper:
[类型]字符串或函数字符串取值:‘original’: 可拖动控件本身移动‘clone’: 将可拖动控件自身克隆一个移动, 自身在原始位置不变函数则必须返回一个DOM元素: 以函数返回的DOM元素移动展现拖动的过程.[默认值]‘original’[产生影响]拖动过程中帮助用户知道当前拖动位置的元素.[代码示例]$(‘.selector’).draggable({ helper: ‘clone’ });helper: function(event){return $( “
(12)opacity:
[类型]浮点数值[默认值]false[产生影响]拖动过程中helper(拖动时跟随鼠标移动的控件)的不透明度.[代码示例]$(‘.selector’).draggable({ opacity: 0.35 });
(13)revert:
[类型]Boolean, 字符串true: 每次拖动停止之后, 元素自动回到原始位置‘invalid’: 除非是一个droppable并且被drop(放)成功了, 才不将元素返回到原始位置.‘valid’: 除invalid之外的其他情况.[默认值]false[产生影响]影响一次拖动之后是否回归到原始位置.[代码示例]$(‘.selector’).draggable({ revert: true });
(14)revertDuration:
[类型]整数[默认值]500[产生影响]revert(回归到原始位置)整个过程需要的时间, 单位是毫秒. 如果设置revert选项设置为false, 则忽略此属性.[代码示例]$(‘.selector’).draggable({ revertDuration: 1000 });
(15)scope:
[类型]字符串[默认值]‘default’[产生影响]在多个draggable和droppable对象的情况下,为了防止混乱,用来和droppable的对象进行分组。只有处在同一个分组里,droppable才会接受。该选项描述一个范围, 和droppable的同名选项结合使用, droppable的accept选项用来设置可以接受的draggable控件, 同时, 可接受的drggable控件受scope选项约束, 必须是同一个scope中的draggable和droppable才可以互相拖放.例如:$(‘#draggable_a’).draggable({scope: ‘a’});$(‘#draggable_b’).draggable({scope: ‘b’});$(‘#droppable_a’).droppable({scope: ‘a’});$(‘#droppable_b’).droppable({scope: ‘b’});droppable控件的accept选项默认是’*', 看起来数draggable_a, draggable_b可以自由的放入到droppable_a和droppable_b中, 但是, 由于scope的约束, draggable_a只能放入到droppable_a, draggable_b只能发乳到droppable_b中.注意: 这个选项就和变量的名称空间的意义类似. 默认值是’default’, 说明如果不指定, 大家都还是有scope的, 名字是default而已.[代码示例]$(‘.selector’).draggable({ scope: ‘tasks’ });
(16)scroll:
[类型]Boolean[默认值]true[产生影响]如果设置为true, 在拖动过程中超出可拖动控件容器的时候, 容器自动增加滚动条[代码示例]$(‘.selector’).draggable({ scroll: false });
(17)scrollSensitivity:
[类型]整数值[默认值]20[产生影响]滚动条的敏感度.下面所属的鼠标指针是指在draggable控件移动过程中, 鼠标所处位置.鼠标指针和与draggable控件所在容器的边距离为多少的时候, 滚动条开始滚动.[代码示例]$(‘.selector’).draggable({ scrollSensitivity: 40 });
(18)scrollSpeed:
<p style="margin-top: 1 margin-bottom: 1 color: #6767
jquery总结3
jquery小结3
1 层次选择器
&div class="clsFraA"&Left&/div&
&div class="clsFraA" id="divMid"&
&span class="clsFraP" id="Span1"&
&span class="clsFraC" id="Span2"&&/span&
&div class="clsFraA"&Right_1&/div&
&div class="clsFraA"&Right_2&/div&
&script type="text/javascript"&
$(function(){ //匹配后代元素
$("#divMid").css("display","block");
$("div span").css("display","block");
$(function(){ //匹配子元素
$("#divMid").css("display","block");
$("div&span").css("display","block");
$(function(){ //匹配divMid后的第一个div
$("#divMid + div").css("display","block");
$("#divMid").next().css("display","block");
$(function(){ //匹配divMid后的所有div元素
$("#divMid ~ div").css("display","block");
$("#divMid").nextAll().css("display","block");
$(function(){ //匹配divMid的所有相邻的div
$("#divMid").siblings("div").css("display","block");
&/script&2 可见性过滤选择器
&span style="display:none"&Hidden&/span&
&div&Visible&/div&
$(function(){ //增加所有可见元素类别
$("span:hidden").show();
$("div:visible").addClass("GetFocus");
$(function(){ //增加所有不可见元素类别
$("span:hidden").show().addClass("GetFocus");
})3 属性过滤选择器
&div id="divID"&ID&/div&
&div title="A"&Title A&/div&
&div id="divAB" title="AB"&ID Title AB&/div&
&div title="ABC"&Title ABC&/div&
$(function(){ //显示所有含有id属性的元素
$("div[id]").show(3000);
$(function(){ //显示所有属性title的值是"A"的元素
$("div[title='A']").show(3000);
$(function(){ //显示所有属性title的值不是"A"的元素
$("div[title!='A']").show(3000);
$(function(){ //显示所有属性title的值以"A"开始的元素
$("div[title^='A']").show(3000);
$(function(){ //显示所有属性title的值以"C"结束的元素
$("div[title$='C']").show(3000);
$(function(){ //显示所有属性title的值中含有"B"的元素
$("div[title*='B']").show(3000);
$(function(){ //显示所有属性title的值中含有"B"且属性id的值是"divAB"的元素
$("div[id='divAB'][title*='B']").show(3000);
})4 子元素过滤选择器
$(function(){ //增加每个父元素下的第2个子元素类别
$("li:nth-child(2)").addClass("GetFocus");
$(function(){ //增加每个父元素下的第一个子元素类别
$("li:first-child").addClass("GetFocus");
$(function(){ //增加每个父元素下的最后一个子元素类别
$("li:last-child").addClass("GetFocus");
$(function(){ //增加每个父元素下只有一个子元素类别
$("li:only-child").addClass("GetFocus");
})5 设置元素的属性attr,比如:
$("img").attr("title", "hello"); //设置title属性
也可以在attr中设置function,比如;
$("img").attr("src", function() { return "Images/img0" + Math.floor(Math.random() * 2 + 1) + ".jpg" }); //设置src属性
删除元素:$("img").removeAttr("src");6 获得HTML及文本的值
$("#divhidd").html();$("#divhidd").text();7 外部插入结点:after,before,insertAfter,insertBefore等都比较容易,重点看clone()
&script type="text/javascript"&
$(function() {
$("img").click(function() {
$(this).clone(true).appendTo("span");
&span&&img title="封面" src="Images/img04.jpg" /&&/span&
当clone(true)时,将该元素的全部行为也复制,比如这个例子中,可以不断点复制出来的图片,不断的复制,而如果$(this).clone()时,则只能点原来的图片8 wrap和wrapInner
最喜爱的体育运动:&span&羽毛球&/span&
最爱看哪类型图书:&span&网络、技术&/span&
$(function() {
$("p").wrap("&b&&/b&");//所有段落标记字体加粗,在p的标签外面加B
$("span").wrapInner("&i&&/i&");//所有段落中的span标记斜体,在SPAN标签内加I
9 遍历元素
each(callback),callback为一个 function函数,可以接受index序号从0开始,比如 $(function() {
$("img").each(function(index) {
//根据形参index设置元素的title属性
this.title = "第" + index + "幅风景图片,alt内容是" + this.
})10 表格框中,实现全选框的功能
/**全选复选框单击事件**/
$("#chkAll").click(function() {
if (this.checked) {//如果自己被选中
$("table tr td input[type=checkbox]").attr("checked", true);
else {//如果自己没有被选中
$("table tr td input[type=checkbox]").attr("checked", false);
/**删除按钮单击事件**/
$("#btnDel").click(function() {
var intL = $("table tr td input:checked:not('#chkAll')"). //获取除全选复选框外的所有选中项
if (intL != 0) {//如果有选中项
$("table tr td input[type=checkbox]:not('#chkAll')").each(function(index) {//遍历除全选复选框外的行
if (this.checked) {//如果选中
$("table tr[id=" + this.value + "]").remove(); //获取选中的值,并删除该值所在的行
})11 事件冒泡
&script type="text/javascript"&
$(function() {
var intI = 0; //记录执行次数
$("body,div,#btnShow").click(function(event) {//点击事件
intI++; //次数累加
$(".clsShow")
.show()//显示
.html("您好,欢迎来到jQuery世界!")//设置内容
.append("&div&执行次数 &b&" + intI + "&/b& &/div&"); //追加文本
event.stopPropagation();//阻止冒泡过程
&div class="clsShow"&&/div&
当不使用 event.stopPropagation()时,将会出现init=3,触发div,body的相同事件12
bind事件中同时响应两个事件
$(".txt").bind({ focus: function() {
$("#divTip")
.show()//显示
.html("执行的是focus事件");//设置文本
change: function() {
$("#divTip")
.show()//显示
.html("执行的是change事件");//设置文本
})13 toggle
toggle(fn,fn1,fn2,...fn):依次执行fn,fn1,按顺序执行,直到执行到fn14 one事件
one为所选的元素仅绑定一次处理函数。one(type,[data],fn)为每一个匹配元素的特定事件(像click)绑定一个一次性的事件处理函数。在每个对象上,这个事件处理函数只会被执行一次。其他规则与bind()函数相同。这个事件处理函数会接收到一个事件对象,可以通过它来阻止(浏览器)默认的行为。如果既想取消默认的行为,又想阻止事件起泡,这个事件处理函数必须返回false。返回值 : jQuery参数 :type (String) : 事件类型data (Object) : (可选) 作为event.data属性值传递给事件对象的额外数据对象fn (Function) : 绑定到每个匹配元素的事件上面的处理函数
$(function() {
function btn_Click() { //自定义事件
this.value = "010-"
$("#Button1").one("click", btn_Click); //绑定自定义事件
则button1只接受点1次15 trigger()
比如希望页面加载后,自动完成某些自定义事件,可以使用trigger,如:
$(function() {
var oTxt = $("input"); //获取文本框
oTxt.trigger("select"); //自动选中文本框
oTxt.bind("btn_Click", function() {//编写文本框自定义事件
var txt = $(this).val(); //获取自身内容
$("#divTip").html(txt); //显示在页面中
oTxt.trigger("btn_Click"); //自动触发自定义事件
})16 一个网页选项卡的效果
&ul id="menu"&
&li class="tabFocus"&家居&/li&电器
&ul id="content"&
&li class="conFocus"&我是家居的内容&/li&欢迎您来到电器城
二手市场,产品丰富多彩
$(function() {
$("#menu li").each(function(index) { //带参数遍历各个选项卡
$(this).click(function() { //注册每个选卡的单击事件
$("#menu li.tabFocus").removeClass("tabFocus"); //移除已选中的样式
$(this).addClass("tabFocus"); //增加当前选中项的样式
//显示选项卡对应的内容并隐藏未被选中的内容
$("#content li:eq(" + index + ")").show()
.siblings().hide();
Netcdf (2)
Netcdf (二)附件文档:
4 NetCDF Java
4.1 概述(Overview)
参考网址:http://www.unidata.ucar.edu/software/netcdf-java/documentation.htm
The NetCDF-Java library implements a Common Data Model (CDM), a generalization of the NetCDF, OpenDAP and HDF5 data models. The library is a prototype for the NetCDF-4 project, which provides a C language API for the "data access layer" of the CDM, on top of the HDF5 file format. The NetCDF-Java library is a 100% Java framework for reading netCDF and other file formats into the CDM, as well as writing to the netCDF-3 file format. The NetCDF-Java library also implements NcML, which allows you to add metadata to CDM datasets, as well as to create virtual datasets through aggregation.
NetCDF-Java库实现了CDM(通用数据模型),CDM包括NetCDF,OpenDAP,HDF5数据模型,它是NetCDF-4项目的一个原型,NetCDF-4项目是紧跟HDF5文件格式后采用C语言作为CDM的的数据访问层API。在CDM中NetCDF-Java包是完全用Java架构来读取NetCDF和其他格式文件,和写netCDF-3格式文件一样。它还实现了NCML,允许你为CDM数据集添加元数据,和通过运算生成实际数据一样。
4.2 CDM(通用数据模型)
参考网址:http://www.unidata.ucar.edu/software/netcdf-java/CDM/index.html
Unidata’s Common Data Model (CDM) is an abstract data model for scientific datasets. It merges the netCDF, OPeNDAP, and HDF5 data models to create a common API for many types of scientific data. The NetCDF Java library is an implementation of the CDM which can read many file formats besides netCDF. We call these CDM files, a shorthand for files that can be read by the NetCDF Java library and accessed through the CDM data model.
Unidata社区的CDM(通用数据模型)是一个科学数据的抽象数据模型,它包括了NetCDF, OPeNDAP, HDF5数据模型并为不同类型的科学数据创建了一个通用的API。NetCDF Java库实现了CDM,CDM除了能读取NetCDF格式外还有其他类型文件,我们把这些CDM文件作为那些能被NetCDF Java库读取和访问的的文件的简称。
4.3 NetCDF-Java/CDM
Architecture
4.4 CDM-FILES
General: NetCDF, OPeNDAP, HDF5, NetCDF4, HDF4, HDF-EOS
Gridded: GRIB-1, GRIB-2, GEMPAK
Radar: NEXRAD 2&3, DORADE, CINRAD, Universal Format, TDWR
Point: BUFR, ASCII
Satellite: DMSP, GINI, McIDAS AREA
Misc: GTOPO, Lightning, etc
Others in development (partial):
AVHRR, GPCP, GACP, SRB, SSMI, HIRS (NCDC)
4.5 Data Access Layer Object Model
4.6 NetCDF举例
4.6.1 下载
下载地址: http://www.unidata.ucar.edu/software/netcdf-java/documentation.htm
在线API: http://www.unidata.ucar.edu/software/netcdf-java/v4.2/javadoc/index.html
目前最新版本为4.2.20
最新版本需要JDK6
生成NC文件
package my.
import java.io.IOE
import java.util.ArrayL
import ucar.ma2.A
import ucar.ma2.DataT
import ucar.nc2.D
import ucar.nc2.NetcdfFileW
public class CreateNetcdf {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
String filename = "testWrite.nc";
NetcdfFileWriteable ncfile = NetcdfFileWriteable.createNew(filename,true); // add
// dimensions
Dimension latDim = ncfile.addDimension("lat", 3);
Dimension lonDim = ncfile.addDimension("lon", 3); // define
// Variable
ArrayList dims = new ArrayList();
dims.add(latDim);
dims.add(lonDim);
ncfile.addVariable("temperature", DataType.DOUBLE, dims);
ncfile.addVariableAttribute("temperature", "units", "K"); // add a
// attribute
Array data = Array.factory(int.class, new int[] { 3 }, new int[] { 1,2,3 });
ncfile.addVariableAttribute("temperature", "scale", data);
// add a string-valued variable: char svar(80)
Dimension svar_len = ncfile.addDimension("svar_len", 80);
dims = new ArrayList();
dims.add(svar_len);
ncfile.addVariable("svar", DataType.CHAR, dims);
// string array: char names(3, 80)
Dimension names = ncfile.addDimension("names", 3);
ArrayList dima = new ArrayList();
dima.add(names);
dima.add(svar_len);
ncfile.addVariable("names", DataType.CHAR, dima);
// how about a scalar variable?
ncfile.addVariable("scalar", DataType.DOUBLE, new ArrayList()); // add
// attributes
ncfile.addGlobalAttribute("yo", "face");
ncfile.addGlobalAttribute("versionD", new Double(1.2));
ncfile.addGlobalAttribute("versionF", new Float(1.2));
ncfile.addGlobalAttribute("versionI", new Integer(1));
ncfile.addGlobalAttribute("versionS", new Short((short) 2));
ncfile.addGlobalAttribute("versionB", new Byte((byte) 3)); // create
ncfile.create();
} catch (IOException e) {
System.err.println("ERROR creating file " + ncfile.getLocation()+ "\n" + e);
会生成一个testWrite.nc文件,该文件不能直接打开,可以通过下载的包中netcdfUI-4.2.jar打开:
4.6.3 读取NC文件
package my.
import java.io.IOE
import java.util.L
import ucar.nc2.D
import ucar.nc2.NetcdfF
import ucar.nc2.V
public class ReadNetcdf {
public static void main(String[] args) {
String filename = "D:\\work\\netcdf\\testWrite.nc";
NetcdfFile ncfile =
ncfile = NetcdfFile.open(filename);
//read dimensions
List&Dimension& list =
ncfile.getDimensions();
for(Dimension d : list){
System.out.println("name="+d.getName()+" length="+d.getLength());
//read variables
List&Variable& variables = ncfile.getVariables();
System.out.println();
for(Variable v : variables){
System.out.println("name="+v.getName()+" NameAndDimension="+v.getNameAndDimensions()+" ElementSize="+v.getElementSize());
} catch (IOException ioe) {
} finally {
if (null != ncfile)
ncfile.close();
} catch (IOException ioe) {
运行打印如下:
name=lat length=3
name=lon length=3
name=svar_len length=80
name=names length=3
name=temperature NameAndDimension=temperature(lat=3, lon=3) ElementSize=8
name=svar NameAndDimension=svar(svar_len=80) ElementSize=1
name=names NameAndDimension=names(names=3, svar_len=80) ElementSize=1
name=scalar NameAndDimension=scalar ElementSize=8
4.6.4 读写文件
package my.
import java.io.IOE
import ucar.ma2.ArrayD
import ucar.ma2.I
import ucar.ma2.InvalidRangeE
import ucar.nc2.D
import ucar.nc2.NetcdfFileW
public class WriteDataToNetcdf {
* @param args
* @throws IOException
public static void main(String[] args) throws IOException {
NetcdfFileWriteable ncfile = NetcdfFileWriteable.openExisting("D:\\work\\netcdf\\testWrite.nc", true);
Dimension latDim = ncfile.getDimensions().get(0);
Dimension lonDim = ncfile.getDimensions().get(1);
ArrayDouble A = new ArrayDouble.D2(latDim.getLength(), lonDim.getLength());
Index ima = A.getIndex();
for (i = 0; i & latDim.getLength(); i++) {
for (j = 0; j & lonDim.getLength(); j++) {
A.setDouble(ima.set(i, j), (double) (2));
int[] origin = new int[2];
ncfile.write("temperature", origin, A);
ncfile.close();
} catch (IOException e) {
System.err.println("ERROR writing file");
} catch (InvalidRangeException e) {
e.printStackTrace();
该方法为Variable temperature进行赋值,可以将修改后的testWrite.nc在netcdfUI-4.2.jar中查看:
double temperature(lat=3, lon=3);
:units = "K";
:scale = 1, 2, 3; // int
{2.0, 2.0, 2.0},
{2.0, 2.0, 2.0},
{2.0, 2.0, 2.0}
4.6.5 读取二维数据
通过v.read()可以读取数据:
Variable v = ncfile.findVariable(varName);
Variable v = ncfile.findVariable(varName);
Array data = v.read("0:2:1, 0:19:1");
package my.
import java.io.IOE
import ucar.ma2.A
import ucar.nc2.NCdumpW;
import ucar.nc2.NetcdfF
import ucar.nc2.V
public class ReadData {
public static void main(String[] args) {
String filename = "D:\\work\\netcdf\\testWrite.nc";
NetcdfFile ncfile =
ncfile = NetcdfFile.open(filename);
//find variable
String variable = "temperature";
Variable varBean = ncfile.findVariable(variable);
//Reading data from a Variable
if(null != varBean) {
Array all = varBean.read();
Array data = varBean.read("0:2:1, 0:2:1");
Array data1 = varBean.read("0:2:2, 0:2:2");
System.out.println("读取所有:\n"+NCdumpW.printArray(all, variable, null));
System.out.println("x轴从0到2 跨度为1 y轴从0到2 跨度为1:\n"+NCdumpW.printArray(data, variable, null));
System.out.println("x轴从0到2 跨度为2 y轴从0到2 跨度为2:\n"+NCdumpW.printArray(data1, variable, null));
if(null != varBean) {
int[] origin = new int[] { 0 , 0};
int[] size = new int[] { 3,3};
Array data2D = varBean.read(origin, size);
System.out.println("读取所有:\n"+NCdumpW.printArray(data2D, variable, null));
if(null != varBean) {
int[] origin = new int[] { 1 , 1};
int[] size = new int[] { 2,1};
Array data2D = varBean.read(origin, size);
System.out.println("读取从第二行第二列开始为起点x数量为1,y数量为2:\n"+NCdumpW.printArray(data2D, variable, null));
System.out.println("由此可得结论:维上的起点都以数组0开始,且阵列顺序在坐标中是从右至左\n如:int[] size = new int[] { 2,1},1代表x轴,2代表的是y轴....");
} catch (Exception ioe) {
ioe.printStackTrace();
} finally {
if (null != ncfile)
ncfile.close();
} catch (IOException ioe) {
打印结果如下:根据结果可以知道read("0:2:2, 0:2:2")和read(origin, size)的差别
读取所有:
temperature =
{0.0, 1.0, 2.0},
{1.0, 2.0, 3.0},
{2.0, 3.0, 4.0}
x轴从0到2 跨度为1 y轴从0到2 跨度为1:
temperature =
{0.0, 1.0, 2.0},
{1.0, 2.0, 3.0},
{2.0, 3.0, 4.0}
x轴从0到2 跨度为2 y轴从0到2 跨度为2:
temperature =
{0.0, 2.0},
{2.0, 4.0}
读取所有:
temperature =
{0.0, 1.0, 2.0},
{1.0, 2.0, 3.0},
{2.0, 3.0, 4.0}
读取从第二行第二列开始为起点x数量为1,y数量为2:
temperature =
由此可得结论:维上的起点都以数组0开始,且阵列顺序在坐标中是从右至左
如:int[] size = new int[] { 2,1},1代表x轴,2代表的是y轴....
4.6.6 多维NetCDF(三维)
4.6.6.1 创建
创建三维NetCDF文件:
package my.
import java.io.IOE
import java.util.ArrayL
import ucar.ma2.A
import ucar.ma2.DataT
import ucar.nc2.D
import ucar.nc2.NetcdfFileW
public class Create3DNetCDF {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
String filename = "test3D.nc";
NetcdfFileWriteable ncfile = NetcdfFileWriteable.createNew(filename,true); // add
Dimension timeDim = ncfile.addDimension("time",2);
Dimension latDim = ncfile.addDimension("lat", 3);
Dimension lonDim = ncfile.addDimension("lon", 3); // define
ArrayList dims = new ArrayList();
dims.add(timeDim);
dims.add(latDim);
dims.add(lonDim);
ncfile.addVariable("temperature", DataType.DOUBLE, dims);
ncfile.addVariableAttribute("temperature", "units", "K"); // add a
Array data = Array.factory(int.class, new int[] { 3 }, new int[] { 1,2,3 });
ncfile.addVariableAttribute("temperature", "scale", data);
ncfile.create();
} catch (IOException e) {
System.err.println("ERROR creating file " + ncfile.getLocation()+ "\n" + e);
4.6.6.2 写数据
package my.
import java.io.IOE
import ucar.ma2.ArrayD
import ucar.ma2.I
import ucar.ma2.InvalidRangeE
import ucar.nc2.D
import ucar.nc2.NetcdfFileW
public class Write3DNetCDF {
public static void main(String[] args) throws IOException {
NetcdfFileWriteable ncfile = NetcdfFileWriteable.openExisting("D:\\work\\netcdf\\test3D.nc", true);
Dimension timeDim = ncfile.getDimensions().get(0);
Dimension latDim = ncfile.getDimensions().get(1);
Dimension lonDim = ncfile.getDimensions().get(2);
ArrayDouble A = new ArrayDouble.D3(timeDim.getLength(),latDim.getLength(), lonDim.getLength());
Index ima = A.getIndex();
for(k = 0; k & timeDim.getLength(); k++){
for (i = 0; i & latDim.getLength(); i++) {
for (j = 0; j & lonDim.getLength(); j++) {
A.setDouble(ima.set(k,i,j), (double) (k+i+j));
int[] origin = new int[3];
ncfile.write("temperature", origin, A);
ncfile.close();
} catch (IOException e) {
System.err.println("ERROR writing file");
} catch (InvalidRangeException e) {
e.printStackTrace();
对应的CDL格式如下:
double temperature(time=2, lat=3, lon=3);
:units = "K";
:scale = 1, 2, 3; // int
{0.0, 1.0, 2.0},
{1.0, 2.0, 3.0},
{2.0, 3.0, 4.0}
{1.0, 2.0, 3.0},
{2.0, 3.0, 4.0},
{3.0, 4.0, 5.0}
4.6.6.3 读数据
package my.
import java.io.IOE
import ucar.ma2.A
import ucar.nc2.NCdumpW;
import ucar.nc2.NetcdfF
import ucar.nc2.V
public class Read3DNetCDF {
public static void main(String[] args) {
String filename = "D:\\work\\netcdf\\test3D.nc";
NetcdfFile ncfile =
ncfile = NetcdfFile.open(filename);
String variable = "temperature";
Variable varBean = ncfile.findVariable(variable);
//read all data
if(null != varBean) {
Array all = varBean.read();
System.out.println("读取所有:\n"+NCdumpW.printArray(all, variable, null));
if(null != varBean) {
int[] origin = new int[] { 0,1,1};
int[] size = new int[] { 2,2,2};
Array data2D = varBean.read(origin, size);
System.out.println("读取从第一维的0开始,第二维从1开始,第三维从1开始,数量分别为2,2,2:\n"+NCdumpW.printArray(data2D, variable, null));
// invoke reduce trans 3D to 2D
if(null != varBean) {
int[] origin = new int[] { 0,1,1};
int[] size = new int[] { 1,2,2};
Array data2D = varBean.read(origin, size).reduce().reduce();
System.out.println("读取从第一维的0开始,第二维从1开始,第三维从1开始,数量分别为1,2,2并转为二维:\n"+NCdumpW.printArray(data2D, variable, null));
} catch (Exception ioe) {
ioe.printStackTrace();
} finally {
if (null != ncfile)
ncfile.close();
} catch (IOException ioe) {
读取所有:
temperature =
{0.0, 1.0, 2.0},
{1.0, 2.0, 3.0},
{2.0, 3.0, 4.0}
{1.0, 2.0, 3.0},
{2.0, 3.0, 4.0},
{3.0, 4.0, 5.0}
读取从第一维的0开始,第二维从1开始,第三维从1开始,数量分别为2,2,2:
temperature =
{2.0, 3.0},
{3.0, 4.0}
{3.0, 4.0},
{4.0, 5.0}
读取从第一维的0开始,第二维从1开始,第三维从1开始,数量分别为1,2,2并转为二维:
temperature =
{2.0, 3.0},
{3.0, 4.0}
4.7 NetCDF-NCML(Modifying existing files)
通过NCML标记语言可以对NetCDF文件修改
参考网址:http://www.unidata.ucar.edu/software/netcdf/ncml/v2.2/Tutorial.html
4.8 NCML- Aggregation
通过NCML合并存在的多个NetCDF文件
参考网站:http://www.unidata.ucar.edu/software/netcdf/ncml/v2.2/Aggregation.html
4.9 NetCDF-IOSP(I/O Service Provide)
参考网址:http://www.unidata.ucar.edu/software/netcdf-java/tutorial/IOSPoverview.html
4.9.1 Overview
A client uses the NetcdfFile, NetcdfDataset, or one of the Scientific Feature Type APIs to read data from a CDM file. These provide a rich and sometimes complicated API to the client. Behind the scenes, when any of these APIs actually read from a dataset, however, they use a very much simpler interface, the I/O Service Provider or IOSP for short. The Netcdf Java library has many implementations of this interface, one for each different file format that it knows how to read. This design pattern is called a Service Provider.
IOSPs are managed by the NetcdfFile class. When a client requests a dataset (by calling NetcdfFile.open), the file is opened as a ucar.unidata.io.RandomAccessFile (an improved version of java.io.RandomAccessFile). Each registered IOSP is then asked "is this your file?" by calling isValidFile( ucar.unidata.io.RandomAccessFile). The first one that returns true claims it. When you implement isValidFile() in your IOSP, it must be very fast and accurate.
4.9.2 IOServiceProvider
package ucar.nc2.
import ucar.ma2.S
import ucar.ma2.InvalidRangeE
import ucar.ma2.StructureDataI
import ucar.nc2.ParsedSectionS
import ucar.nc2.S
import java.io.IOE
import java.nio.channels.WritableByteC
* This is the service provider interface for the low-level I/O access classes (read only).
* This is only used by service implementors.
* The NetcdfFile class manages all registered IOServiceProvider classes.
* When NetcdfFile.open() is called:
* &li& the file is opened as a ucar.unidata.io.RandomAccessF&/li&
* &li& the file is handed to the isValidFile() method of each registered
* IOServiceProvider class (until one returns true, which means it can read the file).&/li&
* &li& the open() method on the resulting IOServiceProvider class is handed the file.&/li&
* @see ucar.nc2.NetcdfFile#registerIOProvider(Class) ;
* @author caron
public interface IOServiceProvider {
* Check if this is a valid file for this IOServiceProvider.
* You must make this method thread safe, ie dont keep any state.
* @param raf RandomAccessFile
* @return true if valid.
* @throws java.io.IOException if read error
public boolean isValidFile( ucar.unidata.io.RandomAccessFile raf) throws IOE
其他方法见官网介绍或API文档。
4.9.3 AbstractIOServiceProvider
Your implementataion class should extend ucar.nc2.iosp.AbstractIOServiceProvider. This provides default implementation of some of the methods, so minimally, you only have to implement 4 methods:
public class MyIosp extends ucar.nc2.iosp.AbstractIOServiceProvider {
public boolean isValidFile(RandomAccessFile raf) throws IOException {}
public void open(RandomAccessFile raf, NetcdfFile ncfile, CancelTask cancelTask) throws IOException {}
public Array readData(Variable v2, Section wantSection) throws IOException, InvalidRangeException {}
public void close() throws IOException {}
public String getFileTypeId() {}
public String getFileTypeVersion() {}
public String getFileTypeDescription();
4.9.4 IOSP-Example
通过IOSP对数据处理生成NetCDF文件已经读取NetCDF数据例子:
参考网址:
http://www.unidata.ucar.edu/software/netcdf-java/tutorial/index.html
图中的例子为雷电数据,卫星数据,雷达数据相关
如果您想提高自己的技术水平,欢迎加入本站官方1号QQ群:&&,&&2号QQ群:,在群里结识技术精英和交流技术^_^
本站联系邮箱:

我要回帖

更多关于 qq音速道具 的文章

 

随机推荐