Skip to content

Commit

Permalink
added folder input argument for cookie cutter
Browse files Browse the repository at this point in the history
  • Loading branch information
domeniconappo committed Jun 27, 2019
1 parent 288d66f commit 66a1679
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 6 deletions.
27 changes: 21 additions & 6 deletions cookie-cutter/nc_cookie_cutter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import os
import sys
import glob

import xarray as xr
import numpy as np
Expand All @@ -14,15 +15,23 @@ def main():
parser.add_argument("-m", "--mask", help='mask file (cookie-cutter), .map if pcraster, .nc if netcdf', required=True)
parser.add_argument("-l", "--list",
help='list of files to be cut, in a text file, one file per line, '
'main variable i.e. pr/e0/tx/tavg must be last variable in the input file', required=True)
'main variable i.e. pr/e0/tx/tavg must be last variable in the input file', required=False)
parser.add_argument("-f", "--folder", help='Directory with netCDF files to be cut', required=False)
parser.add_argument("-o", "--outpath", help='path where to save cut files', required=True)
args = parser.parse_args()

filelist = args.list
mask = args.mask
pathout = args.outpath
input_folder = args.folder
print('File list{} Mask: {} Output: {} Input: {}'.format(filelist, mask, pathout, input_folder))
if filelist:
list_to_cut = open(filelist).readlines()
elif input_folder:
list_to_cut = glob.glob(os.path.join(input_folder, '*.nc'))
else:
raise ValueError('You must issue either list or folder input arguments.')

list_to_cut = open(filelist).readlines()
maskname, ext = os.path.splitext(mask)

if ext == '.map':
Expand All @@ -46,11 +55,11 @@ def main():
x_max = np.max(mask_filter[0])
y_min = np.min(mask_filter[1])
y_max = np.max(mask_filter[1])

print('{} {} {} {}'.format(x_min, x_max, y_min, y_max))
# walk through list_to_cut
for f in list_to_cut:
fileout = os.path.join(pathout, f)
if os.path.isfile(fileout):
fileout = os.path.join(pathout, os.path.basename(f))
if os.path.isfile(fileout) and os.path.exists(fileout):
print(fileout, 'Alreay existing, are you sure you are not swiping original file with cut version? this file will not be overwrote ')
continue
filename, ext = os.path.splitext(f)
Expand All @@ -62,12 +71,18 @@ def main():
except: # file has no time component
nc = xr.open_dataset(f)

var = nc.variables.items()[-1][0]
var = list(nc.variables.items())[-1][0]
if 'lat' in nc.variables:
nc.variables[var].attrs['esri_pe_string'] ='GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.0174532925199433]]"'
else:
nc.variables[var].attrs['esri_pe_string'] ='PROJCS["ETRS_1989_LAEA",GEOGCS["GCS_ETRS_1989",DATUM["D_ETRS_1989",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Azimuthal_Equal_Area"],PARAMETER["False_Easting",4321000.0],PARAMETER["False_Northing",3210000.0],PARAMETER["Central_Meridian",10.0],PARAMETER["Latitude_Of_Origin",52.0],UNIT["Meter",1.0]]'
nc.variables[var].attrs['proj4_params'] = "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs"
if 'time' in nc.variables:
sliced_var = nc[var][:, x_min:x_max+1, y_min:y_max+1]
else:
sliced_var = nc[var][x_min:x_max+1, y_min:y_max+1]

print('Creating ', fileout)
sliced_var.to_netcdf(fileout)
nc.close()

Expand Down
2 changes: 2 additions & 0 deletions cookie-cutter/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dask
xarray
pandas
numpy
netCDF4
13 changes: 13 additions & 0 deletions gfit/gfit2.r
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## Copyright 2019 European Union
##
## Licensed under the EUPL, Version 1.2 or as soon they will be approved by the European Commission subsequent versions of the EUPL (the "Licence");
##
## You may not use this work except in compliance with the Licence.
## You may obtain a copy of the Licence at:
##
## https://joinup.ec.europa.eu/sites/default/files/inline-files/EUPL%20v1_2%20EN(1).txt
##
## Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the Licence for the specific language governing permissions and limitations under the Licence.


##purpose: estimating return levels from a Gumbel extreme value distribution functions fit to annual maxima
##Created: Zuzanna, August 2015;
Expand Down
13 changes: 13 additions & 0 deletions pcr2nc/pcr2nc/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
"""
Copyright 2019 European Union
Licensed under the EUPL, Version 1.2 or as soon they will be approved by the European Commission subsequent versions of the EUPL (the "Licence");
You may not use this work except in compliance with the Licence.
You may obtain a copy of the Licence at:
https://joinup.ec.europa.eu/sites/default/files/inline-files/EUPL%20v1_2%20EN(1).txt
Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the Licence for the specific language governing permissions and limitations under the Licence.
pcr2nc
A tool that convert PCRaster maps to a netCDF4 mapstack CF-1.7
"""
Expand Down
13 changes: 13 additions & 0 deletions pcr2nc/pcr2nc/reader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
"""
Copyright 2019 European Union
Licensed under the EUPL, Version 1.2 or as soon they will be approved by the European Commission subsequent versions of the EUPL (the "Licence");
You may not use this work except in compliance with the Licence.
You may obtain a copy of the Licence at:
https://joinup.ec.europa.eu/sites/default/files/inline-files/EUPL%20v1_2%20EN(1).txt
Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the Licence for the specific language governing permissions and limitations under the Licence.
PCRaster related code. It contains classes representing a PCRaster map and the reader.
"""

Expand Down
14 changes: 14 additions & 0 deletions pcr2nc/pcr2nc/writer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
"""
Copyright 2019 European Union
Licensed under the EUPL, Version 1.2 or as soon they will be approved by the European Commission subsequent versions of the EUPL (the "Licence");
You may not use this work except in compliance with the Licence.
You may obtain a copy of the Licence at:
https://joinup.ec.europa.eu/sites/default/files/inline-files/EUPL%20v1_2%20EN(1).txt
Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the Licence for the specific language governing permissions and limitations under the Licence.
Module containing the code for the NetCDFWriter class
"""

import datetime
import time

Expand Down

0 comments on commit 66a1679

Please sign in to comment.