How to convert .NetCDF reterive from NCEP/NCAR to ASCII files of each month

8 visualizaciones (últimos 30 días)
I have air.mon.mean.nc file prepared by NCEP/NCAR. This file contain monthly temperature. I want to get temperature data in text file for each month since 1985 to 2015.
When i try ncdisp('air.mon.mean.nc')
I got
Source:
E:\meta data\new\data _thesis\NCEP\.Net files\air.mon.mean.nc
Format:
netcdf4_classic
Global Attributes:
description = ' Data from NCEP initialized reanalysis (4x/day). These are interpolated to pressure surfaces from model (sigma) surfaces.'
platform = 'Model'
Conventions = 'COARDS'
NCO = '20121012'
history = 'Mon Jul 5 21:45:36 1999: ncrcat air.mon.mean.nc /Datasets/ncep.reanalysis.derived/pressure/air.mon.mean.nc /dm/dmwork/nmc.rean.ingest/combinedMMs/air.mon.mean.nc
/home/hoop/crdc/cpreanjuke2farm/cpreanjuke2farm Tue Oct 17 20:07:08 1995 from air.85.nc
created 95/02/06 by Hoop (netCDF2.3)
Converted to chunked, deflated non-packed NetCDF4 2014/09'
title = 'monthly mean air from the NCEP Reanalysis'
References = 'http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanalysis.derived.html'
Dimensions:
level = 17
lat = 73
lon = 144
time = 818 (UNLIMITED)
Variables:
level
Size: 17x1
Dimensions: level
Datatype: single
Attributes:
units = 'millibar'
long_name = 'Level'
positive = 'down'
GRIB_id = 100
GRIB_name = 'hPa'
actual_range = [1.00e+03 1.00e+01]
axis = 'Z'
lat
Size: 73x1
Dimensions: lat
Datatype: single
Attributes:
units = 'degrees_north'
actual_range = [9.00e+01 -9.00e+01]
long_name = 'Latitude'
standard_name = 'latitude'
axis = 'Y'
lon
Size: 144x1
Dimensions: lon
Datatype: single
Attributes:
units = 'degrees_east'
long_name = 'Longitude'
actual_range = [0.00e+00 3.58e+02]
standard_name = 'longitude'
axis = 'X'
time
Size: 818x1
Dimensions: time
Datatype: double
Attributes:
long_name = 'Time'
delta_t = '0000-01-00 00:00:00'
avg_period = '0000-01-00 00:00:00'
prev_avg_period = '0000-00-01 00:00:00'
standard_name = 'time'
axis = 'T'
units = 'hours since 1800-01-01 00:00:0.0'
actual_range = [1.30e+06 1.89e+06]
air
Size: 144x73x17x818
Dimensions: lon,lat,level,time
Datatype: single
Attributes:
long_name = 'Monthly Mean of Air temperature'
units = 'degC'
precision = 2
least_significant_digit = 1
var_desc = 'Air Temperature'
level_desc = 'Multiple levels'
statistic = 'Mean'
parent_stat = 'Other'
missing_value = -9.97e+36
valid_range = [-2.00e+02 3.00e+02]
dataset = 'NCEP Reanalysis Derived Products'
actual_range = [-1.09e+02 4.32e+01]
>>
These are my inputs to dimensions in nc files.
time: 444(means for January 1985 and it will be 445 for February 1985 and soon on till 2015)
Level: 0:1:16 (remains same in each file)
Lat:21:1:28 (remains same in each file)
Lon:24:1:31 (remains same in each file)
Save these text files in name like jan, feb,march,aparil,.....dec and make a folder with name of each year like 1985, 1986,1987...2015 where this code save monthly text files of the same year.
At end i have 30 folders(from 1985 to 2015) and 12 text files exporting from nc file in each folder.
My nc file exceeding 5Mb upload size that why i am sharing link NCEP/NCAR where i download my air.mon.mean.nc file. and i am using Rmatlab2013b version of matlab.
output text files for 1985 has been attached remaining are just like of that. Please help!
  5 comentarios
Muhammad Usman Saleem
Muhammad Usman Saleem el 18 de Mzo. de 2016
I find this code, but not idea how can i use it for or how can i set input arguments according to my requirement?
function [ value pressure latitude longitude] = get_NCEP_NCAR(year,month,field)
%Field is the type of variable you want : geopotential_height,temperature,u,v,w
%Dimension of output value is (time,pressure_level,latitude,longitude)
folder='/where_the_netcdf_are/';
%List the files in selected folder
air_list=dir([folder,'air.',num2str(year,'%04i'),'*']);
hgt_list=dir([folder,'hgt.',num2str(year,'%04i'),'*']);
omega_list=dir([folder,'omega.',num2str(year,'%04i'),'*']);
uwnd_list=dir([folder,'uwnd.',num2str(year,'%04i'),'*']);
vwnd_list=dir([folder,'vwnd.',num2str(year,'%04i'),'*']);
if(strcmp(field,'temperature'))
filename=air_list.name;
filepath=[folder,filename];
ncid=netcdf.open(filepath,'NC_NOWRITE');
varid=netcdf.inqVarID(ncid,'air');
end
if(strcmp(field,'geopotential_height'))
filename=hgt_list.name;
filepath=[folder,filename];
ncid=netcdf.open(filepath,'NC_NOWRITE');varid=netcdf.inqVarID(ncid,'hgt');
end
if(strcmp(field,'w'))
filename=omega_list.name;
filepath=[folder,filename];
ncid=netcdf.open(filepath,'NC_NOWRITE');
varid=netcdf.inqVarID(ncid,'omega');
end
if(strcmp(field,'u'))
filename=uwnd_list.name;
filepath=[folder,filename];
ncid=netcdf.open(filepath,'NC_NOWRITE');
varid=netcdf.inqVarID(ncid,'uwnd');
end
if(strcmp(field,'v'))
filename=vwnd_list.name;
filepath=[folder,filename];
ncid=netcdf.open(filepath,'NC_NOWRITE');
varid=netcdf.inqVarID(ncid,'vwnd');
end
varidp=netcdf.inqVarID(ncid,'level');
varidlat=netcdf.inqVarID(ncid,'lat');
varidlon=netcdf.inqVarID(ncid,'lon');
varidt=netcdf.inqVarID(ncid,'time');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
value=netcdf.getVar(ncid,varid,'short');
scale = netcdf.getAtt(ncid,varid,'scale_factor');
offset= netcdf.getAtt(ncid,varid,'add_offset');
value=double(value)*scale+offset;
value=single(permute(value,[4 3 2 1]));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pressure=single(netcdf.getVar(ncid,varidp));
latitude=single(netcdf.getVar(ncid,varidlat));
longitude=single(netcdf.getVar(ncid,varidlon));
%to keep only the time asked for
time=netcdf.getVar(ncid,varidt);
dayssince111=time/24;
datevalue=dayssince111+datenum(1,1,1)-2;
mm=datevec(datevalue); mm=mm(:,2);
value=value(month==mm,:,:,:);
netcdf.close(ncid);
value(value>10^14)=nan;
%pads the data for the month with NAN if not all month is available yet
theoreticaltime=datenum(year,month,1):(datenum(year,month+1,1)-1);
ts=length(theoreticaltime)*4;
dummy=zeros([ts,length(pressure),length(latitude),length(longitude)]);
dummy=dummy*0/0;
dummy(1:size(value,1),:,:,:)=value;
value=dummy;
end
Muhammad Usman Saleem
Muhammad Usman Saleem el 18 de Mzo. de 2016
Editada: Muhammad Usman Saleem el 19 de Mzo. de 2016
I have one .nc file with name air.nc which contains temperature data. I am getting confusion in these line?
function [ value pressure latitude longitude] = get_NCEP_NCAR(year,month,field)
How can i set inputs to my requirements??
%Field is the type of variable you want : geopotential_height,temperature,u,v,w %Dimension of output value is (time,pressure_level,latitude,longitude)
folder='/where_the_netcdf_are/';
my air.nc file is in E:\meta data\new\data _thesis\NCEP\.Net files and this function is also save in the same folder. Tell me how can i arrange my nc files according to below code requirements?
%List the files in selected folder
air_list=dir([folder,'air.',num2str(year,'%04i'),'*']);
hgt_list=dir([folder,'hgt.',num2str(year,'%04i'),'*']);
omega_list=dir([folder,'omega.',num2str(year,'%04i'),'*']);
uwnd_list=dir([folder,'uwnd.',num2str(year,'%04i'),'*']);
vwnd_list=dir([folder,'vwnd.',num2str(year,'%04i'),'*']);
Please assist?

Iniciar sesión para comentar.

Respuesta aceptada

KSSV
KSSV el 15 de Mzo. de 2016
Reading nc file is very easy in MATLAB. Use ncdisp('air.mon.mean.nc') to view the contents of the file. ncdisp shows you variables, dimensions of the file. After looking into variables, you can read what ever the variable you want. To read longitude and latitude: lon = ncread('air.mon.mean.nc','Lon') ; % Lon is variable as given above lat = ncread('air.mon.mean.nc','Lat') ; % LAt is variable as given above
Like wise using ncread you can read all the variables. Once you have the data in workspace, you can write it into any format you want.
  8 comentarios
Muhammad Usman Saleem
Muhammad Usman Saleem el 15 de Mzo. de 2016
what you answered related to nc not with structure. Now i have structure in hand what to do when i make a call to latitude,lon,level,time. I am never experience with nc files so sending you more detail of structure variable created in my workspace
when i click on one of the structure variable, i got
when i click on dimension , i got
'level' 8 false
'lat' 73 false
'lon' 144 false
'time' 818 true
when i click on variable , i got
'level' 1x1 struct
8 'single' 1x7 struct
[] 9.9692100e+36 [] false
'lat' 1x1 struct
73 'single' 1x5 struct
[] 9.9692100e+36 [] false
'lon' 1x1 struct
144 'single' 1x5 struct
[] 9.9692100e+36 [] false
'time' 1x1 struct
818 'double' 1x8 struct
1 9.96920996838687e+36 [] false
'rhum' 1x4 struct
[144,73,8,818] 'single' 1x12 struct
[144,73,1,1] 9.9692100e+36 2 true
when i click on attributes, i got
'description' 'Data from NCEP initialized reanalysis (4x/day). These are interpolated to pressure surfaces from model (sigma) surfaces'
'platform' 'Model'
'Conventions' 'COARDS'
'NCO' '20121012'
'history' 'Thu May 4 18:11:03 2000: ncrcat -d time,0,622 /Datasets/ncep.reanalysis.derived/pressure/rhum.mon.mean.nc ./pressure/rhum.mon.mean.nc
Mon Jul 5 22:22:51 1999: ncrcat rhum.mon.mean.nc /Datasets/ncep.reanalysis.derived/pressure/rhum.mon.mean.nc /dm/dmwork/nmc.rean.ingest/combinedMMs/rhum.mon.mean.nc
created by Cathy Smith 12/5/95
(netCDF2.3)
Converted to chunked, deflated non-packed NetCDF4 2014/09'
'title' 'monthly mean rhum from the NCEP Reanalysis'
'References' 'http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanalysis.derived.html'
when i click on group, i got empty array
when i click on format i got
'netcdf4_classic'
Please read my question and my requirement and please then answer.
Muhammad Usman Saleem
Muhammad Usman Saleem el 17 de Mzo. de 2016
Dear Dr. Siva Srinivas Kolukula ;
I am trying this but last line is not running finely.
ncid1 = netcdf.open('air.mon.mean.nc','NC_NOWRITE');
level1 = netcdf.getVar(ncid1,0,1,16)
lat1 = netcdf.getVar(ncid1,1,21,28)
lon1 = netcdf.getVar(ncid1,2,24,31)
time1 = netcdf.getVar(ncid1,3,444,455)
other two lines are fine.
Error
Error using netcdf.open (line 44) Error using netcdflib The NetCDF library encountered an error during execution of 'getVaraDouble' function - 'Start+count exceeds dimension bound (NC_EEDGE)'.
Error in netcdf.getVar (line 136)
data = netcdflib(funcstr,ncid,varid,varargin{:});
Although when i check no of variables by using
[ndim, nvar, natt, unlim] = netcdf.inq(ncid1)
output
>> [ndim, nvar, natt, unlim] = netcdf.inq(ncid1)
ndim =
4
nvar =
5
natt =
7
unlim =
3

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by