-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added simple scripts to make masks and calculate percentage of missin…
…g data.
- Loading branch information
1 parent
872810a
commit 0befffd
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
""" | ||
@author: Tammas Loughran | ||
""" | ||
import netCDF4 as nc | ||
import numpy as np | ||
# Load file | ||
ncfile = nc.Dataset('HadGHCND_TXTN_1950-2014.nc','r') | ||
data = ncfile.variables['tmax'][:] | ||
# Make mask where data exists at any point in time | ||
mask = np.array(data.any(axis=0).astype(int)) | ||
# Save to mask.nc | ||
maskfile = nc.Dataset('mask.nc','w') | ||
maskfile.createDimension('latitude', size=mask.shape[0]) | ||
maskfile.createDimension('longitude', size=mask.shape[1]) | ||
latsvar = maskfile.createVariable('latitude', float, dimensions=('latitude')) | ||
lonsvar = maskfile.createVariable('longitude', float, dimensions=('longitude')) | ||
maskvar = maskfile.createVariable('mask', int, dimensions=('latitude','longitude')) | ||
setattr(latsvar, 'standard_name', 'latitude') | ||
setattr(latsvar, 'units', 'degrees_north') | ||
setattr(latsvar, 'axis', 'Y') | ||
setattr(lonsvar, 'standard_name', 'longitude') | ||
setattr(lonsvar, 'units', 'degrees_east') | ||
setattr(lonsvar, 'axis', 'X') | ||
latsvar[:] = ncfile.variables['latitude'][:] | ||
lonsvar[:] = ncfile.variables['longitude'][:] | ||
maskvar[:] = mask | ||
ncfile.close() | ||
maskfile.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Fri Jun 1 16:32:54 2018 | ||
@author: Tammas Loughran | ||
""" | ||
import numpy as np | ||
import netCDF4 as nc | ||
import pandas as pd | ||
import datetime as dt | ||
ncfile = nc.Dataset('HadGHCND_TXTN_1950-2014.nc','r') | ||
data = ncfile.variables['tmax'][:] | ||
if type(data)!=np.ma.core.MaskedArray: data = np.ma.array(data, mask=np.isnan(data)) | ||
data = np.logical_not(data.mask) | ||
time = ncfile.variables['time'][:] | ||
dayone = dt.datetime(int(str(time[0])[:4]),int(str(time[0])[4:6]),int(str(time[0])[6:8])) | ||
daylast = dt.datetime(int(str(time[-1])[:4]),int(str(time[-1])[4:6]),int(str(time[-1])[6:8])) | ||
dates = pd.date_range(dayone,daylast) | ||
percent = np.ones((daylast.year-dayone.year+1, data.shape[1], data.shape[2]))*np.nan | ||
for i,year in enumerate(range(dayone.year,daylast.year+1)): | ||
year_index = (dates.year==year)&((dates.month==11)|(dates.month==12)|(dates.month==1)|(dates.month==2)|(dates.month==13)) | ||
percent[i,...] = 100*data[year_index,...].sum(axis=0)/year_index.sum() | ||
outfile = nc.Dataset('percent.nc','w') | ||
outfile.createDimension('time', size=percent.shape[0]) | ||
outfile.createDimension('latitude',size=ncfile.dimensions['latitude'].size) | ||
outfile.createDimension('longitude',size=ncfile.dimensions['longitude'].size) | ||
timevar = outfile.createVariable('time', float, dimensions=('time')) | ||
latsvar = outfile.createVariable('latitude', float, dimensions=('latitude')) | ||
lonsvar = outfile.createVariable('longitude', float, dimensions=('longitude')) | ||
percentvar = outfile.createVariable('percent', float, dimensions=('time','latitude','longitude')) | ||
setattr(timevar, 'units', 'years since 1949-01-01') | ||
setattr(latsvar, 'standard_name', 'latitude') | ||
setattr(latsvar, 'units', 'degrees_north') | ||
setattr(latsvar, 'axis', 'Y') | ||
setattr(lonsvar, 'standard_name', 'longitude') | ||
setattr(lonsvar, 'units', 'degrees_east') | ||
setattr(lonsvar, 'axis', 'X') | ||
timevar[:] = np.array(range(dayone.year, daylast.year+1)) - dayone.year | ||
latsvar[:] = ncfile.variables['latitude'][:] | ||
lonsvar[:] = ncfile.variables['longitude'][:] | ||
percentvar[:] = percent | ||
outfile.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.1.4 | ||
1.1.5 |