def stl_file_maker(file, interval=1.5, threshold=0.35, fname='test.stl', gaussian=1): ''' This uses the stl_tools numpy2stl in order to convert an array into a 3D printable model. This cannot take xyz dimensions and cannot make the full 3D model. It makes the 2D image 3D printable. Parameters: file : str, name of the file, should be png interval : float, rate at which points are taken from the image threshold : float, minimum threshold for intensity value fname : str, name of exported file gaussian : int, number of loops of gaussian filtering data goes through ''' earth_radius = 6371 #km scale_factor_percent = 0.3 #Does not work with cropped image #data = io.read_file('2014_05_27__14_38_31_12__SDO_AIA_AIA_304.jp2') header = io.read_file_header(file+'.jp2') image = read_png(file+'.png') km_per_pixel = km_per_pixel(arcs_per_pix=header[0].__getitem__('IMSCL_MP')) #center points for the earth to scale figure earth_scale_y = 35 earth_scale_x = image.shape[1] - 35 #px earth_box = 2 earth_radius_px = earth_radius / km_per_pixel for xpoint in range(data.shape[0]): for ypoint in range(data.shape[1]): if (np.sqrt((xpoint - earth_scale_x)**2 + (ypoint - earth_scale_y)**2) <= earth_radius_px): data[xpoint][ypoint] = 0.1 elif (((np.absolute(xpoint - (earth_scale_x - earth_scale_y)) < earth_box) & (ypoint < (2. * earth_scale_y))) or (((np.absolute(ypoint - (2. * earth_scale_y)) < earth_box)) & (xpoint > (earth_scale_x - earth_scale_y)))): data[xpoint][ypoint] = 0.05 elif (data[xpoint][ypoint] <= threshold): data[xpoint][ypoint] = 0 else: data[xpoint][ypoint] = (data[xpoint][ypoint] - threshold) / (1 - threshold) data = resize(data, (int(data.shape[0]/interval), int(data.shape[1]/interval))) data = gaussian_filter(data, gaussian) #dimensions in mm, if scale = 100, then the dimensions will be exact numpy2stl(data, fname, scale=100, solid=True, max_width=228.6, max_depth=228.6, max_height=80, force_python=True) subprocess.call(['bash', 'filemover.sh', fname])
def read_header(cls, filepath): """Attempts to detect the datasource type and returns meta-information for that particular datasource.""" header = read_file_header(filepath) for cls in Map.__subclasses__(): if cls.is_datasource_for(header): properties = cls.get_properties(header) properties['header'] = header return properties
def __init__(self, *args, **kwargs): """Creates a new MapHeader instance""" if isinstance(args[0], basestring): # filepath from sunpy.io import read_file_header tags = read_file_header(args[0]) else: # dictionary tags = args[0] # Store all keys as upper-case to allow for case-insensitive indexing tags = dict((k.upper(), v) for k, v in tags.items()) args = (tags,) + args[1:] dict.__init__(self, *args, **kwargs)
def __init__(self, *args, **kwargs): """Creates a new MapHeader instance""" if isinstance(args[0], basestring): # filepath from sunpy.io import read_file_header tags = read_file_header(args[0]) else: # dictionary tags = args[0] # Store all keys as upper-case to allow for case-insensitive indexing tags = dict((k.upper(), v) for k, v in tags.items()) args = (tags, ) + args[1:] dict.__init__(self, *args, **kwargs)
def get_header(self, filepath): """ Reads the header from the file and sanitizes it to Fits standards. Parameters ---------- filepath : `str` The file to be read Returns ------- headers : list A list of headers read from the file """ sunpyhead = read_file_header(filepath) for subhead in sunpyhead: # Remove newlines from comment and history if 'comment' in subhead: subhead['comment'] = subhead['comment'].replace("\n", "") if 'history' in subhead: subhead['history'] = subhead['history'].replace("\n", "") badkeys = [] # dumps header keywords that are NaN for key, value in subhead.items(): if type(value) in (int, float): if np.isnan(value): badkeys += [key] for key in badkeys: subhead.pop(key, None) return sunpyhead
def file_finder(date, local=True, index=0): ''' This function takes in the date from the 3Dplot.py. It then uses this date to access the website that contains all of the XRT images. It uses a findFiles.sh bash script that attempts to download images of a certain url. It attempts to match an image with a date and if successful downloads the images for that date. It then turns the first image into an array and a header. If it was unsuccessful, it iterates through dates until it finds a day with pictures ''' date = date print("initial date is: %s" % date) dirname = [] year_file = date[0:4] month_file = date[5:7] day_file = date[8:10] internet_dir = 'solar.physics.montana.edu/HINODE/XRT/SCIA/synop_official/' local_dir = '/archive/hinode/xrt/level2/synoptics/' if (local): dir = local_dir else: dir = internet_dir counter = 0 while len(dirname) <= 0: #calling bash script if (not local): subprocess.call( ['bash', 'findFiles.sh', year_file, month_file, day_file]) date_file = year_file + '/' + month_file + '/' + day_file + '/' #loop through files in the directory of year, month, and day finding all files/dirs for dirnames in os.walk(dir + date_file): counter += 1 dirname.append(dirnames) print(year_file, month_file, day_file) print(counter) #iterates through dates if initial date is unsuccessful if counter <= 0: if ((int(month_file) == 2) & (int(day_file) >= 28)): day_file = '0' + str(1) month_file = '0' + str(3) elif (((int(month_file) == 1) or (int(month_file) == 3) or (int(month_file) == 5) or (int(month_file) == 7) or (int(month_file) == 8) or (int(month_file) == 10)) & (int(day_file) >= 31)): day_file = '0' + str(1) if (int(month_file) < 9): month_file = '0' + str(int(month_file) + 1) else: month_file = str(int(month_file) + 1) elif (((int(month_file) == 4) or (int(month_file) == 6) or (int(month_file) == 9) or (int(month_file) == 11)) & (int(day_file) >= 30)): day_file = '0' + str(1) if (int(month_file) < 9): month_file = '0' + str(int(month_file) + 1) else: month_file = str(int(month_file) + 1) elif ((int(month_file) == 12) and (int(day_file) >= 31)): day_file = '0' + str(1) month_file = '0' + str(1) year_file = str(int(year_file) + 1) else: if (int(day_file) < 9): day_file = '0' + str(int(day_file) + 1) else: day_file = str(int(day_file) + 1) date_file = year_file + '/' + month_file + '/' + day_file + '/' print('date of image: %s' % date_file) if (local): index = 3 * index + 2 #picks first image in first directory Hname = str(dirname[0][1][0]) filename = str(dirname[1][2][index]) #uses sunpy to read .jp2 file into data and header data = io.read_file(dir + date_file + Hname + '/' + filename) header = io.read_file_header(dir + date_file + Hname + '/' + filename) return data, header
def file_finder(date, local=True, index=0): ''' This function takes in the date from the 3Dplot.py. It then uses this date to access the website that contains all of the XRT images. It uses a findFiles.sh bash script that attempts to download images of a certain url. It attempts to match an image with a date and if successful downloads the images for that date. It then turns the first image into an array and a header. If it was unsuccessful, it iterates through dates until it finds a day with pictures ''' date = date print("initial date is: %s" % date) dirname = [] year_file = date[0:4] month_file = date[5:7] day_file = date[8:10] #This is the path that the file is in and that the image will download to dir_path = os.path.dirname(os.path.realpath(__file__)) internet_dir = 'solar.physics.montana.edu/HINODE/XRT/SCIA/synop_official/' local_dir = '/archive/hinode/xrt/level2/synoptics/' if (local): dir = local_dir else: dir = internet_dir counter = 0 while len(dirname) <= 0: #calling bash script if (not local): #This uses the wget function to access the web page. #-r means it does so recursively so that it gets all the files in the directory #-np means no parent, so that it does not ascend to the parent directory and download all the images #-A means accept list #3 inputs from imageFinder.py are given that specify the date and name of the image command = 'wget -r -np -A .jp2 http://solar.physics.montana.edu/HINODE/XRT/SCIA/synop_official/' + year_file + '/' + month_file + '/' + day_file + '/' os.system(command) date_file = year_file + '/' + month_file + '/' + day_file + '/' #loop through files in the directory of year, month, and day finding all files/dirs for dirnames in os.walk(dir + date_file): counter += 1 dirname.append(dirnames) print(year_file, month_file, day_file) print(counter) #iterates through dates if initial date is unsuccessful if counter <= 0: if ((int(month_file) == 2) & (int(day_file) >= 28)): day_file = '0' + str(1) month_file = '0' + str(3) elif (((int(month_file) == 1) or (int(month_file) == 3) or (int(month_file) == 5) or (int(month_file) == 7) or (int(month_file) == 8) or (int(month_file) == 10)) & (int(day_file) >= 31)): day_file = '0' + str(1) if (int(month_file) < 9): month_file = '0' + str(int(month_file) + 1) else: month_file = str(int(month_file) + 1) elif (((int(month_file) == 4) or (int(month_file) == 6) or (int(month_file) == 9) or (int(month_file) == 11)) & (int(day_file) >= 30)): day_file = '0' + str(1) if (int(month_file) < 9): month_file = '0' + str(int(month_file) + 1) else: month_file = str(int(month_file) + 1) elif ((int(month_file) == 12) and (int(day_file) >= 31)): day_file = '0' + str(1) month_file = '0' + str(1) year_file = str(int(year_file) + 1) else: if (int(day_file) < 9): day_file = '0' + str(int(day_file) + 1) else: day_file = str(int(day_file) + 1) date_file = year_file + '/' + month_file + '/' + day_file + '/' print('date of image: %s' % date_file) if (local): index = 3 * index + 2 #picks first image in first directory Hname = str(dirname[0][1][0]) filename = str(dirname[1][2][index]) #uses sunpy to read .jp2 file into data and header data = io.read_file(dir + date_file + Hname + '/' + filename) header = io.read_file_header(dir + date_file + Hname + '/' + filename) #removes the downloaded files from the online archive os.system('rm -r solar.physics.montana.edu') os.system('rm -r wget-log') return data, header