def todofunction(root_path): # 发现别人插值的没有SpatialReference,还是要自己插值一遍 # todo 为了论文进度,先手动都插值了一遍,等有空了自己调用api做一遍 # 1.下一步要实现 调一下QGIS的插值API,做操作,参考:https://gis.stackexchange.com/questions/100188/how-to-compute-an-interpolation-raster-from-the-python-console-in-qgis/233322#233322 # aro_stage_ETL(root_path,'乳熟') station_list = pandas.read_csv(os.path.join(root_path, 'HHHstations.csv')) result_file = (os.path.join(root_path, 'stations_cx.txt')) for index, lonlatdata in station_list.iterrows(): try: lon = float(lonlatdata['Longitude']) lat = float(lonlatdata['Latitude']) StationID = lonlatdata['StationID'] station_name = lonlatdata['StationName'] for i in range(2001, 2014): year = str(i) stage_start_day, stage_end_day = get_grow_stage_by_lonlat(root_path, lon, lat, year, stage='CX') # print (round(stage_start_day),round(stage_end_day)) fu, start_month, start_day = Common_func.day_num_to_yyyymmdd(year, stage_start_day) fu, end_month, end_day = Common_func.day_num_to_yyyymmdd(year, stage_end_day) row = str(StationID) + ' ' + station_name + ' ' + str(lon) + ' ' + str(lat) + ' ' + year + ' ' + str( round(stage_start_day)) + ' ' + str(round(stage_end_day)) print(row) #Modis_IO.write_txt(result_file, row) except: print('no data')
def get_station_value_by_num_day(root_path, year, daydir, StationID, T_tpye='HighestTemperature'): fu, month, day = Common_func.day_num_to_yyyymmdd(year, daydir) data = pd.read_csv(os.path.join(root_path, 'meteodata.csv')) station_value = data[(data['Year'] == int(year)) & (data['Months'] == int(month)) & (data['Days'] == int(day)) & (data['StationID'] == StationID)].head() try: station_high_value = (station_value[T_tpye].values[0].astype(int)) / 10 except: station_high_value = -273.15 # print(station_high_value) return fu, station_high_value
def get_heat_stress_hours_every_station(root_path, stationID, station_name, lon, lat, year, stage_start_day, stage_end_day, heat_temperature, result_file): """ 计算每个点的高温时长 :param root_path: 数据根目录 :param stationID: 点id :param station_name: 点名字 :param lon: 经度 :param lat: 纬度 :param year: 年份 :param stage_start_day: 生育期起始日期 :param stage_end_day: 生育期截至日期 :param heat_temperature: 高温阈值 :param result_file: 结果保存文件路径 :return: 输出 输入参数+高温时长 """ #heat_temperature =heat_temperature*10 meteo_data = pandas.read_csv(os.path.join(root_path, 'stationdata.csv')) sum_heat_days = 0 sum_heat_hours = 0 for day_num in range(stage_start_day, stage_end_day + 1): fu, month, day = Common_func.day_num_to_yyyymmdd(year, day_num) heat_days = meteo_data[ (meteo_data['StationID'] == stationID) & (meteo_data['Year'] == year) & (meteo_data['Months'] == month) & (meteo_data['Days'] == day) & (meteo_data['HighestTemperature'] >= heat_temperature)] if heat_days.empty: print(station_name + ' ' + str(year) + ' ' + str(day_num) + ' ' + 'no heat!') continue else: try: # 高温时长模型实现 # 参数 太阳赤纬 sun_chiwei = 0.39795 * math.cos(0.98563 * (day_num - 173)) T_max = heat_days['HighestTemperature'].values[0] / 10 T_min = heat_days['LowestTemperature'].values[0] / 10 # fix a bug 天数-1 之后转日期,不能直接日-1 会报错 fu_next, month_next, day_next = Common_func.day_num_to_yyyymmdd( year, day_num + 1) T_min_tomorrow = meteo_data[ (meteo_data['StationID'] == stationID) & (meteo_data['Year'] == year) & (meteo_data['Months'] == month_next) & (meteo_data['Days'] == day_next)]['LowestTemperature'].values[0] / 10 a = math.sin(lat) * math.sin(sun_chiwei) b = math.cos(lat) * math.cos(sun_chiwei) DL = 12 * (1 + (2 / math.pi) * a * (math.sin(a / b))) p = 2 heat_temperature_real = heat_temperature / 10 A1 = math.asin( (heat_temperature_real - T_min) / (T_max - T_min)) A2 = math.asin((heat_temperature_real - T_min_tomorrow) / (T_max - T_min_tomorrow)) heat_stress_hours = (DL + 2 * p) * (1 - (A1 + A2) / math.pi) sum_heat_hours = sum_heat_hours + heat_stress_hours sum_heat_days = sum_heat_days + 1 row = str(stationID) + ' ' + station_name + ' ' + str( lon) + ' ' + str(lat) + ' ' + str(year) + ' ' + str( round(stage_start_day)) + ' ' + str( round(stage_end_day)) + ' ' + str( day_num) + ' ' + str(T_max) + ' ' + str( format(heat_stress_hours, '2f')) print(row) Modis_IO.write_txt(result_file, row) except: continue row = str(stationID) + ' ' + station_name + ' ' + str(lon) + ' ' + str( lat) + ' ' + str(year) + ' ' + str(sum_heat_days) + ' ' + str( format(sum_heat_hours, '2f')) file = str(year) + '.txt' Modis_IO.write_txt(file, row)
station_list = grid_station_data['stationID'].unique() for stationid in station_list: lon, lat = stations.Station_ETL.get_lonlat_by_stationID(stationid) sum_days = len(orig_data[(orig_data['stationID'] == stationid)]) val_days = len( grid_station_data[(grid_station_data['stationID'] == stationid)]) cx_sum_days = 0 cx_val_days = 0 cx_start_fu = 0 cx_end_fu = 0 for year in range(2002, 2014): cx_start, cx_end = stations.Station_ETL.get_cx_by_stationID( stationid, year) cx_start_fu, month, day = Common_func.day_num_to_yyyymmdd( year, cx_start) cx_end_fu, month, day = Common_func.day_num_to_yyyymmdd(year, cx_end) cx_sum_days = cx_sum_days + len( orig_data[(orig_data['stationID'] == stationid) & (orig_data['date'].astype(int) > int(cx_start_fu)) & (orig_data['date'].astype(int) < int(cx_end_fu))]) cx_val_days = len( grid_station_data[(orig_data['stationID'] == stationid) & (grid_station_data['date'] > int(cx_start_fu)) & (grid_station_data['date'] < int(cx_end_fu))]) print(stationid, lon, lat, sum_days, val_days, "%.2f%%" % (val_days / sum_days * 100), cx_start_fu, cx_end_fu, cx_sum_days, cx_val_days, "%.2f%%" % (cx_val_days / cx_sum_days * 100))