Hi Matlab's Community,
I'm a new user of Matlab. I'm practicing my program, but I got the error:
Error using fclose
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in nc_interface (line 98)
fclose(fid);
Error in nc_getatt (line 61)
[method,~,~] = nc_interface(ncfile);
Error in nc_inq (line 69)
A = nc_getatt(ncfile); % NetCDF file global attributes
Error in get_roms_grid (line 98)
Ginfo = nc_inq(Ginp);
Error in coarse2fine (line 59)
C = get_roms_grid(Ginp);
Error in wrf2roms_mw_ref (line 26)
coarse2fine(ParROMSFile,theROMSFile,eval(num2str(pgratio)),eval(num2str(pistart)),eval(num2str(piend)),eval(num2str(pjstart)),eval(n str(pjend)))
Error in generate_roms_grid (line 123)
[rho,projection] =
wrf2roms_mw_ref(theWRFFile,theROMSFile,rlim,npass,order,mini_depth,bathy_source,mod_dom,corr_ocean_mask,num_passes)
Error in run (line 86)
evalin('caller', [script ';']);
Please help me to resolve this problem. I'm really appreciated that.
I'm really sorry because it's quite specific.
Thanks all.

 採用された回答

Walter Roberson
Walter Roberson 2021 年 12 月 17 日

0 投票

if (ne(fid,-1)),
signature = fread(fid,8,'uint8=>uint8');
if (strcmp(char(signature(1:3))', 'CDF')),
if (signature(4) == 1)
ftype = 'NetCDF'; % NetCDF classic
elseif (signature(4) == 2)
ftype = 'NetCDF'; % NetCDF 64bit offset
end
elseif (strcmp(char(signature(2:4))', 'HDF'))
ftype = 'NetCDF4'; % NetCDF4/HDF5
end
end
fclose(fid);
That code uses fclose() even if the fopen() failed.
In short, the file it is trying to open does not exist.

10 件のコメント

Manh Nguyen
Manh Nguyen 2021 年 12 月 17 日
Dear Mr. Walter,
Thank you for your fast reply. Can you kindly give me the way to avoid this error?
Walter Roberson
Walter Roberson 2021 年 12 月 17 日
if fid < 0
error('Failed to open file |%s|', ncfile);
end
signature = fread(fid,8,'uint8=>uint8');
if (strcmp(char(signature(1:3))', 'CDF')),
if (signature(4) == 1)
ftype = 'NetCDF'; % NetCDF classic
elseif (signature(4) == 2)
ftype = 'NetCDF'; % NetCDF 64bit offset
end
elseif (strcmp(char(signature(2:4))', 'HDF'))
ftype = 'NetCDF4'; % NetCDF4/HDF5
else
fclose(fid);
error('Unknown signature for file |%s|', ncfile);
end
fclose(fid);
Manh Nguyen
Manh Nguyen 2021 年 12 月 17 日
Dear Mr. Walter,
Thank you for your help.
After I alter the program according to your suggestion, the error don't appear again.
Thank you very much!
Manh Nguyen
Manh Nguyen 2021 年 12 月 18 日
Dear Mr. Walter,
Thank to your modified command, the error didn't appear again.
Can you kindly help me to debug my code? I can not know how to fix that.
>> run generate_roms_grid.m
ROMS domain 2 information is to be saved to ROMS_COAWST_grd2.nc
theWRFFile =
'wrfinput_d02'
Error using nc_interface (line 87)
Failed to open file |ROMS_COAWST_grd1.nc|
My idea is that I create the ROMS_COAWST_grd1.nc file from wrfinput_d01 then to ROMS_COAWST_grd2.nc from wrfinput_d02. But in the program, it create the 2nd grid before generating the 1st grid leading to this error.
Please help me to solve this problem. I'm really appreciated your help.
I attach all of scripts in the attachments and the wrfinput files are in this link (https://drive.google.com/file/d/1lTPkJyrVYwn8MHrSwXYA4SFVRPCXWxCP/view?usp=sharing)
Thank you again.
Walter Roberson
Walter Roberson 2021 年 12 月 19 日
編集済み: Walter Roberson 2021 年 12 月 19 日
The output you posted was for
mod_dom = 2;
but the code you posted was for
mod_dom = 1;
This makes a difference because the ROMS file name
fname=['ROMS_COAWST_grd',num2str(mod_dom),'.nc']; %Name of output ROMS model NetCDF file
would have been
ROMS_COAWST_grd2.nc
if mod_dom were 2, but the error message is as if fname were ROMS_COAWST_grd1.nc
You seem to have a conflict between your posted code and the messages you posted.
Manh Nguyen
Manh Nguyen 2021 年 12 月 20 日
Dear Mr. Walter.
I'm really sorry because there are many conflicts between the codes and the messages I posted. With mod_dom = 1 option, my code can work successfully. However, if I change mod_dom = 2, the program will get the error:
>> run generate_roms_grid.m
ROMS domain 2 information is to be saved to ROMS_COAWST_grd2.nc
theWRFFile =
'wrfinput_d02'
Error using nc_interface (line 87)
Failed to open file |ROMS_COAWST_grd1.nc|
Error in nc_getatt (line 61)
[method,~,~] = nc_interface(ncfile);
Error in nc_inq (line 69)
A = nc_getatt(ncfile); % NetCDF file global attributes
Error in get_roms_grid (line 98)
Ginfo = nc_inq(Ginp);
Error in coarse2fine (line 59)
C = get_roms_grid(Ginp);
Error in wrf2roms_mw_ref (line 26)
coarse2fine(ParROMSFile,theROMSFile,eval(num2str(pgratio)),eval(num2str(pistart)),eval(num2str(piend)),eval(num2str(pjstart)),eval(n str(pjend)))
Error in generate_roms_grid (line 126)
[rho,projection] =
wrf2roms_mw_ref(theWRFFile,theROMSFile,rlim,npass,order,mini_depth,bathy_source,mod_dom,corr_ocean_mask,num_passes)
Error in run (line 86)
evalin('caller', [script ';']);
This error reported that I did not have the ROMS_COAWST_grd1.nc file. My ideal when using these codes is that creates the 2 file ROMS_COAWST_grd1.nc and ROMS_COAWST_grd2.nc, in which the ROMS_COAWST_grd2.nc is generated after the ROMS_COAWST_grd1.nc file.
Can you kindly help me to address this issue?
Thank you very much.
Walter Roberson
Walter Roberson 2021 年 12 月 20 日
Error using internal.matlab.imagesci.nc/openToRead (line 1274)
Could not open wrfinput_d02_nicholls for reading.
I do not have any of your datasets to test with.
Manh Nguyen
Manh Nguyen 2021 年 12 月 20 日
編集済み: Walter Roberson 2021 年 12 月 20 日
The input datasets are on the Drive, please download them from this link ( https://drive.google.com/file/d/1lTPkJyrVYwn8MHrSwXYA4SFVRPCXWxCP/view?usp=sharing ). I'm so sorry about this inconvenience.
Thank you for your help.
Walter Roberson
Walter Roberson 2021 年 12 月 20 日
wrf2roms_mw_ref() reads the PARENT_ID attribute from the NC file you tell it to read, and uses that parent ID to form the name of the ROMS_COAWST_grd file to read. In this case, the parent ID stored in the file is 1, so it needs ROMS_COAWST_grd1.nc
But even if it used 2 instead of 1 there: coarse2fine() needs to be able to read from the file to get values needed to create the new ROMS file, so if it used 2 at that point, it would be trying to read from ROMS_COAWST_grd2.nc before that file had been created.
The work flow cannot be used to create a new initial ROMS file; it can be used to create a new ROMS file from a wrf file using a different ROMS file as a base
Manh Nguyen
Manh Nguyen 2021 年 12 月 20 日
Thank you for your help.
Although the codes can not run for my cases, I'm really appreciated your help.
I have another idea is that I use these codes to create the ROMS_COAWST_grd1.nc with mod_dom = 1 option then I change mod_dom = 2 to generate the ROMS_COAWST_grd2.nc file. I don't know whether it have any problem but I can run without problem.

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2021 年 12 月 17 日

0 投票

You need not to use fopen and fclose etc for reading ncfiles in MATLAB. Reading ncfiles is very simple in MATLAB. You just need to know two functions: ncinfo/ ncdisp and ncread. Read about these functions.

1 件のコメント

Manh Nguyen
Manh Nguyen 2021 年 12 月 17 日
Dear Dr. KSSV,
Thank you for your reply. Can you kindly help me with fopen and fclose command?
I'm really appreciated that.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Data Import and Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by