climate-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From huiky...@apache.org
Subject [1/2] climate git commit: CLIMATE-692 - A new loader to read NLDAS data with a file list
Date Wed, 14 Oct 2015 06:48:54 GMT
Repository: climate
Updated Branches:
  refs/heads/master 4b37b125c -> 799fde64b


CLIMATE-692 - A new loader to read NLDAS data with a file list

- ocw.data_source.local.load_NLDAS_forcingA_files is added


Project: http://git-wip-us.apache.org/repos/asf/climate/repo
Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/14a86a6c
Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/14a86a6c
Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/14a86a6c

Branch: refs/heads/master
Commit: 14a86a6c47c562702b3760d7a10b7fb0b3713b35
Parents: 4b37b12
Author: huikyole <huikyole@argo.jpl.nasa.gov>
Authored: Tue Oct 13 16:40:56 2015 -0700
Committer: huikyole <huikyole@argo.jpl.nasa.gov>
Committed: Tue Oct 13 16:40:56 2015 -0700

----------------------------------------------------------------------
 ocw/data_source/local.py | 55 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/climate/blob/14a86a6c/ocw/data_source/local.py
----------------------------------------------------------------------
diff --git a/ocw/data_source/local.py b/ocw/data_source/local.py
index ff99aab..3560b99 100644
--- a/ocw/data_source/local.py
+++ b/ocw/data_source/local.py
@@ -477,3 +477,58 @@ def load_dataset_from_multiple_netcdf_files(file_list, variable_name,
     times = numpy.array(times)
     return Dataset(lats, lons, times, data_values, variable_name, name=name)
 
+def load_NLDAS_forcingA_files(file_path=None,
+                      filename_pattern=None,
+                      filelist=None,
+                      variable_name='APCPsfc_110_SFC_acc1h',
+                      name=''):
+    ''' Load multiple NLDAS2 forcingAWRF files containing 2D fields such as precipitation
and surface variables into a Dataset.
+    The dataset can be spatially subset.
+    :param file_path: Directory to the NetCDF file to load.
+    :type file_path: :mod:`string`
+    :param filename_pattern: Path to the NetCDF file to load.
+    :type filename_pattern: :list:`string`
+    :param filelist: A list of filenames
+    :type filelist: :list:`string`
+    :param variable_name: The variable name to load from the NetCDF file.
+    :type variable_name: :mod:`string`
+    :param name: (Optional) A name for the loaded dataset.
+    :type name: :mod:`string`
+    :returns: An OCW Dataset object with the requested variable's data from
+        the NetCDF file.
+    :rtype: :class:`dataset.Dataset`
+    :raises ValueError:
+    '''
+ 
+    if not filelist:
+        NLDAS_files = []
+        for pattern in filename_pattern:
+            NLDAS_files.extend(glob(file_path + pattern))
+    else:
+        NLDAS_files = [line.rstrip('\n') for line in open(filelist)]
+
+    NLDAS_files.sort()
+
+    file_object_first = netCDF4.Dataset(NLDAS_files[0])
+    lats = file_object_first.variables['lat_110'][:]
+    lons = file_object_first.variables['lon_110'][:]
+    lons, lats = numpy.meshgrid(lons, lats)
+
+    times = []
+    nfile = len(NLDAS_files)
+    for ifile, file in enumerate(NLDAS_files):
+        print 'Reading file '+str(ifile+1)+'/'+str(nfile), file
+        file_object = netCDF4.Dataset(file)
+        time_struct_parsed = strptime(file[-20:-7],"%Y%m%d.%H%M")
+        times.append(datetime(*time_struct_parsed[:6]))
+        
+        values0 = file_object.variables[variable_name][:]
+        values0 = numpy.expand_dims(values0, axis=0)
+        if ifile == 0:
+            values = values0
+            variable_unit = file_object.variables[variable_name].units
+        else:
+            values = numpy.concatenate((values, values0))
+        file_object.close()
+    times = numpy.array(times)
+    return Dataset(lats, lons, times, values, variable_name, units=variable_unit, name=name)


Mime
View raw message