def gen_normalize_dem_tif():
    # for the purpose of examination
    arr_dem = np.load(r'E:\MODIS\DEM\dem_normalize.npy')
    plt.imshow(arr_dem)
    plt.colorbar()
    plt.show()
    tif = r'E:\MODIS\DEM\china_1km_dem_wgs1984_resample.tif'
    array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(
        tif)
    to_raster.array2raster(
        r'E:\MODIS\DEM\china_1km_dem_wgs1984_resample_normalize.tif', originX,
        originY, pixelWidth, pixelHeight, arr_dem)
Exemple #2
0
def nc_to_tif(nc,outdir):
    # outdir = this_root+'SPEI\\tif\\'
    import analysis
    analysis.Tools().mk_dir(outdir)
    ncin = Dataset(nc, 'r')

    lat = ncin['lat'][::-1]
    lon = ncin['lon']
    pixelWidth = lon[1]-lon[0]
    pixelHeight = lat[1]-lat[0]
    longitude_start = lon[0]
    latitude_start = lat[0]

    time = ncin.variables['time']

    # print(time)
    # exit()
    # time_bounds = ncin.variables['time_bounds']
    # print(time_bounds)
    start = datetime.datetime(1900, 01, 01)
    # a = start + datetime.timedelta(days=5459)
    # print(a)
    # print(len(time_bounds))
    # print(len(time))
    # for i in time:
    #     print(i)
    # exit()
    # nc_dic = {}
    flag = 0

    valid_year = []
    for i in range(1982, 2016):
        valid_year.append(str(i))

    for i in range(len(time)):

        flag += 1
        # print(time[i])
        date = start + datetime.timedelta(days=int(time[i]))
        year = str(date.year)
        month = '%02d' % date.month
        # day = '%02d'%date.day
        date_str = year + month
        if not date_str[:4] in valid_year:
            continue
        # print(date_str)
        arr = ncin.variables['tmp'][i][::-1]
        arr = np.array(arr)
        grid = arr < 99999
        arr[np.logical_not(grid)] = -999999
        newRasterfn = outdir+date_str+'.tif'
        to_raster.array2raster(newRasterfn,longitude_start,latitude_start,pixelWidth,pixelHeight,arr)
Exemple #3
0
def read_hdf():
    out_dir = this_root + 'NDVI\\tif\\'
    fdir = this_root + 'NDVI\\GIMMS_NDVI\\'
    for f in tqdm(os.listdir(fdir)):
        # for f in os.listdir(fdir):
        if not f.endswith('.hdf'):
            continue
        # if not f == 'ndvi3g_geo_v1_2015_0106.hdf':
        #     continue
        # print(f)
        year = f.split('.')[0].split('_')[-2]
        # print(year)
        f = h5py.File(fdir + f, 'r')
        # print(len(f))
        # continue
        base_month = f['time'][0]
        for i in range(len(f)):
            arr = f['ndvi'][i]
            lon = f['lon']
            lat = f['lat']
            time = f['time'][i]
            month = base_month + (time - base_month) / 0.5
            # print(time_)
            # continue
            month = int(month)
            date = year + '%02d' % month
            # print(date)
            # continue
            newRasterfn = out_dir + '{}.tif'.format(date)
            longitude_start = lon[0]
            latitude_start = lat[0]
            pixelWidth = lon[1] - lon[0]
            pixelHeight = lat[1] - lat[0]
            arr = np.array(arr, dtype=float)
            # print(arr.dtype)
            grid = arr > -3000
            arr[np.logical_not(grid)] = -999999
            # plt.imshow(arr)
            # plt.show()
            to_raster.array2raster(newRasterfn, longitude_start,
                                   latitude_start, pixelWidth, pixelHeight,
                                   arr)
            pass
Exemple #4
0
def contour():
    fdir = u'E:\\FVC内蒙古植被覆盖数据\\1km年值_1978_1985_1995_2005_2018\\'
    outdir = u'E:\\FVC内蒙古植被覆盖数据\\tif\\'
    flist = os.listdir(fdir)
    arr_sum = 0
    for f in flist:
        if f.endswith('.tif'):
            print(f)
            array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(fdir + f)
            array = np.array(array,dtype='float64')
            arr_sum += array
    arr_mean = arr_sum/5.
    grid = arr_mean > -9999
    arr_mean[np.logical_not(grid)] = 255
    # plt.imshow(arr_mean)
    # plt.colorbar()
    # plt.show()
    # exit()
    to_raster.array2raster(outdir+'mean.tif',originX, originY, pixelWidth, pixelHeight,arr_mean)
Exemple #5
0
def mean_1km():
    fdir = r'E:\1km_fvc_monthly\glass\\'
    out_dir = r'E:\1km_fvc_annual\glass\\'
    flist = os.listdir(fdir)

    one_year = {}
    for y in range(2000, 2017):
        one_year[str(y)] = []
    for f in flist:

        year = f.split('_')[1].split('-')[0]
        for y in range(2000, 2017):
            if year == str(y):
                one_year[year].append(f)

    for y in one_year:
        print(y)
        array_sum = 0.
        flag = 0.
        for mon in one_year[y]:
            array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(
                fdir + mon)
            # array = np.ma.masked_where(array == 255, array)
            # plt.imshow(array, 'jet')
            # plt.colorbar()
            # plt.show()
            array_sum += array
            flag += 1.
        array_mean = array_sum / flag
        # array_mean = np.ma.masked_where(array_mean>254,array_mean)
        newRasterfn = out_dir + y + '.tif'
        longitude_start = originX
        latitude_start = originY
        pixelWidth = pixelWidth
        pixelHeight = pixelHeight
        array = array_mean
        to_raster.array2raster(newRasterfn, longitude_start, latitude_start,
                               pixelWidth, pixelHeight, array)
Exemple #6
0
def modify_2018_1km_tif():
    fdir = u'E:\\FVC内蒙古植被覆盖数据\\1km月值_1978_1985_1995_2005_2018\\'
    flist = os.listdir(fdir)
    selected = []
    for f in flist:
        if f.endswith('.tif') and '_2018-' in f:
            print(f)
            selected.append(f)
    selected = selected[::2]
    print()

    for f in selected:
        # print(f)
        array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(fdir+f)
        break
        # arrs.append(array)

    maxarr = np.zeros_like(array)
    for f in selected:
        print(f)
        array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(fdir+f)
        maxarr = np.maximum(maxarr,array)

    to_raster.array2raster(fdir+'2018_new.tif',originX, originY, pixelWidth, pixelHeight,maxarr)
Exemple #7
0
        temp = []
        for j in i:
            if j < -999:
                val = 255
            elif -999 < j < 0:
                val = 0
            elif 0 <= j <=1:
                val = int(j*100)
            elif j > 1:
                val = 100
            else:
                val = 255
            temp.append(val)
            # print val
        new_array.append(temp)
    # new_array = np.array(new_array,dtype=int)

    new_array = np.array(new_array,dtype=int)
    # print 'ploting'
    # plt.imshow(new_array)
    # plt.colorbar()
    # print 'done'
    # plt.show()
    # exit()
    # print 'saving'
    newRasterfn = (save_dir+f).decode('gbk').encode('utf-8')
    to_raster.array2raster(newRasterfn,originX,originY,pixelWidth,pixelHeight,new_array)
    time_end = time.time()
    log_process.process_bar(flag,len(flist),time_init,time_start,time_end,f)
    flag+=1
Exemple #8
0
def nc_to_tif(nc,outdir):
    # outdir = this_root+'SPEI\\tif\\'
    import analysis
    analysis.Tools().mk_dir(outdir,force=1)
    ncin = Dataset(nc, 'r')
    # print(ncin.variables)
    # exit()
    lat = ncin['lat']
    lon = ncin['lon']
    pixelWidth = lon[1]-lon[0]
    pixelHeight = lat[1]-lat[0]
    longitude_start = lon[0]
    latitude_start = lat[0]

    time = ncin.variables['time']

    # print(time)
    # exit()
    # time_bounds = ncin.variables['time_bounds']
    # print(time_bounds)
    start = datetime.datetime(1900, 01, 01)
    # a = start + datetime.timedelta(days=5459)
    # print(a)
    # print(len(time_bounds))
    # print(len(time))
    # for i in time:
    #     print(i)
    # exit()
    # nc_dic = {}
    flag = 0

    # valid_year = []
    # for i in range(1982, 2016):
    #     valid_year.append(str(i))
    # for i in time:
    #     print(i)
    # exit()
    for i in range(len(time)):

        flag += 1
        # print(time[i])
        date = start + datetime.timedelta(days=int(time[i]))
        year = str(date.year)
        month = '%02d' % date.month
        # day = '%02d'%date.day
        date_str = year + month
        # if not date_str[:4] in valid_year:
        #     continue
        # print(date_str)
        # exit()
        ndv = np.nan
        arr = ncin.variables['def'][i]
        for name,variable in ncin.variables.items():
            for var in variable.ncattrs():
                if var == 'missing_value':
                    ndv = variable.getncattr(var)
        if np.isnan(ndv):
            raise IOError('no key missing_value')
        arr = np.array(arr)
        #### mask ####
        grid = arr == 32768
        # print ndv
        arr[grid] = -999999
        # arr[grid] = np.nan
        # plt.imshow(arr)
        # plt.colorbar()
        # plt.show()
        newRasterfn = outdir+date_str+'.tif'
        to_raster.array2raster(newRasterfn,longitude_start,latitude_start,pixelWidth,pixelHeight,arr)