声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 3554|回复: 2

[Fortran] 用fortran如何把dat格式的文件转换成nc格式的

[复制链接]
发表于 2008-1-16 22:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
把div.dat转换成nc格式的文件。div.dat的描述文件div.ctl如下:
******************************************************************************
dset  D:\jimian\netcdf\fortran\div.dat
title average t
undef -9.96921e+36
xdef  144 linear 0 2.5
ydef  73 linear  -90 2.5
zdef  23 levels 1 2 3 5 7 10 20 30
        50 70 100 150 200 250 300 400 500 600
        700 775 850 925 1000
tdef  366 linear 00z01Jan1976 1440mn
vars 1
d  23 -999 average uwnd
endvars
******************************************************************************
下面是fortran程序
生成的文件div_test.nc,可用sdfopen打开,但是画图的时候除了前面两个时次以外(这两个时次画出来的图形也是错的),始终显示:
Cosntant field, Value = -1.62507e-06

请高手指点一下。
*******************************************************************************
     program fgennc
     include 'netcdf.inc'
real div(144,73,23,366)
     integer start(4),count(4)
     data start/1,1,1,1/
     data count/144,73,23,366/
* error status return
     integer  iret
* netCDF id
     integer  ncid
* dimension ids
     integer  longitude_dim
     integer  latitude_dim
     integer  levelist_dim
     integer  time_dim
* dimension lengths
     integer  longitude_len
     integer  latitude_len
     integer  levelist_len
     integer  time_len
     parameter (longitude_len = 144)
     parameter (latitude_len = 73)
     parameter (levelist_len = 23)
     parameter (time_len = 366)
* variable ids
     integer  longitude_id
     integer  latitude_id
     integer  levelist_id
     integer  time_id
     integer  d_id
* rank (number of dimensions) for each variable
     integer  longitude_rank
     integer  latitude_rank
     integer  levelist_rank
     integer  time_rank
     integer  d_rank
     parameter (longitude_rank = 1)
     parameter (latitude_rank = 1)
     parameter (levelist_rank = 1)
     parameter (time_rank = 1)
     parameter (d_rank = 4)
* variable shapes
     integer  longitude_dims(longitude_rank)
     integer  latitude_dims(latitude_rank)
     integer  levelist_dims(levelist_rank)
     integer  time_dims(time_rank)
     integer  d_dims(d_rank)
* data variables
     real  longitude(longitude_len)
     real  latitude(latitude_len)
     integer  levelist(levelist_len)
integer  time(time_len)
* attribute vectors
     integer  int2val(1)
     double precision  doubleval(1)

open(1,file='D:\div.dat',form='binary'
    1,status='old')
     read(1)((((div(i,j,k,l),i=1,144),j=1,73),k=1,23),l=1,1,366)
     close(1)
     print*,'read binary data to div_ok!'

C * enter define mode
     iret = nf_create('div_test.nc', NF_CLOBBER, ncid)
     call check_err(iret)
* define dimensions
     iret = nf_def_dim(ncid, 'longitude', 144, longitude_dim)
     call check_err(iret)
     iret = nf_def_dim(ncid, 'latitude', 73, latitude_dim)
     call check_err(iret)
     iret = nf_def_dim(ncid, 'levelist', 23, levelist_dim)
     call check_err(iret)
     iret = nf_def_dim(ncid, 'time', 366, time_dim)
     call check_err(iret)
* define variables
     longitude_dims(1) = longitude_dim
     iret = nf_def_var(ncid, 'longitude', NF_REAL, longitude_rank, long
    1itude_dims, longitude_id)
     call check_err(iret)
     latitude_dims(1) = latitude_dim
     iret = nf_def_var(ncid, 'latitude', NF_REAL, latitude_rank, latitu
    1de_dims, latitude_id)
     call check_err(iret)
     levelist_dims(1) = levelist_dim
     iret = nf_def_var(ncid, 'levelist', NF_INT, levelist_rank, levelis
    1t_dims, levelist_id)
     call check_err(iret)
     time_dims(1) = time_dim
     iret = nf_def_var(ncid, 'time', NF_INT, time_rank, time_dims, time
    1_id)
     call check_err(iret)
     d_dims(4) = time_dim
     d_dims(3) = levelist_dim
     d_dims(2) = latitude_dim
     d_dims(1) = longitude_dim
     iret = nf_def_var(ncid, 'd', NF_INT2, d_rank, d_dims, d_id)
     call check_err(iret)
* assign attributes
     iret = nf_put_att_text(ncid, longitude_id, 'units', 12, 'degrees_e
    1ast')
     call check_err(iret)
     iret = nf_put_att_text(ncid, longitude_id, 'long_name', 9, 'longit
    1ude')
     call check_err(iret)
     iret = nf_put_att_text(ncid, latitude_id, 'units', 13, 'degrees_no
    1rth')
     call check_err(iret)
     iret = nf_put_att_text(ncid, latitude_id, 'long_name', 8, 'latitud
    1e')
     call check_err(iret)
     iret = nf_put_att_text(ncid, levelist_id, 'units', 9, 'millibars')
     call check_err(iret)
     iret = nf_put_att_text(ncid, levelist_id, 'long_name', 14, 'pressu
    1re_level')
     call check_err(iret)
     iret = nf_put_att_text(ncid, time_id, 'units', 32, 'hours since 19
    100-01-01 00:00:0.0')
     call check_err(iret)
     iret = nf_put_att_text(ncid, time_id, 'long_name', 4, 'time')
     call check_err(iret)
     doubleval(1) = 5.20944886392983d-009
     iret = nf_put_att_double(ncid, d_id, 'scale_factor', NF_DOUBLE, 1,
    1 doubleval)
     call check_err(iret)
     doubleval(1) = -1.62507044442481d-006
     iret = nf_put_att_double(ncid, d_id, 'add_offset', NF_DOUBLE, 1, d
    1oubleval)
     call check_err(iret)
     int2val(1) = -32767
     iret = nf_put_att_int(ncid, d_id, '_FillValue', NF_INT2, 1, int2va
    1l)
     call check_err(iret)
     int2val(1) = -32767
     iret = nf_put_att_int(ncid, d_id, 'missing_value', NF_INT2, 1, int
    12val)
     call check_err(iret)
     iret = nf_put_att_text(ncid, d_id, 'units', 5, 's**-1')
     call check_err(iret)
     iret = nf_put_att_text(ncid, d_id, 'long_name', 10, 'Divergence')
     call check_err(iret)
     iret = nf_put_att_text(ncid, NF_GLOBAL, 'Conventions', 6, 'CF-1.0'
    1)
     call check_err(iret)
     iret = nf_put_att_text(ncid, NF_GLOBAL, 'history', 43, '2006-12-03
    1 12:42:05 GMT by mars2netcdf-0.92')
     call check_err(iret)
* leave define mode
     iret = nf_enddef(ncid)
     call check_err(iret)
* store longitude
     data longitude /0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, 22.5, 25,
    127.5, 30, 32.5, 35, 37.5, 40, 42.5, 45, 47.5, 50, 52.5, 55, 57.5,
    260, 62.5, 65, 67.5, 70, 72.5, 75, 77.5, 80, 82.5, 85, 87.5, 90, 92
    3.5, 95, 97.5, 100, 102.5, 105, 107.5, 110, 112.5, 115, 117.5, 120,
    4 122.5, 125, 127.5, 130, 132.5, 135, 137.5, 140, 142.5, 145, 147.5
    5, 150, 152.5, 155, 157.5, 160, 162.5, 165, 167.5, 170, 172.5, 175,
    6 177.5, 180, 182.5, 185, 187.5, 190, 192.5, 195, 197.5, 200, 202.5
    7, 205, 207.5, 210, 212.5, 215, 217.5, 220, 222.5, 225, 227.5, 230,
    8 232.5, 235, 237.5, 240, 242.5, 245, 247.5, 250, 252.5, 255, 257.5
    9, 260, 262.5, 265, 267.5, 270, 272.5, 275, 277.5, 280, 282.5, 285,
    + 287.5, 290, 292.5, 295, 297.5, 300, 302.5, 305, 307.5, 310, 312.5
    1, 315, 317.5, 320, 322.5, 325, 327.5, 330, 332.5, 335, 337.5, 340,
    2 342.5, 345, 347.5, 350, 352.5, 355, 357.5/
     iret = nf_put_var_real(ncid, longitude_id, longitude)
     call check_err(iret)
* store latitude
     data latitude /90, 87.5, 85, 82.5, 80, 77.5, 75, 72.5, 70, 67.5, 6
    15, 62.5, 60, 57.5, 55, 52.5, 50, 47.5, 45, 42.5, 40, 37.5, 35, 32.
    25, 30, 27.5, 25, 22.5, 20, 17.5, 15, 12.5, 10, 7.5, 5, 2.5, 0, -2.
    35, -5, -7.5, -10, -12.5, -15, -17.5, -20, -22.5, -25, -27.5, -30,
    4-32.5, -35, -37.5, -40, -42.5, -45, -47.5, -50, -52.5, -55, -57.5,
    5 -60, -62.5, -65, -67.5, -70, -72.5, -75, -77.5, -80, -82.5, -85,
    6-87.5, -90/
     iret = nf_put_var_real(ncid, latitude_id, latitude)
     call check_err(iret)
* store levelist
     data levelist /1, 2, 3, 5, 7, 10, 20, 30, 50, 70, 100, 150, 200, 2
    150, 300, 400, 500, 600, 700, 775, 850, 925, 1000/
     iret = nf_put_var_int(ncid, levelist_id, levelist)
     call check_err(iret)
* store time
     data time /666192, 666216, 666240, 666264, 666288, 666312, 666336,
    1666360, 666384, 666408, 666432, 666456, 666480, 666504, 666528,
    2666552, 666576, 666600, 666624, 666648, 666672, 666696, 666720,
    3666744, 666768, 666792, 666816, 666840, 666864, 666888, 666912,
    4666936, 666960, 666984, 667008, 667032, 667056, 667080, 667104,
    5667128, 667152, 667176, 667200, 667224, 667248, 667272, 667296,
    6667320, 667344, 667368, 667392, 667416, 667440, 667464, 667488,
    7667512, 667536, 667560, 667584, 667608, 667632, 667656, 667680,
    8667704, 667728, 667752, 667776, 667800, 667824, 667848, 667872,
    9667896, 667920, 667944, 667968, 667992, 668016, 668040, 668064,
    +668088, 668112, 668136, 668160, 668184, 668208, 668232, 668256,
    1668280, 668304, 668328, 668352, 668376, 668400, 668424, 668448,
    2668472, 668496, 668520, 668544, 668568, 668592, 668616, 668640,
    3668664, 668688, 668712, 668736, 668760, 668784, 668808, 668832,
    4668856, 668880, 668904, 668928, 668952, 668976, 669000, 669024,
    5669048, 669072, 669096, 669120, 669144, 669168, 669192, 669216,
    6669240, 669264, 669288, 669312, 669336, 669360, 669384, 669408,
    7669432, 669456, 669480, 669504, 669528, 669552, 669576, 669600,
    8669624, 669648, 669672, 669696, 669720, 669744, 669768, 669792,
    9669816, 669840, 669864, 669888, 669912, 669936, 669960, 669984,
    +670008, 670032, 670056, 670080, 670104, 670128, 670152, 670176,
    1670200, 670224, 670248, 670272, 670296, 670320, 670344, 670368,
    2670392, 670416, 670440, 670464, 670488, 670512, 670536, 670560,
    3670584, 670608, 670632, 670656, 670680, 670704, 670728, 670752,
    4670776, 670800, 670824, 670848, 670872, 670896, 670920, 670944,
    5670968, 670992, 671016, 671040, 671064, 671088, 671112, 671136,
    6671160, 671184, 671208, 671232, 671256, 671280, 671304, 671328,
    7671352, 671376, 671400, 671424, 671448, 671472, 671496, 671520,
    8671544, 671568, 671592, 671616, 671640, 671664, 671688, 671712,
    9671736, 671760, 671784, 671808, 671832, 671856, 671880, 671904,
    +671928, 671952, 671976, 672000, 672024, 672048, 672072, 672096,
    1672120, 672144, 672168, 672192, 672216, 672240, 672264, 672288,
    2672312, 672336, 672360, 672384, 672408, 672432, 672456, 672480,
    3672504, 672528, 672552, 672576, 672600, 672624, 672648, 672672,
    4672696, 672720, 672744, 672768, 672792, 672816, 672840, 672864,
    5672888, 672912, 672936, 672960, 672984, 673008, 673032, 673056,
    6673080, 673104, 673128, 673152, 673176, 673200, 673224, 673248,
    8673272, 673296, 673320, 673344, 673368, 673392, 673416, 673440,
    9673464, 673488, 673512, 673536, 673560, 673584, 673608, 673632,
    +673656, 673680, 673704, 673728, 673752, 673776, 673800, 673824,
    1673848, 673872, 673896, 673920, 673944, 673968, 673992, 674016,
    2674040, 674064, 674088, 674112, 674136, 674160, 674184, 674208,
    3674232, 674256, 674280, 674304, 674328, 674352, 674376, 674400,
    4674424, 674448, 674472, 674496, 674520, 674544, 674568, 674592,
    5674616, 674640, 674664, 674688, 674712, 674736, 674760, 674784,
    6674808, 674832, 674856, 674880, 674904, 674928, 674952/

     iret = nf_put_var_int(ncid, time_id, time)
     call check_err(iret)

* store div
     status=nf_put_vara_int2(ncid, d_id,start,count,div)
     print*,'put div ok!'
*close dataset
     status = nf_close ( ncid )
     call check_err( iret )

     end

     SUBROUTINE check_err(status)
     include 'netcdf.inc'
     INTEGER status
     IF (STATUS .NE. NF_NOERR) THEN
     PRINT *, NF_STRERROR(status)
     STOP 'Stopped'
     ENDIF
     END


先谢谢
回复
分享到:

使用道具 举报

发表于 2008-1-18 10:41 | 显示全部楼层
前面两个时次


是什么意思?
 楼主| 发表于 2008-1-18 11:32 | 显示全部楼层

回复 2楼 的帖子

tdef  366 linear 00z01Jan1976 1440mn
就是有366个时刻,当t=1,和t=2的时候,可以画出来(但是和原始数据画图比较,是错的)
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-17 17:35 , Processed in 0.090459 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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