factory.py 2.12 KB
Newer Older
James Harle's avatar
James Harle committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
'''
This is generic file loader factory.

@author: Mr. Srikanth Nagella
'''

#Global Imports
import os
#Local Imports
from pynemo.reader.ncml import Reader as NcMLReader
from pynemo.reader.ncml import NcMLFile
from pynemo.reader.directory import Reader as DirectoryReader

from netCDF4 import Dataset

def GetReader(uri, t_adjust, reader_type=None):
    if reader_type is None:
        print uri
        if uri.endswith(".ncml"):
            reader_type = "NcML"
        elif os.path.isdir(uri):
            reader_type = "Directory"
        else:
            print "Error input should be a NcML file or URL or a Local directory"
            return None
    if reader_type == "NcML":
        return NcMLReader(uri,t_adjust)
    else:
        return DirectoryReader(uri, t_adjust)
    
    
class NetCDFFile(object):
    def __init__(self, filename):
        self.nc = Dataset(filename)
    
    def __getitem__(self,val):
        return self.nc.variables[val]
    
    def close(self):
        self.nc.close()


def GetFile(uri):
    if uri.endswith(".ncml"):
        return NcMLFile(uri)
    else:
        return NetCDFFile(uri)
# from netcdf import GetFile as netcdf_get_file
# from netcdf import GetRepository as netcdf_repository
# from ncml import GetFile as ncml_get_file 
# from ncml import GetRepository as ncml_repository
# 
#     
#     
# import os
# def GetRepository(src_dir, grid, t_adjust, reader_type="netcdf"):
#     """ Generic method to return the repository either netcdf or ncml based on some
#     logic, now passing as reader_type to choose """
#     if reader_type == "netcdf":
#         return netcdf_repository(src_dir, grid, t_adjust)
#     elif reader_type == "ncml":
#         return ncml_repository(src_dir, grid, t_adjust)
#       
# def GetFile(file_path, reader_type="netcdf"):
#     """ Generic method to return the file object either netcdf or ncml based on some
#     logic, now passing as reader_type to choose"""
#     if reader_type == "netcdf":
#         return netcdf_get_file(file_path)
#     elif reader_type == "ncml":
#         return ncml_get_file(file_path)