Ejemplo n.º 1
0
def RequestInformation():
    sys.path.insert(0, r'EMC_SRC_PATH')
    import IrisEMC_Paraview_Lib as lib
    from paraview import util
    from os.path import splitext
    import IrisEMC_Paraview_Utils as utils
    import IrisEMC_Paraview_Param as param

    depth_begin = float(Depth_begin)
    depth_end = float(Depth_end)
    File_name = File_name.strip()
    ext = None
    if File_name in list(param.filesDict.values()) or not (
            File_name.lower().endswith(param.filesExtDict['ssl'].lower())
            or File_name.lower().endswith(param.filesExtDict['geo'].lower())):
        if utils.support_nc():
            ext = param.filesExtDict['ssl']
        else:
            ext = param.filesExtDict['geo']

    fileFound, address, source = lib.find_file(File_name,
                                               loc=r'EMC_MODELS_PATH',
                                               ext=ext)
    if not fileFound:
        raise Exception('model file "' + address + '" not found! Aborting.')
    Latitude_begin, Latitude_end, Longitude_begin, Longitude_end = lib.get_area(
        Area, Latitude_begin, Latitude_end, Longitude_begin, Longitude_end)
    this_filename, extension = splitext(address)
    if extension.lower() == '.nc':
        nx, ny, nz = lib.read_netcdf_model(address,
                                           Latitude_variable,
                                           Longitude_variable,
                                           Depth_variable,
                                           (Latitude_begin, Longitude_begin),
                                           (Latitude_end, Longitude_end),
                                           depth_begin,
                                           depth_end,
                                           Vertical_Scaling,
                                           inc=Sampling,
                                           extent=True)
    else:
        try:
            nx, ny, nz = lib.read_geocsv_model_3d(
                address, (Latitude_begin, Longitude_begin),
                (Latitude_end, Longitude_end),
                depth_begin,
                depth_end,
                Vertical_Scaling,
                inc=Sampling,
                extent=True)
        except Exception:
            raise Exception('cannot recognize model file "' + address +
                            '"! Aborting.')

    # ABSOLUTELY NECESSARY FOR THE READER TO WORK:
    util.SetOutputWholeExtent(self, [0, nx, 0, ny, 0, nz])
Ejemplo n.º 2
0
def RequestInformation():
    from os.path import splitext
    sys.path.insert(0, r'EMC_SRC_PATH')
    import IrisEMC_Paraview_Lib as lib
    from paraview import util
    import IrisEMC_Paraview_Utils as utils
    import IrisEMC_Paraview_Param as param

    Latitude_Begin, Latitude_End, Longitude_Begin, Longitude_End = lib.get_area(
        Area, Latitude_Begin, Latitude_End, Longitude_Begin, Longitude_End)

    if len(Alternate_FileName.strip()) > 0:
        FileName = Alternate_FileName.strip()
        Label = ' '.join(['SLAB', lib.file_name(Alternate_FileName).strip()])
    else:
        FileName = lib.usgsSlabKeys[Slab]
        Label = ' '.join(['USGS Slab 1.0 -', lib.usgsSlabValues[Slab].strip()])

    FileName = FileName.strip()
    ext = None
    if FileName in list(param.usgsSlabDict.keys()):
        if utils.support_nc():
            ext = param.usgsSlabExtDict['ssl']
        else:
            ext = param.usgsSlabExtDict['geo']

    fileFound, address, source = lib.find_file(FileName,
                                               loc=r'EMC_SLABS_PATH',
                                               ext=ext)

    if not fileFound:
        raise Exception('model file "' + address + '" not found! Aborting.')

    this_filename, extension = splitext(address)

    if extension.lower() == '.grd':
        nx, ny, nz = lib.read_slab_file(address,
                                        (Latitude_Begin, Longitude_Begin),
                                        (Latitude_End, Longitude_End),
                                        inc=Sampling,
                                        extent=True)
    elif extension.lower() == '.csv':
        nx, ny, nz = lib.read_geocsv_model_2d(
            address, (Latitude_Begin, Longitude_Begin),
            (Latitude_End, Longitude_End),
            Sampling,
            0,
            extent=True)

    else:
        raise Exception('cannot recognize model file "' + address +
                        '"! Aborting.')

    # ABSOLUTELY NECESSARY FOR THE READER TO WORK:
    util.SetOutputWholeExtent(self, [0, nx, 0, ny, 0, nz])
Ejemplo n.º 3
0
def RequestInformation():
    sys.path.insert(0, r'EMC_SRC_PATH')
    from os.path import splitext
    import IrisEMC_Paraview_Lib as lib
    from paraview import util
    import IrisEMC_Paraview_Utils as utils
    import IrisEMC_Paraview_Param as param

    if len(Alternate_FileName.strip()) > 0:
        file_name = Alternate_FileName.strip()
    else:
        file_name = lib.topoKeys[TopoFile]

    file_name = file_name.strip()
    ext = None
    if file_name in list(param.topoDict.keys()):
        if utils.support_nc():
            ext = param.topoExtDict['ssl']
        else:
            ext = param.topoExtDict['geo']

    fileFound, address, source = lib.find_file(file_name,
                                               loc=r'EMC_MODELS_PATH',
                                               ext=ext)
    Latitude_Begin, Latitude_End, Longitude_Begin, Longitude_End = lib.get_area(
        Area, Latitude_Begin, Latitude_End, Longitude_Begin, Longitude_End)

    this_filename, extension = splitext(address)
    if extension.lower() in ['.nc', '.grd']:
        nx, ny, nz = lib.read_netcdf_topo_file(
            address, (Latitude_Begin, Longitude_Begin),
            (Latitude_End, Longitude_End),
            Sampling,
            Roughness,
            lon_var=Longitude_Variable,
            lat_var=Latitude_Variable,
            elev_var=Elevation_Variable,
            extent=True)
    else:
        try:
            nx, ny, nz = lib.read_geocsv_model_2d(
                address, (Latitude_Begin, Longitude_Begin),
                (Latitude_End, Longitude_End),
                Sampling,
                Roughness,
                extent=True)
        except Exception:
            raise Exception('cannot recognize model file "' + address +
                            '"! Aborting.')

    # ABSOLUTELY NECESSARY FOR THE READER TO WORK:
    util.SetOutputWholeExtent(self, [0, nx, 0, ny, 0, nz])
Ejemplo n.º 4
0
def RequestData():
    # V.2019.079
    import sys
    sys.path.insert(0, r'EMC_SRC_PATH')
    from paraview.simple import RenameSource, GetActiveViewOrCreate, ColorBy, GetDisplayProperties, GetActiveSource
    import numpy as np
    import csv
    import os
    from os.path import splitext
    from vtk.util import numpy_support as nps
    import IrisEMC_Paraview_Lib as lib
    import IrisEMC_Paraview_Utils as utils
    import IrisEMC_Paraview_Param as param

    File_name = File_name.strip()
    ext = None
    if File_name in list(param.filesDict.values()) or not (
            File_name.lower().endswith(param.filesExtDict['ssl'].lower())
            or File_name.lower().endswith(param.filesExtDict['geo'].lower())):
        if utils.support_nc():
            ext = param.filesExtDict['ssl']
        else:
            ext = param.filesExtDict['geo']
    depth_begin = float(Depth_begin)
    depth_end = float(Depth_end)
    if depth_begin > depth_end:
        raise Exception('Begin Depth < End Depth! Aborting.')

    Latitude_begin, Latitude_end, Longitude_begin, Longitude_end = lib.get_area(
        Area, Latitude_begin, Latitude_end, Longitude_begin, Longitude_end)

    if len(Latitude_variable.strip()) <= 0 or len(
            Longitude_variable.strip()) <= 0 or len(
                Depth_variable.strip()) <= 0:
        raise Exception('Latitude, Longitude and Depth variable are required')

    # make sure we have input files
    fileFound, address, source = lib.find_file(File_name,
                                               loc=r'EMC_MODELS_PATH',
                                               ext=ext)
    if not fileFound:
        raise Exception('model file "' + address + '" not found! Aborting.')

    filename = lib.file_name(File_name)
    if len(Label.strip()) <= 0:
        if source == filename:
            Label = "%s " % (filename)
        else:
            Label = "%s from %s " % (filename, source)

    sg = self.GetOutput()  # vtkPolyData

    this_filename, extension = splitext(address)
    if extension.lower() in ['.nc', '.grd']:
        X, Y, Z, V, meta = lib.read_netcdf_model(
            address, Latitude_variable, Longitude_variable, Depth_variable,
            (Latitude_begin, Longitude_begin), (Latitude_end, Longitude_end),
            depth_begin, depth_end, Vertical_Scaling, Sampling)
    else:
        try:
            X, Y, Z, V, meta = lib.read_geocsv_model_3d(
                address, (Latitude_begin, Longitude_begin),
                (Latitude_end, Longitude_end), depth_begin, depth_end,
                Vertical_Scaling, Sampling)
        except Exception as e:
            raise Exception('cannot recognize model file "' + address +
                            '"! Aborting.\n' + str(e))

    nx = len(X)
    if nx <= 0:
        raise Exception('No data found!')
    ny = len(X[0])
    nz = len(X[0][0])
    sg.SetDimensions(nx, ny, nz)

    # make geometry
    points = vtk.vtkPoints()
    for k in range(nz):
        for j in range(ny):
            for i in range(nx):
                points.InsertNextPoint((X[i, j, k], Y[i, j, k], Z[i, j, k]))
    sg.SetPoints(points)

    # make geometry
    count = 0
    for var in list(V.keys()):
        scalars = vtk.vtkFloatArray()
        scalars.SetNumberOfComponents(1)
        scalars.SetName(var)
        for k in range(nz):
            for j in range(ny):
                for i in range(nx):
                    if V[var][i, j, k] == float('nan'):
                        scalars.InsertNextValue(float('nan'))
                    else:
                        scalars.InsertNextValue(V[var][i, j, k])
        if count == 0:
            sg.GetPointData().SetScalars(scalars)
        else:
            sg.GetPointData().AddArray(scalars)
        count += 1

    # store boundary metadata
    field_data = sg.GetFieldData()
    field_data.AllocateArrays(3)  # number of fields

    data = vtk.vtkFloatArray()
    data.SetName('Latitude\nRange (deg)')
    data.InsertNextValue(meta['lat'][0])
    data.InsertNextValue(meta['lat'][1])
    field_data.AddArray(data)

    data = vtk.vtkFloatArray()
    data.SetName('Longitude\nRange (deg)')
    data.InsertNextValue(meta['lon'][0])
    data.InsertNextValue(meta['lon'][1])
    field_data.AddArray(data)

    data = vtk.vtkFloatArray()
    data.SetName('Depths (km)')
    for d in sorted(meta['depth']):
        data.InsertNextValue(d)
    field_data.AddArray(data)

    data = vtk.vtkStringArray()
    data.SetName('Source')
    data.InsertNextValue(File_name)
    field_data.AddArray(data)

    label_2 = " - %s (lat:%0.1f,%0.1f, lon:%0.1f,%0.1f, depth:%0.1f - %0.1f)" % (
        lib.areaValues[Area], meta['lat'][0], meta['lat'][1], meta['lon'][0],
        meta['lon'][1], meta['depth'][0], meta['depth'][-1])
    RenameSource(' '.join([Label.strip(), label_2.strip()]))
    sg.SetFieldData(field_data)
Ejemplo n.º 5
0
def RequestData():
    # V.2019.030
    import sys
    sys.path.insert(0, r'EMC_SRC_PATH')
    from paraview.simple import RenameSource, GetActiveViewOrCreate, ColorBy, GetDisplayProperties, GetActiveSource
    import numpy as np
    import csv
    import os
    from os.path import splitext
    from vtk.util import numpy_support as nps
    import IrisEMC_Paraview_Lib as lib
    import IrisEMC_Paraview_Utils as utils
    import IrisEMC_Paraview_Param as param

    # elevation units in meters
    Roughness = -1 * float(Roughness)

    baseline = float(Depth_Bias)

    if len(Alternate_FileName.strip()) > 0:
        file_name = Alternate_FileName.strip()
        label = ' '.join(['Topo', lib.file_name(Alternate_FileName).strip()])
    else:
        file_name = lib.topoKeys[TopoFile]
        label = lib.topoValues[TopoFile]

    file_name = file_name.strip()
    ext = None
    if file_name in list(param.topoDict.keys()):
        if utils.support_nc():
            ext = param.topoExtDict['ssl']
        else:
            ext = param.topoExtDict['geo']

    # make sure we have input files
    file_found, address, source = lib.find_file(file_name,
                                                loc=r'EMC_MODELS_PATH',
                                                ext=ext)

    if not file_found:
        raise Exception('Topo file "' + address + '" not found! Aborting.')

    this_filename, extension = splitext(address)

    sg = self.GetOutput()  # vtkPolyData

    Latitude_Begin, Latitude_End, Longitude_Begin, Longitude_End = lib.get_area(
        Area, Latitude_Begin, Latitude_End, Longitude_Begin, Longitude_End)

    label2 = " - %s (%0.1f,%0.1f,%0.1f,%0.1f)" % (
        lib.areaValues[Area], Latitude_Begin, Latitude_End, Longitude_Begin,
        Longitude_End)

    if extension.lower() in ['.nc', '.grd']:

        X, Y, Z, V, label = lib.read_netcdf_topo_file(
            address, (Latitude_Begin, Longitude_Begin),
            (Latitude_End, Longitude_End),
            Sampling,
            Roughness,
            base=baseline,
            lon_var=Longitude_Variable,
            lat_var=Latitude_Variable,
            elev_var=Elevation_Variable,
            unit_factor=float(Unit_Factor),
            extent=False)

    else:
        try:
            X, Y, Z, V, meta = lib.read_geocsv_model_2d(
                address, (Latitude_Begin, Longitude_Begin),
                (Latitude_End, Longitude_End),
                Sampling,
                Roughness,
                base=baseline,
                unit_factor=float(Unit_Factor),
                extent=False)

        except Exception:
            raise Exception('cannot recognize model file "' + address +
                            '"! Aborting.')

    nx = len(X)
    ny = len(X[0])
    nz = len(X[0][0])
    sg.SetDimensions(nx, ny, nz)

    #
    # make geometry
    #
    points = vtk.vtkPoints()
    for k in range(nz):
        for j in range(ny):
            for i in range(nx):
                points.InsertNextPoint((X[i, j, k], Y[i, j, k], Z[i, j, k]))
    sg.SetPoints(points)

    #
    # make geometry
    #
    count = 0
    for var in list(V.keys()):
        scalars = vtk.vtkFloatArray()
        scalars.SetNumberOfComponents(1)
        scalars.SetName(var)
        for k in range(nz):
            for j in range(ny):
                for i in range(nx):
                    scalars.InsertNextValue(V[var][i, j, k])
        if count == 0:
            sg.GetPointData().SetScalars(scalars)
        else:
            sg.GetPointData().AddArray(scalars)
        count += 1

    # store metadata
    fieldData = sg.GetFieldData()
    fieldData.AllocateArrays(3)  # number of fields

    data = vtk.vtkFloatArray()
    data.SetName('Latitude\nRange (deg)')
    data.InsertNextValue(Latitude_Begin)
    data.InsertNextValue(Latitude_End)
    fieldData.AddArray(data)

    data = vtk.vtkFloatArray()
    data.SetName('Longitude\nRange (deg)')
    data.InsertNextValue(Longitude_Begin)
    data.InsertNextValue(Longitude_End)
    fieldData.AddArray(data)

    data = vtk.vtkStringArray()
    data.SetName('Source')
    data.InsertNextValue(source)
    fieldData.AddArray(data)
    RenameSource(' '.join(
        [label.strip(), 'from',
         source.strip(), label2.strip()]))
Ejemplo n.º 6
0
def RequestData():
    # R.1.2018.346
    import sys
    sys.path.insert(0, r'EMC_SRC_PATH')
    from paraview.simple import RenameSource, GetActiveViewOrCreate, ColorBy, GetDisplayProperties, GetActiveSource
    import numpy as np
    import csv
    import os
    from os.path import splitext
    from vtk.util import numpy_support as nps
    import IrisEMC_Paraview_Lib as lib
    import urllib.parse
    import IrisEMC_Paraview_Utils as utils
    import IrisEMC_Paraview_Param as param

    USGS = True
    if len(Alternate_FileName.strip()) > 0:
        FileName = Alternate_FileName.strip()
        Label = ' '.join(['SLAB', lib.file_name(Alternate_FileName).strip()])
        USGS = False
    else:
        FileName = lib.usgsSlabKeys[Slab]

    FileName = FileName.strip()
    ext = None
    if FileName in list(param.usgsSlabDict.keys()):
        if utils.support_nc():
            ext = param.usgsSlabExtDict['ssl']
        else:
            ext = param.usgsSlabExtDict['geo']

    depthFactor = -1

    Latitude_Begin, Latitude_End, Longitude_Begin, Longitude_End = lib.get_area(
        Area, Latitude_Begin, Latitude_End, Longitude_Begin, Longitude_End)
    Label2 = " - %s (lat:%0.1f,%0.1f, lon:%0.1f,%0.1f)" % (
        lib.areaValues[Area],
        Latitude_Begin,
        Latitude_End,
        Longitude_Begin,
        Longitude_End,
    )

    # Make sure we have input files
    fileFound, address, source = lib.find_file(FileName,
                                               loc=r'EMC_SLABS_PATH',
                                               ext=ext)
    if not fileFound:
        raise Exception('model file "' + address + '" not found! Aborting.')

    this_filename, extension = splitext(address)
    sg = self.GetOutput()  # vtkPolyData

    if extension.lower() == '.grd':
        X, Y, Z, V, label = lib.read_slab_file(
            address, (Latitude_Begin, Longitude_Begin),
            (Latitude_End, Longitude_End),
            inc=Sampling,
            depth_factor=depthFactor,
            extent=False)
    elif extension.lower() == '.csv':
        X, Y, Z, V, meta = lib.read_geocsv_model_2d(
            address, (Latitude_Begin, Longitude_Begin),
            (Latitude_End, Longitude_End),
            Sampling,
            1,
            base=0,
            unit_factor=depthFactor,
            extent=False)
        label = ''

    else:
        raise Exception('cannot recognize model file "' + address +
                        '"! Aborting.')

    nx = len(X)
    ny = len(X[0])
    nz = len(X[0][0])
    sg.SetDimensions(nx, ny, nz)

    #
    # make geometry
    #
    points = vtk.vtkPoints()
    for k in range(nz):
        for j in range(ny):
            for i in range(nx):
                points.InsertNextPoint((X[i, j, k], Y[i, j, k], Z[i, j, k]))
    sg.SetPoints(points)

    #
    # make geometry
    #
    count = 0
    for var in list(V.keys()):
        scalars = vtk.vtkFloatArray()
        scalars.SetNumberOfComponents(1)
        scalars.SetName(var)
        for k in range(nz):
            for j in range(ny):
                for i in range(nx):
                    scalars.InsertNextValue(depthFactor * V[var][i, j, k])
        if count == 0:
            sg.GetPointData().SetScalars(scalars)
        else:
            sg.GetPointData().AddArray(scalars)
        count += 1

        # store USGS metadata
    if USGS:
        fieldData = sg.GetFieldData()
        fieldData.AllocateArrays(3)  # number of fields

        data = vtk.vtkFloatArray()
        data.SetName('Latitude\nRange (deg)')
        data.InsertNextValue(
            lib.usgsSlabRangeDict[lib.usgsSlabKeys[Slab]]['Y'][0])
        data.InsertNextValue(
            lib.usgsSlabRangeDict[lib.usgsSlabKeys[Slab]]['Y'][1])
        fieldData.AddArray(data)

        data = vtk.vtkFloatArray()
        data.SetName('Longitude\nRange (deg)')
        minX = lib.usgsSlabRangeDict[lib.usgsSlabKeys[Slab]]['X'][0]
        if minX > 180:
            minX -= 360
        maxX = lib.usgsSlabRangeDict[lib.usgsSlabKeys[Slab]]['X'][1]
        if maxX > 180:
            maxX -= 360
        xMin = min([minX, maxX])
        xMax = max([minX, maxX])
        data.InsertNextValue(xMin)
        data.InsertNextValue(xMax)
        fieldData.AddArray(data)

        data = vtk.vtkFloatArray()
        data.SetName('Depth to Slab\nRange (km)')
        data.InsertNextValue(
            abs(lib.usgsSlabRangeDict[lib.usgsSlabKeys[Slab]]['Z'][1]))
        data.InsertNextValue(
            abs(lib.usgsSlabRangeDict[lib.usgsSlabKeys[Slab]]['Z'][0]))
        fieldData.AddArray(data)

        data = vtk.vtkStringArray()
        data.SetName('Source')
        data.InsertNextValue(lib.usgsSlab_URL)
        Label = ' '.join([
            'USGS Slab 1.0:', lib.usgsSlabValues[Slab].strip(), 'from',
            urllib.parse.urlparse(lib.usgsSlab_URL).netloc.strip()
        ])
        data.InsertNextValue(lib.usgsSlabKeys[Slab])
        fieldData.AddArray(data)

    RenameSource(' '.join([Label.strip(), Label2.strip()]))
Ejemplo n.º 7
0
def read_netcdf_model(model_file,
                      lat_variable,
                      lon_variable,
                      depth_variable,
                      ll,
                      ur,
                      depth_min,
                      depth_max,
                      roughness,
                      inc,
                      extent=False):
    """read in an EMC Earth model in the netCDF format

      Keyword arguments:
      model_file: model file
      ll: lower-left coordinate
      ur: upper-right coordinate
      depth_min: minimum depth
      depth_max: maximum depth
      inc: grid sampling interval

      Return values:
      x: x-coordinate  normalized to the radius of the Earth
      y: y-coordinate  normalized to the radius of the Earth
      z: z-coordinate  normalized to the radius of the Earth
      meta: file metadata information
    """

    # ParaView on some platforms does not have SciPy module
    if not utils.support_nc():
        print(
            "[ERR] Cannot read netCDF files on this platform, try GeoCSV format!"
        )
        return [], [], [], [], {}

    # NetCDF files, when opened read-only, return arrays that refer directly to memory-mapped data on disk:
    print(f"[INFO] Reading model file {model_file}")
    data = netcdf.netcdf_file(model_file, 'r')
    variables = []
    for name in list(data.variables.keys()):
        if name not in (depth_variable, lon_variable, lat_variable):
            variables.append(name)

    # expects variables be a function of latitude, longitude and depth, find the order
    var = variables[0]
    for i, value in enumerate(data.variables[var].dimensions):
        if value == depth_variable:
            depth_index = i
        elif value == lon_variable:
            lon_index = i
        else:
            lat_index = i

    lat = data.variables[lat_variable][:].copy()
    lon = data.variables[lon_variable][:].copy()
    lon, lon_map = utils.lon_180(lon, fix_gap=True)

    depth = data.variables[depth_variable][:].copy()

    # select the values within the ranges (this is to get a count only)
    latitude, longitude, depth2 = get_points_in_volume(lat, lon, depth, ll, ur,
                                                       inc, depth_min,
                                                       depth_max)

    # model data grid definition
    V = {}
    nx = len(longitude)
    ny = len(depth2)
    nz = len(latitude)
    if extent:
        return nx - 1, ny - 1, nz - 1
    index = [-1, -1, -1]
    meta = {
        'depth': [],
        'lat': [100, -100],
        'lon': [400, -400],
        'source': model_file
    }

    missing_value = None
    if hasattr(data.variables[var], 'missing_value'):
        missing_value = float(data.variables[var].missing_value)

    for l, var_val in enumerate(variables):
        X = np.zeros((nx, ny, nz))
        Y = np.zeros((nx, ny, nz))
        Z = np.zeros((nx, ny, nz))
        v = np.zeros((nx, ny, nz))
        data_in = data.variables[var_val][:].copy()

        # increment longitudes, we want to keep the first and last longitude regardless of inc
        for i, lon_val in enumerate(lon):
            for j, depth_val in enumerate(depth):
                for k, lat_val in enumerate(lat):
                    if lon_val in longitude and lat_val in latitude and depth_val in depth2:
                        meta['lon'] = [
                            min(meta['lon'][0], lon_val),
                            max(meta['lon'][0], lon_val)
                        ]
                        meta['lat'] = [
                            min(meta['lat'][0], lat_val),
                            max(meta['lat'][0], lat_val)
                        ]
                        if depth_val not in meta['depth']:
                            meta['depth'].append(depth_val)
                        x, y, z = llz2xyz(lat_val, lon_val,
                                          depth_val * roughness)
                        ii = longitude.index(lon_val)
                        jj = depth2.index(depth_val)
                        kk = latitude.index(lat_val)

                        X[ii, jj, kk] = x
                        Y[ii, jj, kk] = y
                        Z[ii, jj, kk] = z

                        index[depth_index] = j
                        index[lat_index] = k
                        index[lon_index] = i

                        this_value = data_in[index[0]][index[1]][index[2]]

                        if this_value is None:
                            v[ii, jj, kk] = None
                        elif this_value is not None:
                            if this_value == missing_value:
                                v[ii, jj, kk] = None
                                this_value = None
                            else:
                                v[ii, jj, kk] = this_value
                        else:
                            v[ii, jj, kk] = this_value

        V[var_val] = v
    data.close()
    return X, Y, Z, V, meta
Ejemplo n.º 8
0
def find_file(address, loc, query='', ext=None):
    """find a file either locally or via a URL

    Keyword arguments:
    address: file address, path, url, etc.
    loc: location of the file
    query: URL query string

    Return values:
    found: indicating if the operation was a success
    address: full address of the local file
    source: where the file came from
    """

    found = False

    # For default files, the calling script sends the proper extension to use depending on the OS support.
    if ext is not None:
        address = ''.join([address, ext])
    if address.lower().endswith('.nc') and not utils.support_nc():
        print(
            "[ERR] Cannot read netCDF files on this platform, try GeoCSV format!"
        )
        return False, address, address

    # It is a full path to a file?
    source = address
    if os.path.isfile(address):
        origin = read_info_file(source)
        return True, address, origin

    # It is a file under the data directory?
    elif os.path.isfile(os.path.join(loc, address)):
        source = os.path.join(loc, address)
        origin = read_info_file(source)
        return True, source, origin

    # Other possibilities, URL?
    else:
        # Check the DMC URL.
        if loc in (pathDict['EMC_BOUNDARIES_PATH'],
                   pathDict['EMC_MODELS_PATH'],
                   pathDict['EMC_VOLCANOES_PATH']):
            source = irisEMC_Files_URL + address
            if is_url_valid(source):
                found, destination, origin = get_file_from_url(
                    source, loc, filename=os.path.join(loc, address))

        # USGS Slab 1.0
        elif loc == pathDict['EMC_SLABS_PATH']:
            source = usgsSlab_URL + address
            if is_url_valid(source):
                found, destination, origin = get_file_from_url(
                    source, loc, filename=os.path.join(loc, address))

        # Earthquakes.
        elif loc == pathDict['EMC_EARTHQUAKES_PATH']:
            source = query
            if is_url_valid(source):
                found, destination, origin = get_file_from_url(
                    source, loc, filename=os.path.join(loc, address))
                if found:
                    fp = open(destination, 'r+')
                    catalog = fp.read()
                    fp.seek(0, 0)
                    fp.write(eq_header(source))
                    fp.write(catalog)
                    fp.close()

        # Did we find the file?
        if found:
            return found, destination, origin

        # Did user provide a URL.
        else:
            found, destination, origin = get_file_from_url(address, loc, query)
            return found, destination, origin
Ejemplo n.º 9
0
def read_slab_file(model_file, ll, ur, inc=1, depth_factor=-1, extent=False):
    """read in a 2-D netCDF Slab file

    Keyword arguments:
    model_file: model file
    ll: lower-left coordinate
    ur: upper-right coordinate
    inc: grid sampling interval

    RReturn values:
    X: x-coordinate  normalized to the radius of Earh
    Y: y-coordinate  normalized to the radius of Earh
    Z: z-coordinate  normalized to the radius of Earh
    label: file label
    """

    # ParaView on some systems does not have SciPy module
    if not utils.support_nc():
        print(
            "[ERR] Cannot read netCDF files on this platform, try GeoCSV format!"
        )
        return [], [], [], [], ''

    z_variable = 'z'
    lon_variable = 'x'
    lat_variable = 'y'
    depth = 0

    # model data
    data = netcdf.netcdf_file(model_file, 'r')
    lat = data.variables[lat_variable][:].copy()
    lon = data.variables[lon_variable][:].copy()
    lon = utils.lon_180(lon)
    elevation_data = data.variables[z_variable][:].copy()

    data.close()
    dep = [depth]
    variables = [z_variable]

    # select the values within the ranges (this is to get a count only)
    latitude, longitude, depth2 = get_points_in_area(lat, lon, dep, ll, ur,
                                                     inc)

    # model data grid definition
    V = {}
    nx = len(longitude)
    ny = len(depth2)
    nz = len(latitude)

    if extent:
        return nx - 1, ny - 1, nz - 1

    label = ''
    if len(depth2):
        label = "%0.1f-%0.1fkm" % (min(depth2), max(depth2))

    for l, var_value in enumerate(variables):
        X = np.zeros((nx, ny, nz))
        Y = np.zeros((nx, ny, nz))
        Z = np.zeros((nx, ny, nz))
        v = np.zeros((nx, ny, nz))

        for i, lon_val in enumerate(lon):
            for j, depth_val in enumerate(dep):
                for k, lat_val in enumerate(lat):
                    if lon_val in longitude and lat_val in latitude and depth_val in depth2:
                        ii = longitude.index(lon_val)
                        jj = depth2.index(depth_val)
                        kk = latitude.index(lat_val)
                        x, y, z = llz2xyz(lat[k], lon[i],
                                          elevation_data[k][i] * depth_factor)
                        X[ii, jj, kk] = x
                        Y[ii, jj, kk] = y
                        Z[ii, jj, kk] = z

                        v[ii, jj, kk] = elevation_data[k][i]

    V[z_variable] = v
    return X, Y, Z, V, label
Ejemplo n.º 10
0
def read_netcdf_topo_file(model_file,
                          ll,
                          ur,
                          inc,
                          roughness,
                          lon_var='longitude',
                          lat_var='latitude',
                          elev_var='elevation',
                          base=0,
                          unit_factor=1,
                          extent=False):
    """read in etopo, a 2-D netCDF topo file

    Keyword arguments:
    model_file: model file
    ll: lower-left coordinate
    ur: upper-right coordinate
    inc: grid sampling interval
    roughness: set the variable as depth and use this for exaggeration
    extent: should only compute model extent? (True or False)

    RReturn values:
    X: x-coordinate  normalized to the radius of Earh
    Y: y-coordinate  normalized to the radius of Earh
    Z: z-coordinate  normalized to the radius of Earh
    label: file label
    """

    # ParaView on some platforms does not have SciPy module

    if not utils.support_nc():
        print("[ERR] Sorry, cannot read netCDF files on this platform!")
        return 0, 0, 0

    z_variable = elev_var
    lon_variable = lon_var
    lat_variable = lat_var
    depth = base
    # model data
    data = netcdf.netcdf_file(model_file, 'r')
    lat = data.variables[lat_variable][:].copy()
    lon = data.variables[lon_variable][:].copy()
    lon = utils.lon_180(lon)
    elevation_data = data.variables[z_variable][:].copy()

    data.close()
    dep = [depth]
    variables = [z_variable]

    # select the values within the ranges (this is to get a count only)
    latitude, longitude, depth2 = get_points_in_area(lat, lon, dep, ll, ur,
                                                     inc)

    # model data grid definition
    V = {}
    nx = len(longitude)
    ny = len(depth2)
    nz = len(latitude)

    if extent:
        return nx - 1, ny - 1, nz - 1

    label = ''
    if hasattr(data, 'description'):
        label = data.description
    elif hasattr(data, 'title'):
        label = data.title

    for l, var_value in enumerate(variables):
        X = np.zeros((nx, ny, nz))
        Y = np.zeros((nx, ny, nz))
        Z = np.zeros((nx, ny, nz))
        v = np.zeros((nx, ny, nz))

        for i, lon_val in enumerate(lon):
            for j, depth_val in enumerate(dep):
                for k, lat_val in enumerate(lat):
                    if lon_val in longitude and lat_val in latitude and depth_val in depth2:
                        ii = longitude.index(lon_val)
                        jj = depth2.index(depth_val)
                        kk = latitude.index(lat_val)
                        # "+" since it is elevation but we already making roughness negative to make it positive up
                        x, y, z = llz2xyz(
                            lat_val, lon_val, depth_val +
                            (elevation_data[k][i] * roughness * unit_factor))
                        X[ii, jj, kk] = x
                        Y[ii, jj, kk] = y
                        Z[ii, jj, kk] = z

                        v[ii, jj,
                          kk] = elevation_data[k][i] * float(unit_factor)

    V[z_variable] = v
    return X, Y, Z, V, label