请选择 进入手机版 | 继续访问电脑版
爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 5280|回复: 8

[混合编程] IDL编程学习之输出不限宽度的txt

[复制链接]

新浪微博达人勋

发表于 2018-8-9 23:21:58 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
在用IDL把数据输出为txt时经常遇到宽度受限的情况,
比如:
IDL> openw,lun,'C:\Users\朽木\Desktop\456.txt',/get_lun
IDL> printf,lun,findgen(20,20)
IDL> close,/all

文件里是这样的:
QQ截图20180809230702.png
再比如,
IDL> openw,lun,'C:\Users\朽木\Desktop\456.txt',/get_lun
IDL> printf,lun,indgen(20,20)
IDL> close,/all

文件里是这样的:
QQ截图20180809230811.png
我以前一直以为限制列数,其实是因为我一直使用浮点型数据的缘故,
输出整型数组就变成了10列,可见并不是限制列数。
经我试验,把每行数组转为字符串就没有宽度限制了,代码如下,生成的没有宽度限制的B.txt见附件:
function txt,name,x
  openw,lun,'C:\Users\朽木\Desktop\'+name+'.txt',/get_lun
  printf,lun,x
  close,/all
end
pro out
  tic
A=[123.456789D,123.456789D,123.456789D,123.456789D,$
  123.456789D,123.456789D,123.456789D,123.456789D,$
  123.456789D,123.456789D,123.456789D,123.456789D]
  ; 语法:STRJOIN( String [, Delimiter])
B=strjoin(strtrim(string(a),2),',')
B=B+B+B+B+B+B
TXT=TXT('B',B)
  toc
end

QQ截图20180809231407.png
所以,完全可以写一个函数,把数据按字符串输出,
但可惜的是,输出数字的精度还是被自动截断了!
控制台中,
IDL> 123.456789
       123.45679
IDL> 123.456789D
       123.45678900000000
可见数字尾部加上双精度符号D就会完整精度输出,
但是你在脚本里写print,123.456789D,控制台却输出:
       123.45679
看来,控制台和脚本还是有差异。
难道,为了输出完整精度,只能把各个数位的数字自己提取出来?我ca~~

B.txt

716 Bytes, 下载次数: 0, 下载积分: 金钱 -5

密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-8-10 09:03:27 | 显示全部楼层
根据上楼分析,做了个输出不限宽度数据的函数,代码如下:
;把变量x输出为name.txt,name是字符串
function txt,name,x
  tic
  sizex=size(x)
  ;如果x是向量
  if(sizex[0] eq 1)then begin
    B=strjoin(strtrim(string(x),2),' ')
    openw,lun,'C:\Users\YGDY\Desktop\'+name+'.txt',/get_lun,/append
    printf,lun,B
    close,/all
  end
  ;如果x是二维数组
  if(sizex[0] eq 2)then begin
    for i=0,sizex[2]-1 do begin
      a=x[*,i]
      B=strjoin(strtrim(string(a),2),' ')
      openw,lun,'C:\Users\YGDY\Desktop\'+name+'.txt',/get_lun,/append
      printf,lun,B
      close,/all
    endfor
  endif
  ;x就不是向量,又不是二维数组,就报错,不过我还不知道是否存在这种情况
  if(sizex[0] ne 1 and sizex[0] ne 2)then begin
    warning=dialog_message('error!')
  endif
  toc
end

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-8-26 22:22:31 | 显示全部楼层
openw, lun, file, /get_lun, width=1000
这样可以输出1000个宽度的。所以你的做法复杂了
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-8-26 22:24:58 | 显示全部楼层
IDL默认80个字符。在openw的时候,加入width=1000,可自定义宽度。
The desired output width. If no output width is specified, IDL uses the following rules to determine where to break lines:
    If the output file is a terminal, the terminal width is used.
    Otherwise, a default of 80 columns is used.
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-8-26 22:26:03 | 显示全部楼层
openw, lun, 'D:\a.txt', /get_lun, width=1000
printf, lun, fingen(50, 50)
free_lun, lun
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-9-3 09:02:44 | 显示全部楼层
ybmy001 发表于 2018-8-26 22:26
openw, lun, 'D:\a.txt', /get_lun, width=1000
printf, lun, fingen(50, 50)
free_lun, lun

原来是这样啊
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-9-3 10:45:41 | 显示全部楼层
ybmy001 发表于 2018-8-26 22:26
openw, lun, 'D:\a.txt', /get_lun, width=1000
printf, lun, fingen(50, 50)
free_lun, lun

大神,
精确输出的问题,我已经写个函数暂时解决了,
但123.45678987654321这种位数很多的数字的精确读取还不行,
float型只能支持6位有效数字,double型只支持8位有效数字,
怎么才能精确读入呢??
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-9-3 10:45:46 | 显示全部楼层
ybmy001 发表于 2018-8-26 22:26
openw, lun, 'D:\a.txt', /get_lun, width=1000
printf, lun, fingen(50, 50)
free_lun, lun

大神,
精确输出的问题,我已经写个函数暂时解决了,
但123.45678987654321这种位数很多的数字的精确读取还不行,
float型只能支持6位有效数字,double型只支持8位有效数字,
怎么才能精确读入呢??
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2018-9-3 11:24:32 | 显示全部楼层
IDL> print,115.123456,format='(d12.7)'
115.1234589
利用format关键字,似乎也只能精确到小数点后第5位
经纬度值小数点后第6位是米级别,第5位是十米级,勉强可以缓解地图定位误差问题
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表