王国杰的博客分享 http://blog.sciencenet.cn/u/gwangcc Be Silly

博文

Read arcgrid daily rainfall from CMA into matlab and netcdf

已有 3848 次阅读 2013-12-29 18:12 |系统分类:科研笔记

# Linux

ls -a > filelist.txt

# matlab

clear;
fid = fopen('filelist.txt','r+');
nday = 18993; % number of files
rain=nan(72,128,19358);
rain=single(rain);
for iday = 1:nday
  %step 1; read one line
  file_name = fgetl(fid);  
  %step2 load arcgrid file
  eval(['arcgridread ' file_name ';'])
  r=ans;
  clear ans
  rain(:,:,iday)=single(r);
end
fclose(fid);
save rain_china_19612013.mat rain

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%% in Matlab
rain=permute(rain,[2 1 3]);

rain=flipdim(rain,2);
[mlon, mlat, mt]=size(rain);
lon=72.25:0.5:135.75;
lat=18.25:0.5:54;
time=1:mt;
ncid = netcdf.create('prep.nc', 'CLOBBER');
% Crear dimensiones
dimid_lon = netcdf.defDim(ncid,'longitude',mlon);
dimid_lat = netcdf.defDim(ncid,'latitude',mlat);
dimid_time = netcdf.defDim(ncid,'time',mt);
% Create variables
varid_lon = netcdf.defVar(ncid,'longitude','double',dimid_lon);
netcdf.putAtt(ncid,varid_lon,'long_name','Longitude')
netcdf.putAtt(ncid,varid_lon,'units','degrees_east')
%
varid_lat = netcdf.defVar(ncid,'latitude','double',dimid_lat);
netcdf.putAtt(ncid,varid_lat,'long_name','Latitude')
netcdf.putAtt(ncid,varid_lat,'units','degrees_north')
%
varid_time = netcdf.defVar(ncid,'time','double',dimid_time);
netcdf.putAtt(ncid,varid_time,'long_name','Time')
netcdf.putAtt(ncid,varid_time,'units','Days since 1961-01-01 00:00:00')
%
varid_prep = netcdf.defVar(ncid,'prep','double',[dimid_lon,dimid_lat,dimid_time]);
netcdf.putAtt(ncid,varid_prep,'long_name','Variable')
netcdf.putAtt(ncid,varid_prep,'units','mm')
netcdf.putAtt(ncid,varid_prep,'missing_value',-9999)
netcdf.endDef(ncid)
%
netcdf.putVar(ncid,varid_lon,lon);
netcdf.putVar(ncid,varid_lat,lat);
netcdf.putVar(ncid,varid_time,time);
netcdf.putVar(ncid,varid_prep,rain);
%
netcdf.close(ncid)






http://blog.sciencenet.cn/blog-569118-753960.html

上一篇:Instantaneous frequency (IF) estimation based on Two-step me
下一篇:Histogram with superimposed fitted normal density.

1 陈昌春

该博文允许注册用户评论 请点击登录 评论 (4 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2021-4-13 09:19

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部