clear all; close all; clc
% Read SPC storm report file, grid data and convert to report days

%regions_boundary = (25 50 -125 -65);

data = importdata('/export/cpc-lw-kharnos/kirstin.harnos/SSW/TOR/1950-2017_torn.csv');
yyyy = 2017;

year  = str2double(data.textdata(:,2));
month = str2double(data.textdata(:,3));
day   = str2double(data.textdata(:,4));

size = data.data(:,3);
lat  = data.data(:,8);
lon  = data.data(:,9);

% Filter to 1950-present, MAM, and CONUS
year  = year(year>=1950 & year<=yyyy);
month = month(year>=1950 & year<=yyyy);
day   = day(year>=1950 & year<=yyyy);
size  = size(year>=1950 & year<=yyyy);
lat   = lat(year>=1950 & year<=yyyy);
lon   = lon(year>=1950 & year<=yyyy);

year  = year(month==3 | month==4 | month==5);
month = month(month==3 | month==4 | month==5);
day   = day(month==3 | month==4 | month==5);
size  = size(month==3 | month==4 | month==5);
lat   = lat(month==3 | month==4 | month==5);
lon   = lon(month==3 | month==4 | month==5);

year = year(lat<50 & lat>25);
month = month(lat<50 & lat>25);
day   = day(lat<50 & lat>25);
size  = size(lat<50 & lat>25);
lat   = lat(lat<50 & lat>25);
lon   = lon(lat<50 & lat>25);

year = year(lon<-66 & lon>-125);
month = month(lon<-66 & lon>-125);
day   = day(lon<-66 & lon>-125);
size  = size(lon<-66 & lon>-125);
lat   = lat(lon<-66 & lon>-125);
lon   = lon(lon<-66 & lon>-125);

%round lat and lon to nearest degree
lat = round(lat);
lon = round(lon);

% Filter to size
year = year(size>=1);
month = month(size>=1);
day   = day(size>=1);
size  = size(size>=1);
lat   = lat(size>=1);
lon   = lon(size>=1);


%Create multidimensional array
dummy = [year, month, day, lat, lon, size];

dum = unique(dummy,'rows');

unique_year  = dum(:,1);
unique_month = dum(:,2);
unique_lat   = dum(:,4);
unique_lon   = dum(:,5);
unique_size  = dum(:,6);

% Write results into netCDF
%Create File
ncid = netcdf.create('Unique_tornado_days_1950-2017.nc','NC_WRITE');

%Define variable dimensions
dimIndex = netcdf.defDim(ncid,'idx_1',length(unique_year));

%Define Variable
year_data  = netcdf.defVar(ncid,'unique_year','int',[dimIndex]);
month_data = netcdf.defVar(ncid,'unique_month','int',[dimIndex]);
lat_data   = netcdf.defVar(ncid,'unique_lat','int',[dimIndex]);
lon_data   = netcdf.defVar(ncid,'unique_lon','int',[dimIndex]);
size_data  = netcdf.defVar(ncid,'unique_size','int',[dimIndex]);
netcdf.endDef(ncid); %End defining

%Put variable into file
netcdf.putVar(ncid,year_data,unique_year);
netcdf.putVar(ncid,month_data,unique_month);
netcdf.putVar(ncid,lat_data,unique_lat);
netcdf.putVar(ncid,lon_data,unique_lon);
netcdf.putVar(ncid,size_data,unique_size);
netcdf.close(ncid);  %Close file




