Hi everybody,
I wrote a script that averages data from many netcdf files. Opening them with ncwrite out of the folder NCEP_Reanalysis works fine and all the calculations are excuted perfectly. However, trying to save the averaged data seems to be a problem. The names of the different properties I'm averaging are saved in the cell array properties and are accessed in the loop via the variable property.
Using the following code:
nccreate(sprintf('NCEP_Reanalysis\average.%s.nc',properties{property}),sprintf('%s',properties{property}),propNEW) nccreate(sprintf('NCEP_Reanalysis\average.%s.nc',properties{property}),'lon',LON) nccreate(sprintf('NCEP_Reanalysis\average.%s.nc',properties{property}),'lat',LAT) nccreate(sprintf('NCEP_Reanalysis\average.%s.nc',properties{property}),'level',LEVEL) nccreate(sprintf('NCEP_Reanalysis\average.%s.nc',properties{property}),'time',TIME)
returns the following error:
Error using internal.matlab.imagesci.nc/openToWrite (line 1176) Unable to open NCEP_Reanalysisverage.air.nc for writing.
Error in internal.matlab.imagesci.nc/openToAppend (line 1243) this.openToWrite();
Error in internal.matlab.imagesci.nc (line 123) this.openToAppend();
Error in nccreate (line 120) ncObj = internal.matlab.imagesci.nc(ncFile,'a', formatStr);
Error in averageNCEP (line 74) nccreate(sprintf('NCEP_Reanalysis\average.%s.nc',properties{property}),sprintf('%s',properties{property}),propNEW)
So I tried something similar using this code:
ncidout = netcdf.create(sprintf('NCEP_Reanalysis\average.%s.nc',properties{property}),'NETCDF4'); dimidlon = netcdf.defDim(ncidout,'lon',144); dimidlat = netcdf.defDim(ncidout,'lat',73); dimidlevel = netcdf.defDim(ncidout,'level',17); dimidtime = netcdf.defDim(ncidout,'time',1464); lon_ID=netcdf.defVar(ncidout,'lon','double',[dimidlon]); lat_ID=netcdf.defVar(ncidout,'lat','double',[dimidlat]); level_ID=netcdf.defVar(ncidout,'level','double',[dimidlevel]); time_ID=netcdf.defVar(ncidout,'time','double',[dimidtime]); prop_ID = netcdf.defVar(ncid,sprintf('%s',properties{property}),'double',[dimidlon dimidlat dimidlevel dimidtime]); netcdf.endDef(ncidout); netcdf.putVar(ncidout,lon_ID,LON); netcdf.putVar(ncidout,lat_ID,LAT); netcdf.putVar(ncidout,level_ID,LEVEL); netcdf.putVar(ncidout,time_ID,TIME); netcdf.putVar(ncidout,prop_ID,propNEW); netcdf.close(ncidout)
which gives me this error:
Error using netcdflib The NetCDF library encountered an error during execution of 'create' function – 'Permission denied (13)'.
Error in netcdf.create (line 53) ncid = netcdflib('create', filename, mode);
Error in averageNCEP (line 80) ncidout = netcdf.create(sprintf('NCEP_Reanalysis\average.%s.nc',properties{property}),'NETCDF4');
I tried removing read-only attributes of the folder NCEP_Reanalysis and all folders further up the hierarchy with the console and the command (Operating system: Windows 7):
attrib -r +s E:\VM_Ubuntu_16.04.1_shared_folder\NCEP_Reanalysis
without success. The error occurs in the first iterartion and everything before it works fine. I'm attaching the whole code anyway even so it's over a hundred lines just to give you the big picture:
%%Average the NCEP-Reanalysis Data from 1948 to 2015, does not work for different interval of years
LON = ncread('NCEP_Reanalysis\1948\air.1948.nc','lon'); LAT = ncread('NCEP_Reanalysis\1948\air.1948.nc','lat'); LEVEL = ncread('NCEP_Reanalysis\1948\air.1948.nc','level'); TIME = ncread('NCEP_Reanalysis\1948\air.1948.nc','time'); properties = {'air','hgt','omega','rhum','shum','uwind','vwind'}; propNEW = zeros(144,73,17,1464); nextpropertyFLAG = 1; % 1=next property, else = current property
LeapYearCompensator = zeros(144,73,17,4); for property = 1:length(properties) disp('++++++++++++++++++++++++++++++++++++') for year = 1948:2014 disp('******************************************************') fprintf('loading %s.%d.nc\n',properties{property},year) if nextpropertyFLAG == 1 prop1 = ncread(['NCEP_Reanalysis\' sprintf('%d',year) '\' sprintf('%s.%d.nc',properties{property},year)],sprintf('%s',properties{property})); else prop1 = propNEW; end fprintf('loading %s.%d.nc\n',properties{property},year+1) prop2 = ncread(['NCEP_Reanalysis\' sprintf('%d',year+1) '\' sprintf('%s.%d.nc',properties{property},year+1)],sprintf('%s',properties{property})); [d11,d12,d13,d14] = size(prop1); [d21,d22,d23,d24] = size(prop2); if d11 ~= d21 error(['longitutdes of do not match\n' 'lon of ' sprintf('%s.%d.nc',properties{property},year) ' = ' sprintf('%d',d11) '\n' 'lon of ' sprintf('%s.%d.nc',properties{property},year+1) ' = ' sprintf('%d',d21)]) end if d12 ~= d22 error(['latitudes of do not match\n' 'lat of ' sprintf('%s.%d.nc',properties{property},year) ' = ' sprintf('%d',d12) '\n' 'lat of ' sprintf('%s.%d.nc',properties{property},year+1) ' = ' sprintf('%d',d22)]) end if d13 ~= d23 error(['levels of do not match\n' 'levels of ' sprintf('%s.%d.nc',properties{property},year) ' = ' sprintf('%d',d13) '\n' 'levels of ' sprintf('%s.%d.nc',properties{property},year+1) ' = ' sprintf('%d',d23)]) end if d11 ~= 144 error('more than 144 longitude values -> preallocated array not big enough') end if d12 ~= 73 error('more than 73 latitude values -> preallocated array not big enough') end if d13 ~= 17 error('more than 17 levels -> preallocated array not big enough') end if d14 ~= 1460 && d14 ~= 1464 error(sprintf('invalid time steps in %s.%d.nc',properties{property},year)) end if d24 ~= 1460 && d24 ~= 1464 error(sprintf('invalid time steps in %s.%d.nc',properties{property},year+1)) end if d14 == 1460 % prop1 is data contained in a leap year
prop1 = cat(4,prop1(:,:,:,1:(31+28)*4),LeapYearCompensator,prop1(:,:,:,((31+28)*4+1):1460)); % complement the extra day with values of 0
elseif d24 == 1460 prop2 = cat(4,prop2(:,:,:,1:(31+28)*4),LeapYearCompensator,prop2(:,:,:,((31+28)*4+1):1460)); % complement the extra day with values of 0 end for time = 1:1464 if mod(time,4) == 0 fprintf('finished\t property:\t %s,\t years:\t %d\t and\t %d,\t day:\t %d\n',properties{property},year,year+1,time/4) end for lon = 1:144 for lat = 1:73 for level = 1:17 propNEW(lon,lat,level,time) = (prop1(lon,lat,level,time)+prop2(lon,lat,level,time))/2; end end end end % Save average property
fprintf('saving average.%s.nc\n',properties{property})% nccreate(sprintf('NCEP_Reanalysis\average.%s.nc',properties{property}),sprintf('%s',properties{property}),propNEW)
% nccreate(sprintf('NCEP_Reanalysis\average.%s.nc',properties{property}),'lon',LON)
% nccreate(sprintf('NCEP_Reanalysis\average.%s.nc',properties{property}),'lat',LAT)
% nccreate(sprintf('NCEP_Reanalysis\average.%s.nc',properties{property}),'level',LEVEL)
% nccreate(sprintf('NCEP_Reanalysis\average.%s.nc',properties{property}),'time',TIME)
ncidout = netcdf.create(sprintf('NCEP_Reanalysis\average.%s.nc',properties{property}),'NETCDF4'); dimidlon = netcdf.defDim(ncidout,'lon',144); dimidlat = netcdf.defDim(ncidout,'lat',73); dimidlevel = netcdf.defDim(ncidout,'level',17); dimidtime = netcdf.defDim(ncidout,'time',1464); lon_ID=netcdf.defVar(ncidout,'lon','double',[dimidlon]); lat_ID=netcdf.defVar(ncidout,'lat','double',[dimidlat]); level_ID=netcdf.defVar(ncidout,'level','double',[dimidlevel]); time_ID=netcdf.defVar(ncidout,'time','double',[dimidtime]); prop_ID = netcdf.defVar(ncid,sprintf('%s',properties{property}),'double',[dimidlon dimidlat dimidlevel dimidtime]); netcdf.endDef(ncidout); netcdf.putVar(ncidout,lon_ID,LON); netcdf.putVar(ncidout,lat_ID,LAT); netcdf.putVar(ncidout,level_ID,LEVEL); netcdf.putVar(ncidout,time_ID,TIME); netcdf.putVar(ncidout,prop_ID,propNEW); netcdf.close(ncidout) nextpropertyFLAG = nextpropertyFLAG + 1; disp('******************************************************') end nextpropertyFLAG = 1; disp('++++++++++++++++++++++++++++++++++++')end
Any ideas on how to fix this?
Best Answer