diff --git a/framework/climate/urls.py b/framework/climate/urls.py index 6610659..26a29eb 100644 --- a/framework/climate/urls.py +++ b/framework/climate/urls.py @@ -16,7 +16,8 @@ path('select_temp_urls', views.select_temp_urls, name='select_temp_urls'), path('get_content', views.FolderContentView.as_view(), name='get_content'), path('get_temp_file', views.TempDownloadView.as_view(), name='get_temp_file'), - path('get_temp_file_metadata', views.get_ncfile_metadata, name='get_ncfile_metadata') + path('get_temp_file_metadata', views.get_ncfile_metadata, name='get_ncfile_metadata'), + path('access_tif', views.access_tif_from_ncfile, name="access_tif"), ] urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json']) diff --git a/framework/climate/views.py b/framework/climate/views.py index 59c40ee..54c7f57 100644 --- a/framework/climate/views.py +++ b/framework/climate/views.py @@ -201,8 +201,8 @@ def cache_tif_from_nc(filename_in: str, foldertype: str, temp_doc: TempResultFil temp_doc.st_mtime_tif = fileversion_out temp_doc.save() return True, "" - except Exception as e: - print(e) + except Exception: + # print(e) return False, "Conversion failed" @@ -398,36 +398,71 @@ def init_temp_results_folders(force_update=False, delete_all=False): # post creation handling (?) print(f"Finished TempResultFiles Init. Created {created_objs_counter} database objects.") - # init_temp_results_folders() -# def test_init_nc(): -# filenames = [test_file_name] -# folder = "water_budget" +@api_view(["GET"]) +def access_tif_from_ncfile(request): + foldertype = parse_temp_foldertype_from_param(request.GET.get("type", default=None)) + filename = parse_temp_filename_from_param(request.GET.get("name", default=None), foldertype) -# for n in filenames: -# succ, helper = extract_ncfile_metadata(n, folder_list[folder]) + if foldertype is False or filename is False: + return HttpResponseBadRequest() -# if not succ: -# fail_msg = helper -# print("Could not extract metadata:", fail_msg) -# else: -# new_file_object: TempResultFile = helper -# print("Extracted metadata for file: ", n) -# print(new_file_object.band_metadata) + filepath = os.path.join(folder_list['raw'][foldertype], filename) + cat_filename = temp_cat_filename(foldertype, filename) + temp_doc: TempResultFile = TempResultFile.get_by_cat_filename(cat_filename) + # indicates that the database entry for current file + # needs to be updated + update_doc = False + if temp_doc is None: + update_doc = True + else: + fileversion = os.stat(filepath).st_mtime + if not temp_doc.check_raw_version(fileversion): + update_doc = True -# def test_nc(): -# folder = "water_budget" + if update_doc: + succ, msg = extract_ncfile_metadata(filename, folder_list['raw'][foldertype], foldertype, force_update=True) + if not succ: + # could not extract raw file metadata ... + # thus can not properly translate to tif + return HttpResponse(f"Could not extract raw file metadata. Reason: {msg}", status=204) -# # file does not exist -# print(TempResultFile.get_file_metadata("qwr