Exemple #1
0
def ij_extract(drew):
    for file in os.listdir(main):
        nc = Dataset(file, 'r')

        tos = nc.variables['tos']
        drew.lat_Val = nc.variables['lat']
        drew.lon_Val = nc.variables['lon']
        time = nc.variables['time']

        print_Close_Statement(file)

        for i in range(drew.lat_Val.shape[0]):
            for j in range(drew.lon_Val.shape[1]):
                if (drew.lat_Val[i][j] > mlat(drew) and drew.lat_Val[i][j] <
                        plat(drew)) and (drew.lon_Val[i][j] > mlon(drew)
                                         and drew.lon_Val[i][j] < plon(drew)):
                    dist_DP = math.sqrt((drew.lon - drew.lon_Val[i][j])**2 +
                                        (drew.lat - drew.lat_Val[i][j])**2)
                    if dist_DP < drew.closest_dist:
                        drew.closest_dist = dist_DP
                        drew.closest_i = i
                        drew.closest_j = j
        drew.print_closest_pnt()

        # Extract data from model for the appropriate time series - Arcpy
        coords = "i " + str(drew.closest_i) + ";j " + str(drew.closest_j)
        out_csv_file = out_dir + "\\" + drew.site + "_" + file + ".xls"

        # Process: Make NetCDF Table View
        arcpy.MakeNetCDFTableView_md(file, "tos", "raw_table", "time", coords,
                                     "BY_VALUE")
        tbl_to_csv(out_csv_file)
Exemple #2
0
def r_extract(
    drew
):  # Don't know if this works yet as I don't have the files yet with rlat/rlong
    for file in os.listdir(main):
        nc = Dataset(file, 'r')

        tos = nc.variables['tos']
        lat = nc.variables['lat']
        lon = nc.variables['lon']
        time = nc.variables['time']
        rlat = nc.variables['rlat']
        rlon = nc.variables['rlon']
        model_rlat = rlat[:]
        model_rlon = rlon[:]

        #Find Model Data Location
        print_Close_Statement(file)
        for i in range(lat.shape[0]):
            for j in range(lon.shape[1]):
                if (lat[i][j] > mlat(drew) and lat[i][j] < plat(drew)) and (
                        lon[i][j] > mlon(drew) and lon[i][j] < plon(drew)):
                    dist_DP = math.sqrt((drew.lon - lon[i][j])**2 +
                                        (drew.lat - lat[i][j])**2)
                    if dist_DP < drew.closest_dist:
                        drew.closest_dist = dist_DP
                        drew.closest_i = i
                        drew.closest_j = j
                        drew.closest_rlat = model_rlat[i]
                        drew.closest_rlon = model_rlon[j]

        print 'Closest model point to Drew Point:'
        print 'Lat:', lat[drew.closest_i][drew.closest_j], 'Lon:', lon[
            drew.closest_i][
                drew.
                closest_j], 'rlat:', drew.closest_rlat, 'rlon:', drew.closest_rlon, '\n'

        # Extract data from model for the appropriate time series - Arcpy
        coords = "rlat " + str(drew.closest_rlat) + ";rlon " + str(
            drew.closest_rlon)
        out_csv_file = out_dir + "\\" + drew.site + "_" + file + ".xls"

        # Process: Make NetCDF Table View
        arcpy.MakeNetCDFTableView_md(file, "tos", "raw_table", "time",
                                     "rlon -153.5;rlat 72.500001", "BY_VALUE")
        tbl_to_csv(out_csv_file)
Exemple #3
0
def uas_extract(drew):
    for file in os.listdir(main):
        nc = Dataset(file, 'r')

        uas = nc.variables['uas']
        lat = nc.variables['lat']
        lon = nc.variables['lon']
        time = nc.variables['time']

        model_lat = lat[:]
        model_lon = lon[:]

        # Find Model Data Location
        print_Close_Statement(file)

        for u_lat in model_lat:
            for u_lon in model_lon:
                if (u_lat > mlat(drew)
                        and u_lat < plat(drew)) and (u_lon > mlon(drew)
                                                     and u_lon < plon(drew)):
                    dist_DP = math.sqrt((drew.lon - u_lon)**2 +
                                        (drew.lat - u_lat)**2)
                    if dist_DP < drew.closest_dist:
                        drew.closest_dist = dist_DP
                        drew.closest_lat = u_lat
                        drew.closest_lon = u_lon
        drew.print_closest_pnt()

        #Extract data from model for the appropriate time series - Arcpy
        coords = "lat " + str(drew.closest_lat) + ";lon " + str(
            drew.closest_lon)
        out_csv_file = out_dir + "\\" + drew.site + "_" + file + ".xls"

        # Process: Make NetCDF Table View
        arcpy.MakeNetCDFTableView_md(file, "uas", "raw_table", "time", coords,
                                     "BY_VALUE")
        tbl_to_csv(out_csv_file)
Exemple #4
0
    def makeNetCDFTable(self, inpnt, variableName, rowDim, outTableView):
        '''
         Method Name:  makeNetCDFTable
         Description:  Creates a netCDF Table view from the first point selected
                       on the map, the variable choosen and the row dimension.  The
                       table contains every variable value at that particular point
                       for each slice of the row dimension.  For example: Every sea
                       temperature (Variable) value at each elevation(Row Dimension)
                       for a particular point(Input Point) in sea.
         Input:        
                       inpnt:         The selected Point
                       variableName:  The variable to get each value
                       rowDim:        How to slice up the netCDF file
                       outTableView:  The table being outputed
        '''

        netCDFSource = str(self.__sourceLocation)

        lonVar = self.getLonDimension()
        latVar = self.getLatDimension()

        #Printing out the inputs
        arcpy.AddMessage("Input NetCDF: " + netCDFSource)
        arcpy.AddMessage("Variable Name: " + str(variableName))
        arcpy.AddMessage("Row Dim: " + str(rowDim))
        arcpy.AddMessage("Lat Dim: " + latVar)
        arcpy.AddMessage("Lon Dim: " + lonVar)

        copyStartTime = time.time()

        arcpy.CopyFeatures_management(inpnt, r'in_memory\updateFeat')

        elapsedTime = (time.time() - copyStartTime)
        arcpy.AddMessage("Copy Features " + str(elapsedTime))

        is0to360 = self.__is0to360()

        #Only getting the first point.  Others ignored
        with arcpy.da.SearchCursor('in_memory\updateFeat',
                                   ('SHAPE@X', 'SHAPE@Y')) as cursor:
            for row in cursor:

                # Store x,y coordinates of current point
                if is0to360:
                    self.__lonValue = row[0] + 180
                else:
                    self.__lonValue = row[0]

                self.__latValue = row[1]

                arcpy.AddMessage(lonVar + ": " + str(self.__lonValue) + " " +
                                 latVar + ": " + str(self.__latValue))

        netCDFStartTime = time.time()
        arcpy.MakeNetCDFTableView_md(
            netCDFSource, variableName, outTableView, rowDim, lonVar + " " +
            str(self.__lonValue) + ";" + latVar + " " + str(self.__latValue),
            "BY_VALUE")

        elapsedNetCDFTime = (time.time() - copyStartTime)
        arcpy.AddMessage("Make netCDF Table Time: " + str(elapsedNetCDFTime))
Exemple #5
0
print 'Calculating closest model point to Drew Point... \n'
for i in range(lat.shape[0]):
    for j in range(lon.shape[1]):
        if lat[i][j] > (DP_lat - tol) and lat[i][j] < (DP_lat + tol):
            if lon[i][j] > (DP_lon - tol) and lon[i][j] < (DP_lon + tol):
                dist_DP = math.sqrt((DP_lon - lon[i][j])**2 +
                                    (DP_lat - lat[i][j])**2)

                if dist_DP < closest_dist:
                    closest_dist = dist_DP
                    closest_i = i
                    closest_j = j

print 'Closest model point to Drew Point:'
print 'Lat:', lat[closest_i][closest_j], 'Lon:', lon[closest_i][
    closest_j], 'i:', closest_i, 'j:', closest_j

## Extract data from model for the appropriate time series - Arcpy
coords = "i " + str(closest_i) + ";j " + str(closest_j)

out_csv_file = "C:\\Users\\Euan-Angus MacLeod\\Google Drive\\CSCE690_ Coastal Project\\test.xls"

# Process: Make NetCDF Table View
arcpy.MakeNetCDFTableView_md(file, "tos", "raw_table", "time", coords,
                             "BY_VALUE")

# Process: Table To CSV
arcpy.TableToExcel_conversion("raw_table", out_csv_file, "ALIAS", "CODE")

#current problem: not getting access to the output table for
    #ncVarsXYTimeDim = ['T2_MIN', 'T2_MAX'] #NSN:REMOVE
    #print ncVarsXYTimeDim #NSN:REMOVE

    # Find out the size of ncTimeDim
    ncXDimSize = ncFileProp.getDimensionSize(ncXDim)
    ncYDimSize = ncFileProp.getDimensionSize(ncYDim)
    ncTimeDimSize = ncFileProp.getDimensionSize(ncTimeDim)
    #print ncXDimSize, ncYDimSize, ncTimeDimSize #NSN:REMOVE
    #ncTimeDimSize = 2 #NSN:REMOVE

    ##==============================================================================
    # Make lon and lat tables to get distances between points
    # Minimum interval will be used to compute cellsize

    ncXDimAuCoordTableView = "ncXDimAuCoordTableView"
    result = arcpy.MakeNetCDFTableView_md(inNetCDFFile, ncXDimAuCoord,
                                          ncXDimAuCoordTableView, ncXDim)

    searchCursor = arcpy.da.SearchCursor(ncXDimAuCoordTableView,
                                         [ncXDimAuCoord])
    ncXDimAuCoordValues = [row[0] for row in searchCursor]
    ncXDimAuCoordInterval = min(
        abs(ncXDimAuCoordValues[1] - ncXDimAuCoordValues[0]),
        abs(ncXDimAuCoordValues[ncXDimSize - 1] -
            ncXDimAuCoordValues[ncXDimSize - 2]))

    ncYDimAuCoordTableView = "ncYDimAuCoordTableView"
    result = arcpy.MakeNetCDFTableView_md(inNetCDFFile, ncYDimAuCoord,
                                          ncYDimAuCoordTableView, ncYDim)

    searchCursor = arcpy.da.SearchCursor(ncYDimAuCoordTableView,
                                         [ncYDimAuCoord])
#-*- coding=utf-8 -*-
import arcpy
import csv
import numpy
csv_file = open(
    'G:\downscaling\china_america_meterology\data_daily_summary\usa_5_station\position_5.csv',
    'r')
csv_read = csv.reader(csv_file)
#data=[row for row in csv_read]
id = []
lon = []
lat = []
for row in csv_read:
    if row[0] != 'id':
        id.append(row[0])
        lat.append(row[-2])
        lon.append(row[-1])
lon = numpy.array(lon)
lat = numpy.array(lat)
for i in range(0, len(lon)):
    LON = float(lon[i]) + 360
    LAT = float(lat[i])
    print 'lat=%f,lon=%f' % (LAT, LON)
    print 'site%s' % id[i]
    #这种方式取点按照先谁取谁的原则
    arcpy.MakeNetCDFTableView_md(\
        'G:\downscaling\GCMs\pr_day_bcc-csm1-1_historical_r1i1p1_18500101-20121230.nc','pr','bbc_pr_%s'%id[i],"time","lon %f;lat %f"%(LON,LAT),"BY_VALUE")
    arcpy.CopyRows_management('bbc_pr_%s' % id[i],
                              "G:\downscaling\GCMs\pr\\bbc_pr_%s.csv" % id[i])
# Enter for loop for each feature/row
#
for row in rows:
    # Create the geometry object 'feat'
    #
    feat = row.getValue(shapefieldname)
    pnt = feat.getPart()

    # Print x,y coordinates of current point
    #
    XDim = pnt.X
    YDim = pnt.Y

    arcpy.AddMessage("X: " + str(pnt.X) + "  Y: " + str(pnt.Y))

# Process: Calculate Value
#arcpy.CalculateValue_management("getXY(r\"%Input_Point%\")", "def getXY(inpnt):\\n  import arcpy  \\n  for row in arcpy.SearchCursor(inpnt):       \\n    feat = row.shape\\n    pnt = feat.getPart()\\n  return str(pnt.X)", "Double")

# Process: Calculate Value (2)
#arcpy.CalculateValue_management("getXY(r\"%Input_Point%\")", "def getXY(inpnt):\\n  import arcpy  \\n  for row in arcpy.SearchCursor(inpnt):       \\n    feat = row.shape\\n    pnt = feat.getPart()\\n  return pnt.Y", "Double")

arcpy.AddMessage("lon " + str(XDim) + ";lat " + str(YDim))

# Process: Make NetCDF Table View
#arcpy.MakeNetCDFTableView_md(input_Net_CDF, "height", cf14_View4, "time", "lon %XDim%;lat %YDim%", "BY_VALUE")
arcpy.MakeNetCDFTableView_md(input_Net_CDF, "INUNDATION_RECURRENCE",
                             cf14_View4, "time",
                             "lon " + str(XDim) + ";lat " + str(YDim),
                             "BY_VALUE")

print(arcpy.GetMessages())
if not os.path.exists(NetCDF_location + '\\csv'):
    os.mkdir(NetCDF_location + '\\csv')

env.workspace = NetCDF_location

for root, dirs, files in os.walk(NetCDF_location):
    for name in files:
        if name[-3:] == '.nc':
            if 'pr' in name:
                Variables = "pr;time"
                outTableView = "PR_View"
                rowDimension = "time"
                dimensionValue = ""
                valueSelectionMethod = "BY_VALUE"
                arcpy.MakeNetCDFTableView_md(root + '\\' + name, Variables,
                                             outTableView, rowDimension,
                                             dimensionValue,
                                             valueSelectionMethod)
            elif 'tasmax' in name:
                Variables = "tasmax;time"
                outTableView = "Tasmax_View"
                rowDimension = "time"
                dimensionValue = ""
                valueSelectionMethod = "BY_VALUE"
                arcpy.MakeNetCDFTableView_md(root + '\\' + name, Variables,
                                             outTableView, rowDimension,
                                             dimensionValue,
                                             valueSelectionMethod)
            elif 'tasmin' in name:
                Variables = "tasmin;time"
                outTableView = "Tasmin_View"
                rowDimension = "time"
                    DP_lon + s_tol):
                #print 'lon: ',  lon[rlat][rlon]
                dist_DP = math.sqrt((DP_lon - lon[rlat][rlon])**2 +
                                    (DP_lat - lat[rlat][rlon])**2)

                if dist_DP < closest_dist:
                    closest_dist = dist_DP
                    closest_indx_rlat = rlat
                    closest_indx_rlon = rlon
                    closest_rlat = model_rlat[rlat]
                    closest_rlon = model_rlon[rlon]

                #print 'Lat:', lat[rlat][rlon], 'Lon:', lon[rlat][rlon], 'rlat:', model_rlat[rlat], 'rlon:', model_rlon[rlon]

print 'Closest model point to Drew Point:'
print 'Lat:', lat[closest_indx_rlat][closest_indx_rlon], 'Lon:', lon[
    closest_indx_rlat][
        closest_indx_rlon], 'rlat:', closest_rlat, 'rlon:', closest_rlon

## Extract data from model for the appropriate time series - Arcpy
coords = "rlat " + str(closest_rlat) + ";rlon " + str(closest_rlon)

out_csv_file = "C:\\Users\\Euan-Angus MacLeod\\Google Drive\\CSCE690_ Coastal Project\\testrlatrlon.xls"

# Process: Make NetCDF Table View
arcpy.MakeNetCDFTableView_md(file, "tos", "raw_table", "time",
                             "rlon -153.5;rlat 72.500001", "BY_VALUE")

# Process: Table To CSV
arcpy.TableToExcel_conversion("raw_table", out_csv_file, "ALIAS", "CODE")