爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 13056|回复: 24

ncl trmm卫星格式转换

[复制链接]
发表于 2014-7-17 10:21:29 | 显示全部楼层 |阅读模式

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

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

x
请问哪位大神有ncl trmm卫星bin格式转nc格式的程序啊?
密码修改失败请联系微信:mofangbao
 楼主| 发表于 2014-7-18 12:16:14 | 显示全部楼层
longlivehj 发表于 2014-7-18 10:48
点回复,否则不能及时看到。
如果是对局部地区进行研究,就只去除相应空间范围的数据就好。
不用显式的 ...

恩。好的!我前面在画模式图的时候模拟中国区域之后就需要画整个区域的,也出现了内存不够,你说的这个我到可以在这个卫星数据里面用!
密码修改失败请联系微信:mofangbao
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2014-7-17 10:23:54 | 显示全部楼层
最好是那种批量转的!
密码修改失败请联系微信:mofangbao
发表于 2014-7-17 10:51:17 | 显示全部楼层
咦?昨天给你的链接不好用啊?
密码修改失败请联系微信:mofangbao
 楼主| 发表于 2014-7-17 10:58:09 | 显示全部楼层
昨天你给的链接程序挺好的,那个程序是画一个时次的,我想做一个月平均,直接读bin格式的不会操作,如果能转换成nc格式就知道怎么做了,那个程序后面也有一个转换为nc的程序,就是不知道怎么批量处理
密码修改失败请联系微信:mofangbao
发表于 2014-7-17 11:09:35 | 显示全部楼层
本帖最后由 longlivehj 于 2014-7-17 11:38 编辑
风之牧语 发表于 2014-7-17 10:58
昨天你给的链接程序挺好的,那个程序是画一个时次的,我想做一个月平均,直接读bin格式的不会操作,如果能 ...

就在那个程序上改的。把diri改成bin文件所在目录。
diri   = "./"                             ; input directory
两个输出,一个是nc,文件名与bin除了后缀,其它完全相同;另外一个是ps,提供预览。




; --------------------------3B42RT-----------------------------
; Header:
; Each file starts with a header that is one 2-byte-integer
;   row in length, or 2880 bytes.
; Following the header, 4 data fields appear:
;   precipitation (2-byte integer), precipitation_error (2-byte integer)
;   source (1-byte integer), precipitation_uncalibrated (2-byte integer)
; All fields are 1440x480 grid boxes (0-360E,60N-S).
;   The first grid box center is at (0.125E,59.875N)
; The binary file is 'big endian'
; -------------------------------------------------------------
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
;***************************************************************
; User Input
;***************************************************************

begin
                                             ; INPUT
   diri   = "./"                             ; input directory

   fili   = systemfunc("ls " + diri + "/*.bin")        ; binary uncompressed
                                             ; OUTPUT
   netCDF = True                             ; generate netCDF file
   PLOT   = True                             ; generate plots

   do i = 0, dimsizes(fili) - 1
       sfx = get_file_suffix(fili(i), 0)
       if (netCDF) then
           ncDir   = "./"                        ; directory for netCDF output
           ncFil   = sfx@fBase + ".nc"    ; netCDF name output
       end if

       if (PLOT) then
           pltDir  = "./"                        ; directory for plot output
           pltName = sfx@fBase      ; plot name root
           pltType = "ps"                                          
       end if

       nlat   = 480
       mlon   = 1440
       lhead  = 2880     ; characters (lhead/2=1440 type short)
       lheads = lhead/2  ; header length as type 'short'

       print("lhead="+lhead+"  nlat="+nlat+"  " \
            +"mlon="+mlon+"   nlat*mlon="+nlat*mlon)

       prcScale = 0.01   
       errScale = 0.01   

    ;***************************************************************
    ; End User Input
    ;***************************************************************
    ; Miscellaneous: Parse the file name to extract strings/ date info
    ;***************************************************************
       filc        = tochar( fili(i) )
       yyyy_str    = tostring(filc( 7:10))    ; "yyyy"
       mon_str     = tostring(filc(11:12))    ; "mon"
       dd_str      = tostring(filc(13:14))    ; "dd"
       hh_str      = tostring(filc(15:16))    ; "hr"        

       yyyy        = toint( yyyy_str )        ; full year
       mm          = toint( mon_str )         ; month
       dd          = toint( dd_str )          ; day  of month
       hh          = toint( hh_str )
       print("yyyy="+yyyy+"  mm="+mm+"  dd="+dd+"  hh="+hh)

    ;***************************************************************
    ; Read BigEndian binary file
    ;***************************************************************
    ; Read Header (2880 characters (character=byte in length)           
    ;***************************************************************
       setfileoption("bin","ReadByteOrder","BigEndian")

       hdrc   = fbindirread (diri+fili(i), 0, lhead , "character")  ; header
       hdrs   = tostring(hdrc)        ; create string
       print(hdrs)
     ;;print(hdrc)     

    ;***************************************************************
    ; Read precip (short); skip header; read type short; reshape & scale
    ; Read error  (short); skip header and precip; read err; reshape & scale
    ;***************************************************************
       works  = fbindirread (diri+fili(i), 0, -1, "short")  
       works  = where(works.le.toshort(-31998) .or. works.ge.toshort(31999)\
                     ,toshort(-31999), works)
       works@_FillValue = toshort(-31999)
     ;;print(works(lhead/2:lhead/2+100))

       starts = lhead/2                ; length of header in terms of "short"
       lasts  = starts+nlat*mlon-1
       prc    = onedtond(works(starts:lasts)*prcScale, (/nlat,mlon/) )   

       starts = lasts+1  
       lasts  = starts+nlat*mlon-1
       err    = onedtond(works(starts:lasts)*errScale, (/nlat,mlon/) )   

    ;***************************************************************
    ; Read source (byte); skip header (char), precip and error (short)
    ;***************************************************************

       startb = lhead + 4*nlat*mlon     ; 4=2_bytes(short)*2_variables
       lastb  = startb+nlat*mlon-1
       workb  = fbindirread (diri+fili(i), 0, -1, "byte")  
       workb@_FillValue = tobyte(0)
       src    = onedtond(workb(startb:lastb),(/nlat,mlon/) )

       delete(workb)       ; no longer needed

    ;***************************************************************
    ; Read uncalibrated precip (short); skip header, precip, error, source
    ;***************************************************************

       starts = lhead/2 + 2*nlat*mlon + (nlat*mlon)/2
       lasts  = starts+nlat*mlon-1
       print("prcu: starts="+starts+"   lasts="+lasts)
       prcu   = onedtond(works(starts:lasts)*prcScale, (/nlat,mlon/) )   

       delete(works)       ; no longer needed

    ;***************************************************************
    ; Add meta data
    ;***************************************************************
       prc@units       = "mm/hr"
       prc@long_name   = "TRMM_3B42RT: Hourly Rain Rate"
      ;prc@_FillValue  =           ; ??
       printMinMax(prc,0)

       err@units       = "mm/hr"
       err@long_name   = "TRMM_3B42RT: Hourly Rain Rate"
      ;err@_FillValue  =
       printMinMax(err,0)

       src@long_name   = "Data Source"
       src@source      = "0=no observation, 1=AMSU, 2=TMI, 3=AMSR, 4=SSMI, "+\
                         "5=SSMIS, 6=MHS, 30=AMSU&MHS avg, 31=conical avg, "+\
                         "50=IR, 1,2,3,4,5,6+100=Sparce Sample"

       prcu@units       = "mm/hr"
       prcu@long_name   = "TRMM_3B42RT: Uncalibrated Hourly Rain Rate"
       prcu@information = "Multi-satellite precipitation before this calibration"
      ;prcu@_FillValue  =
       printMinMax(prcu,0)

    ;*****************************************************
    ; Create TRMM coordinate variables. See README
    ;*****************************************************
       lat        = 59.875 - ispan(0,nlat-1,1)*0.25
       lon        = ispan(0,mlon-1,1)*0.25

       lat@long_name = "latitude"
       lat@units  = "degrees_north"
       lat!0      = "lat"
       lat&lat    =  lat

       lon@long_name = "longitude"
       lon@units  = "degrees_east"
       lon!0      = "lon"
       lon&lon    =  lon

    ;***************************************************************
    ; Associate the spatial coordinates with variables
    ;***************************************************************
       prc!0      = "lat"                   ; 1st ... name the dimensions
       prc!1      = "lon"
       prc&lat    =  lat                    ; create coordinate variable
       prc&lon    =  lon                    

       err!0      = "lat"                  
       err!1      = "lon"
       err&lat    =  lat                  
       err&lon    =  lon                    

       src!0      = "lat"               
       src!1      = "lon"
       src&lat    =  lat               
       src&lon    =  lon                    

       prcu!0     = "lat"                   ; 1st ... name the dimensions
       prcu!1     = "lon"
       prcu&lat   =  lat                    ; create coordinate variable
       prcu&lon   =  lon                    

       printVarSummary(prc)
       printMinMax(prc, 0)
       printVarSummary(err)
       printMinMax(err, 0)
       printVarSummary(src)
       printMinMax(src, 0)
       printVarSummary(prcu)
       printMinMax(prcu, 0)

    ;************************************************
    ; Create plot ?
    ;************************************************
       if (PLOT) then
           wks    = gsn_open_wks(pltType, pltName)
           colors = (/"white","black", "Snow"    \
                   ,"PaleTurquoise","PaleGreen","SeaGreen3" ,"Yellow"  \
                   ,"Orange","HotPink","Red","Violet", "Purple", "Brown"/)

           gsn_define_colormap(wks, colors)               ; generate new color map
      
           res                      = True     ; plot mods desired
          ;res@gsnDraw              = False    ; let gsn_panel do plotting
          ;res@gsnFrame             = False
           res@gsnMaximize          = True     ; make ps/eps/pdf large
         
           res@cnFillOn             = True     ; turn on color fill
           res@cnLinesOn            = False    ; turn of contour lines
           res@cnFillMode           = "RasterFill"         ; Raster Mode
           res@cnLineLabelsOn       =  False       ; Turn off contour lines
           res@cnLevelSelectionMode = "ExplicitLevels"              
           res@cnMissingValFillPattern = 0
           res@cnMissingValFillColor   = "black"

           res@lbOrientation        = "vertical"   ; vertical label barb's
           res@lbLabelFontHeightF   = 0.012        ; change font size
           res@pmLabelBarOrthogonalPosF = -0.01    ; move a bit to left
           res@pmLabelBarWidthF     =  0.1     
           res@mpMinLatF            = -50.   
           res@mpMaxLatF            =  50.
           res@mpCenterLonF         = 210.
           res@mpFillOn             = False
           res@mpOutlineOn          = True
           res@mpOutlineBoundarySets  = "National"   ; turn on country boundaries

          ;res@mpShapeMode          = "FreeAspect"
          ;res@vpWidthF             = 0.8
          ;res@vpHeightF            = 0.4
      
          ;res@cnLevels             = (/0.1,1,2.5,5,10,15,20,25,50,75/) ; "mm/3hr"
           res@cnLevels             = (/0.1,1,2,3,4,5,7.5,10,15,20/) ; "mm/3hr"
           res@tiMainString         = fili(i)
           prc    = where (prc.lt.0, prc@_FillValue, prc)    ; bogus
           plot   = gsn_csm_contour_map_ce(wks,prc, res)
       end if
      
    ;************************************************
    ; Create netCDF ?
    ; Recommend to always create a 'time' dimension
    ;************************************************
      
       if (netCDF) then
           ntim     = 1

           tunits   = "hours since 1997-01-01 00:00:0.0"
           time     = (/cd_inv_calendar(yyyy,mm,dd,hh, 0,0d0,tunits, 0)/)
           time!0   = "time"

           date     = yyyy*1000000 + mm*10000 + dd*100 + hh
           date!0   = "time"
           date@units = "yyyymmddhh"

           nline    = inttochar(10)         ; new line character
           
           globeAtt              = 1
           globeAtt@title        = "TRMM_3B42"   
           globeAtt@ftp          = nline + \
                   "http://disc.sci.gsfc.nasa.gov/data/datapool/TRMM_DP/01_Data_Products/02_Gridded/06_3-hour_Gpi_Cal_3B_42" +nline
           globeAtt@ID           = "3B42RT"
           globeAtt@description  = nline + \
    "????" + nline
           globeAtt@creation_date= systemfunc ("date" )
         
           NCFILE = ncFil
           system ("/bin/rm -f " +  NCFILE)    ; remove any pre-exist file
               
           ncdf   = addfile(NCFILE,"c")     
         
          ;setfileoption(ncdf, "definemode", True)
         
           fileattdef( ncdf, globeAtt )        ; create the global [file] attributes
                                             
           dimNames = (/"time", "lat", "lon" /)  
           dimSizes = (/ ntim ,  nlat,  mlon /)
           dimUnlim = (/ True , False, False /)   
           filedimdef(ncdf, dimNames  , dimSizes,  dimUnlim )
         
           filevardef   (ncdf, "time"  , typeof(time), getvardims(time) )
           filevarattdef(ncdf, "time", time)
         
           filevardef   (ncdf, "lat", typeof(lat), getvardims(lat))
           filevarattdef(ncdf, "lat", lat)
              
           filevardef   (ncdf, "lon", typeof(lon), getvardims(lon))
           filevarattdef(ncdf, "lon", lon)
         
           filevardef   (ncdf, "date"  , typeof(date), getvardims(date) )
           filevarattdef(ncdf, "date", date)
         
           filevardef    (ncdf, "PRC"  , typeof(prc) , (/ "time", "lat", "lon" /) )
           filevarattdef (ncdf, "PRC"  , prc)

           filevardef(ncdf, "RELERR"   , typeof(err), (/ "time", "lat", "lon" /) )
           filevarattdef(ncdf, "RELERR", err)
         
           ncdf->time   = (/ time /)
           ncdf->lat    = (/ lat /)
           ncdf->lon    = (/ lon /)
           ncdf->date   = (/ date /)
           ncdf->PRC(0:0,:,:) = (/ prc  /)
           ncdf->RELERR(0:0,:,:) = (/ err  /)

       end if    ; netCDF

   end do
end
密码修改失败请联系微信:mofangbao
 楼主| 发表于 2014-7-17 11:16:42 | 显示全部楼层
是的!这一点我明白,程序里面都是直接赋给输出nc文件的文件名,我想做的是,批量读取bin格式文件,并且按照相同的文件名转换成nc格式!
密码修改失败请联系微信:mofangbao
发表于 2014-7-17 11:25:48 | 显示全部楼层
TRMM不是有NC格式的么
密码修改失败请联系微信:mofangbao
 楼主| 发表于 2014-7-17 11:27:33 | 显示全部楼层
嗯!但是我现在的数据是bin格式的,就学习一下怎么处理这种格式的数据
密码修改失败请联系微信:mofangbao
发表于 2014-7-17 11:30:33 | 显示全部楼层
风之牧语 发表于 2014-7-17 11:16
是的!这一点我明白,程序里面都是直接赋给输出nc文件的文件名,我想做的是,批量读取bin格式文件,并且按 ...

上面贴出的就是改好的批量处理的程序!
你用用试试。
密码修改失败请联系微信:mofangbao
 楼主| 发表于 2014-7-17 12:11:39 | 显示全部楼层
哦!没有仔细看,汗!
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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