I have multiple text files, each file is data for 1 day with 3 columns, 1 columns for longtitude, 1 columns for latitude and 1 columns for precip value.
It looks like that
108.650 13.950 0.000
108.450 13.383 0.000
105.833 22.150 0.000
106.217 21.300 -99.000
104.283 22.533 0.000
105.717 9.283 0.100
The value of longtitude and latitude is same for all file.
Now, I want to merge all file to 1 netcdf or text file with dimension is 455x455x365 (455 is the number of long and lat, 365 is precip value for each day of a year)
How can I do that?

댓글 수: 2

Geoff Hayes
Geoff Hayes 2019년 5월 21일
minh - do you have 365 files (one for each day of the year)? Does each file have 455 rows? Please clarify.
minh lan
minh lan 2019년 5월 21일
Geoff Hayes,
Yes, I have 365 files and each file has 455 rows and 3 columns,

댓글을 달려면 로그인하십시오.

 채택된 답변

KSSV
KSSV 2019년 5월 21일
편집: KSSV 2019년 5월 21일

1 개 추천

txtfiles = dir('*.txt') ;
ncfile = 'myfile.nc' ;
N = length(txtfiles) ;
nx = 455 ; ny = 455 ; % number of lat, lon
for i = 1:N
data = importdata(txtfiles(i).name) ;
lon = data(:,1) ;
lat = data(:,2) ;
p = data(:,3) ;
if i == 1
% Do interpolation
idx = boundary(lon,lat) ;
xi = linspace(min(lon),max(lon),nx) ;
yi = linspace(min(lat),max(lat),ny) ;
[X,Y] = meshgrid(xi,yi) ;
idx = inpolygon(X,Y,lon(idx),lat(idx)) ;
Z = griddata(lon,lat,p,X,Y) ;
Z(~idx) = NaN ;
% Longitude
nccreate(ncfile,'lon','Dimensions',{'lon',1,nx}) ;
ncwrite(ncfile,'lon',xi) ;
ncwriteatt(ncfile,'lon','long_name','longitude');
% Latitude
nccreate(ncfile,'lat','Dimensions',{'lat', 1, ny})
ncwrite(ncfile,'lat',yi)
ncwriteatt(ncfile,'lat','long_name','latitude');
% Precipitation
nccreate(ncfile,'time','Dimensions',{'time',1,Inf}) ;
nccreate(ncfile,'p','Dimensions',{'lon',nx,'lat',ny,'time',Inf})
ncwriteatt(ncfile,'p','long_name','precipitation');
ncwrite(ncfile,'time',i,i) ;
ncwrite(ncfile,'p',Z,[1,1,i])
end
Z = griddata(lon,lat,p,X,Y) ;
Z(~idx) = NaN ;
ncwrite(ncfile,'time',i,i) ;
ncwrite(ncfile,'p',Z,[1,1,i])
end

댓글 수: 11

KSSV,
p = reshape(data(:,3),nx,ny) ;
why you need reshape here? And when I run code, It has error.
"To RESHAPE the number of elements must not change."
And I see nx, ny is the number and data(:,3) is matrix 455x1
KSSV
KSSV 2019년 5월 21일
As you said, you have 455 lats and lons.....I though your data is structured. Attach one file.
minh lan
minh lan 2019년 5월 21일
I attach one file of a day below. And I have 365 file like this
KSSV
KSSV 2019년 5월 21일
YOu data is a scatter data. YOu want to save it as scaatered data ot grid?
minh lan
minh lan 2019년 5월 21일
I want to save it as grid data.
KSSV
KSSV 2019년 5월 21일
Edited the code.
minh lan
minh lan 2019년 5월 21일
I still have error:
'lon' exists in file. Can not modify existing variables.
How can I fix it?
KSSV
KSSV 2019년 5월 21일
YOu have to delete the previous nc file...if it exists.
minh lan
minh lan 2019년 5월 21일
Thank you so much.
minh lan
minh lan 2019년 5월 21일
One more question,
If I want to have scatter data to compare the different map between the scattered data and the data after interpolation?
How can I change your code?
Thank you.
KSSV
KSSV 2019년 5월 21일
USe lon,lat,p for scattered data.....use X,Y,Z for grid data.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

태그

질문:

2019년 5월 20일

댓글:

2019년 5월 21일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by