converted the test script to .py format

This commit is contained in:
2025-09-25 18:22:33 +03:00
parent 5edb153e38
commit fd66d31658
2 changed files with 83 additions and 170 deletions

View File

@@ -1,170 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "4cffd6c5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"RA List: [np.float64(24.1741435187182), np.float64(25.759787769012984), np.float64(49.32806570275772), np.float64(55.50613654382996), np.float64(60.97590646798826), np.float64(63.018113741788355), np.float64(143.0416839498853), np.float64(161.69014062163393), np.float64(176.53216723085026), np.float64(183.45178780664702), np.float64(191.28607761407375), np.float64(194.20387680952962), np.float64(204.38304472867193), np.float64(339.2669893687885), np.float64(0.8130105246497964), np.float64(164.03825008738184)]\n",
"Dec List: [np.float64(15.783869592105441), np.float64(13.645005280982105), np.float64(-41.10817408896737), np.float64(-47.22221653992977), np.float64(-43.34878852480052), np.float64(-32.87392378612486), np.float64(21.501827494459036), np.float64(11.819887017791151), np.float64(47.49512808371984), np.float64(14.900261322222237), np.float64(-0.4622668171007003), np.float64(-8.524938659400307), np.float64(8.885499320910338), np.float64(34.4157929700228), np.float64(16.14532447253357), np.float64(6.1729868291531)]\n"
]
}
],
"source": [
"import csv\n",
"from astropy.coordinates import SkyCoord\n",
"import astropy.units as u\n",
"\n",
"# Initialize empty lists for RA and Dec\n",
"ra_list = []\n",
"dec_list = []\n",
"\n",
"# Define the path to your CSV file\n",
"csv_file_path = \"GeVgal.csv\"\n",
"\n",
"# Open and read the CSV file\n",
"with open(csv_file_path, 'r') as csvfile:\n",
" # Use csv.reader to handle the file, skipping the header row\n",
" csv_reader = csv.reader(csvfile)\n",
" next(csv_reader) # Skip the header row\n",
"\n",
" # Loop through each row in the CSV\n",
" for row in csv_reader:\n",
" try:\n",
" # Extract l and b, which are in the second and third columns (index 1 and 2)\n",
" l = float(row[1])\n",
" b = float(row[2])\n",
"\n",
" # Create a SkyCoord object with galactic coordinates\n",
" galactic_coord = SkyCoord(l=l*u.degree, b=b*u.degree, frame='galactic')\n",
"\n",
" # Convert to ICRS (equatorial) coordinates\n",
" icrs_coord = galactic_coord.icrs\n",
"\n",
" # Append the RA and Dec values to the lists\n",
" ra_list.append(icrs_coord.ra.deg)\n",
" dec_list.append(icrs_coord.dec.deg)\n",
"\n",
" except (ValueError, IndexError) as e:\n",
" # Handle potential errors if a row is malformed\n",
" print(f\"Skipping a malformed row: {row} - Error: {e}\")\n",
"\n",
"# Now, ra_list and dec_list contain the converted coordinates\n",
"print(\"RA List:\", ra_list)\n",
"print(\"Dec List:\", dec_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ff1d339a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Request successful!\n",
"{\n",
" \"Status\": 0,\n",
" \"ErrorMessage\": \"\",\n",
" \"ClassicUpperLimit\": 36.99647944311798,\n",
" \"ClassicLowerLimit\": 0.0,\n",
" \"ClassicOneSideUL\": 33.54440898622437,\n",
" \"ClassicCountRateUpperLimit\": 0.022452965897451615,\n",
" \"ClassicCountRateLowerLimit\": 0.0,\n",
" \"ClassicCountRateOneSideUL\": 0.020357922763323065,\n",
" \"ClassicFluxUpperLimit\": 7.504496105362505e-13,\n",
" \"ClassicFluxLowerLimit\": 0.0,\n",
" \"ClassicFluxOneSideUL\": 6.804265983763352e-13,\n",
" \"BayesianUpperLimit\": 33.83295926177776,\n",
" \"BayesianLowerLimit\": 0.10792639479099073,\n",
" \"BayesianOneSideUL\": 33.727107645421896,\n",
" \"BayesianCountRateUpperLimit\": 0.020533042385357955,\n",
" \"BayesianCountRateLowerLimit\": 6.549995291856849e-05,\n",
" \"BayesianCountRateOneSideUL\": 0.020468801604397097,\n",
" \"BayesianFluxUpperLimit\": 6.862796537256179e-13,\n",
" \"BayesianFluxLowerLimit\": 2.189216978388652e-15,\n",
" \"BayesianFluxOneSideUL\": 6.841325222832595e-13,\n",
" \"FluxEstimate\": 3.2382519031831067e-13,\n",
" \"ApertureCounts\": 94,\n",
" \"ApertureBackgroundCounts\": 78.03571428571428,\n",
" \"SourceCounts\": 15.964285714285722,\n",
" \"Exposure\": 2057.325295693534,\n",
" \"SourceRate\": 0.009688639787230046,\n",
" \"BackgroundRate\": 0.03793066388142964,\n",
" \"NormalizedBackgroundRate\": 1.6914547063541448e-05,\n",
" \"Contamination\": false,\n",
" \"CountMap\": {\n",
" \"healpix\": [],\n",
" \"counts\": [],\n",
" \"exposure\": [],\n",
" \"nside\": 4096,\n",
" \"order\": \"ring\",\n",
" \"radius_as\": 0.0\n",
" }\n",
"}\n"
]
}
],
"source": [
"import requests\n",
"import json\n",
"\n",
"# Define the URL of your Django API endpoint\n",
"url = \"http://localhost:8000/api/stacked-upper-limit/\"\n",
"\n",
"payload = {\n",
" \"ra\": ra_list, # List of RA values\n",
" \"dec\": dec_list, # List of Dec values\n",
" \"cl\": 0.95, # A numeric confidence level\n",
" \"survey\": \"1-4\", # A string for the survey parameter\n",
" \"mr\": 0 # A numeric value for map_radius\n",
"}\n",
"\n",
"try:\n",
" # Send the PUT request with the JSON payload\n",
" response = requests.put(url, json=payload)\n",
"\n",
" # Check the response status code\n",
" response.raise_for_status() # This will raise an HTTPError for bad responses (4xx or 5xx)\n",
"\n",
" # Print the JSON response from the server\n",
" print(\"Request successful!\")\n",
" print(json.dumps(response.json(), indent=4))\n",
"\n",
"except requests.exceptions.HTTPError as err:\n",
" print(f\"HTTP Error: {err}\")\n",
" print(f\"Response body: {err.response.text}\")\n",
"except requests.exceptions.RequestException as err:\n",
" print(f\"An error occurred: {err}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv-pypy",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

83
stack_request_test.py Normal file
View File

@@ -0,0 +1,83 @@
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import csv
from astropy.coordinates import SkyCoord
import astropy.units as u
# Initialize empty lists for RA and Dec
ra_list = []
dec_list = []
# Define the path to your CSV file
csv_file_path = "GeVgal.csv"
# Open and read the CSV file
with open(csv_file_path, 'r') as csvfile:
# Use csv.reader to handle the file, skipping the header row
csv_reader = csv.reader(csvfile)
next(csv_reader) # Skip the header row
# Loop through each row in the CSV
for row in csv_reader:
try:
# Extract l and b, which are in the second and third columns (index 1 and 2)
l = float(row[1])
b = float(row[2])
# Create a SkyCoord object with galactic coordinates
galactic_coord = SkyCoord(l=l*u.degree, b=b*u.degree, frame='galactic')
# Convert to ICRS (equatorial) coordinates
icrs_coord = galactic_coord.icrs
# Append the RA and Dec values to the lists
ra_list.append(icrs_coord.ra.deg)
dec_list.append(icrs_coord.dec.deg)
except (ValueError, IndexError) as e:
# Handle potential errors if a row is malformed
print(f"Skipping a malformed row: {row} - Error: {e}")
# Now, ra_list and dec_list contain the converted coordinates
print("RA List:", ra_list)
print("Dec List:", dec_list)
# In[ ]:
import requests
import json
# Define the URL of your Django API endpoint
url = "http://localhost:8000/api/stacked-upper-limit/"
payload = {
"ra": ra_list, # List of RA values
"dec": dec_list, # List of Dec values
"cl": 0.95, # A numeric confidence level
"survey": "1-4", # A string for the survey parameter
"mr": 0 # A numeric value for map_radius
}
try:
# Send the PUT request with the JSON payload
response = requests.put(url, json=payload)
# Check the response status code
response.raise_for_status() # This will raise an HTTPError for bad responses (4xx or 5xx)
# Print the JSON response from the server
print("Request successful!")
print(json.dumps(response.json(), indent=4))
except requests.exceptions.HTTPError as err:
print(f"HTTP Error: {err}")
print(f"Response body: {err.response.text}")
except requests.exceptions.RequestException as err:
print(f"An error occurred: {err}")