% Extract ENVISAT waveforms
%
clc;
close all;
format long;
%
% Program: Extract MWS data(waveforms)
% Waveforms are considered from 2007
% Give: Name of input(.N1 and .hdf) - and output files
%
% Read 18 Hz Waveform MDS from ENVISAT
% Open and read ('r') N1 data file
fid = fopen('N1_format/RA2_MWS_2POF-P20071022_215428_00003017A062_00402_29513_0828.N1','r');
% Determine the offset to be skipped so that focus is on waveforms
% Offset : Size of MPH, SPH, RA-2 and Wm
% MPH
MPH_SIZE = 1247;
offset_num_datasets = 1247-143;
% Go to point of data of interest with respect to an offset relative to origin
% ' bof' = beginning of file
status = fseek(fid,offset_num_datasets,'bof');
% SPH
% Read the number of elements specified by size (9) ; the data
% format is specified by precision ( ' uchar ')
% ' uchar ' = Data type specifier : Unsigned integer, 8 bit
s_sph_size=fread(fid,9,'uchar','b');
% ... and skip argument. 'b' specifies the number of bytes to skip after
% each precision value is read
v_sph_size=fread(fid,11,'uchar','b');
% Convert v_sph_size to character string and convert it into integers
% of field width 10£¬i . e. read data from the MATLAB string variable
% and convert it according to the specified format string . Format is
% a string specifying the format of the data to be read
SPH_SIZE=sscanf(char(v_sph_size(2:ll)),'%l0');
space=fread(fid, 8,'uchar','b');
% Find NUM_DSD in field 38 in MPH
s_num_dsd=fread(fid,8,'uchar','b');
v_num_dsd=fread(fid,11,'uchar','b');
NUM_DSD=sscanf(char(v_num_dsd(2:11)),'%10d');
space=fread(fid,1,'uchar1, 'b');
% Find DSD_SIZE in field 39 in MPH
s_dsd_size=fread(fid,9,'uchar','b');
v_dsd_size=fread(fid,11,'uchar','b');
DSD_SIZE=sscanf (char(v_dsd_size(2:ll)),'%lOd');
space=fread(fid,8,'uchar','b');
% Give the number of data sets
% Find DSD_SIZE in field 40 in MPH
% Should equal three : RA¡ª2£¬MWRj 18 Hz Waveforms
s_num_data_sets=fread(fid,14,'uchar','b');
v_num_data_sets=fread(fid,11,'uchar','b');
NUM_DATA_SETS=sscanf(char(v_num_data_sets(2:ll)),'%10d');
space=fread(fid,42,'uchar','b');
clear offset_num_datasets s_sph_size v_sph_size s_num_dsd
clear s_dsd_size v_dsd_size
clear v_num_dsd s_num_data_sets v_num_data_sets
% Read offset and LAT + LON
offset_dsd_lat_long = MPH_SIZE + SPH_SIZE - (DSD_SIZE*50);
offset_ds_offset = offset_dsd_lat_long + 123;
status = fseek(fid, offset_ds_offset,'bof');
s_ds_off_set_2 = fread(fid,10,'uchar','b');
v_ds_off_set_2 = fread(fid,21,'uchar','b');
DS2_OFFSET = sscanf(char(v_ds_offset_2(2:21)),'%20d');
space = fread(fid,8,'uchar','b');
s_ds_size_2 = fread(fid,8,' uchar','b');
v_ds_off_set_2 = fread(fid,21,'uchar','b');
DS2_SIZE = sscanf(char(v_ds_offset_2(2:21)),'%20d');
space = fread(fid,8,'uchar','b');
s_num_dsr_2 =fread(fid,8,'uchar','b');
v_num_dsr_2 = fread(fid,11,'uchar','b');
NUM_DSR2 =sscanf (char(v_num_dsr_2(2:11)),'%20d');
space = fread(fid,1,'uchar','b');
s_dsr_size_2 = fread(fid,9,'uchar','b');
v_dsr_size_2 = fread(fid,11,'uchar','b');
DSR2_SIZE=sscanf (char(v_dsr_size_2(2:ll)),'%20d');
space =fread(fid,41,'uchar','b');
clear offset_ds_offset off set_dsd_lat_long
clear offset_num_datasets s_ds_offset_2 v_ds_offset_2
clear s_ds_size_2 v_ds_offset_2 s_num_dsr_2 v_num_dsr_2
clear s_dsr_size_2 v_dsr_size_2
% Find the waveform [FFT]
% Size of wave = Amount of data points * Amount of observations
% = 2709 * 128
% = 346752
status = fseek(fid, DS2_0FFSET + 28,'bof');
wave = fread(fid, 128 * NUM_DSR2, '128*short', DSR2_SIZE - 256,'b');
% Read HDF file for same N1 file so that the coordinates corresponding to
% the waveforms can be found
% Input file
file_name_GDR = 'Elevations_HDF/20071022_62_29513.hdf';
% Read subsets of the RA2 level 2 dataset
GDR_data = hdfread(file_name_GDR, 'RA2_DATA_SET_F0R_LEVEL_21','Fields',{'Record Index','lat','lon'});
% Extract information: Record index coordinates
% Change format of data from uintl6 to double p
record_index = GDR_data{1 ,1}';
record_index = double(record_index);
% Positions
LAT =GDR_data{2 ,1} ;% [deg]
LAT =double(LAT);
LON =GDR_data{3,1} ;% [deg]
LON =double(LON);
% Find the coordinates in the observed area
eof = length(LAT);
j = 0;
for i = 1:eof
if ((LAT(i)>= 68.5) && (LAT(i) <=72.5)) && ((LON(i)>= -38 && (LON(i)<=-32))
j = j + 1;
LAT_corr(j,1) = LAT(i);
LON_corr(j,1) = LON(i);
iterations(j,1) = i;
end
end
% Find the waveforms of interest given by the iteration number
% Create array,waveforms_res, which hold X data points in units [FFT]:
% X = amount_data_wavef * length(iterations)
% = 128 * 64
% = 8192
amount_data_wavef = 128;
k = 0;
eof = length(wave);
for m = 1:eof
if (m>=iteration(1) && m<= iterations(end))
for n = (amount_data_wavef * (m-1)+1):(amount_data_wavef * m)
k = k + 1;
waveforms_res(k,1) = wave(m);
end
end
end
% make new iteration array with the same size as waveforms_res
for i = 1:length(iterations)
for j = 1:128
iterations_res(j,i) = iterations(i);
end
end
iterations_res = reshape(iterations_res,[size(iterations_res,1)*size(iterations_res,2) 1]);
clear i j k m n status space fid
% Save data files
datvar_wvf = [iterations_res,waveforms_res];
fout = sprintf('Waveforms/New_100510/wvf_20071022_62_29513_new.txt',1);
save(fout,'datvar_wvf','-ascii');
评论0